PatchingMatching
PatchingMatching

Reputation: 91

Python, f-string formatting float value to exact amount decimals

So I'm trying to learn Python, and decided to try and do some problems on Kattis, this one to be more precise. I've managed to scrape together some code that prints the correct value the case they provide.

import functools

for _ in range(int(input())):
  d = list(map(int, input().split()))
  avg = functools.reduce(lambda a, b: a + b, d[1:]) / d[0]
  print(f'{100 * len(list(filter(lambda x: x > avg, d))) / d[0]:.3f}' + '%')

But when I submit the code it fails one of the two test cases saying it gets the wrong answer. I would guess that the fault lies somewhere in the f-string formatting that goes on because it seems like avg gets the right value. So I'm hoping that there might be some more talented people that might spot the error I'm missing.

Upvotes: 1

Views: 2056

Answers (1)

galaxyan
galaxyan

Reputation: 6111

It is not f string issue. you mis-calculate the percentage. The first value should not be included.

 print(f'{100 * len(list(filter(lambda x: x > avg, d[1:]))) / d[0]:.3f}' + '%')

Upvotes: 3

Related Questions