Image source:
Nick Depree
Every day Cookie Monster gets cookies from the store, and eats cookies from his cookie jar. The following python code simulates his actions from yesterday.
A call to the function cookie_monster_simulation() returns an integer. What is this integer?
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Details and assumptions
range(0, 88) = [0, 1, 2, 3, ... 87]
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.
But if we not specify by curly braces then it execute only one statement????? plz elaborate it!!!
Log in to reply
It sounds like you're unfamiliar with Python's syntax. Unlike languages closer to C in syntax, Python does not use curly braces to set aside blocks of statements, instead white space is used. In this case, code is in a block if it is all indented the same amount.
If you see the for-loop, everytime i gets a value, it substracts just one number, so you must substact 8 8 from 1 0 0 , hence the answer is 1 2
cookies=100 at start. cookies=cookies+1 makes cookies=101 and cookies=cookies-1 makes cookies=100. Then the loop starts with i in range(0,88),i,e, the loop runs 88 times and we can see that in the loop there is net decrement of cookie by 1 in each turn, so cookies=100-(1+1+1+..upto 88 terms) = 100-88 = 1 2
thanks for brefing
Every pair of code containing (cookies = cookies + 1) and (cookies = cookies - 1) cancel out because they do not affect the number of cookies. Now, we are left with cookies = cookies - 1 in the for loop which is done 88 times.
Thus, answer = initial no. of cookies - 88*1 = 12
C++ int fn(); void main() { int ans=0; ans=fn(); cout<<ans; getch(); } int fn() { int c=100; for(int i=0;i<88;i++) {c=c-1;} return c; }
100 + 1 = 101 101 - 1 = 100 100 - 88 = 12
because it at every iteration gain a cookie and eat 2 cookies
Its Simple Iterations Were From 0,88 means 0 to 87 iterations total 88 iterations each iteration adds 1 and subtracts 2 that means each iteration subtracts 1 so 88 iterations subtract 1 each iterations will result 88 subtractions total cookes 100 - 88 cookies = 12 are left :)
at first =100; if i=0; then 100+1-2=99; at last 100-88=12;
initially cookies=100 for every i 1 cookie is subtracted and at last for i=88 cookies=100-88=12 simple :)
assume c=cookies; then run following code you will get c=12
void main() { int c = 100,i;
c = c + 1;
c=c-1;
for(i=0;i<88;i++)
{
c = c + 1;
c = c - 1 ;
c = c - 1;
}
printf("%d",c);
getch();
}
12
static int cookies() { int c=100; c=c+1; c=c-1; for(int i =0; i<88;i++) { c=c+1; c=c-1; c=c-1; } return c; }
using namespace std;
int cookie() { int cookies=100;
cookies++; cookies--;
for(int i=0;i<88;i++) { cookies++; cookies--; cookies--;
}
return cookies;
}
int main() {
int s=cookie();
cout<<s;
return 0; }
12
100 + (88 * ( 1 - 1 - 1)) = 12
(100 + 1 -1 -1) - 87 ----> 99 - 87 = 12 (ANSWER)
We can disregard the increment and decrement cookie amount lines since they cancel out to just leave you with 100 cookies. We can also ignore the first two lines of the for loop because they too cancel. Now, we can see that 100 will be incremented 88 times because of the range 0-88. So, just 100-88 = 12
At the end, the code becomes a series of descending numbers 9 9 , 9 8 , 9 7 , . . . , 1 4 , 1 3 , 1 2
Hence, a call to the function will return 1 2
(100+88)-88*2=12
Well, the core of the cookie monster simulation() method is its for loop. It cycles from range(0, 88) = [0, 1, 2, 3, ... 87] , which means there will be 88 executions of the instructions inside it. There are 3 instructions that are contained in the loop.
The variable -cookie- will have its value increased by 1
Then, the variable -cookie- will have its value decreased by 1
Lastly, the variable -cookie- will have its value decreased by 1 again
If we add up all these, the result would be having only one instruction ( +1-1-1 will be -1 ),repeated 88 times. The cookie balance is going to be smaller by 1 unit, each iteration, which makes us draw the conlusion that by the end of the foor loop ( which symbolizes 88 days ), there will be 100 ( initial cookies ) - 88 = 12 cookies left.
I just found out that there are 88 iterations. And in each iteration, he loses on cookie. He had 100 cookies in the start, then after 88 iterations he would have (100 - 88) = 12 cookies!!
it is one* cookie! sorry!
0-87 is 88 numbers. 100-88=12
did the same thing
There is a net loss of 1 cookie for each n. The range (0,88) spans 88 n, so 88 total cookies are lost over this range, leaving 12.
For each iteration of the for loop, the number of cookie decrease by 1. So after 88 iteration, the number of cookie left which is return by the function = 100 - 88 = 12
Before the loop, the Cookie Monster gets a cookie and eats a cookie, so the total is unchanged. In the loop, the monster gets a cookie and eats two cookies, so in every recurrence the number of cookies is decreased by one. After 88 times, the number of cookies is 100 - 88 = 12.
For each iteration of the loop, a cookie gets reduced. So, after 88 iterations, 88 cookies will be eaten by the monster. Initially he had 100 in the jar. Hence he is left with 100-88=12 cookies at the end.
add 1 and subtract 2 with 100 , 88 times!!
Dude, so simple +1 and -1 cancel each other out 100-88 = 12 simple subtraction... it's great being a nerd!
Problem Loading...
Note Loading...
Set Loading...
Note that Cookie Monster starts with 100 cookies. Before the for loop, he gets a cookie and eats a cookie, getting a net gain of 0 cookies. Now, in the for loop, he gets a cookie, eats one, and then eats one, having a net gain of − 1 cookies per loop. Since he repeats this 8 8 times, he ends up with 1 0 0 − 8 8 = 1 2 cookies.