Write your code: Decimal to Binary
Contents
Write your code: Decimal to Binary#
Write a function that returns the binary value (as string) for a positive integer decimal number
Function definition#
def dec_to_binary(n):
# write your code here
File "/tmp/ipykernel_2001/1859163576.py", line 3
# write your code here
^
SyntaxError: unexpected EOF while parsing
Using a recursive function:
Testing#
Check if your function returns the expected value using the cell below.
import unittest
class UnitTests(unittest.TestCase):
def test_type(self):
self.assertEqual(type(dec_to_binary(10)), type('1010'), 'The function should return a string')
def test_size(self):
self.assertEqual(len(dec_to_binary(100)), 7, 'The function should return a string with lenght=7')
def test_result_odd(self):
self.assertEqual(dec_to_binary(101), '1100101', 'The binarie for 200 should be 1100101')
def test_result_even(self):
self.assertEqual(dec_to_binary(200), '11001000', 'The binarie for 200 should be 11001000')
unittest.main(argv=[''], verbosity=2,exit=False)