How to solve the BallPark Order Challange ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to solve the BallPark Order Challange ?

I managed to solve the BallPark Order challenge on my PC yet here in SoloLearn app does not pass the test cases! Any help how could I do it would be appreciated! My code it attached! https://code.sololearn.com/cNQgbgtWhP4m/?ref=app

23rd Dec 2019, 4:14 PM
Roberto M
Roberto M - avatar
30 Answers
+ 10
order = input() #'Mierda Mierda Mierda Mierda' #input() list_ord = {'Nachos': 6, 'Pizza': 6, 'Cheeseburger': 10, 'Water': 4, 'Coke': 5} productos = ['Nachos', 'Pizza', 'Cheeseburger', 'Water', 'Coke'] suma = 0 new_ord = order.split(" ") for i in new_ord: if (i in productos) == True: suma += list_ord.get(i) else: suma += list_ord.get('Coke') print(round(suma*1.07, 2))
25th Feb 2022, 5:38 AM
Herbeth Cortez Gonzalo
Herbeth Cortez Gonzalo - avatar
+ 5
a=input() a=a.split(" ") sum=0 for i in a: if i=="Nachos": sum+=6 elif i=="Pizza": sum+=6 elif i=="Cheeseburger": sum+=10 elif i=="Water": sum+=4 else: sum+=5 print(round(sum*1.07,2))
6th Sep 2022, 7:29 AM
Casmir Gindol
Casmir Gindol - avatar
+ 3
Thank You very much Thomas! Funny, I wrote the exact same code 1h ago and I delete it as it would print the prices separately and not as a total. Now I realised it was because I did not indent Print () function properly. Regards,
23rd Dec 2019, 7:54 PM
Roberto M
Roberto M - avatar
+ 2
a=input() b=a.split() my_dict= {"Cheeseburger":10, "Water":4, "Coke" : 5, "Nachos": 6, "Pizza": 6} res=0 for i in b : if i in my_dict : res= res+my_dict[i] else: res = res + my_dict["Coke"] sonuc= float(res*1.07) print(round(sonuc,2)) I've found that, I've missed to "round"
26th Jan 2023, 7:51 PM
Mustafa Zengin
Mustafa Zengin - avatar
+ 1
Sometimes, the input string have an \r at the end. Try adding .strip() to your input (likely the last) and see if that solves the problem.
23rd Dec 2019, 5:43 PM
HonFu
HonFu - avatar
+ 1
You will get the input as one String. In the task descriped as: Input Format You are given a string of the four items that you've been asked to order that are separated by spaces. So you can't get it item by item. Put the whole order in a list. Like this: order = input(). split()
23rd Dec 2019, 5:51 PM
Coding Cat
Coding Cat - avatar
+ 1
How do I convert that list [] populated by strings to int(numbers) ?
23rd Dec 2019, 6:35 PM
Roberto M
Roberto M - avatar
+ 1
I managed to solve this on my PC. I get the total for every combination of 4 items. But here in SoloLearn?! I have to do it exactly as the TEST Cases and it doesn't work. Do you have any tips as to how to solve this ?
23rd Dec 2019, 7:20 PM
Roberto M
Roberto M - avatar
+ 1
Yes, include "Nachos"
23rd Dec 2019, 7:26 PM
Dennis
Dennis - avatar
+ 1
Fine Delete the print Now we make a loop over that list: sum = 0 for i in order: if i == 'Nachos' sum += 6 elif i == 'Coke' sum += 5 . . else: sum +=5 This added All items. The last "else" is for the case, the ordered item is not to have. print(sum) #to check if it works as expected
23rd Dec 2019, 7:40 PM
Coding Cat
Coding Cat - avatar
+ 1
And next month you will do that with a dict like this price = {'Nachos':6.00, 'Pizza': 6.00, 'Cheeseburger': 10.00, 'Water': 4.00, 'Coke': 5.00} It's shorter and a little bit nicer 😉
23rd Dec 2019, 8:01 PM
Coding Cat
Coding Cat - avatar
+ 1
bit of tip, try using a for loop instead to avoid repeating codes
24th Dec 2019, 8:04 AM
Shen Bapiro
Shen Bapiro - avatar
+ 1
I think it’s more beautiful to use dictionaries to solve this problem: menu = {'Nachos': 6, 'Pizza': 6, 'Cheeseburger': 10, 'Water': 4, 'Coke': 5} order=input() total = 0 ord= order.split() for i in ord: if i in menu: total += menu[i] else: total += menu["Coke"] print(total+(total*0.07)) It’s crucial to split the input otherwise the output number will be much higher than expected.
14th Aug 2022, 3:33 PM
Nastaran Salimi
Nastaran Salimi - avatar
+ 1
x = input().split(" ") menu = {"Pizza"} sum = 0 for i in x: if i=="Pizza": sum +=6 elif i=="Nachos": sum +=6 elif i=="Cheeseburger": sum+=10 elif i=="Water": sum+=4 else: sum+=5 fsum = sum + (sum*7)/100 print (fsum)
26th Jan 2023, 2:41 PM
Amirmohammad Rabiee
Amirmohammad Rabiee - avatar
0
You have to read the items from the list. For exsample, if order is your list: item1 = order[0] item2 = order[1] and so on. And if you have the items, maybe go your way with the cases and prices.
23rd Dec 2019, 6:45 PM
Coding Cat
Coding Cat - avatar
0
We can do that STEP by STEP if you want. start with order = input(). split() print(order) # to See what we have. Make input in one line: Coke Nachos Coke SUBMIT Do you See the list?
23rd Dec 2019, 7:29 PM
Coding Cat
Coding Cat - avatar
0
Yes ['Coke', 'Nachos', 'Coke']
23rd Dec 2019, 7:32 PM
Roberto M
Roberto M - avatar
0
You're welcome
23rd Dec 2019, 7:55 PM
Coding Cat
Coding Cat - avatar
0
I'm still a beginner, so my code was a little longer/ less efficient than some of these others, but here's what worked for me: #include <iostream> #include <string> using namespace std; void orders(string ordArr[], int size) { int f = 0; for (int i = 0; i < size; ++i) { if (ordArr[i] == "Cheeseburger") f += 10; else if (ordArr[i] == "Nachos") f += 6; else if (ordArr[i] == "Pizza") f += 6; else if (ordArr[i] == "Water") f += 4; else f += 5; } int subtotal = f; float tax = subtotal * 0.07; float total = f + tax; cout << total << endl; } int main() { string arr[4] = {"", "", "", ""}; cin >> arr[0] >> arr[1] >> arr[2] >> arr[3]; orders(arr, 4); return 0; }
2nd Nov 2022, 5:07 PM
Cody Morris
Cody Morris - avatar
0
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { try { Scanner scan = new Scanner(System.in); HashMap<String, Integer> prices = new HashMap<>(); prices.put("Nachos", 6); prices.put("Pizza", 6); prices.put("Cheeseburger", 10); prices.put("Water", 4); prices.put("Coke", 5); double finalprice = 0; String[] input = new String[4]; for (int i = 0; i < input.length; i++) { String check = scan.next(); if (prices.containsKey(check)) { input[i] = check; } else input[i] = "Coke"; } for (String h : input) { prices.containsKey(h); finalprice += prices.get(h); } float tax = (float) ((finalprice * 0.07)); System.out.printf("%,.2f", finalprice + tax); scan.close(); } catch (Exception t) { System.out.println("Invalid Input"); } } } //for java users Using HashMap and for Loops this can be done easliy.Hope this helps
18th Nov 2022, 11:01 AM
Isuru
Isuru - avatar