Guest User

Untitled

a guest
Dec 13th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. def prob5():
  2.     def helper1(n, m, a):
  3.         list = [ ]
  4.         list.append(a)# saves the last number = 9
  5.         for n in range(1, n): # checks every number for n up to 9
  6.             num = [ 0 ] * m
  7.             for i in range(m):
  8.                 if i == 0:
  9.                     num[i] = list[n-1][1]
  10.                 elif i == m - 1:
  11.                     num[i] = list[n-1][i-1]
  12.                 else:
  13.                     num[i] = list[n-1][i-1] + list[n-1][i+1]
  14.             list.append(num)
  15.         return sum(map(sum, list)) # i keep the numbers mapped rather than using of using dictionary which i helps
  16.                                    # i use that to memmoize
  17.    
  18.     def helper2(n, start, end):
  19.         m = end - start + 1
  20.         if start == 0:
  21.             a = [ 0 ] + [ 1 ] * (m - 1)
  22.         else:
  23.             a = [ 1 ] * m
  24.         return helper1(n, end - start + 1, a) # plus one so i can use range and still get the correct numbers
  25.    
  26.     N = 40
  27.     return helper2(N, 0, 9) - helper2(N, 0, 8) - helper2(N, 1, 9) + helper2(N, 1, 8) # returns the count of numbers
  28.     """
  29.    here i got a bit confused for it always returned 1931330459886 so i battled a bit more and got it after some
  30.    some googeling :) that you need to find where the number has 0 and 9 than subtrackt the numbers which have
  31.    0 but not 9 , neither 0 ore 9 and than add the numbers that include 9 but not 0
Add Comment
Please, Sign In to add comment