mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-06-17 11:29:27 -04:00
fixes
This commit is contained in:
parent
aa5010594e
commit
7b9e953f5a
194 changed files with 28 additions and 34 deletions
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