JamCoders

💾 Download

Lecture 1 Extra Exercises

Question 2 asks you to write functions. We have only briefly covered functions in class, so if you have having trouble, feel free to skip these questions or ask us for help!

Question 1: Power!

1.1

The exponentiation operator in python is **. You might have seen the ^ character on your calculator. They do the same thing.
For example :
2**2 = 4
3**3 = 27
25**0.5 = 5

Compute $2^{10}$:

In [ ]:
# Write and test your code here.

1.2

Does Python do exponentiation first, or other operations like +, -, *, /, //, %? In other words, find out how the power operator (**) ranks in terms of order of operations.

(For example, you could test if a * b ** c == (a * b) ** c)

In [ ]:

1.3

What types can you exponentiate?

In [8]:
# Explore exponentiation here
Out[8]:
5

Question 2: Functions!

2.1

Write a function kthDigit that takes two integer arguments n and k and returns the k-th digit of n ?
For example :
kthDigit(567, 2) should return 6
kthDigit(324877, 5) should return 2
kthDigit(235468, 1) should return 8

In [ ]:
# Write and test your code here.

2.2

Write a function kEdit that takes three integer arguments n, k, and e with $0 \leq e \leq 9$ that replaces the k-th digit of n with e.

Examples :

kEdit(1234, 2, 0) should return 1204
kEdit(34578, 3, 1) should return 34178

In [ ]:
# Write and test your code here.

2.3

An RGB value is three colors that combine to make up one color. Write a function rgbconvert that takes in a 9 digit integer n, and returns 3 integers that represents each RGB value.

Examples:

rgbconvert(255255255) => 255, 255, 255

rgbconvert(132234075) => 132, 234, 75

In [ ]:
# Write and test your code here.

Question 3: Guess and Check!

3.1

Given this code:

x = 12
y = 5
z = y
y = 12
x = 1
z = x + y + z

what is the value of z? Verify your solution by running the code above.

In [ ]:
# Your answer:


# Verify your answer here.

3.2

Given this code:

z = 5 * (12 % 1) / 18
x = 23 - 14
y = x * z
z = 2 * x

what is the value of z? Verify your solution by running the code above.

In [ ]:
# Your answer:


# Verify your answer here.

3.3

Given this code:

d = 1
c = d
b = c
a = b
c = 2

what is the value of a? Verify your solution by running the code above.

In [ ]:
# Your answer:


# Verify your answer here.