Pages

CodeEval Panacea - truth or lie

Given the string representation of two lists of numbers, the first ones in hexadecimal format, the latter in binary, return True if and only if the sum of the first ones is strictly less then the sum of the second ones.
This is the CodeEval problem #237, and here I give my Python 3 solution.

The sample provided could be easily converted in these python test cases:
def test_provided_1(self):
    self.assertTrue(solution('64 6e 78 | 100101100 11110'))

def test_provided_2(self):
    self.assertTrue(solution('5e 7d 59 | 1101100 10010101 1100111'))

def test_provided_3(self):
    self.assertFalse(solution('93 75 | 1000111 1011010 1100010'))
After splitting the input line, we just have to use the built-in function int(), passing as second parameter the expected base:
hexes = [int(x, 16) for x in data[0].split()]
bins = [int(x, 2 ) for x in data[1].split()]
And then it is just a matter of comparing the sum() of the two lists:
return sum(hexes) <= sum(bins)
I have pushed the test case and the python script to GitHub.

No comments:

Post a Comment