Skip to content

Instantly share code, notes, and snippets.

@mie00
Last active October 23, 2017 15:30
Show Gist options
  • Save mie00/060233cb610bd0d3a8763ec43deb292e to your computer and use it in GitHub Desktop.
Save mie00/060233cb610bd0d3a8763ec43deb292e to your computer and use it in GitHub Desktop.
For bills and stuff
#!/usr/bin/env python
def calc(gross, delivery, d):
"""
usage:
>>> calc(120, 9, {
'Ashraf': 40,
'Thabet': 20,
'Awadi': 20,
'Ghanem': 20,
})
net: 100
Ghanem: 24.5
Thabet: 24.5
Ashraf: 46.5
Awadi: 24.5
total: 120.0
"""
s = sum(map(lambda (x, y): y, d.items()))
a2 = [(name, (lambda x:int(x) + [0,0.5,0.5,1][int(4 * (x % 1))])(i * 1.0 * (gross - delivery) / s + 1.0 * delivery / len(d))) for (name, i) in d.items()]
print("net: %s"%(s))
print('\n'.join(map(lambda (x, y): '%s: %s'%(x, y), a2)))
print('total: %s'%sum(map(lambda (x, y):y, a2)))
@abom
Copy link

abom commented Oct 23, 2017

Updated to use keyword arguments instead, it won't accept duplicated entries as passing a dict, and of course less typing 😆
https://gist.github.com/abom/db0f8c66782ee91939226b80a6a1397d

I was wondering what is wrong, at the end it was a duplicated dict item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment