Practice 4: Loop and Iteration
Guide (Please read!!!)
There are 3+1 levels of difficulty of practice problems which will help guide you on how you do the assignment. 1.-3. are mandatory. If it is too easy, try speed-running the easy ones.
- Concept (π) will help you recheck yourself whether you understood the content.
- Apply (ππ) will step up some difficulty where you have to apply several concepts.
- Technique (πππ) is more challenging.
- Real-world Examples (π) are optional.
The problems will state inputs and outputs of the program. Inputs and outputs can be more than one, one each, see examples for more details.
Special instructions for this and further problems
Keep in mind that in Python, there is print('x', end='')
which removes the newline character
when printing on the screen. Further problems may require that.
Problems
(π) 1. Print from m to n
Write a program that prints integers from m to n
Input:
- Integer m
- Integer n
Output:
- Integers from m to n separated by space
Examples
Input: 4
10
-----
Output: 4 5 6 7 8 9 10
(π) 2. Print capped
Write a program that prints k *
characters per line with total of n *
.
Input:
- Integer n
- Integer k
Output:
- Stars (See examples)
Examples
Input: 30
8
-----
Output: * * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * *
Because: print k stars per line with total of n stars
(π) 3. Sum until n
Write a program that finds maximum k such that sum of 1..k is strictly less than n.
Input:
- Integer n
Output:
- Integer k
- Sum of 1..k (1 + 2 + 3 + β¦ + k)
Examples
Input: 20
-----
Output: 5
15
(π) 4. Is it prime?
A prime number p is a number that only p and 1 can divide while p > 1. Write a program that check if the integer that user inputs is a prime or not.
Input:
- Integer n
Output:
- PRIME or NOT PRIME
Examples
Input: 7
-----
Output: PRIME
(π) 5. Sum of n numbers as the user wishes
Write a program that takes n as an input. Then, for the next n lines, user can input integer in each line. The program should output the sum of that list of numbers by user.
Input:
- Integer n
- Next n lines: integers to be summed
Output:
- Sum of those integers
Examples
Input: 5
2
4
5
9
9
-----
Output: 29
(π) 6. Multiplication Table
Write a program that prints the multiplication table of n from 1 to 12:
n x 1 = ...
n x 2 = ...
...
n x 12 = ...
Input:
- Integer n
Output:
- Multiplication Table of n
Examples
Input: 13
-----
Output: 13 x 1 = 13
13 x 2 = 26
13 x 3 = 39
13 x 4 = 52
13 x 5 = 65
13 x 6 = 78
13 x 7 = 91
13 x 8 = 104
13 x 9 = 117
13 x 10 = 130
13 x 11 = 143
13 x 12 = 156
(π) 7. Find maximum and minimum
Write a program that takes n as an input. Then, for the next n lines, user can input integer in each line. The program should output the max and min of that list of numbers by user. Do it however you want to do.
Input:
- Integer n
- Next n lines: integers to be summed
Output:
- Minimum number in the list
- Maximum number in the list
Examples
Input: 5
4
2
5
9
9
-----
Output: 2
9
(π) 8. Count Odd-even
Write a program that takes n as an input. Then, for the next n lines, user can input integer in each line. The program should output the count of odd numbers in the list.
Input:
- Integer n
- Next n lines: integers to be summed
Output:
- Number of odd numbers
Examples
Input: 5
4
2
5
8
9
-----
Output: 2
(π) 9. Selected sum
Write a function that takes a list as a parameter, then returns a sum of that list with condition that the number is even.
Template (copy and fill code)
# Your code begin
def f(inp):
...
# Your code end
print(f([1, 2, 3, 4, 5, 6, 7]))
print(f([1, 1, 2, 3, 5, 8]))
print(f([]))
Input:
- None
Output:
- None
Examples
Input: -
-----
Output: 12
10
0
(ππ) 10. Cycle the list
Write a function that takes a number n and a list as parameters. The function shall cycle the first element to the back for n times. The function should return the new list and should not change anything in the list from parameter.
Template (copy and fill code)
# Your code begin
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
def f(n, inp):
...
# Your code end
print(f(0, x))
print(f(1, x))
print(f(3, x))
print(x)
Input:
- None
Output:
- None
Examples
Input: -
-----
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9] # cycle 0 times
[2, 3, 4, 5, 6, 7, 8, 9, 1] # cycle 1 times
[4, 5, 6, 7, 8, 9, 1, 2, 3] # cycle 3 times
[1, 2, 3, 4, 5, 6, 7, 8, 9] # print x
(ππ) 11. Split the list
Write a function that takes a list as a parameter. The function shall split the list into 3 lists. Each result list will have the member from the original list every 3 members. See examples for more details.
E.g.,
list1 = [x[0], x[3], x[6], ...]
list2 = [x[1], x[4], x[7], ...]
list3 = [x[2], x[5], x[8], ...]
Template (copy and fill code)
# Your code begin
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def f(inp):
list1 = []
list2 = []
list3 = []
...
return list1, list2, list3
# Your code end
for e in f(x):
print(e)
Input:
- None
Output:
- None
Examples
Input: -
-----
Output: [1, 4, 7, 10]
[2, 5, 8]
[3, 6, 9]
(π) 12. Factorial
Write a n factorial function.
Template (copy and fill code)
# Your code begin
def factorial(n):
...
# Your code end
print(factorial(0))
print(factorial(3))
print(factorial(5))
Input:
- None
Output:
- None
Examples
Input: -
-----
Output: 1
6
120
(ππ) 13. Binomial Coefficient
Write a program that print binomial coefficients of order n.
Lookup on how you can use combinatorial formula to calculate the coefficient.
Use the factorial function from the problem above.
Input:
- Integer n
Output:
- Binomial coefficients of order n
Examples
Input: 1
-----
Output: 1 1
=====
Input: 2
-----
Output: 1 2 1
=====
Input: 3
-----
Output: 1 3 3 1
=====
Input: 5
-----
Output: 1 5 10 10 5 1
=====
Input: 0
-----
Output: 1
(π) 14. Print Square n
Write a program that prints a square dimension n x n from stars *
.
Input:
- Integer n
Output:
- Square
Examples
Input: 5
-----
Output: * * * * *
* * * * *
* * * * *
* * * * *
* * * * *
(π) 15. Print Rectangle m x n
Write a program that prints a rectangle dimension m x n from stars *
.
Input:
- Integer m (width)
- Integer n (height)
Output:
- Rectangle
Examples
Input: 7
3
-----
Output: * * * * * * *
* * * * * * *
* * * * * * *
(π) 16. Print Symmetrical Triangle n
Write a program that prints a triangle dimension n x n from stars *
.
Input:
- Integer n
Output:
- Triangle
Examples
Input: 5
-----
Output: *
* *
* * *
* * * *
* * * * *
(π) 17. Print Upside-down Symmetrical Triangle n
Write a program that prints a triangle dimension n x n from stars *
.
Input:
- Integer n
Output:
- Upside down Triangle
Examples
Input: 5
-----
Output: * * * * *
* * * *
* * *
* *
*
(π) 18. Print Symmetrical Triangle n with column number
Write a program that prints a triangle dimension n x n from column number.
Input:
- Integer n
Output:
- Triangle
Examples
Input: 5
-----
Output: 1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
(π) 19. Print Symmetrical Triangle n with row number
Write a program that prints a triangle dimension n x n from row number.
Input:
- Integer n
Output:
- Triangle
Examples
Input: 5
-----
Output: 1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
(π) 20. Print Symmetrical Triangle n with increasing number
Write a program that prints a triangle dimension n x n from increasing number.
Input:
- Integer n
Output:
- Triangle
Examples
Input: 5
-----
Output: 1
2 3
4 5 6
7 8 9 10
11 12 13 14
(π) 21. Print Double Triangle
Write a program that prints a triangle with stars *
. (See examples.)
Input:
- Integer n
Output:
- Triangle
Examples
Input: 5
-----
Output: *
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
(π) 22. Print Symmetrical Triangle (Flipped) n
Write a program that prints a flipped triangle dimension n x n from stars *
.
Input:
- Integer n
Output:
- Triangle
Examples
Input: 5
-----
Output: *
* *
* * *
* * * *
* * * * *
Hint: try replacing space ' '
with '-'
so you can see whatβs going on.
(π) 23. Print Symmetrical Triangle (Flipped) n with numbers
Write a program that prints a triangle with numbers. (See examples.)
Input:
- Integer n
Output:
- Triangle
Examples
Input: 4
-----
Output: 1
2 3
4 5 6
7 8 9 10
9 8 7
6 5
4
(π) 24. Print Hollow Square n
Write a program that prints a hollow square dimension n x n from stars *
.
Input:
- Integer n
Output:
- Hollow Square
Examples
Input: 5
-----
Output: * * * * *
* *
* *
* *
* * * * *
(π) 25. Print Hollow Symmetrical Triangle n
Write a program that prints a hollow triangle dimension n x n from stars *
.
Input:
- Integer n
Output:
- Hollow Triangle
Examples
Input: 5
-----
Output: *
* *
* *
* *
* * * * *
(π) 26. Print Diamond
Write a program that prints a diamond with stars *
. (See examples.)
Input:
- Integer n
Output:
- Diamond
Examples
Input: 4
-----
Output: *
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
(ππ) 27. Print X
Write a program that prints an X with stars *
. (See examples.)
Input:
- Integer n
Output:
- X
Examples
Input: 4
-----
Output: * *
* *
* *
*
* *
* *
* *
(ππ) 28. Print Pyramid
Write a program that prints a pyramid with stars *
. (See examples.)
Input:
- Integer n
Output:
- Pyramid
Examples
Input: 4
-----
Output: *
* *
* * *
* * * *
(π) 29. Harmonic sum n
Write a program that finds the harmonic sum 1..n.
Input:
- Integer n
Output:
- Harmonic sum of 1..n (1/1 + 1/2 + 1/3 + β¦ + 1/n)
Examples
Input: 5
-----
Output: 2.283333333333333
(π) 30. Print Symmetrical Triangle n with alternating binary
Write a program that prints a triangle dimension n x n from binary.
The leftmost element is always 1.
Input:
- Integer n
Output:
- Triangle
Examples
Input: 5
-----
Output: 1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
(π) 31. Series sum
Write a program that finds the series sum of 1 + 11 + 111 + 1111 + β¦ ; n terms.
Input:
- Integer n
Output:
- Series sum
Examples
Input: 5
-----
Output: 12345
Because 1 + 11 + 111 + 1111 + 11111
(π) 32. Perfect Square
Write a program that check if the number n is the perfect square or not.
Input:
- Integer n
Output:
- YES or NO
Examples
Input: 88
-----
Output: NO
Because 88 is not a perfect square.
(π) 33. Fibonacci Sequence
Write a program that print first n Fibonacci numbers.
Fibonacci sequences can be defined as:
F(n) = F(n - 1) + F(n - 2); F(0) = 1, F(1) = 1, n >= 0 and is integer.
Input:
- Integer n
Output:
- Fibonacci sequence
Examples
Input: 8
-----
Output: 1 1 2 3 5 8 13 21
(ππ) 34. Print Symmetrical Triangle n from alphabets
Write a program that prints a triangle dimension n x n from alphabets.
Input:
- Integer n
Output:
- Triangle
Examples
Input: 5
-----
Output: A
A B
A B C
A B C D
A B C D E
(πππ) 35. GCD
Write a program that find the Greatest Common Divisor (GCD) of two integers.
Input:
- Integer a
- Integer b
Output:
- GCD(a, b)
Examples
Input: 6
21
-----
Output: 3
=====
Input: 6
5
-----
Output: 1
=====
Input: 20000
20002
-----
Output: 2
(πππ) 36. List Sliding
Write a function that takes a list as a parameter. The function shall return the maximum sum of 3 consecutive numbers in the list.
Explanation
[1, 2, 3, 4, 5, 6, 7, 8, 99, 10, 11, 12, 13]
~~~~~~~ -> 1 + 2 + 3 = 6
~~~~~~~ -> 2 + 3 + 4 = 9
~~~~~~~ -> 3 + 4 + 5 = 12
...
Template (copy and fill code)
# Your code begin
x = [1, 2, 3, 4, 5, 6, 7, 8, 99, 10, 11, 12, 13]
y = [1, 1, 1, 1, 1, 1, 1]
def f(inp):
...
# Your code end
print(f(x))
print(f(y))
Input:
- None
Output:
- None
Examples
Input: -
-----
Output: 120 # 99 + 10 + 11 is maximum
3 # 1 + 1 + 1 is maximum
(ππ) 37. Pair Sum
Write a function that takes a list as a parameter. The function shall return the maximum sum of 2 numbers from the list.
E.g., the list [1, 2, 3, 4]
can have a pair sum of:
1 + 2
1 + 3
1 + 4
2 + 3
2 + 4
3 + 4
Template (copy and fill code)
# Your code begin
x = [1, 2, 3, 4, 5, 6, 7, 8, 99, 10, 11, 12, 13]
y = [1, 1, 1, 1, 1, 1, 1]
def f(inp):
...
# Your code end
print(f(x))
print(f(y))
Input:
- None
Output:
- None
Examples
Input: -
-----
Output: 112 # 99 + 13 is maximum
2 # 1 + 1 is maximum
(πππ) 38. Value Pairs
Write a function that takes 2 lists as parameters.
The function shall print every combination of cartesian product of
list1
X list2
.
Explanation
list1 = [1, 2, 3]
list2 = [a, b]
The function should print:
1 a
1 b
2 a
2 b
3 a
3 b
Template (copy and fill code)
x = [1, 2, 3]
y = [8, 9, 0]
# Your code begin
def f(list1, list2):
...
# Your code end
f(x, y)
Input:
- None
Output:
- Next
len(list1) * len(list2)
lines: Cartesian productβs elements in each line
Examples
Input: -
-----
Output: 1 8
1 9
1 0
2 8
2 9
2 0
3 8
3 9
3 0
(πππ) 39. Closest distance
Write a program that finds a pair of coordinates that are closest to each other.
Template (copy and fill code)
n = int(input())
coords = [list(map(float, input().strip().split())) for i in range(n)]
# Your code begin
...
# Your code end
Tips: You can iterate through 2 values at the same time in for loop:
for x, y in coords:
# you can use value x and y here now.
Tips: You can iterate through value in the list and counting at the same time in for loop with enumerate
:
for i, x in enumerate(inp):
# i will be the index 0, 1, 2, ... , len(inp)-1
# x will be the value in inp list
Tips: Combining with above two, you get:
for i, (x, y) in enumerate(coords):
# i will be the index 0, 1, 2, ... , len(inp)-1
# you can use value x and y here now.
Input:
- Integer n indicating number of coordinates
- Next n lines: coordinate x, y separated by space.
Output:
- (x1, y1) (x2, y2) that are closest to each other
- distance between those two coordinates
Examples
Input: 3
3.0 4.0
9.4 5.5
6.7 -10.9
-----
Output: (3.0, 4.0) (9.4, 5.5)
6.573431371817919
(ππ) 40. Collatz Conjecture
Write a function that prints a sequence satisfying Collatz Conjecture.
Read more about this on Wikipedia
In short, it is defined as:
Template (copy and fill code)
Do it on your own.
Input:
- None
Output:
- None
Examples
Input: 12
-----
Output: 12 6 3 10 5 16 8 4 2 1
(ππ) 41. Insert Sorted
Write a function that takes a list and an element k to be inserted as parameters. The function will insert an element into the list without returning anything. The order of list after insertion will be sorted in ascending order.
Explanation
list1 = []
insert(list1, 5) # list1 becomes [5]
insert(list1, 9) # list1 becomes [5, 9]
insert(list1, 10) # list1 becomes [5, 9, 10]
insert(list1, 10) # list1 becomes [5, 9, 10, 10]
insert(list1, 2) # list1 becomes [2, 5, 9, 10, 10]
insert(list1, 3) # list1 becomes [2, 3, 5, 9, 10, 10]
Template (copy and fill code)
x = []
# Your code begin
def insert(list1, k):
...
# Your code end
print(x)
for e in [5, 9, 10, 10, 2, 3]:
f(x, e)
print(x)
Input:
- None
Output:
- None
Examples
Input: -
-----
Output: []
[5]
[5, 9]
[5, 9, 10]
[5, 9, 10, 10]
[2, 5, 9, 10, 10]
[2, 3, 5, 9, 10, 10]
(π) 42. Insert if not exist
Write a function that takes a list and an element k to be inserted as parameters. The function will insert an element into the list without returning anything. If k already exists, in the list, the function should not do anything.
In Python, you can use expression x in y
to check if an
element x
is in the collection y
or not. This expression is either
True
or False
.
Explanation
list1 = []
insert(list1, 5) # list1 becomes [5]
insert(list1, 9) # list1 becomes [5, 9]
insert(list1, 10) # list1 becomes [5, 9, 10]
insert(list1, 10) # list1 becomes [5, 9, 10]
insert(list1, 2) # list1 becomes [5, 9, 10, 2]
insert(list1, 3) # list1 becomes [5, 9, 10, 2, 3]
insert(list1, 3) # list1 becomes [5, 9, 10, 2, 3]
Template (copy and fill code)
x = []
# Your code begin
def insert(list1, k):
...
# Your code end
print(x)
for e in [5, 9, 10, 10, 2, 3, 3]:
f(x, e)
print(x)
Input:
- None
Output:
- None
Examples
Input: -
-----
Output: []
[5]
[5, 9]
[5, 9, 10]
[5, 9, 10]
[5, 9, 10, 2]
[5, 9, 10, 2, 3]
[5, 9, 10, 2, 3]