bill splitter by yungh og

This commit is contained in:
Samartha 2022-10-06 23:05:55 +05:30
parent aeff825da8
commit 363b2bae2e
3 changed files with 156 additions and 5 deletions

View File

@ -17,16 +17,16 @@
# How to start
* Well it's quite simple just thought of any Idea that can be automated
* Equip your weapon, I mean Python and required libraries 😂 (**[Refer here](https://automatetheboringstuff.com/)** for some basic guides to enhance Python language)
* Yess That's all you need 😋
- Well it's quite simple just thought of any Idea that can be automated
- Equip your weapon, I mean Python and required libraries 😂 (**[Refer here](https://automatetheboringstuff.com/)** for some basic guides to enhance Python language)
- Yess That's all you need 😋
## Hacktoberfest 2022 Update
**See full details and guidelines on**
* [Hacktober cheasheat](https://github.com/metafy-social/daily-python-scripts/blob/master/HACKTOBERFEST.md)
* [Official website](https://hacktoberfest.digitalocean.com/)
- [Hacktober cheasheat](https://github.com/metafy-social/daily-python-scripts/blob/master/HACKTOBERFEST.md)
- [Official website](https://hacktoberfest.digitalocean.com/)
<h1 align=center> How to Contribute 🤔 </h1>
@ -41,7 +41,9 @@ You can contribute by adding new scripts, improving current scripts or Documenta
- [x] Please use flake8 linting in your code (See below to check flake8 linting)
# Contributors 💪😎
Thanks a lot for spending your time helping! Keep rocking 🍻
<!-- readme: contributors -start -->
<table>
<tr>
@ -345,5 +347,14 @@ Thanks a lot for spending your time helping! Keep rocking 🍻
<sub><b>Sameer Sahu</b></sub>
</a>
</td></tr>
<tr>
<td align="center">
<a href="https://github.com/yunghog">
<img src="https://avatars.githubusercontent.com/u/41548444?v=4" width="100;" alt="yunghog"/>
<br />
<sub><b>YUNGH OG</b></sub>
</a>
</td>
</tr>
</table>
<!-- readme: contributors -end -->

View File

@ -0,0 +1,50 @@
# Bill Splitter
> Python script to split bills equally
## Introduction
Imagine you go to a restaurant with four of your friends. The bill will be $105. You give $50 and others chip in $40, $10 and $5 and another friend dont have change so he didn't give. Bill should have been split $21 each way so now you need to calculate who owes howmuch to whom. This python script will solve this problem. The script takes contributions (individual share) to a bill and then gives who owes how much to whom with minimum number of transaction
## Usage
Run the script.py
```bash
cd bill_splitter
python script.py
```
Enter the number of people among whom the bill is supposed to be split. eg: `2`
```
Number of participants : 2
```
Enter the name of participant and his/her contribution to the bill separated by a space. eg: `John 50`
```
Enter name and contribution of participant 1 :
John 50
Enter name and contribution of participant 2 :
Travis 100
```
Output
```
John paid $50
Travis paid $100
--------------------------------
Total pool amount : $150
Per head : $75.0
--------------------------------
John should pay $25.0 to Travis
--------------------------------
```
## Conclusion
The algorithm is designed in such a way that it will generate a result that has least number of transaction involved to achieve maximum efficiency.
## Author : Samartha | [@yunghog](https://github.com/yunghog)

View File

@ -0,0 +1,90 @@
# python script to split bills among a group
from os import system
# function to create contribution object from input string
def createContribution(s):
s = s.split(' ')
return {'name': s[0], 'contribution': int(s[1])}
# function to split bill among group
def split_bill(pool: "list[dict]") -> dict:
contribution_list = [x["contribution"] for x in pool]
total = sum(contribution_list)
each = total / len(contribution_list)
more = []
less = []
solution = []
for i in pool:
if i["contribution"] < each:
less.append(
{"name": i["name"], "contribution": each - i["contribution"]})
else:
more.append(
{"name": i["name"], "contribution": i["contribution"] - each})
for i in more:
a = i["contribution"]
m = [{"name": k["name"], "contribution": 0} for k in less]
for j in range(len(less)):
b = less[j]["contribution"]
if a == 0:
m[j]["contribution"] = 0
elif a - b == 0:
a = a - b
m[j]["contribution"] = b
less[j]["contribution"] = 0
elif a - b > 0:
a = a - b
less[j]["contribution"] = 0
m[j]["contribution"] = b
elif a - b < 0:
less[j]["contribution"] = b - a
m[j]["contribution"] = a
a = 0
solution.append({"name": i["name"], "payment": m})
return {"solution": solution, "total": total, "each": each, "pool": pool}
# function to print solution in a format
def print_solution(result: dict) -> None:
for i in result["pool"]:
print(f"{i['name']} paid ${i['contribution']}")
print("------------------------------------------------------------------")
print(f"Total pool amount : ${result['total']}")
print(f"Per head : ${result['each']}")
print("------------------------------------------------------------------")
for i in result["solution"]:
for j in i["payment"]:
if j["contribution"] > 0:
print(
f"{j['name']} should pay ${j['contribution']} to {i['name']}")
print("------------------------------------------------------------------")
def print_banner():
banner = '''
---------------------------------------------------------------------------
'''
print(banner)
system('cls')
print_banner()
pool = []
n = int(input('Number of participants : '))
for i in range(n):
print('Enter name and contribution of participant ', i+1, ' : ')
pool.append(createContribution(input()))
system('cls')
print_banner()
print_solution(split_bill(pool))