diff --git a/README.md b/README.md index 22ca1ec..2ae0500 100644 --- a/README.md +++ b/README.md @@ -352,6 +352,14 @@ Thanks a lot for spending your time helping! Keep rocking 🍻
Sameer Sahu - + + + + yunghog +
+ YUNGH OG +
+ + - + \ No newline at end of file diff --git a/scripts/bill_splitter/README.md b/scripts/bill_splitter/README.md new file mode 100644 index 0000000..76a33c5 --- /dev/null +++ b/scripts/bill_splitter/README.md @@ -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) diff --git a/scripts/bill_splitter/script.py b/scripts/bill_splitter/script.py new file mode 100644 index 0000000..a13a79a --- /dev/null +++ b/scripts/bill_splitter/script.py @@ -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))