# Lecture 1, Part 2 Exercises¶

## Question 1¶

Use code for exercises 1.1-1.3

### 1.1¶

Use function print to print "Daniel". On a new line, print his occupation - "Lecturer". Finish by printing another new line with his favorite TAs - "Jessica", "Jess" and "Jessica Lee".

### 1.2¶

Given the code below, print x, y, and z on three different lines.

In [ ]:
x = 17
y = 19
z = 42


### 1.3¶

Given the code below, print u, v, and w on the same line.

In [ ]:
u = [3, 5, 8]
v = True
w = 3.14


## Question 2¶

Use code for exercises 2.1-2.3

### 2.1¶

Given the list list1, print the first element.

In [ ]:
list1 = ["coder", 2018, "addis"]


### 2.2¶

Given the list list2, print the last element in two ways.

In [ ]:
list2 = ['apple', 'orange', 'banana', 'pear', 'dragonfruit', 'lychee', 'starfruit']


### 2.3¶

Given list3, print the 5th through 8th indexed elements

Hint: You should be printing 4 elements

In [ ]:
list3 = [True, False, True, True, True, False, True, True, True, False]


### 2.4¶

Given list4 print the last 5 elements in two ways.

In [ ]:
list4 = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]


### 2.5¶

Given list5, create the list [4,3,"hello world", "Jelani"] using one line of code. How do you know you've succeeded?

Hint: print out the list to check

In [ ]:
list5 = [4, 3, 7, 'Jelani']


### 2.6¶

Given str1, create the string cat by using slicing.

In [ ]:
str1 = "caterpillar"
print(str1[:])  # modify this line


## Question 3¶

### 3.1¶

Try executing the code below. Can you change the output by changing only 1 character (there may be multiple solutions)?

In [ ]:
x = 42
y = 111
print(x < y)


### 3.2¶

What is different in the case below?

x = "42"
y = "111"
print(x < y)

### 3.3¶

Construct an 4 digit number and a 5 digit number such that the 4 digit number is greater than the 5 digit number when they are of type string.

### 3.4¶

What would be the result of the following operations?

'A' == 'a'
'Brunch'< 'banana'
'coffee' < 'Coffee'
'Dog' > 'cat'
'Daniel' != 'daniel'
'jessica' > 'jess'
'CORINA' < 'CORONA'

### 3.5¶

Check your answer in the box below. Can you figure out the rules for string comparisons?

In [ ]:
# Explore here
# print('A' == 'a')


## Question 4¶

### 4.1¶

Write a function that takes x as an argument, and returns 5 * x.

In [ ]:
def multFive(x):
return    # place code here


### 4.2¶

Write a function that takes x as an argument and returns x + 2.

In [ ]:
def addTwo(x):
return  # place code here


### 4.3¶

Write a function that returns 5 * x + 2, without using any arithmetic operations.

Hint: use multFive and addTwo. Please run the cells for exercises 4.1 and 4.2 before running this cell.

## Question 5¶

### 5.1¶

Write a function called addXtoY that takes two arguments x and y, and returns their sum.

### 5.2¶

Write a function called multiplyXandY that takes two arguments x and y, and returns their product.

### 5.3¶

Use the functions from 5.1 and 5.2, and add at least 3 functions similar to those above that simulate a calculator's functions.

### 5.4¶

Use your calculator functions to write a function that represents $f(x) = 5x + 4$.

In [ ]:
def f(x):
return # place code here


## Question 6¶

### 6.1¶

Write a function called concatXandY that takes two strings x and y, and concatenates them together.

### 6.2¶

Write a function concatXYZ that takes three strings x, y, and z, and concatenates them together without using any arithmethic operations.

## Question 7¶

### 7.1¶

Write a function cToF that converts Celsius to Fahrenheit using the equation $F = C \times \frac{9}{5} + 32$.

### 7.2¶

Write a function fToC that converts Fahrenheit to Celsius using the equation $C = (F - 32) \times \frac{5}{9}$.

### 7.3¶

What happens when you call cToF(fToC(x))? Try for a few values of x. Can you prove it?

## Question 8¶

### 8.1¶

Write a function called pythagoreanTriplet that takes in 3 integers which will represent triangle side lengths and returns a bool, to determine if it forms a right-angled triangle. The arguments are guaranteed to be sorted in increasing order.

Hint: A triangle is a right-angled when side lengths a, b, and c satisfy $a^{2} + b^{2} = c^{2}$.

In [ ]:
# write function here

# some unit tests
print(pythagoreanTriplet(3, 4, 5))


## Question 9¶

### 9.1¶

A palindrome is a string that is the same when read forwards and backwards. For example, "madam", "alabala", and "12321" are palindromes but "daniel", "hello", and "1234" are not. Write a function threePalindrome that takes in a THREE-LETTER string, and returns a boolean that is True when the string is palindrome, and False when it is not.

### 9.2¶

Write a function fourPalindrome that checks if a four-letter string is a palindrome.

## Question 10¶

### 10.1¶

Look at the function below. Without running it, can you predict the outcomes of calling funkyFunc(100), funkyFunc(123456), and funkyFunc(420)?

def funkyFunc(x):
z = x * 10
x = x % 10
z = z + x
return z
In [ ]:
# for x = 100,     funkyFunc(x) = ??
# for x = 123456,  funkyFunc(x) = ??
# for x = 420,      funkyFunc(x) = ??


Can you write a function funkierFunc which takes one argument and for which the following equation is true for any $x>0$:
funkierFunc ( funkyFunc(x) ) = x