Guest User

Untitled

a guest
Jan 7th, 2016
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. #char_set = ['1','2','3','4','5','6','7','8','9','+','-','*','/']
  4. char_set = ['1','2','3','4','5','6','7','+','-','*','/']
  5. best_str = ""
  6. best_val = 0
  7.  
  8. def is_op(c):
  9. if (c=='+' or c=='-' or c=='*' or c=='/'):
  10. return 1
  11. else:
  12. return 0
  13.  
  14. def is_legal(str):
  15. if is_op(str[0]):
  16. return 0
  17. elif is_op(str[len(str)-1]):
  18. return 0
  19. else:
  20. for i in range(len(str)-1):
  21. if is_op(str[i]) and is_op(str[i+1]):
  22. return 0
  23. return 1
  24.  
  25.  
  26. def permute_and_evaluate(current_str, char_list):
  27. global best_str
  28. global best_val
  29.  
  30. if len(char_list) == 0:
  31. if is_legal(current_str):
  32. current_val = eval(current_str)
  33. if (current_val > best_val):
  34. best_val = current_val
  35. best_str = current_str
  36. print "best string so far: ", best_str
  37. print "value is : ", best_val
  38. print " "
  39. else:
  40. for c in char_list:
  41. next_str = current_str + str(c)
  42. next_char_list = list(char_list)
  43. next_char_list.remove(c)
  44. permute_and_evaluate(next_str, next_char_list)
  45.  
  46. permute_and_evaluate("", char_set)
  47.  
  48. print "Best string is ", best_str
  49. print "Value is ", best_val
Advertisement
Add Comment
Please, Sign In to add comment