Squirrel breaks a bunch of nuts, which is equal to the factorial of the number N, and finds emeralds in them. The number of emeralds is obtained equal to the first digit of factorial.

Function

int squirrel (int N)

receives a non-negative integer N as a parameter, and returns the first digit of factorial N!

def squirrel(self, N): s = 1 factorial = 1 emerald = 0 nf = 0 if(N == 0 or N == 1): emerald = 1 return emerald for i in range(2, N+1): factorial *= i nf = factorial while nf != 0: nf = nf//10 s *= 10 emerald = int(factorial//(s / 10)) return (emerald) 

like tests did

 import random class my_work: def squirrel(self, N): s = 1 factorial = 1 emerald = 0 nf = 0 if(N == 0 or N == 1): emerald = 1 return emerald for i in range(2, N+1): factorial *= i nf = factorial while nf != 0: nf = nf//10 s *= 10 emerald = int(factorial//(s / 10)) return (emerald) class my_test: def test1(self): test = my_work() print(test.squirrel(0)) def test2(self): test = my_work() print(test.squirrel(1)) def test3(self): test = my_work() for i in range(10): y = random.randint(0,10) print(test.squirrel(y)) test = my_test() test.test3() 

Closed due to the fact that off-topic participants Kromster , mkkik , Enikeyschik , 0xdb , Avernial 15 May at 1:21 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Kromster, Enikeyschik, 0xdb, Avernial
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • And what a mistake? - mkkik

1 answer 1

With a syntax error, the program does not start, but this one works somehow, despite the integer division by a real number.

More thrown out:

 def squirrel(N): emerald = 1 for i in range(2, N+1): emerald *= i while emerald >= 10: emerald //= 10 return emerald 
  • And instead of str(emerald)[0] loop? - gil9red
  • str(reduce(mul, range(2, N + 1)))[0] - mkkik
  • @ gil9red And why, if str inside does about the same, and it also allocates space for the line? Or because the built-in function will work faster than its code? - MBo
  • More precisely while emerald> 9: otherwise, the answer you can give 10. - coder675
  • @ coder675 Right, thanks - MBo