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()