fork download
  1. # -*- coding:utf-8 -*-
  2.  
  3. #[1,1,5,8]から10を作る
  4. #『Nexus 7 : 10 Puzzle』――http://w...content-available-to-author-only...e.com/watch?v=JslkN8IulNA
  5.  
  6. def getope():
  7. ope = ['+','-','*','/']
  8. opelist = []
  9. for x in ope:
  10. for y in ope:
  11. for z in ope:
  12. #opes = unique([x,y,z])
  13. opes = [x,y,z]
  14. if(len(opes)==3):
  15. opelist.append(opes)
  16. return opelist
  17.  
  18. def getnum():
  19. num = [1,1,5,8]
  20. numlist=[]
  21. enu = range(len(num))
  22. for w in enu:
  23. for x in enu:
  24. for y in enu:
  25. for z in enu:
  26. nums = unique([w,x,y,z])
  27. if(len(nums)==4):
  28. numlist.append([str(float(num[a])) for a in nums])
  29. return numlist
  30.  
  31. def getparent():
  32. parlist=[]
  33. parlist.append(['(',')','','','','','',''])
  34. parlist.append(['(','','',')','','','',''])
  35. parlist.append(['(','','','','',')','',''])
  36. parlist.append(['(','','','','','','',')'])
  37. parlist.append(['','','(',')','','','',''])
  38. parlist.append(['','','(','','',')','',''])
  39. parlist.append(['','','(','','','','',')'])
  40. parlist.append(['','','','','(',')','',''])
  41. parlist.append(['','','','','(','','',')'])
  42. parlist.append(['','','','','','','(',')'])
  43. return parlist
  44.  
  45. def unique(old_list):
  46. new_list=[]
  47. for i in old_list:
  48. if i not in new_list:
  49. new_list.append(i)
  50. return new_list
  51.  
  52. def main():
  53. numlist = getnum()
  54. opelist = getope()
  55. parlist = getparent()
  56. for n in numlist:
  57. for o in opelist:
  58. for p in parlist:
  59. syn = p[0] + n[0] + p[1] + o[0] +p[2]+ n[1] +p[3]+ o[1] +p[4] + n[2] +p[5]+ o[2] +p[6] + n[3] + p[7]
  60. try:
  61. a = eval(syn)
  62. except ZeroDivisionError:
  63. pass
  64. if(a==10):
  65. print(syn+' = 10')
  66.  
  67. if(__name__=='__main__'):
  68. main()
Success #stdin #stdout 0.3s 7896KB
stdin
Standard input is empty
stdout
8.0/(1.0-1.0/5.0) = 10
8.0/(1.0-1.0/5.0) = 10