Layla sells ice cream for 3 dollars per scoop, and she wrote the following python function to help her calculate how much to charge for n scoops.
1 2 3 4 5 6 7 8 9 |
|
There is a number A , such that calling calculate_n_times_3(A) returns 33. What is A ?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
I don't understand your solution. I just divided 33 by 3 and the answer is 11. 33 represents the total sales and 3 is the price. I got 11 as the number of ice cream bought.
So I did. But Icaro tries to explain the solution with the coherent python logic.
Yes, I am trying to explain using the code's logic and demonstrating how the loop works during the execution. It's a programming problem, so...
n = A = 11.
While i < 11 will make the loop runs 10 times (from 0 to 10).
(10*3) + 3 = 33;
answer = 0+3; i turns 1;
answer = 3+3; i turns 2;
answer = 6+3; i turns 3;
...
answer = 27+3 and i turns 9;
answer = 30+3 and i turns 10;
Here we have (i < n), or (10 < 11) == (true). Loop breaks.
So, answer returns 33 when n = 11.!
Here,the loop runs n times and the value of n is passed into the function .Every time the loop runs, the variable answer is incremented 3 times which shows that this loop basically gives the table of 3 Hence,if the function is returning 33 it means that the loop must run 11 times as 11*3=33 So, the value of n should be 11 so,answer is 11.
when n=11, while loop will be iterated 11 times (as i<n) to return 33. If condition is defined as(i<=n) then, for result 33 n should be 10.
yup u r rid i got it
It's looping problem:
if n = 1 1
So, answer=answer+3 looping from n=0 until n=10.
Since answer (0)=0, this program is recursive until n=10.
So, 3+3+...+3=33, where A=11
This is called as iteration in programming. Here it will be iterated for 11 times starting from 0 to 10. for each iteration answer is added with 3 so 11 times which can be simplified as (11*3 = 33)
The counter adds 1 and the function adds 3 . This is done from i = 0 to i = n − 1 , that is: a total of n times. Therefore, the program adds 3 a total of n times, i.e. finds 3 ⋅ n . Since we have 3 n = 3 3 , the original argument must have been 1 1 .
Nice problems, you can solve with some modification of the code like this:
# Layla's calculator - in Python
def calculate n times_3(n):
i = 0
answer = 0
while (i < n):
answer = answer + 3
i = i + 1
return answer
now you can call to get A :
for j in range(0,20):
if calculate_n_times_3(j) == 33:
print(j)
Using loop, from 0 to 20 (you can edit that, but, I can get answer just stopped at 11). So, the answer about number A is 11 .
Problem Loading...
Note Loading...
Set Loading...
n = A = 11.
While i < 11 will make the loop runs 10 times (from 0 to 10).
(10*3) + 3 = 33;
answer = 0+3; i turns 1;
answer = 3+3; i turns 2;
answer = 6+3; i turns 3;
...
answer = 27+3 and i turns 9;
answer = 30+3 and i turns 10;
Here we have (i < n), or (10 < 11) == (true). Loop breaks.
So, answer returns 33 when n = 11.!
Hope it helps!