0

Alogrithm

Hello how do I: Create a function called "converter" and Inside the function, implement an algorithm to convert decimal numbers between 0 and 255 to their binary equivalents. For any invalid input, return string Invalid input. something similar to this: import unittest class BinaryConverterTestCases(unittest.TestCase): def test_conversion_one(self): result = binary_converter(0) self.assertEqual(result, '0', msg='Invalid conversion') def test_conversion_two(self): result = binary_converter(62) self.assertEqual(result, '111110', msg='Invalid conversion') def test_no_negative_numbers(self): result = binary_converter(-1) self.assertEqual(result, 'Invalid input', msg='Input below 0 not allowed') def test_no_numbers_above_255(self): result = binary_converter(300) self.assertEqual(result, 'Invalid input', msg='Input above 255 not allowed')

5th Jan 2017, 10:45 PM
WILLIAMS FALODUN
WILLIAMS FALODUN - avatar
1 Odpowiedź
0
def converter(z): return int (str(bin(z))[2:])
5th Jan 2017, 10:55 PM
Maxim Kuzmin
Maxim Kuzmin - avatar