CodeEval Testing

We have a string in input representing two versions of a text, separated by " | ", that we have to check for changes. If there is no difference, we return 'Done', otherwise a growing alert message. More detail on the CodeEval page for problem #225. Here I present my Python 3 solution.

This is the tests I have extracted from the given samples.
def test_provided_1(self):
    self.assertEqual('Low', solution('Heelo Codevval | Hello Codeeval'))

def test_provided_2(self):
    self.assertEqual('Critical', solution('hELLO cODEEVAL | Hello Codeeval'))

def test_provided_3(self):
    self.assertEqual('Done', solution('Hello Codeeval | Hello Codeeval'))
Notice that the two versions have the same length, and that the error checking is case sensitive.

After splitting the input line on ' | ' I simply count the bugs comparing the elements in the same position for the two components in the list:
bugs = 0
for i in range(len(data[0])):
    if data[0][i] != data[1][i]:
        bugs += 1
Then it is just a matter of swithing on the bugs and returning an appropriated message. However, for some obscure reason, Python does not have a switch statement. So the better I could think of, was a if/elif/else structure:
if bugs == 0:
    return 'Done'
elif bugs < 3:
    return 'Low'
elif bugs < 5:
    return 'Medium'
elif bugs < 7:
    return 'High'
    return 'Critical'
That's it. Submitted to CodeEval, than pushed to GitHub, both test case and python script.

No comments:

Post a Comment