+ 1

Is there a shorter way to solve this code coach challenge? (Ballpark Order)

menu = { "nachos": 6, "pizza": 6, "cheeseburger": 10, "coke": 5, "water": 4 } total = 0 tax = 0.07 order = input().lower() orderList = order.split(" ") for item in orderList: if item not in menu: item = 'coke' total += menu[item] total += (total * tax) print(total)

13th Aug 2023, 12:18 AM
Jayp Bazar
Jayp Bazar - avatar
14 Answers
+ 6
One-liner, tested and proven to pass: print(sum({"Pizza":6.42, "Water":4.28, "Nachos":6.42, "Cheeseburger":10.7 }.get(order, 5.35) for order in input().split())) Note that it applies the tax to the price up front. It works well, because base prices are in whole dollar amounts. Coke is omitted because it is the same price as Other.
13th Aug 2023, 5:39 AM
Brian
Brian - avatar
+ 5
Alaa Aldeen Shammr true, keeping it organized and readable is a must if you plan to keep the code maintainable. coding challenges are free from this constraint, though. safety and sanity is optional...😁. Exploring unconventional options is a good way to get a deeper understanding on the quirks of the language. all work and no play is never good. Just always be aware that being fancy and adventurous is not for serious production codes. a time and place for everything.
14th Aug 2023, 12:44 AM
Bob_Li
Bob_Li - avatar
+ 4
Bob_Li there was a similar discussion for a C solution. https://www.sololearn.com/Discuss/3172787/?ref=app You might enjoy my obfuscated C version that resulted. It uses a case statement to compare the string values. Not easy to do in C, and not portable, but it is fast and small: https://code.sololearn.com/co06Hb0zK1yl/?ref=app
13th Aug 2023, 8:10 AM
Brian
Brian - avatar
+ 3
Bob_Li menu = { "nachos": 6, "pizza": 6, "cheeseburger": 10, "coke": 5, "water": 4 } print(sum([menu.get(x,5) for x in input().lower().split(' ')])*1.07)
13th Aug 2023, 4:02 AM
Alaa Aldeen Shammr
Alaa Aldeen Shammr - avatar
+ 3
Brian interesting method. Might be going offtopic, but it is still the same challenge. My C solution codebit is in the comment under your code. I basicalliy chained ternary expressions. I tried to modify it following your method but was stopped at the second test due to a strange rounding error. All other tests passed. The original strcmp method I used also passed. Any ideas why the result was different for this particular case? The computation should be identical.
13th Aug 2023, 1:29 PM
Bob_Li
Bob_Li - avatar
+ 3
the coke is used as the default base value. I feel it's something sneakier...
13th Aug 2023, 2:02 PM
Bob_Li
Bob_Li - avatar
+ 3
lis=input().split() cost=0 for i in lis: if(i=='Nachos' or i=='Pizza'): cost=cost+6 elif(i=='Cheeseburger'): cost=cost+10 elif(i=='Water'): cost=cost+4 elif(i=='Coke'): cost=cost+5 else: cost=cost+5 print(cost+0.07*cost)
13th Aug 2023, 7:03 PM
Jakku Shashank
Jakku Shashank - avatar
+ 3
There is a large debate on this topic, some prefer short code and other readability is their first priority. But in my opinion as long as your code works it's fine, after that you can decide how to balance between short code and readability. For example this code, beyond the fact that it's an answer for a challenge, if i am writing it I would make sure to put the menu in a separate variable in case I want to add more items to it. Also tax_percentage it would be good if I put it in a variable . say if i want to change it from 7% to 5% instead of digging into the code and I might ruin things it would be nice if I can change the variable directly.
14th Aug 2023, 12:31 AM
Alaa Aldeen Shammr
Alaa Aldeen Shammr - avatar
+ 2
Alaa Aldeen Shammr doing some more trimming, (just for the challenge, constants could be hardcoded): total = 0 for item in input().lower().split(' '): total += { "nachos": 6, "pizza": 6, "cheeseburger": 10, "coke": 5, "water": 4 }.get(item,5) print(total*107/100) #interesting, but 1.07 doesn't work...
13th Aug 2023, 2:45 AM
Bob_Li
Bob_Li - avatar
+ 2
I detected the source of the "rounding" problem and replied to Bob_Li's comment there with the explanation. Actually his revision was failing to recognize "Water" due to a dirty buffer and so it added an extra dollar.
13th Aug 2023, 6:40 PM
Brian
Brian - avatar
+ 2
Brian "dirty buffer"... 😁 That's another new term to add to my debug dictionary. <i>bug discovered</i> Amazing as always.
13th Aug 2023, 9:45 PM
Bob_Li
Bob_Li - avatar
+ 1
menu = { "nachos": 6, "pizza": 6, "cheeseburger": 10, "coke": 5, "water": 4 } total = 0 tax = 0.07 orders = input().lower().split(' ') for item in orders: total += menu.get(item,5) total += (total * tax) print(total)
13th Aug 2023, 1:02 AM
Alaa Aldeen Shammr
Alaa Aldeen Shammr - avatar
+ 1
Alaa Aldeen Shammr Brian nice code evolution. 😎
13th Aug 2023, 7:35 AM
Bob_Li
Bob_Li - avatar
+ 1
My first attempt in solving this problem, only the first and second test cases were correct. Later I realized that I didn't include the coke if the order is not in menu. If that's not your problem, probably your calculation of the total amount has something wrong
13th Aug 2023, 1:41 PM
Jayp Bazar
Jayp Bazar - avatar