PROG0130 - Positional notation

no tags 

A positional system is a numeral system in which a number is represented by a range of symbols — usually, Arabic numbers and lowercase letters are used — of which the position based on the radix determines the contribution of the number. The usual radix is ten. It is doubtful that this radix stems from the fact that people counted on their fingers. A number like 1234 than means: $1 \times 1000 + 2 \times 100 + 3 \times 10 + 4 \times 1$. The position of the digit determines the contribution in squares of the radix 10 to the number.

An integer $x$ is expressed in the decimal positional system as a sequence of terms of powers of another integer: the radix $a$: $$x = \sum_{i=0}^kx_ia^i$$ of $$x = x_ka^k + \ldots + x_2a^2 + x_1a^1 + x_0a^0$$ to the co-ordinates $x_i \in \mathbb{N}$ of which applies that $0 \leq x_i < a$. In the $a$-numbered system, $x$ is then represented as a sequence of symbols: $$x_k\ldots x_2x_1x_0$$ The coefficients $x_i$ form the symbols of that number. The leftmost symbol $x_k$ is the coefficient of the highest power of the radix, the rightmost $x_0$ is the coefficient of the unities (the 0th power of the radix).

As such, the number $1234_{10}$ is written as $3412_7$ in the 7-numbered system, because: $$1234_{10} = 3 \times 7^3+ 4 \times 7^2 + 1 \times 7^1 + 2 \times 7^0$$

Assignment

Write a function positionalsystem that converts the representation of a number in the positional system to a given radix $a_1$ to the representation of that same number in the positional system of an other radix $a_2$. Give this function the three parameters below:

  • a string with the representation of a number in a positional system with given radix $a_1$
  • the radix $a_1$
  • the radix $a_2$
def positionalsystem(number, radix1, radix2)

The function must print a string that represents the number in the positional system with radix $a_2$. You may assume that $0 < a_1, a_2 \leq 36$. For the string representation of numbers in a positional system, three accepted symbols are used:

  • the values 0, 1, …, 9 are represented by the Arabic digits '0', '1', …, '9'
  • the values 10, 11, …, 35 are represented by the letters 'a', 'b', …, 'z'

Example

>>> positionalsystem('1234', 10, 7)
'3412'
>>> positionalsystem('1234', 10, 16)
'4d2'
>>> positionalsystem('4d2', 16, 10)
'1234'
>>> bin(1234)[2:] == positionalsystem('1234', 10, 2)
True
>>> oct(1234)[2:] == positionalsystem('1234', 10, 8)
True
>>> hex(1234)[2:] == positionalsystem('1234', 10, 16)
True

Een positiestelsel is een talstelsel waarin een getal wordt voorgesteld door een reeks symbolen — doorgaans worden hiervoor Arabische cijfers en kleine letters gebruikt — waarvan de positie op basis van een gekozen grondtal de bijdrage aan het getal bepaalt. Het gebruikelijke grondtal is tien. Men twijfelt er niet aan dat dit grondtal is ontstaan doordat de mensen op hun vingers telden. Een getal als 1234 heeft dan de betekenis: $1 \times 1000 + 2 \times 100 + 3 \times 10 + 4 \times 1$. De positie van een cijfer bepaalt de bijdrage in machten van het grondtal 10 aan het getal.

Een natuurlijk getal $x$ laat zich in het decimale positiestelsel uitdrukken als een reeks termen van machten van een ander natuurlijk getal: het grondtal $a$: $$x = \sum_{i=0}^kx_ia^i$$ of $$x = x_ka^k + \ldots + x_2a^2 + x_1a^1 + x_0a^0$$ waarbij voor de coëfficiënten $x_i \in \mathbb{N}$ geldt dat $0 \leq x_i < a$. In het $a$-tallige stelsel wordt $x$ dan voorgesteld als de reeks symbolen: $$x_k\ldots x_2x_1x_0$$ De coëfficiënten $x_i$ vormen in volgorde de symbolen van het getal. Het meest linkse symbool $x_k$ is de coëfficiënt van de hoogste macht van het grondtal, het meest rechtse $x_0$ de coëfficiënt van de eenheden (de 0-de macht van het grondtal).

Zo wordt in het 7-tallig stelsel het getal $1234_{10}$ geschreven als $3412_7$, want: $$1234_{10} = 3 \times 7^3+ 4 \times 7^2 + 1 \times 7^1 + 2 \times 7^0$$

Opgave

Schrijf een functie positiestelsel die de voorstelling van een getal in een positiestelsel met een gegeven grondtal $a_1$ omzet in de voorstelling van datzelfde getal, maar dan in een positiestelsel met een ander grondtal $a_2$. Aan deze functie moeten de volgende drie parameters doorgegeven worden:

  • een string met de voorstelling van een getal in een positiestelsel met gegeven grondtal $a_1$
  • het grondtal $a_1$
  • het grondtal $a_2$
def positiestelsel(getal, grondtal1, grondtal2)

De functie moet een string teruggeven die het getal in het positiestelsel met grondtal $a_2$ voorstelt. Je mag ervan uitgaan dat $0 < a_1, a_2 \leq 36$. Bij de stringvoorstelling van getallen in een positiestelsel worden de gangbare symbolen gebruikt:

  • de waarden 0, 1, …, 9 worden voorgesteld door de Arabische cijfers '0', '1', …, '9'
  • de waarden 10, 11, …, 35 worden voorgesteld door de letters 'a', 'b', …, 'z'

Voorbeeld

>>> positiestelsel('1234', 10, 7)
'3412'
>>> positiestelsel('1234', 10, 16)
'4d2'
>>> positiestelsel('4d2', 16, 10)
'1234'
>>> bin(1234)[2:] == positiestelsel('1234', 10, 2)
True
>>> oct(1234)[2:] == positiestelsel('1234', 10, 8)
True
>>> hex(1234)[2:] == positiestelsel('1234', 10, 16)
True


Added by:Peter Dawyndt
Date:2011-08-07
Time limit:5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:PY_NBC
Resource:None