Super Sale (Code Coach) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Super Sale (Code Coach)

All the text cases but test case 4 are correct. And text case 4 is locked, can anyone tell me what is wrong: import numpy as np # Introduced numpy as np def savings(x): x = x.split(",") # This splits the input into a list # We get the index of the maximum value and remove the value from the list cost = x.index(max(x)) expensive = float(x.pop(cost)) # Then introduce it into a variable # We created variables initial_price and discounted_price initial_price = expensive discounted_price = expensive # We then iterate over the remaining items in the list for rem in x: initial_price += float(rem) # Added them to the initial discounted_price += float(rem) * 0.7 # discounted them b4 adding tax = (initial_price - discounted_price) * 1.07 # We subtracted the price to get savings and added tax(7%) print(int(np.floor(tax))) purchase = input() savings(purchase) Also would appreciate it if you could make it cleaner

29th Dec 2022, 12:53 PM
T0N1
T0N1 - avatar
14 Answers
+ 3
ok. Yes. It depends on input values. For example: if input have a 10, then if you convert to float, it becomes 10.0 but if you search max index 10.0 will not be in your original list. If you convert to int, then values like 10.2 will be convert to int as 10 so this also won't exist in original list. Without convert, max ( x) return maximum string , ex: "10", "8" returns "8", instead of 10 because of str comparisons. So type mismatches leads to error or wrong result. So better to convert first to floats at first, then there is no need to float cast in loop. edit: T0N1 x = list( map(float,x.split(",")) ) cost = x.index( max(x) )
29th Dec 2022, 1:37 PM
Jayakrishna 🇮🇳
+ 7
this is how i would have done it: inp = list(map(float, input().split(','))) # input, split and map to float as a list prices = sum(inp) - max(inp) # calculate the relevant price print(int(prices * 0.3 * 1.07)) # output savings as int
30th Dec 2022, 9:03 PM
Lothar
Lothar - avatar
+ 3
Ion Kare you do know that you deleted the max number 😐
30th Dec 2022, 6:11 PM
T0N1
T0N1 - avatar
+ 2
a = [float(i) for i in input().split(",")] a.remove(max(a)) b = sum(a) / 100 * 30 print(int((b * 7) / 100 + b)) cleaner
30th Dec 2022, 3:18 AM
Ion Kare
Ion Kare - avatar
+ 2
def savings(x): x = list(map(float,x.split(","))) # cost = x.index(max(x)) expensive = float(x.pop(cost)) # initial_price = expensive + sum(x) discounted_price = expensive + sum(x)*0.7 tax = (initial_price - discounted_price) * 1.07 print(int(tax)) purchase = input() savings(purchase) #can be like this, if you need improvement as clean code..
30th Dec 2022, 6:40 PM
Jayakrishna 🇮🇳
+ 1
max(x) ? What it does? Expecting a max string or max float type values? For later, convert first x to list of float values....
29th Dec 2022, 1:00 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 I tried that it kept returning Typeerror
29th Dec 2022, 1:02 PM
T0N1
T0N1 - avatar
+ 1
Code? Where? What changes you did?
29th Dec 2022, 1:04 PM
Jayakrishna 🇮🇳
+ 1
What change you did? Try this cost = x.index(str(max(map(int,x)))) Instead of cost = x.index(max(x))
29th Dec 2022, 1:14 PM
Jayakrishna 🇮🇳
+ 1
Returns ValueError
29th Dec 2022, 1:16 PM
T0N1
T0N1 - avatar
+ 1
Try cost = x.index(str(max(map(float,x)))) It would be better, first convert all to float list, instead of when using..
29th Dec 2022, 1:23 PM
Jayakrishna 🇮🇳
+ 1
Since you are creating a new list(map) max number (float) isn't present in original list
29th Dec 2022, 1:28 PM
T0N1
T0N1 - avatar
+ 1
Thanks Jayakrishna🇮🇳 You solved the problem
29th Dec 2022, 3:17 PM
T0N1
T0N1 - avatar