mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-06-18 11:59:33 -04:00
organization in the src structure, modification of README
This commit is contained in:
parent
c2ca11f247
commit
6afe96fa4d
165 changed files with 64 additions and 184 deletions
src/builtin_structures/numbers
24
src/builtin_structures/numbers/convert_from_decimal.py
Normal file
24
src/builtin_structures/numbers/convert_from_decimal.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
def convert_from_decimal(number, base):
|
||||
''' convert any decimal number to another base. '''
|
||||
multiplier, result = 1, 0
|
||||
while number > 0:
|
||||
result += number%base*multiplier
|
||||
multiplier *= 10
|
||||
number = number//base
|
||||
return result
|
||||
|
||||
|
||||
def test_convert_from_decimal():
|
||||
number, base = 9, 2
|
||||
assert(convert_from_decimal(number, base) == 1001)
|
||||
print('Tests passed!')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_convert_from_decimal()
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue