Problem 6 - Problem with some rounded numbers some are ok others dont

Back to Problem Solutions forum

joisaac_     2020-10-05 14:24:16

This is my result

16 14 2 11 12 17 -3 11928 0 192557 12 1 15385 3 2958 17 10 14 2 -2 -3 10 14 2230 3 3 9 50012

This is the expected result

16 14 2 11 13 17 -3 11928 0 192557 12 1 15385 3 2958 17 11 14 2 -2 -3 10 14 2230 3 3 9 50012

I'm getting a 12 but I should get a 13, the same goes for 10, I should get an 11

This is my code:

def rounding():

    raw_lst = [int(s) for s in input().split()]
    split_lst = [raw_lst[i:i+2] for  i in range(0, len(raw_lst),2)]
    result_lst = [round(a/b) for a,b in split_lst]

    return str(result_lst).replace(',','').replace('[','').replace(']','').replace('\n', '').replace('  ',' ')

Why I get a 12 but I should get a 13, the same goes for 10, I should get an 11

Alexandr Milovantsev     2020-10-06 02:53:49

Python embedded round() function does a "bank" rounding, not a "math" one. So you get error when rounding numbers with exact 0.5 fraction part.

In python you have to imlement your own rounding function.

Please login and solve 5 problems to be able to post at forum