Skip to content

Commit ecc40e8

Browse files
committed
Exercise: reversing names, make folder for solutions
1 parent 9463fed commit ecc40e8

File tree

6 files changed

+183
-56
lines changed

6 files changed

+183
-56
lines changed

06_Strings.ipynb

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,72 @@
9191
"print(quote_list)"
9292
]
9393
},
94+
{
95+
"cell_type": "markdown",
96+
"metadata": {},
97+
"source": [
98+
"### Joining strings\n",
99+
"\n",
100+
"We can also go the other way, joining a list of strings to a single string.\n",
101+
"The syntax is different, we must first specify the string that is used to \"glue\" the parts together.\n",
102+
"This is the opposite of the separator above."
103+
]
104+
},
105+
{
106+
"cell_type": "code",
107+
"execution_count": 4,
108+
"metadata": {},
109+
"outputs": [
110+
{
111+
"name": "stdout",
112+
"output_type": "stream",
113+
"text": [
114+
"Strange women lying in ponds, distributing swords, is no basis for a system of government!\n"
115+
]
116+
}
117+
],
118+
"source": [
119+
"joined_list = \",\".join(quote_list)\n",
120+
"print(joined_list)"
121+
]
122+
},
123+
{
124+
"cell_type": "markdown",
125+
"metadata": {},
126+
"source": [
127+
"### Exercise: reversing names\n",
128+
"\n",
129+
"Complete the function below which reverses names.\n",
130+
"\"Doe, John\" should become \"John Doe\".\n",
131+
"\n",
132+
"To reverse a list, you can use the method `reverse()`."
133+
]
134+
},
135+
{
136+
"cell_type": "code",
137+
"execution_count": 5,
138+
"metadata": {},
139+
"outputs": [
140+
{
141+
"name": "stdout",
142+
"output_type": "stream",
143+
"text": [
144+
"None\n"
145+
]
146+
}
147+
],
148+
"source": [
149+
"def reverse_name(name):\n",
150+
" \"\"\"\n",
151+
" Convert a name from inverted form to regular\n",
152+
" >>> reverse_name(\"Doe, John\")\n",
153+
" 'John Doe'\n",
154+
" \"\"\"\n",
155+
" pass #your code here\n",
156+
"\n",
157+
"print(reverse_name(\"Doe, John\"))"
158+
]
159+
},
94160
{
95161
"cell_type": "markdown",
96162
"metadata": {},
@@ -102,7 +168,7 @@
102168
},
103169
{
104170
"cell_type": "code",
105-
"execution_count": 4,
171+
"execution_count": 6,
106172
"metadata": {},
107173
"outputs": [
108174
{
@@ -121,7 +187,7 @@
121187
},
122188
{
123189
"cell_type": "code",
124-
"execution_count": 5,
190+
"execution_count": 7,
125191
"metadata": {},
126192
"outputs": [
127193
{
@@ -148,7 +214,7 @@
148214
},
149215
{
150216
"cell_type": "code",
151-
"execution_count": 6,
217+
"execution_count": 8,
152218
"metadata": {},
153219
"outputs": [
154220
{

07_Dictionaries.ipynb

100755100644
File mode changed.
File renamed without changes.

solutions_03-04.ipynb renamed to solutions/solutions_03-04.ipynb

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -135,59 +135,6 @@
135135
"\n",
136136
"print(\"Number of dissents:\", dissent_count)"
137137
]
138-
},
139-
{
140-
"cell_type": "markdown",
141-
"metadata": {},
142-
"source": [
143-
"## Exercise: Count opinions per author"
144-
]
145-
},
146-
{
147-
"cell_type": "code",
148-
"execution_count": 4,
149-
"metadata": {},
150-
"outputs": [
151-
{
152-
"name": "stdout",
153-
"output_type": "stream",
154-
"text": [
155-
"Number of opinions per author:\n",
156-
"JUSTICE EPSTEIN wrote 3 opinions\n",
157-
"JUSTICE GOLDENHERSH wrote 2 opinions\n",
158-
"JUSTICE CONNORS wrote 2 opinions\n",
159-
"JUSTICE HARRIS wrote 2 opinions\n",
160-
"PRESIDING JUSTICE GALLAGHER wrote 2 opinions\n"
161-
]
162-
}
163-
],
164-
"source": [
165-
"import requests\n",
166-
"import json\n",
167-
"\n",
168-
"\n",
169-
"URL = \"https://api.case.law/v1/cases/?jurisdiction=ill&full_case=true&decision_date_min=2011-01-01&page_size=20\"\n",
170-
"data = requests.get(URL).json()\n",
171-
"\n",
172-
"author2count = {}\n",
173-
"\n",
174-
"cases = data[\"results\"]\n",
175-
"for case in cases:\n",
176-
" opinions = case[\"casebody\"][\"data\"][\"opinions\"]\n",
177-
" for opinion in opinions:\n",
178-
" author = opinion[\"author\"]\n",
179-
" if author in author2count:\n",
180-
" author2count[author] += 1\n",
181-
" else:\n",
182-
" author2count[author] = 1\n",
183-
"\n",
184-
"# Sorting by count\n",
185-
"sorted_authors = sorted(author2count, key=lambda author: author2count[author], reverse=True)\n",
186-
"print(\"Number of opinions per author:\")\n",
187-
"for author in sorted_authors[:5]:\n",
188-
" count = author2count[author]\n",
189-
" print(\"%s wrote %d opinions\" % (author, count))\n"
190-
]
191138
}
192139
],
193140
"metadata": {
File renamed without changes.

solutions/solutions_06_07.ipynb

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Exercise: reversing names"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [
15+
{
16+
"name": "stdout",
17+
"output_type": "stream",
18+
"text": [
19+
"John Doe\n"
20+
]
21+
}
22+
],
23+
"source": [
24+
"def reverse_name(name):\n",
25+
" \"\"\"\n",
26+
" Convert a name from inverted form to regular\n",
27+
" >>> reverse_name(\"Doe, John\")\n",
28+
" 'John Doe'\n",
29+
" \"\"\"\n",
30+
" name_parts = name.split(\", \")\n",
31+
" name_parts.reverse()\n",
32+
" reversed = \" \".join(name_parts)\n",
33+
" return reversed\n",
34+
"\n",
35+
"\n",
36+
"print(reverse_name(\"Doe, John\"))"
37+
]
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"metadata": {},
42+
"source": [
43+
"## Exercise: Count opinions per author"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 2,
49+
"metadata": {},
50+
"outputs": [
51+
{
52+
"name": "stdout",
53+
"output_type": "stream",
54+
"text": [
55+
"Number of opinions per author:\n",
56+
"JUSTICE EPSTEIN wrote 3 opinions\n",
57+
"JUSTICE GOLDENHERSH wrote 2 opinions\n",
58+
"JUSTICE CONNORS wrote 2 opinions\n",
59+
"JUSTICE HARRIS wrote 2 opinions\n",
60+
"PRESIDING JUSTICE GALLAGHER wrote 2 opinions\n"
61+
]
62+
}
63+
],
64+
"source": [
65+
"import requests\n",
66+
"import json\n",
67+
"\n",
68+
"\n",
69+
"URL = \"https://api.case.law/v1/cases/?jurisdiction=ill&full_case=true&decision_date_min=2011-01-01&page_size=20\"\n",
70+
"data = requests.get(URL).json()\n",
71+
"\n",
72+
"author2count = {}\n",
73+
"\n",
74+
"cases = data[\"results\"]\n",
75+
"for case in cases:\n",
76+
" opinions = case[\"casebody\"][\"data\"][\"opinions\"]\n",
77+
" for opinion in opinions:\n",
78+
" author = opinion[\"author\"]\n",
79+
" if author in author2count:\n",
80+
" author2count[author] += 1\n",
81+
" else:\n",
82+
" author2count[author] = 1\n",
83+
"\n",
84+
"# Sorting by count\n",
85+
"sorted_authors = sorted(author2count, key=lambda author: author2count[author], reverse=True)\n",
86+
"print(\"Number of opinions per author:\")\n",
87+
"for author in sorted_authors[:5]:\n",
88+
" count = author2count[author]\n",
89+
" print(\"%s wrote %d opinions\" % (author, count))\n"
90+
]
91+
}
92+
],
93+
"metadata": {
94+
"kernelspec": {
95+
"display_name": "Python 3",
96+
"language": "python",
97+
"name": "python3"
98+
},
99+
"language_info": {
100+
"codemirror_mode": {
101+
"name": "ipython",
102+
"version": 3
103+
},
104+
"file_extension": ".py",
105+
"mimetype": "text/x-python",
106+
"name": "python",
107+
"nbconvert_exporter": "python",
108+
"pygments_lexer": "ipython3",
109+
"version": "3.6.3"
110+
}
111+
},
112+
"nbformat": 4,
113+
"nbformat_minor": 2
114+
}

0 commit comments

Comments
 (0)