Simulating Cookie Monster Behavior

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
# Python code example
def cookie_monster_simulation():
    cookies = 100

    cookies = cookies + 1  # get a cookie!
    cookies = cookies - 1  # eat a cookie!
    for i in range(0, 88):
        cookies = cookies + 1  # get a cookie!
        cookies = cookies - 1  # eat a cookie!
        cookies = cookies - 1  # eat a cookie!

    return cookies

Details and assumptions

range(0, 88) = [0, 1, 2, 3, ... 87]


The answer is 12.

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.

29 solutions

Daniel Liu
Dec 15, 2013

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 0 cookies. Now, in the for loop, he gets a cookie, eats one, and then eats one, having a net gain of 1 -1 cookies per loop. Since he repeats this 88 88 times, he ends up with 100 88 = 12 100-88=\boxed{12} cookies.

But if we not specify by curly braces then it execute only one statement????? plz elaborate it!!!

kashif ali - 7 years, 3 months ago

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.

Ryan Braam - 7 years, 2 months ago
Mateo Torres
Dec 15, 2013

If you see the for-loop, everytime i i gets a value, it substracts just one number, so you must substact 88 88 from 100 100 , hence the answer is 12 12

Prasun Biswas
Dec 16, 2013

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 = 12 \boxed{12}

thanks for brefing

khantania khan.77 - 7 years, 4 months ago
Ryan Soedjak
Dec 16, 2013

100-88=12

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

Santosh Shaw
Mar 20, 2014

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; }

Sadia Siddiqa
Mar 13, 2014

100 + 1 = 101 101 - 1 = 100 100 - 88 = 12

Hafeez Khoso
Mar 13, 2014

because it at every iteration gain a cookie and eat 2 cookies

Saddam Ranjhani
Mar 13, 2014

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;

Pooja Joya
Feb 23, 2014

initially cookies=100 for every i 1 cookie is subtracted and at last for i=88 cookies=100-88=12 simple :)

Pushpendra Kumar
Feb 21, 2014

assume c=cookies; then run following code you will get c=12

include<stdint.h>

include<conio.h>

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

kashif ali - 7 years, 3 months ago
Johnson Thangjam
Feb 12, 2014

100-88= 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; }

Suchait Gaurav
Feb 1, 2014

include<iostream>

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

kashif ali - 7 years, 3 months ago
Ivan Dimitrov
Jan 5, 2014

100 + (88 * ( 1 - 1 - 1)) = 12

Budi Utomo
Dec 23, 2013

(100 + 1 -1 -1) - 87 ----> 99 - 87 = 12 (ANSWER)

  • The function starts with 100 cookies.
  • Adding 1 and Subtracting 1 is the same as doing nothing.
  • The loop is executed 88 times.
  • Inside the loop we can cross adding 1 to subtracting 1.
  • Inside the loop the code basically only subtracts 1.
  • Subtracts 88 from 100.
Andrew Tiu
Dec 18, 2013

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

Manoj Pandey
Dec 16, 2013

At the end, the code becomes a series of descending numbers 99 , 98 , 97 , . . . , 14 , 13 , 12 {99,98,97, ... ,14,13,12}

Hence, a call to the function will return 12 12

(100+88)-88*2=12

Muhammad Anwar - 7 years, 5 months ago
A.J Pradana
Jul 28, 2013

100 - 1(88) = 100 - 88 = 12

Razvan Barbu
Jul 24, 2013

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.

  1. The variable -cookie- will have its value increased by 1

  2. Then, the variable -cookie- will have its value decreased by 1

  3. 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.

Siddharth Kannan
Jul 23, 2013

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!

Siddharth Kannan - 7 years, 10 months ago
Kevin Larsen
Jul 23, 2013

0-87 is 88 numbers. 100-88=12

did the same thing

Space Hero - 7 years, 10 months ago
Rachael Affenit
Jul 23, 2013

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.

Yun Kai Lim
Jul 22, 2013

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

Battista Lonardi
Jul 22, 2013

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!

David Kroell - 7 years, 10 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...