Exercise 2
Problem 1
You inspected the url
printed by your code as an exercise for Unit1 and also saved it to a CSV file. Now since you have learnt to write procedures, make decisions, and also know how to iterate, do the following:
- Write a procedure
inspect_url
that takes as input all theurl
s you found (of course, one at a time) and associate a category with it (absolute or relative). - In case of absolute, it separates the protocols, name, and domain (as done during previous exercise).
- Saves each of the url alongwith the category, protcols, name, and domain in separate fields of CSV file. Your CSV file should contain a proper header for all fields. In case of a relative
url
, you should fill in the required fields from parenturl
. You should define a proceduresave_url
for this purpose.
Note
Ignore any other possibilities that come along. You are free to make your assumptions where neeeded!
Problem 2
Define a procedure squarify
that takes as input a string and outputs a string with uppercase 'S' followed by the input string. For example:
squarify('lant') # -> Slant
Problem 3
Define a procedure median
that takes as input three numbers and outputs the median of three numbers. For example:
median(1,2,3) # -> 2
median(9,3,6) # -> 6
median(7,8,7) # -> 7
Problem 4
You are given 3 loops below. For each of the loop you need to identify if it:
a. always finishes
b. sometimes runs forever
c. unkonow (to anyone)
Loop1:
n = <any positive number>
i = 0
while i <= n:
i = i + 1
Loop2:
n = <any positive number>
i = 0
while i <= n:
i = i * 1
n = n + 1
if i > n:
break
Loop3:
n = <any positive number>
while n != 1:
if n % 2 == 0:
n = n/2
else:
n = 3*n + 1
Problem 5
Define a procedure find_last
that takes as input two strings, a search string and a target string, and outputs the last position in the search string where the target string appears, or -1
if there arte no accurences.
Problem 6
Define a procedure print_multiplication_table
that takes as input a whole number, and prints a multiplication table showing all the whole multiplications upto an including the input number in exacting the format given in the example below:
print_multiplication_table(2)
# 1 * 1 = 1
# 1 * 2 = 2
# 2 * 1 = 2
# 2 * 2 = 4
Best!