Final challenge: a list from start to finish
After this: Predict, complete and fix programs that build and go through a list.
This challenge has four steps, and each step is scored on its own. You will predict what a list holds after three lines build it up, complete the condition of a search loop, work out what a second name and a real copy each see, and fix a loop that starts from the wrong value. Every step shows the code in Python and in Java; the answers are the same in both. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 4Predict it
Predict it
A list is built up in three steps. What does this print?
scores = [5, 9, 2]scores.append(7)scores.insert(0, 1)print(scores[1])print(len(scores))ArrayList<Integer> scores = new ArrayList<>(List.of(5, 9, 2));scores.add(7);scores.add(0, 1);System.out.println(scores.get(1));System.out.println(scores.size());Complete the search
Complete the missing line so that found becomes Truetrue when the list holds 60. Write the whole line, including the colon at the endbrackets and the opening brace.
marks = [72, 85, 60, 91]found = Falsefor n in marks: ____________________ # ← is this the mark we are looking for, 60? found = Trueprint(found)ArrayList<Integer> marks = new ArrayList<>(List.of(72, 85, 60, 91));boolean found = false;for (int n : marks) { ____________________ // ← is this the mark we are looking for, 60? found = true; }}System.out.println(found);Two names and a copy
b is a second name for the list a names. c is a real copy. What does this print?
a = [1, 2]b = ac = list(a)a.append(3)print(len(b))print(len(c))ArrayList<Integer> a = new ArrayList<>(List.of(1, 2));ArrayList<Integer> b = a;ArrayList<Integer> c = new ArrayList<>(a);a.add(3);System.out.println(b.size());System.out.println(c.size());Fix it
This program is meant to find the smallest mark, 60. It prints 0. Which one change makes it right for any list of marks?
marks = [72, 85, 60]smallest = 0for n in marks: if n < smallest: smallest = nprint(smallest)Output
0
ArrayList<Integer> marks = new ArrayList<>(List.of(72, 85, 60));int smallest = 0;for (int n : marks) { if (n < smallest) { smallest = n; }}System.out.println(smallest);Output
0
- Predict what a built-up list holds, complete a search loop, work out what two names and a copy see, and fix a loop that starts from the wrong value.