fork download
  1. def has_two_consecutive_odds(n):
  2. def odd(d):
  3. return d % 2 == 1
  4.  
  5. def even(d):
  6. return d % 2 == 0
  7.  
  8. for (o1,o2,e1,e2,e3) in [(0,1,2,3,4), (1,2,0,3,4), (2,3,0,1,4), (3,4,0,1,2)]:
  9. if odd(n[o1]) and odd(n[o2]) and even(n[e1]) and even(n[e2]) and even(n[e3]):
  10. return True
  11. return False
  12.  
  13. def counting():
  14. digits = {0,1,2,3,4,5,6}
  15. numbers = [-1,-1,-1,-1,-1]
  16. visited = {
  17. 0: False, 1: False, 2: False,
  18. 3: False, 4: False, 5: False,
  19. 6: False
  20. }
  21. count = [0]
  22.  
  23. def backtrack(i):
  24. def has_five_digits():
  25. return numbers[0] != 0
  26.  
  27. def is_even():
  28. return numbers[4] % 2 == 0
  29.  
  30. for d in digits:
  31. if not visited[d]:
  32. visited[d] = True
  33. numbers[i] = d
  34. if i == 4:
  35. if has_five_digits() and is_even() and has_two_consecutive_odds(numbers):
  36. count[0] += 1
  37. else:
  38. backtrack(i+1)
  39. visited[d] = False
  40.  
  41. backtrack(0)
  42. return count[0]
  43.  
  44. if __name__ == '__main__':
  45. print(counting())
Success #stdin #stdout 0.02s 27704KB
stdin
Standard input is empty
stdout
360