Skip to content

Commit a9ecd3d

Browse files
committed
Make the user inspect repeated subscripts with birdseye.
Make the example nested list easier to type in. Make all indices of triple subscripting different. The exercise requires List[List[str]] Small tweaks to text
1 parent 6f95707 commit a9ecd3d

File tree

2 files changed

+80
-79
lines changed

2 files changed

+80
-79
lines changed

backend/main/chapters/c08_nested_loops.py

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
VerbatimStep,
1313
Disallowed,
1414
search_ast,
15+
Step,
1516
)
1617

1718

@@ -1016,7 +1017,7 @@ def solution(self, strings: List[str]):
10161017
(["Alice", "went", "to", "school"], 'w')
10171018
]
10181019

1019-
class double_subscripting(ExerciseStep):
1020+
class double_subscripting(Step):
10201021
"""
10211022
You may have solved it like this:
10221023
@@ -1031,7 +1032,24 @@ class double_subscripting(ExerciseStep):
10311032
10321033
Take a good look at this syntax. If it looks new and fancy, it's not.
10331034
It's just the usual syntax for subscripting, applied twice.
1035+
Try it in birdseye to see how Python breaks it down into smaller pieces.
1036+
"""
1037+
1038+
expected_code_source = "birdseye"
1039+
program_in_text = False
1040+
1041+
def program(self):
1042+
strings = ["abc", "def", "ghi"]
1043+
print(strings[1][0])
1044+
1045+
def check(self):
1046+
return search_ast(
1047+
self.tree,
1048+
ast.Subscript(value=ast.Subscript()),
1049+
)
10341050

