The diabolical Professor Boolean has captured you and a group of your hapless rabbit kin as test subjects for his terrible experiments! You're not sure what his real plans are, but currently it seems he's trying to make everyone faster and smarter? He's exposing rabbit test subjects to novel chemicals, genetic manipulations, and pathogens; then measuring their completion time for various puzzles and exercises. Then again, there's a rumor he's developing a kind of zombie rabbit. You don't want to become a zombit!

Unfortunately, due to insubordination, and laziness, Professor Boolean just "eliminated" the lab assistant tracking all data from this research. Now, he's forcing you to sort through the notes and find something useful from the chaos. You have no choice but to abide by your captors evil rules. For now.

Of the subjects that have survived, each has a distinct file, with anywhere from 1 to 100 measurements of completion time for the tests. The measurements from the before and after cases are listed separately, but the ordering has been mixed up. You have to figure out the degree of improvement (0% to 99%, rounded to the nearest whole number) based on the two lists of results.

For example, if the first list of times is [22.2, 46, 100.8] and the second list is [23, 11.1, 50.4] you would return 50, because the times got 50% shorter: the 22.2 entry improved to 11.1, the 46 improved to 23, and the 100.8 improved to 50.4. Even though the data points are in different order, each improves by the same amount.

Write a function answer(x, y) which takes two lists of floating point performance scores and returns the improvement percentage, rounded to the nearest integer.

Test cases:


(double list) y = [1.0]
(double list) x = [1.0]


(int) 0


(double list) y = [2.2999999999999998, 15.0, 102.40000000000001, 3486.8000000000002]
(double list) x = [23.0, 150.0, 1024.0, 34868.0]


(int) 90

The test cases are successful, but for some reason when I try to verify my answer, it says: There was a problem evaluating your code.

Is there a better way to solve this?

def answer(y,x):
    totalTime1 = 0
    totalTime2 = 0
    for time in x:
        totalTime1 += time
    for time in y:
        totalTime2 += time
    improvement = (1-(totalTime1 / totalTime2)) * 100
    return int(improvement)
  • \$\begingroup\$ Welcome to Code Review! Does your code work as expected? While it does pass the test cases, you do say how the service rejects your code. \$\endgroup\$
    – SirPython
    Mar 28, 2016 at 23:38

1 Answer 1


For one thing, the assignment was to define a function answer(x, y), not answer(y, x). You could also put the built-in function sum() to use. I believe the key point is rounded to the nearest whole number. int() always goes down. That is 4.9 results in 4, but rounding would result in 5. You can use the built-in function round(). In Python3 it returns an integer, but in Python2 it returns a float. To take care of both cases, convert the rounded version to an int:

def answer(x, y):
    return int(round((1-(sum(y)/sum(x))) * 100))
  • \$\begingroup\$ The assignment was answer(x,y) but in their solution.py file, the header was answer(y,x), and thanks totally forgot that it was round not truncate. \$\endgroup\$
    – Bob
    Mar 28, 2016 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.