Advertisement
Vermiculus

question 2

Mar 16th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. import string
  2.  
  3. alph = [' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
  4.  
  5. def factorial(n):
  6.     f = 1
  7.     while(n > 1):
  8.         f = f * n
  9.         n = n - 1
  10.     return f
  11.  
  12. def binomial(a,b):
  13.     if b > a:
  14.         return 0
  15.     return factorial(a)/(factorial(b)*factorial(a-b))
  16.  
  17. def clean(s):
  18.     return s.rjust(5, ' ').lower()
  19.  
  20. def nxt(s):
  21.  
  22.     TERM = ['v','w','x','y','z']
  23.    
  24.     start = 4
  25.  
  26.     if(s[start] != TERM[start]): # If the index doesn't need to roll over,
  27.         s[start] = alph[alph.index(s[start]) + 1] # Increment the last letter
  28.     else: # We need to do le count!
  29.  
  30.         # find where we should start the count
  31.         while(s[start] == TERM[start]):
  32.             start = start - 1
  33.  
  34.         # count it! :D
  35.         s[start] = alph[alph.index(s[start]) + 1]
  36.         t = start + 1
  37.  
  38.         # make necessary trailing incrementations
  39.         while(t < 5):
  40.             s[t] = alph[alph.index(s[t-1])+1]
  41.             t = t + 1
  42.  
  43.     return s
  44.  
  45. def brute_gen():
  46.     l = list()
  47.    
  48.     e = ['v','w','x','y','z']
  49.     s = [' ',' ',' ',' ','a']
  50.  
  51.     while(s != e):
  52.         l.append(str(s))
  53.         s = nxt(s)
  54.     l.append(e)
  55.  
  56.     return l
  57.  
  58. def Problem2(s):
  59.     s = clean(s)
  60.     l = brute_gen()
  61.     m = [' ',' ',' ',' ',' ']
  62.     for x in range(5):
  63.         m[x] = s[x]
  64.     m = str(m)
  65.     if (l.count(m) != 0):
  66.         print l.index(m)
  67.     else:
  68.         print 'No.'
  69.  
  70. def valid(s):
  71.     if(len(s) != 5):
  72.         return False
  73.  
  74.     for c in s:
  75.         if(alph.count(c) == 0):
  76.             return False
  77.  
  78.     for c in s:
  79.         if(alph.index(c) == -1):
  80.             return False
  81.  
  82.     for i in range(4):
  83.         if(alph.index(s[i]) >= alph.index(s[i+1])):
  84.             return False
  85.     return True
  86.  
  87.  
  88. Problem2("r")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement