+ 2
How to optimize this Code [Question closed]
3 Réponses
+ 5
Chris Jakob ,
we do not require the additiinal lists as used in your code.
suggestion how to solve this exercise:
> get current_case:
use a variable (current_total) to accumulate the total of sum of sales for current case
iterate over the dict, read values (age).
if age >= 18 add 20 to current_total, otherwise add 5
> get new_case:
use a variable (new_total) to accumulate the total of sum of sales for new case
iterate over the dict, read values (age).
if age >= input-number add 20 to current_total, otherwise add 5
now apply the mentioned formula to the 2 calculated total sales values:
((new_case-current_case)/current_case)*100
the output value should be converted to an *integer value,* as mentioned in the task description.
+ 2
the goal of the code was to find out how much more money you can earn in percentage if you lower the age limit it was never intended to raise the age limit
0
Though the question is closed, there seems to be double counting on blp if input age would be over 18 and double checking in the loop.
For example if the input is 30, blp would be ( 20 * <number of ages greater than or equal to 18>) + (5 * <number of ages less than 30>)
18-29 would be counted twice.
On the other hand, if input age would always be less than 18, the checks could be reduced. Suppose we still want the lists:
for value in data.values():
if value >= 18:
x.append(value)
else:
y.append(value)
if value >= age:
z.append(value)
else:
oz.append(value)