Waste less time on Facebook — follow Brilliant.
×

Computer Science Warmups

Computers encrypt digital information and allow people to collaborate across the globe. Computer science studies the theory behind mechanisms like these and the practical details needed to build them. See more

Common Computing Mistakes

Samantha doesn't care about the name of her variables after she has defined them. This often results in duplicate names in her Python code.

What is the output of the code below?

1
2
3
4
5
6
variable = 1    
variable = 2    
VARIABLE = 3     
VARIABLE = 4    

print variable + 2 * VARIABLE

Graham has written a "dump" function to sell all the shares of his tech stocks in case of bad news in the market. However, he hasn't tested his code, and he's made an important mistake:

1
2
3
tech_stocks=["AAPL","IBM","GOOG","ORCL","HPQ"]      
for stock in [1, 2, 3, 4, 5]:     
    sell_shares(tech_stocks[stock])     

Why will this code fail?

Tony wants to print out the full lyrics to the song "99 Bottles of Beer on the Wall," so he's written the code below. The song is supposed to involve taking down each of the 99 bottles in turn and passing them around until all the bottles have been taken down and passed around.

1
2
3
4
5
6
7
8
9
number=99
while number > 1:
# each loop prints one verse of the song
    print number + " bottles of beer on the wall." 
    print number + " bottles of beer on the wall." 
    print "Take one down; pass it around." 
    number = number -1
    print number + " bottles of beer on the wall." 
    print " "

Xavier works at a sporting goods shop in the online sales department, and he is debugging the code below, which is designed to call the purchase_item() function, until all desired items have been bagged. The purchase_item() function will purchase exactly 1 item each time it is called.

What is wrong with the code?

1
2
3
4
5
purchased=0
quantity_desired = 10

while purchased < quantity_desired:
    purchase_item()

John can't get his code to work correctly. He's trying to calculate the Fibonacci sequence, but his code isn't working. What is he doing wrong?

1
2
3
4
5
6
7
8
9
def fib(n):    
   if n==0:
      return 0
   if n==1:    
      return 1
   else:
      fib(n-1) + fib(n-2)

print fib(4)
×

Problem Loading...

Note Loading...

Set Loading...