This repository has been archived by the owner on Dec 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathpractice_3_lists.py
191 lines (155 loc) · 5.34 KB
/
practice_3_lists.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""
Programming 2023
Seminar 3
Data Type: Lists
"""
# Common information about lists
# lists are mutable
# lists are iterable
# Create a list
example = [1, 2, 3]
print(example)
# List concatenation, the original list doesn't change
first_list = example + [2, 3, 4]
print(example)
print(first_list)
# List changes
example.append(2)
example.extend([2, 3, 4])
print(example)
# List copy
# import copy
first_test = [1, 2, 3, [1, 2, 3]]
test_copy = first_test.copy()
print(first_test, test_copy)
test_copy[3].append(4)
print(first_test, test_copy)
first_test = [1, 2, 3, [1, 2, 3]]
# test_deepcopy = copy.deepcopy(first_test)
# test_deepcopy[3].append(4)
# print(first_test, test_deepcopy)
# List methods (some of them)
# .insert(index, item) -> inserts the given item on the mentioned index
# .remove(item) - removes the first occurrence of the given item from the list
# .pop() or .pop(index) – removes the item from the given index
# (or the last item) and returns that item
# .index(item) – returns the index of the first occurrence
# .sort() – sorts the list in place i.e modifies the original list
# .reverse() – reverses the list in place
# .copy() – returns a shallow copy of the list
# TASKS
# Task 1:
# easy level
def count_evens(nums: list) -> int:
"""
Return the number of even ints in the given array.
"""
# student realization goes here
# Function calls with expected result:
# count_evens([2, 1, 2, 3, 4]) → 3
# count_evens([2, 2, 0]) → 3
# count_evens([1, 3, 5]) → 0
# Task 2:
# easy level
def sum13(nums: list) -> int:
"""
Return the sum of the numbers in the array, returning 0 for an empty array.
Except the number 13 is very unlucky,
so it does not count and numbers that come after a 13
also do not count.
"""
# student realization goes here
# Function calls with expected result:
# sum13([1, 2, 2, 1]) → 6
# sum13([1, 1]) → 2
# sum13([1, 2, 2, 1, 13]) → 6
# sum13([1, 2, 2, 1, 13, 5, 6]) → 6
# Task 3
# easy level
def sum67(nums: list) -> int:
"""
Return the sum of the numbers in the array,
except ignore sections of numbers starting with a 6 and extending to the next 7
(every 6 will be followed by at least one 7).
Return 0 for no numbers.
"""
# student realization goes here
# Function calls with expected result:
# sum67([1, 2, 2]) → 5
# sum67([1, 2, 2, 6, 99, 99, 7]) → 5
# sum67([1, 1, 6, 7, 2]) → 4
# Task 4
# easy level
def create_phone_number(nums: list) -> str:
"""
Write a function that accepts an array of 10 integers (between 0 and 9),
that returns a string of those numbers in the form of a phone number.
"""
# student realization goes here
# Function calls with expected result:
# create_phone_number([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
# => returns "(123) 456-7890"
# Task 5
# medium level
def check_exam(correct_answers: list, student_answers: list) -> int:
"""
The first input array is the key to the correct answers to an exam,
like ["a", "a", "b", "d"].
The second one contains a student's submitted answers.
The two arrays are not empty and are the same length.
Return the score for this array of answers,
giving +4 for each correct answer,
-1 for each incorrect answer,
and +0 for each blank answer, represented as an empty string.
If the score < 0, return 0.
"""
# student realization goes here
# Function calls with expected result:
# check_exam(["a", "a", "b", "b"], ["a", "c", "b", "d"]) → 6
# check_exam(["a", "a", "c", "b"], ["a", "a", "b", ""]) → 7
# check_exam(["a", "a", "b", "c"], ["a", "a", "b", "c"]) → 16
# check_exam(["b", "c", "b", "a"], ["", "a", "a", "c"]) → 0
# Task 6
# medium level
def who_likes_it(names: list) -> str:
"""
You probably know the "like" system from Facebook and other pages.
People can "like" blog posts, pictures or other items.
We want to create the text that should be displayed next to such an item.
"""
# student realization goes here
# Function calls with expected result:
# [] --> "no one likes this"
# ["Peter"] --> "Peter likes this"
# ["Jacob", "Alex"] --> "Jacob and Alex like this"
# ["Max", "John", "Mark"] --> "Max, John and Mark like this"
# ["Alex", "Jacob", "Mark", "Max"] --> "Alex, Jacob and 2 others like this"
# Task 7
# medium level
def find_anagrams(words: list) -> list:
"""
What is an anagram?
Two words are anagrams of each other if they both contain the same letters.
'abba' and 'baab' are anagrams
'abba' and 'bbaa' are anagrams
'abba' and 'abbba' are not anagrams
'abba' and 'abca' are not anagrams
Write a function that will find all the anagrams of a word from a list.
"""
# student implementation goes here
# Function calls with expected result:
# find_anagrams('abba') => ['aabb', 'bbaa']
# find_anagrams('racer') => ['carer', 'racer', ...]
# Task 8
# medium level
def scramble(words: list) -> bool:
"""
Complete the function scramble(words: list)
that returns true if a portion of str1 characters can be rearranged to match str2,
otherwise returns false.
"""
# student implementation goes here
# Function calls with expected result:
# scramble(['rkqodlw', 'world']) ==> True
# scramble(['cedewaraaossoqqyt', 'codewars']) ==> True
# scramble(['katas', 'steak']) ==> False