def func(x):
return x*(x-4)
x = 3
print(func(x))
What's the output?
Out: -3
Evaluate the following expression. If the following will produce an error, say so instead of evaluating.
print('cat' + 'dog')
Out: catdog
Integer, Float, Boolean, List, String.
If you wanted a variable to keep track of how many times a user has started your program, what variable type would you choose?
Integer
Accumulator, Aggregator, Fixed, Most-Recent, One-way-flag, Best-so-far, Stepper, Walker.
You want to use a variable to keep track of how many times someone has said hello to you in a day.
Accumulator.
Convert the following binary number to decimal:
1101
8+4+0+1 = 13
def func(a,b):
return 3*a+2*b
a = 5
b = 3
print(func(b,a))
Out: 19
Evaluate the following expression. If the following will produce an error, say so instead of evaluating.
print(3**3-7)
Out: 20
Integer, Float, Boolean, List, String.
If you wanted to store whether or not a user had clicked on a part of the screen, what variable type would be best to hold this information?
Since this is a True/False value, this would be a Boolean.
Accumulator, Aggregator, Fixed, Most-Recent, One-way-flag, Best-so-far, Stepper, Walker.
You count the number of words on the first page of a book and store that number to a variable. You go through the book page by page, and if a page has more words than the value stored in your variable, you assign the number of words on that page to your variable.
Best-so-far.
Convert the following decimal number to binary:
47
47 - 32 = 15
15 - 8 = 7
7 - 4 = 3
3 - 2 = 1
1 - 1 = 0
101111
def func(a,b):
return 2*a+b
a = 5
b = 3
a = func(b,a)
b = func(b,a)
print(a, b)
Out: 11 17
Evaluate the following expression. If the following will produce an error, say so instead of evaluating.
a=3
print(a*5-(a+4))
Out: 8
Integer, Float, Boolean, List, String.
You want to keep track of how long each user who accesses a specific page on your website stays on that page before leaving, down to one-tenth of a second in accuracy.
List of floats.
Accumulator, Aggregator, Fixed, Most-Recent, One-way-flag, Best-so-far, Stepper, Walker.
Var1: As you read through a book, you keep track of your current page number.
Var2: As you read through a book, you store the contents of the current page you're reading into this variable.
Var1: Stepper (position within an iterable)
Var2: Walker (value at a specific position within an iterable).
Convert the following decimal number to binary:
358
358-256=102
102-64=38
38-32=6
6-4=2
2-2=0
101100110
def func(x,y):
if x < y:
return y - x
else:
return x - y
What does this function accomplish? What is the equivalent mathematical operation?
The function returns the positive difference between the two arguments.
This is equivalent to |x-y|.
a = 5
b = 12
print( (a**2 + b**2)**0.5 )
# for a bonus 50
print( (a+b)%3 )
Out: 13
Out: 2
Integer, Float, Boolean, List, String.
You want to keep track of whether or not each user who has an account on your website has or has not agreed to the Acceptable Use Policy.
List of Booleans.
Accumulator, Aggregator, Fixed, Most-Recent, One-way-flag, Best-so-far, Stepper, Walker.
Weird roles:
Normally, we think of a list as an aggregator, but if values are not being added over time, a list doesn't have to be an aggregator.
If a list holds the 3-dimensional (x,y,z) coordinate of a hidden loot box in a level of a game you have designed, what variable role best describes this list/tuple?
Fixed, since the value presumably won't change within the level.
What is the largest number that can be represented by a 10 digit binary number?
1023
def func(x):
if x == 0 or x == 1:
return 1
else
return x*func(x-1)
What does this recursive function accomplish? What is the equivalent mathematical operation? It might be helpful to try a few inputs.
This is the factorial function.
Ex func(5) = 5! = 5*4*3*2*1 = 120
Evaluate the following expression. If the following will produce an error, say so instead of evaluating.
a=4
b=3
print(a**(b-1)-len(str(a+3*b)))
Out: 14
Integer, Float, Boolean, List, String.
You want to keep track of the GPS coordinates -- two numbers, latitude and longitude, accurate to 3 decimal places -- for each user who accesses your website.
List of list of floats.
Accumulator, Aggregator, Fixed, Most-Recent, One-way-flag, Best-so-far, Stepper, Walker.
Weird roles:
Normally, we think of a list as an aggregator, but if values are not being added over time, a list doesn't have to be an aggregator.
A list of three numbers keeps track of your characters position in a 3d game you are designing (x,y,z). Your characters changes position based on user input and based on other unpredictable things that happen to the character when the game is running.
Translate the following binary tuple to decimal, and use that information to determine what color this would be if the tuple were interpreted as a pixel.
(11011011,00100100,10101010)
(219,36,170)
Lots of red, lots of blue, should be purple or pink-ish.