Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- 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.
- ( Ex: 8 can be (A+E) or (A+B+C) but result should be A+E as we used just 2 letters)
- """
- A = 1
- B = A*2+ 1
- C = 4
- D = 1
- E = B*1 + 4
- finalvalue = 8
- minimum = float("inf")
- dict1 = {"A": 1, "B": 3, "C": 4, "D": 1, "E": 7 }
- value_chosen = 8
- string1 = ""
- for i, j in dict1.items():
- if abs(j - value_chosen) < minimum:
- final_value = j
- minimum = abs(j - value_chosen)
- print(final_value)
- # Here i am getting value 7 , then reading that to string
- x = (list(dict1.keys())[list(dict1.values()).index(final_value)])
- string1 = string1 + x
- print(string1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement