Last active
May 9, 2021 10:57
-
-
Save ahmed4end/e3de22462c35f246fc38440d8938cbda to your computer and use it in GitHub Desktop.
return money amount in change .
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
change = lambda t, r=[], coins={5,3,2,1}: r if t==0 else change(*(lambda g: (t-g, r+[g]))(t-min(t-i for i in coins-{sum(r)+t} if t-i>=0))) | |
print(change(999)) | |
#better attempt | |
change = lambda x,y=[]: y if x<=0 else change(*(lambda series: (x-[i for i in series if x>=i][0], y+[[i for i in series if x>=i][0]]))([200, 100, 50, 20, 10, 5, 1])) | |
#with roman encode method | |
solution = lambda x,y="": y if x<=0 else solution(*(lambda series: (x-[i for i in series if x>=i][0], y+{1000:"M", 500:"D", 100:"C", 50:"L", 10:"X", 5:"V", 1:"I"}[[i for i in series if x>=i][0]]))([1000, 500, 100, 50, 10, 5, 1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment