Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. """
  2. I have a letters which each one has weight like A to E also i know the finalvalue which is 8. Basically i need to find the combination ( + ) of letters which will add up to finalvalue with least count of letters.
  3. ( Ex: 8 can be (A+E) or (A+B+C) but result should be A+E as we used just 2 letters)
  4. """
  5.  
  6. A = 1
  7. B = A*2+ 1
  8. C = 4
  9. D = 1
  10. E = B*1 + 4
  11. finalvalue = 8
  12.  
  13. minimum = float("inf")
  14. dict1 = {"A": 1, "B": 3, "C": 4, "D": 1, "E": 7 }
  15. value_chosen = 8
  16. string1 = ""
  17. for i, j in dict1.items():
  18. if abs(j - value_chosen) < minimum:
  19. final_value = j
  20. minimum = abs(j - value_chosen)
  21.  
  22. print(final_value)
  23.  
  24. # Here i am getting value 7 , then reading that to string
  25.  
  26. x = (list(dict1.keys())[list(dict1.values()).index(final_value)])
  27. string1 = string1 + x
  28. print(string1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement