+ 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)
14 Antworten
+ 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.
+ 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.
+ 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
+ 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)
+ 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.
+ 3
the coke is used as the default base value. I feel it's something sneakier...
+ 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)
+ 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.
+ 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...
+ 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.
+ 2
Brian
"dirty buffer"... 😁
That's another new term to add to my debug dictionary.
<i>bug discovered</i>
Amazing as always.
+ 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)
+ 1
Alaa Aldeen Shammr
Brian
nice code evolution. 😎
+ 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