1051+
class double_subscripting_exercise(ExerciseStep):
1052+
"""
10351053
Using this syntax, modify the program to print the last letter of the second-to-last string in the list `strings`.
10361054
You must use a single expression like above, and you are not allowed to use `len`.
10371055
Your solution should work for any non-empty list of strings.
@@ -1054,9 +1072,11 @@ def check(self):
10541072
This is very similar to the previous exercise.
10551073
Do you remember how to access the last position of a list (without using `len`)?
10561074
Similarly how do you access the second-to-last position in a list?
1075+
If you can't remember, you can Google it!
10571076
Indexing works similarly on lists and strings.
10581077
Do you get an `index out of range` error? Is it for a string, or a list? Why?
10591078
Make sure you are not confusing the order of the list index and the string index.
1079+
Use birdseye if you're having trouble.
10601080
"""
10611081
tests = [
10621082
(["abc", "de", "fghi", "jklmn", "o"], 'n'),
@@ -1075,81 +1095,40 @@ class first_nested_list_example(VerbatimStep):
10751095
"""
10761096

10771097
def program(self):
1078-
strings = [
1079-
[
1080-
"hello there",
1081-
"how are you",
1082-
],
1083-
[
1084-
"goodbye world",
1085-
"hello world",
1086-
]
1087-
]
1098+
strings = [['hello', 'there'], ['how', 'are', 'you']]
10881099
print(strings[1][0])
10891100

1090-
predicted_output_choices = ["""\
1091-
hello there
1092-
""", """\
1093-
how are you
1094-
""", """\
1095-
goodbye world
1096-
""", """\
1097-
hello world
1098-
""", """\
1099-
hello there
1100-
how are you
1101-
""", """\
1102-
goodbye world
1103-
hello world
1104-
"""]
1101+
predicted_output_choices = [
1102+
"hello", "there", "how", "are", "you",
1103+
"['hello', 'there']", "['how', 'are', 'you']",
1104+
'h', 't', 'e', 'a',
1105+
]
11051106

11061107
class triple_subscripting(ExerciseStep):
11071108
"""
11081109
As you can see Python allows us to have *nested lists*: a list where each element is another list (we refer to them as *sublists*).
1109-
This idea is very powerful and it will allow us to create more complicated and interesting programs.
11101110
11111111
We can use subscripting even more than twice.
11121112
Write a program that takes a nested list `strings` like above,
1113-
and prints the **first letter of the first string in the second sublist**.
1114-
Use only a single expression like in the previous exercise. For example, if
1115-
1116-
__copyable__
1117-
strings = [
1118-
[
1119-
"hello there",
1120-
"how are you",
1121-
],
1122-
[
1123-
"goodbye world",
1124-
"hello world",
1125-
]
1126-
]
1127-
1128-
it should print `g`.
1113+
and prints the **first letter of the third string in the second sublist**.
1114+
Use only a single expression like in the previous exercise.
1115+
For example, for the list above, it should print `y`.
11291116
"""
11301117

1131-
def solution(self, strings: List[str]):
1132-
print(strings[1][0][0])
1118+
def solution(self, strings: List[List[str]]):
1119+
print(strings[1][2][0])
11331120

11341121
hints = """
11351122
This is very similar to the previous exercises.
11361123
How many times do you need to use subscripting?
11371124
First you need to access a sublist.
11381125
Then a string in that sublist.
11391126
Then a letter in that string.
1127+
Use birdseye if you're having trouble.
11401128
"""
11411129

11421130
tests = [
1143-
([
1144-
[
1145-
"hello there",
1146-
"how are you",
1147-
],
1148-
[
1149-
"goodbye world",
1150-
"hello world",
1151-
]
1152-
], 'g'),
1131+
([['hello', 'there'], ['how', 'are', 'you']], 'y'),
11531132
([
11541133
[
11551134
"The cat stretched.",
@@ -1163,7 +1142,7 @@ def solution(self, strings: List[str]):
11631142
[
11641143
"Aaron made a picture."
11651144
]
1166-
], "T")
1145+
], "s")
11671146
]
11681147

11691148
class nested_list_nested_loop_example(VerbatimStep):

backend/main/tests/test_transcript.json

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12691,6 +12691,32 @@
1269112691
},
1269212692
"step": "string_list_exercise"
1269312693
},
12694+
{
12695+
"page": "Introducing Nested Lists",
12696+
"program": [
12697+
"strings = [\"abc\", \"def\", \"ghi\"]",
12698+
"print(strings[1][0])"
12699+
],
12700+
"response": {
12701+
"message": "",
12702+
"passed": true,
12703+
"result": [
12704+
{
12705+
"color": "white",
12706+
"text": "d"
12707+
},
12708+
{
12709+
"color": "white",
12710+
"text": "\n"
12711+
},
12712+
{
12713+
"color": "white",
12714+
"text": ">>> "
12715+
}
12716+
]
12717+
},
12718+
"step": "double_subscripting"
12719+
},
1269412720
{
1269512721
"get_solution": [
1269612722
"print(strings[-2][-1])"
@@ -12718,42 +12744,38 @@
1271812744
}
1271912745
]
1272012746
},
12721-
"step": "double_subscripting"
12747+
"step": "double_subscripting_exercise"
1272212748
},
1272312749
{
1272412750
"page": "Introducing Nested Lists",
1272512751
"program": [
12726-
"strings = [",
12727-
" [",
12728-
" \"hello there\",",
12729-
" \"how are you\",",
12730-
" ],",
12731-
" [",
12732-
" \"goodbye world\",",
12733-
" \"hello world\",",
12734-
" ]",
12735-
"]",
12752+
"strings = [['hello', 'there'], ['how', 'are', 'you']]",
1273612753
"print(strings[1][0])"
1273712754
],
1273812755
"response": {
1273912756
"message": "",
1274012757
"passed": true,
1274112758
"prediction": {
12742-
"answer": "goodbye world",
12759+
"answer": "how",
1274312760
"choices": [
12744-
"hello there",
12745-
"how are you",
12746-
"goodbye world",
12747-
"hello world",
12748-
"hello there\nhow are you",
12749-
"goodbye world\nhello world",
12761+
"hello",
12762+
"there",
12763+
"how",
12764+
"are",
12765+
"you",
12766+
"['hello', 'there']",
12767+
"['how', 'are', 'you']",
12768+
"h",
12769+
"t",
12770+
"e",
12771+
"a",
1275012772
"Error"
1275112773
]
1275212774
},
1275312775
"result": [
1275412776
{
1275512777
"color": "white",
12756-
"text": "goodbye world"
12778+
"text": "how"
1275712779
},
1275812780
{
1275912781
"color": "white",
@@ -12769,20 +12791,20 @@
1276912791
},
1277012792
{
1277112793
"get_solution": [
12772-
"print(strings[1][0][0])"
12794+
"print(strings[1][2][0])"
1277312795
],
1277412796
"page": "Introducing Nested Lists",
1277512797
"program": [
12776-
"strings = [['hello there', 'how are you'], ['goodbye world', 'hello world']]",
12777-
"print(strings[1][0][0])"
12798+
"strings = [['hello', 'there'], ['how', 'are', 'you']]",
12799+
"print(strings[1][2][0])"
1277812800
],
1277912801
"response": {
1278012802
"message": "",
1278112803
"passed": true,
1278212804
"result": [
1278312805
{
1278412806
"color": "white",
12785-
"text": "g"
12807+
"text": "y"
1278612808
},
1278712809
{
1278812810
"color": "white",

0 commit comments

Comments
 (0)