forked from calistus-igwilo/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.py
235 lines (180 loc) · 5.8 KB
/
strings.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""
The folowing text is given:
text = 'python is a popular programming language.'
Use the appropriate method to replace the first letter
of the text with uppercase. Print the result to console
"""
from typing import Counter
text = 'python is a popular programming language.'
print(text.capitalize())
# Count the number of occurrences of the letter 'p' and
# print the result to the consle as shown
# Number of occurrencies: 4
#cnt = Counter(text)
print(f"Number of occurences: {text.count('p')}")
# Check if string ends with a substring
"""
The following codes are given:
code1 = 'FVNISJND-XX-2020'
code2 = 'FVNISJND-XY-2019'
Using the appropriate method check if the codes end in '2020'.
Print the results to the console as shown below:
code1: True
code2: False
"""
code1 = 'FVNISJND-XX-2020'
code2 = 'FVNISJND-XY-2019'
print(f"code1: {code1.endswith('2020')}")
print(f"code2: {code2.endswith('2020')}")
"""
The following paths are given:
path1 = 'https://e-smartdata.teachable.com/p/sciezka-data-scientist-machine-learning-engineer'
path2 = 'https://e-smartdata.teachable.com/p/sciezka-data-scientist-deep-learning-engineer'
path3 = 'https://e-smartdata.teachable.com/p/sciezka-bi-analyst-data-analyst'
Using the appropriate method, find the word 'scientist' in the given paths,
returning the index for the first letter of the found word. If the word is
not in the path, the method should return -1. Print the result to the console
as shown below:
path1: 49
path2: 49
path3: -1
"""
path1 = 'https://e-smartdata.teachable.com/p/sciezka-data-scientist-machine-learning-engineer'
path2 = 'https://e-smartdata.teachable.com/p/sciezka-data-scientist-deep-learning-engineer'
path3 = 'https://e-smartdata.teachable.com/p/sciezka-bi-analyst-data-analyst'
print(f"path1: {path1.find('scientist')}")
print(f"path2: {path2.find('scientist')}")
print(f"path3: {path3.find('scientist')}")
# isalnum
"""
The following codes are given:
code1 = 'FVNISJND-20'
code2 = 'FVNISJND20'
Using the appropriate method, check whether the codes consist only of
alphanumeric characters (numbers + letters). Print the result to
the console as shown below:
code1: False
code2: True
"""
code1 = 'FVNISJND-20'
code2 = 'FVNISJND20'
print(f'code1: {code1.isalnum()}')
print(f"code2: {code2.isalnum()}")
# strip()
"""
The following text is given:
text = ' Google Colab '
Using appropriate method, remove whitespace characters around the text.
Print the result to the console
"""
text = ' Google Colab '
print(text.strip())
"""
The following text is given:
txt = ",,,,,rrttgg.....banana....rrr"
Using appropriat method, remove the characters: ,.grt
Print the output to the console as shown below:
banana
"""
txt = ",,,,,rrttgg.....banana....rrr"
x = txt.strip(",.grt")
print(x)
# replace()
"""
The following code is given:
code = 'FVNISJND-XX'
Using the appropriate method, replace the dash with a space.
Print the output to the console as shown below:
FVNISJND XX
"""
code = 'FVNISJND-XX'
print(code.replace('-', ' '))
# remove all dashes from the text below
text = '340-23-245-235'
print(text.replace("-", ''))
# split()
"""
The folowing text is given:
text = 'Open,High,Low,Close'
Using the appropriate method split the text by comma.
Print the result to the console as shown below:
['Open', 'High', 'Low', 'Close']
"""
text = 'Open,High,Low,Close'
print(text.split(','))
# The following text is given
#
# text = """Python is a general-purpose language.
# Python is popular."""
# Using appropriate method, split the text into sentences.
# print the result to the console as shown belos
#
# ['Python is a general-purpose language.', 'Python is popular']
#
text = """Python is a general-purpose language.
Python is popular."""
print(text.strip('"').split('\n'))
"""
The following variable is given:
num = 34
Using the appropriate method for an object of type str, print
the variable num preceded by four zeros to console as shown:
000034
"""
num = 34
print(str(num).zfill(6))
"""
From the given url:
url = 'https://e-smartdata.teachable.com/p/sciezka-data-scientist-machine-learning-engineer'
extract the slug after the last character '/'. Then replace all dashes
with spaces and print the result to console as shown below:
"""
url = 'https://e-smartdata.teachable.com/p/sciezka-data-scientist-machine-learning-engineer'
slug = url.split('/')[4] # or url.split('/)[-1]
print(slug.replace('-', ' '))
"""
Write 3 Python functions that parse and output only the date and time part of each log
in log.txt as follows.
Content of "log.txt":
10.1.2.1 - car [01/Mar/2022:13:05:05 +0900] "GET /python HTTP/1.0" 200 2222
10.1.1.9 - bike [01/Mar/2022:13:05:10 +0900] "GET /python HTTP/1.0" 200 2222
Expected output:
01/Mar/2022:13:05:05 +0900
01/Mar/2022:13:05:10 +0900
"""
def parse1():
for line in open("log.txt"):
print(line.split("[")[1].split("]")[0])
def parse2():
for line in open("log.txt", "rw"):
print(" ".join(line.split()[3:5]).strip("[]"))
def parse3():
for line in open("log.txt"):
print(re.split("\[|\]", line)[1])
class FunEvent:
def __init__(self, tags, year):
self.tags = tags
self.year = year
def __str__(self):
return f"FunEvent(tags={self.tags}, year={self.year})"
tags = ["google", "ml"]
year = 2022
bootcamp = FunEvent(tags, year)
print(f'First bootcamp: {bootcamp}')
tags.append("bootcamp")
year = 2023
print(bootcamp)
class BaseLayer:
def __init__(self, name=""):
self.name = name
def __repr__(self):
return f"{self.name}Layer"
class ActivationLayer(BaseLayer):
def __init__(self, size):
super().__init__("Activation")
self.size = size
class FCLayer(BaseLayer):
def __init__(self, size):
super().__init__("FullyConnected")
self.size = size
print(FCLayer(42))