-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathpractice_4.py
195 lines (152 loc) · 5.28 KB
/
practice_4.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
192
193
194
195
"""
Programming 2021
Seminar 4
Data Type: String
"""
# Common information about strings
#
# strings are immutable
# strings are iterable
# strings are case-sensitive
# Create a string
example = 'Hello' # or "Hello"
print(example)
# String concatenation
greeting = example + ' there!'
print(greeting)
# String multiplication
several_hellos = example * 5
print(several_hellos)
# String formatting
# .format() method
example = '{} there!'.format(example)
print(example)
# f-strings
example = f'{greeting} - this is the "greeting" variable.'
print(example)
# String methods (some of them)
# .split() -> split by the delimiter
# .join() - join by the delimiter
# .upper() - uppercase copy of the string
# .lower() - lowercase copy of the string
# .isalpha() - if all the characters in the text are letters
# .strip() - remove the given element (space by default) from the both ends of the string
# .find() - search the string for the specified value (return the index of the first occurrence)
# TASKS
# Task 1:
def multiply_string(input_string: str, how_many: int) -> str:
"""
Given a string and a non-negative number,
display the given string the number of times given in the `how_many`.
"""
# student realisation goes here
# Function calls with expected result:
# multiply_string('Hi', 2) → 'HiHi'
# multiply_string('Hi', 3) → 'HiHiHi'
# multiply_string('Hi', 1) → 'Hi'
# multiply_string('Hi', 0) → ''
# Task 2:
def front_times(input_string: str, how_many: int):
"""
Given the string, take its three leading characters and display them that many times as in `how_many`.
"""
# student realisation goes here
# Function calls with expected result:
# front_times('Chocolate', 2) → 'ChoCho'
# front_times('Chocolate', 3) → 'ChoChoCho'
# front_times('Abc', 3) → 'AbcAbcAbc'
# front_times('A', 4) → 'AAAA'
# front_times('', 4) → ''
# front_times('Abc', 0) → ''
# Task 3:
def extra_end(input_string: str):
"""
Given the string, take its two last characters and display them three times.
"""
# student realisation goes here
# Function calls with expected result:
# extra_end('Hello') → 'lololo'
# extra_end('ab') → 'ababab'
# extra_end('Hi') → 'HiHiHi'
# extra_end('Code') → 'dedede'
# Task 4:
def make_abba(first_string: str, second_string: str):
"""
Given two strings, concatenate them as a reflection.
"""
# student realisation goes here
# make_abba('Hi', 'Bye') → 'HiByeByeHi'
# make_abba('Yo', 'Alice') → 'YoAliceAliceYo'
# make_abba('What', 'Up') → 'WhatUpUpWhat'
# make_abba('', 'y') → 'yy'
# Task 5
def reverse_word(sentence: str):
"""
Write a function that takes in a string of one or more words,
and returns the same string, but with all five or more letter words reversed.
Strings passed in will consist of only letters and spaces.
Spaces will be included only when more than one word is present.
"""
pass
# reverse_word("Hey fellow warriors") == "Hey wollef sroirraw"
#
# reverse_word("This is a test") == "This is a test"
#
# reverse_word("This is another test") == "This is rehtona test"
# Task 6
def generate_hashtag(input_string: str) -> str:
"""
The marketing team is spending way too much time typing in hashtags.
Let's help them with our own Hashtag Generator!
Here's the deal:
It must start with a hashtag (#).
All words must have their first letter capitalized.
If the final result is longer than 140 chars it must return false.
If the input or the result is an empty string it must return false.
Examples
" Hello there thanks for trying my quiz" => "#HelloThereThanksForTryingMyQuiz"
" Hello World " => "#HelloWorld"
"" => false
"""
pass
# Task 7:
def combo_string(first_string: str, second_string: str):
"""
Given two strings, concatenate like the following: shorter+longer+shorter
"""
# student realisation goes here
# combo_string('Hello', 'hi') → 'hiHellohi'
# combo_string('hi', 'Hello') → 'hiHellohi'
# combo_string('aaa', 'b') → 'baaab'
# combo_string('', 'bb') → 'bb'
# combo_string('aaa', '1234') → 'aaa1234aaa'
# combo_string('bb', 'a') → 'abba'
# Task 1: advanced
def string_splosion(input_string: str):
"""
Given the string, format it like in the example.
"""
# student realisation goes here
# Function calls with expected result:
# string_splosion('Code') → 'CCoCodCode'
# string_splosion('abc') → 'aababc'
# string_splosion('ab') → 'aab'
# string_splosion('Kitten') → 'KKiKitKittKitteKitten'
# string_splosion('x') → 'x'
# Task 2: advanced
def string_match(first_string: str, second_string: str):
"""
Given two strings, find the number of times an arbitrary substring (with length of 2)
is found at the same position in both strings.
"""
# student realisation goes here
# Function calls with expected result:
# string_match('xxcaazz', 'xxbaaz') → 3
# NOTE: «xx», «aa» and «az» are found at the same position in both strings
# string_match('abc', 'abc') → 2
# string_match('abc', 'axc') → 0
# string_match('he', 'hello') → 1
# string_match('h', 'hello') → 0
# string_match('aabbccdd', 'abbbxxd') → 1
# string_match('aaxxaaxx', 'iaxxai') → 3
# string_match('iaxxai', 'aaxxaaxx') → 3