-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathW3_programming_assignment.py
91 lines (70 loc) · 2.61 KB
/
W3_programming_assignment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
""" 1) Write a function contracting(l) that takes as input a list of integer l and returns
True if the absolute difference between each adjacent pair of elements strictly decreases.
Here are some examples of how your function should work.
>>> contracting([9,2,7,3,1])
True
>>> contracting([-2,3,7,2,-1])
False
>>> contracting([10,7,4,1])
False"""
def contracting(l):
diff= abs(l[1] - l[0])
for i in range(2, len(l)):
if abs(l[i] - l[i-1]) < diff:
diff = abs(l[i] - l[i-1])
else:
return False
return True
""" 2)In a list of integers l, the neighbours of l[i] are l[i-1] and l[i+1]. l[i] is a hill
if it is strictly greater than its neighbours and a valley if it is strictly less than its neighbours.
Write a function counthv(l) that takes as input a list of integers l and returns a list [hc,vc]
where hc is the number of hills in l and vc is the number of valleys in l.
Here are some examples to show how your function should work.
>>> counthv([1,2,1,2,3,2,1])
[2, 1]
>>> counthv([1,2,3,1])
[1, 0]
>>> counthv([3,1,2,3])
[0, 1]"""
def counthv(l):
hc, vc = 0, 0
for i in range(1, len(l)-1):
if l[i] > l[i-1] and l[i] > l[i+1]:
hc += 1
elif l[i] < l[i-1] and l[i] < l[i+1]:
vc += 1
return [hc, vc]
""" 3) A square n×n matrix of integers can be written in Python as a list with n elements,
where each element is in turn a list of n integers, representing a row of the matrix. For instance, the matrix
1 2 3
4 5 6
7 8 9
would be represented as [[1,2,3], [4,5,6], [7,8,9]].
Write a function leftrotate(m) that takes a list representation m of a square matrix as input, and returns the matrix obtained by rotating the original matrix counterclockwize by 90 degrees. For instance, if we rotate the matrix above, we get
3 6 9
2 5 8
1 4 7
Your function should not modify the argument m provided to the function rotate().
Here are some examples of how your function should work.
>>> leftrotate([[1,2],[3,4]])
[[2, 4], [1, 3]]
>>> leftrotate([[1,2,3],[4,5,6],[7,8,9]])
[[3, 6, 9], [2, 5, 8], [1, 4, 7]]
>>> leftrotate([[1,1,1],[2,2,2],[3,3,3]])
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]"""
def leftrotate(m):
l = len(m)
arr = [[0 for i in range(l)] for i in range(l)]
for i in range(l):
for j in range(l):
arr[i][j] = m[j][(l-1)-i]
return arr
# def leftrotate(m):
# l = len(m)
# arr = []
# for i in range(l):
# sub_arr = []
# for j in range(l):
# sub_arr.append(m[j][(l-1)-i])
# arr.append(sub_arr)
# return arr