You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: backend/main/chapters/c06_lists.py
+33-21Lines changed: 33 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -822,7 +822,7 @@ class append_vs_concatenate(VerbatimStep):
822
822
823
823
- **`append`**: Add an element to the end of the list. `nums.append(4)` changes the list to `[1, 2, 3, 4]`.
824
824
- **`len`**: Returns the number of elements. `len(nums)` is `3`.
825
-
- **`range`**: `range(n)` is an object similar to the list of numbers from 0 to `n - 1`. In particular, `range(len(nums))` is like `[0, 1, 2]`.
825
+
- **`range`**: `range(n)` is an object similar to the list of numbers from 0 to `n - 1`.That means it contains `n` numbers.In particular, `range(len(nums))` is like `[0, 1, 2]`, which are the indices of every element in `nums`.
826
826
- **`subscripting`**: Get a value at an index. `nums[0]` is 1, `nums[1]` is 2, `nums[2]` is 3.
Your solution should have exactly three statements: `x = ['a', 'b', 'c']`, `y = ` followed by one line copied exactly from the list, and `print(y)`.
969
-
Which function/method allows you to create a new list?
970
-
Start with the original list `x`.
971
-
Then add something to it.
970
+
Which lines of code create a new list rather than modifying?
971
+
`x` is a list. Each element of `x` is a string.
972
+
You can add lists together, you can add strings together, but you can't add a string and a list.
973
+
How do you make a list containing one element?
972
974
"""
973
975
974
976
classremove_exercise(VerbatimStep):
@@ -993,9 +995,11 @@ def program(self):
993
995
hints="""
994
996
Your solution should have exactly three statements: `x = [1, 2, 0, 3]`, one line copied exactly from the list (no additions), and `print(x)`.
995
997
What does `x.remove(0)` do?
996
-
Which function/method can do the same thing?
997
-
Does that function/method need something else to work?
998
-
Maybe another function/method?
998
+
It removes an element!
999
+
Which function/method can also remove an element?
1000
+
The other function/method can't simply be told 'remove 0', it needs a different kind of information.
1001
+
Specifically, it needs to be told where 0 is.
1002
+
Which function/method provides that kind of information?
999
1003
"""
1000
1004
1001
1005
final_text="""
@@ -1045,21 +1049,25 @@ class count_in_sorted_sum(VerbatimStep):
1045
1049
1046
1050
__copyable__
1047
1051
x = [1, 2, 0, 3]
1048
-
print(1 in x)
1052
+
y = 1 in x
1053
+
print(y)
1049
1054
1050
-
Replace the part `1 in x` with one line from the list above that does the same thing.
1055
+
Replace the part `1 in x`(leave in the `y = `)with one line from the list above that does the same thing.
1051
1056
"""
1052
1057
1053
1058
program_in_text=False
1054
1059
1055
1060
defprogram(self):
1056
1061
x= [1,2,0,3]
1057
-
print(x.count(1)>0)
1062
+
y=x.count(1)>0
1063
+
print(y)
1058
1064
1059
1065
hints="""
1060
-
Your solution should have exactly two statements: `x = [1, 2, 0, 3]` and `print()` where one line is copied exactly from the list above and put inside the parentheses.
1061
-
When is `1 in x` true?
1062
-
What other function/method can tell us the same thing? And how?
1066
+
Your solution should have exactly three statements: `x = ['a', 'b', 'c']`, `y = ` followed by one line copied exactly from the list, and `print(y)`.
1067
+
When is `1 in x` True?
1068
+
When `1` is in `x`!
1069
+
Could be that `1` is in `x` once, or twice, or three times...
1070
+
...but not zero times!
1063
1071
"""
1064
1072
1065
1073
classaverage_exercise(VerbatimStep):
@@ -1072,7 +1080,7 @@ class average_exercise(VerbatimStep):
1072
1080
print(y)
1073
1081
1074
1082
Replace the part after `y = ` with one line from the list above.
1075
-
The final program should print the average of the numbers in `x`.
1083
+
The final program should print the average(technically the *mean*)of the numbers in `x`.
1076
1084
"""
1077
1085
1078
1086
program_in_text=False
@@ -1084,9 +1092,10 @@ def program(self):
1084
1092
1085
1093
hints="""
1086
1094
Your solution should have exactly three statements: `x = [15, 12, -6, 3]`, `y = ` followed by one line copied exactly from the list, and `print(y)`.
1095
+
If you're not sure, look up how to calculate the average/mean.
1087
1096
To calculate the average of numbers in `x` we need two things.
1088
-
Which two functions/methodscan do those?
1089
-
How do youuse those twotogether, to calculate the average?
1097
+
Which two functions/methodsgive you those two things?
1098
+
How do youcombine those twothings to calculate the average?
1090
1099
"""
1091
1100
1092
1101
classsum_range_exercise(VerbatimStep):
@@ -1099,7 +1108,7 @@ class sum_range_exercise(VerbatimStep):
1099
1108
print(y)
1100
1109
1101
1110
Replace the part after `y = ` with one line from the list above.
1102
-
The final program should print thevalue of thesum: `1 + 2 + 3 + ... + x`.
1111
+
The final program should print theresult ofadding up allthenumbers from `1` to `x` inclusive, i.e. `1 + 2 + 3 + ... + x`.
1103
1112
"""
1104
1113
1105
1114
program_in_text=False
@@ -1112,7 +1121,8 @@ def program(self):
1112
1121
hints="""
1113
1122
Your solution should have exactly three statements: `x = 100`, `y = ` followed by one line copied exactly from the list, and `print(y)`.
1114
1123
What function/method can be used to add up things?
1115
-
Which function/method gives us something similar to the list of numbers `1, 2, 3, ..., x`?
1124
+
Which function/method gives us the numbers `1, 2, 3, ..., x`?
1125
+
You have to make a small tweak, otherwise that last number `x` will be left out.
Your solution should have exactly three statements: `x = [12, -6, 2, -1, 3]`, `y = ` followed by one line copied exactly from the list, and `print(y)`.
1140
1150
The numbers in `x` seem to be all out of order. Can you do something about that?
1151
+
If you figured that part out, try using that function in the shell to play around with it.
1152
+
How would you use that function to get the smallest value in a list? What about the biggest?
1141
1153
After that, how can you get the *second* smallest value?