Skip to content

Commit de7599a

Browse files
authored
Merge pull request #2 from fmipython/workshop1
Add workshop1
2 parents 4f67f17 + e50936b commit de7599a

1 file changed

Lines changed: 260 additions & 0 deletions

File tree

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Workshop 1: Задачи по теми 1 и 2"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"## Задача 1\n",
15+
"\n",
16+
"По дадена дата във формат `YYYY-MM-DD` (тип `str`) да се върне наредена тройка от числа (`int`), съответстващи на годината, месеца и деня.\n",
17+
"\n",
18+
"* Hint: може да използвате `str.split`"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"metadata": {},
24+
"execution_count": null,
25+
"outputs": [],
26+
"source": [
27+
"def extract_date_components(date_string):\n",
28+
" pass # your code here\n",
29+
"\n",
30+
"\n",
31+
"# Tests:\n",
32+
"\n",
33+
"assert extract_date_components('1970-01-01') == (1970, 1, 1)\n",
34+
"assert extract_date_components('2023-10-22') == (2023, 10, 22)\n",
35+
"assert extract_date_components('0000-12-25') == (0, 12, 25)\n",
36+
"assert extract_date_components('2009-02-29') == (2009, 2, 29), \"no one said it should be a valid date\"\n",
37+
"\n",
38+
"\"✅ All OK!\""
39+
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"metadata": {},
44+
"source": [
45+
"## Задача 2\n",
46+
"\n",
47+
"Напишете функция `format_date_components`, която:\n",
48+
"\n",
49+
"* приема 3 параметъра: `year`, `month`, `day` (тип `int`)\n",
50+
"* приема oще 1 параметър `separator` (тип `str`), по подразбиране със стойност `'-'`\n",
51+
"* връща `str` с годината, месеца и деня, разделени със `separator` символа\n",
52+
"* цифрите на месеца и деня да са точно 2\n",
53+
"\n",
54+
"Number formatting reference: https://www.pythoncheatsheet.org/cheatsheet/string-formatting#formatting-digits"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"metadata": {},
60+
"execution_count": null,
61+
"outputs": [],
62+
"source": [
63+
"# write your code here\n",
64+
"\n",
65+
"# Tests:\n",
66+
"\n",
67+
"assert format_date_components(2023, 10, 22) == '2023-10-22'\n",
68+
"assert format_date_components(2112, 12, 21, separator='/') == '2112/12/21'\n",
69+
"assert format_date_components(1970, 1, 1) == '1970-01-01', \"check your leading zeroes formatting!\"\n",
70+
"assert format_date_components(1, 12, 25) == '0001-12-25', \"leading zeroes for the year as well!\"\n",
71+
"\n",
72+
"\"✅ All OK!\""
73+
]
74+
},
75+
{
76+
"cell_type": "markdown",
77+
"metadata": {},
78+
"source": [
79+
"## Задача 3\n",
80+
"\n",
81+
"Съединете двата `dict`-а в един.\n"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"metadata": {},
87+
"execution_count": null,
88+
"outputs": [],
89+
"source": [
90+
"def merge(d1, d2):\n",
91+
" pass # write your code here\n",
92+
"\n",
93+
"\n",
94+
"# Tests:\n",
95+
"\n",
96+
"assert merge({\"name\": \"Pesho\", \"year\": 1999}, {\"groups\": [\"A\", \"B\"]}) == {\"name\": \"Pesho\", \"year\": 1999, \"groups\": [\"A\", \"B\"]}\n",
97+
"assert merge({}, {}) == {}\n",
98+
"\n",
99+
"\"✅ All OK!\""
100+
]
101+
},
102+
{
103+
"cell_type": "markdown",
104+
"metadata": {},
105+
"source": [
106+
"## Задача 4\n",
107+
"\n",
108+
"Намерете броя на цифрите на дадено число (в десетична бройна система)."
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"metadata": {},
114+
"execution_count": null,
115+
"outputs": [],
116+
"source": [
117+
"def digits_count(n):\n",
118+
" pass # write your code here\n",
119+
"\n",
120+
"\n",
121+
"# Tests:\n",
122+
"\n",
123+
"assert digits_count(0) == 1\n",
124+
"assert digits_count(1) == 1\n",
125+
"assert digits_count(9) == 1\n",
126+
"assert digits_count(10) == 2\n",
127+
"assert digits_count(666) == 3\n",
128+
"assert digits_count(1234) == 4\n",
129+
"assert digits_count(9876543210) == 10\n",
130+
"assert digits_count(31415926535897932384626433832795028841971693993751058209749445923078164062862089986280) == 86\n",
131+
"\n",
132+
"\"✅ All OK!\""
133+
]
134+
},
135+
{
136+
"cell_type": "markdown",
137+
"metadata": {},
138+
"source": [
139+
"## Задача 5\n",
140+
"\n",
141+
"Пребройте колко различни символа има в даден низ."
142+
]
143+
},
144+
{
145+
"cell_type": "code",
146+
"metadata": {},
147+
"execution_count": null,
148+
"outputs": [],
149+
"source": [
150+
"def unique_chars_count(s):\n",
151+
" pass # write your code here\n",
152+
"\n",
153+
"# Tests:\n",
154+
"\n",
155+
"assert unique_chars_count(\"abcdef\") == 6\n",
156+
"assert unique_chars_count(\"aabbcc\") == 3\n",
157+
"assert unique_chars_count(\"abcabc\") == 3\n",
158+
"assert unique_chars_count(\"aaaaaa\") == 1\n",
159+
"assert unique_chars_count(\"\") == 0\n",
160+
"\n",
161+
"\"✅ All OK!\""
162+
]
163+
},
164+
{
165+
"cell_type": "markdown",
166+
"metadata": {},
167+
"source": [
168+
"## Задача 6\n",
169+
"\n",
170+
"Цензурирайте всички лоши думички в даден текст, заменяйки ги със звездички. \n",
171+
"\n",
172+
"* Лошите думички сa дадени в списъка `BAD_WORDS`.\n",
173+
"* Върнете текста, като замените всички лоши думички със звездички (`\"*\"`) (толкова на брой, колкото символи има въпросната дума).\n",
174+
"* За простота не проверявайте за главни и малки букви, а ги заменяйте както са ви дадени.\n",
175+
"* Hint: за да създадете стринг с `n` на брой символа `\"x\"`, използвайте `n * \"x\"`.\n",
176+
"* Hint 2: ползвайте `str.split` и `str.join`"
177+
]
178+
},
179+
{
180+
"cell_type": "code",
181+
"metadata": {},
182+
"execution_count": null,
183+
"outputs": [],
184+
"source": [
185+
"BAD_WORDS = [\n",
186+
" \"fuck\",\n",
187+
" \"shit\",\n",
188+
" \"bullshit\",\n",
189+
" \"bastard\",\n",
190+
" \"bitch\",\n",
191+
" \"whore\",\n",
192+
" \"damn\",\n",
193+
"]\n",
194+
"\n",
195+
"\n",
196+
"def censored(text):\n",
197+
" pass # write your code here\n",
198+
"\n",
199+
"\n",
200+
"# Tests:\n",
201+
"\n",
202+
"assert censored(\"this line should not be censored at all\") == \"this line should not be censored at all\"\n",
203+
"assert censored(\"fuck this bitch\") == \"**** this *****\"\n",
204+
"assert censored(\"This task is UTTER BULLshit\") == \"This task is UTTER ********\", \"be careful with the case!\"\n",
205+
"assert censored(\"\") == \"\"\n",
206+
"\n",
207+
"\"✅ All OK!\""
208+
]
209+
},
210+
{
211+
"cell_type": "markdown",
212+
"metadata": {},
213+
"source": [
214+
"## Задача 7\n",
215+
"\n",
216+
"Създайте функция `find_shortest_and_longest_words`, която:\n",
217+
"* Приема 2 параметъра - списък и буква\n",
218+
"* Връща наредена двойка от най-късата и най-дългата дума в списъка, която започва с дадената буква"
219+
]
220+
},
221+
{
222+
"cell_type": "code",
223+
"metadata": {},
224+
"execution_count": null,
225+
"outputs": [],
226+
"source": [
227+
"def find_shortest_and_longest_words(words, letter):\n",
228+
" pass\n",
229+
"\n",
230+
"# Tests\n",
231+
"\n",
232+
"assert find_shortest_and_longest_words([\"abracadabra\", \"banana\", \"kugelschreiber\", \"ant\", \"almost\", \"thisshouldbeaverylongword\"], \"a\") == (\"ant\", \"abracadabra\")\n",
233+
"assert find_shortest_and_longest_words([\"abracadabra\", \"banana\", \"kugelschreiber\", \"ant\", \"almost\", \"thisshouldbeaverylongword\"], \"b\") == (\"banana\", \"banana\")\n",
234+
"\n",
235+
"\"✅ All OK!\""
236+
]
237+
}
238+
],
239+
"metadata": {
240+
"kernelspec": {
241+
"display_name": "Python 3",
242+
"language": "python",
243+
"name": "python3"
244+
},
245+
"language_info": {
246+
"codemirror_mode": {
247+
"name": "ipython",
248+
"version": 3
249+
},
250+
"file_extension": ".py",
251+
"mimetype": "text/x-python",
252+
"name": "python",
253+
"nbconvert_exporter": "python",
254+
"pygments_lexer": "ipython3",
255+
"version": "3.11.5"
256+
}
257+
},
258+
"nbformat": 4,
259+
"nbformat_minor": 2
260+
}

0 commit comments

Comments
 (0)