Final challenge: five of the ten again
After this: Name the pattern, predict the output, complete a line and give the cost for five of the ten problems in a new form.
This challenge brings back five of the ten problems in a new form, with the pattern first each time. You will name the pattern for a problem in new words, predict the best profit on four days, complete the flip line in reverse, count the islands in a chequered grid, and say the cost of a merge. Each step is scored on its own. The steps with code show it in Python and in Java, and 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 5Name the pattern
Name the pattern
A web page is made of tags such as <b> and </b>. You must check that every opening tag is closed, and that the tags are closed in the right order: <b><i></i></b> is fine, <b><i></b></i> is not. Which pattern fits?
Predict the profit
Best day to buy and sell, on four days. What does this print?
def max_profit(prices): lowest = float("inf") best = 0 for p in prices: lowest = min(lowest, p) best = max(best, p - lowest) return best
print(max_profit([2, 4, 1, 5]))public class Main { static int maxProfit(int[] prices) { int lowest = Integer.MAX_VALUE; int best = 0; for (int p : prices) { lowest = Math.min(lowest, p); best = Math.max(best, p - lowest); } return best; }
public static void main(String[] args) { System.out.println(maxProfit(new int[] {2, 4, 1, 5})); }}Complete the flip
Complete the line that turns the current node round, so that the program prints the reversed list 3 -> 2 -> 1.
class Node: def __init__(self, value, next=None): self.value = value self.next = next
def __repr__(self): return f"{self.value} -> {self.next}"
def reverse(head): prev = None cur = head while cur: nxt = cur.next ____________________ # ← the flip prev = cur cur = nxt return prev
print(reverse(Node(1, Node(2, Node(3)))))class Node { int value; Node next;
Node(int value) { this.value = value; } Node(int value, Node next) { this.value = value; this.next = next; }
public String toString() { return value + " -> " + next; }}
public class Main { static Node reverse(Node head) { Node prev = null; Node cur = head; while (cur != null) { Node nxt = cur.next; ____________________ // ← the flip prev = cur; cur = nxt; } return prev; }
public static void main(String[] args) { System.out.println(reverse(new Node(1, new Node(2, new Node(3))))); }}Count the islands
A chequered grid: land in the four corners and in the middle. What does this print?
def count_islands(grid): rows = len(grid) cols = len(grid[0]) if grid else 0 seen = set() def sink(r, c): if r < 0 or r >= rows or c < 0 or c >= cols: return if grid[r][c] != "1" or (r, c) in seen: return seen.add((r, c)) sink(r + 1, c); sink(r - 1, c); sink(r, c + 1); sink(r, c - 1) count = 0 for r in range(rows): for c in range(cols): if grid[r][c] == "1" and (r, c) not in seen: count += 1 sink(r, c) return count
print(count_islands(["101", "010", "101"]))public class Main { static int countIslands(String[] grid) { int rows = grid.length; int cols = rows == 0 ? 0 : grid[0].length(); boolean[][] seen = new boolean[rows][cols]; int count = 0; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { if (grid[r].charAt(c) == '1' && !seen[r][c]) { count++; sink(grid, seen, r, c); } } } return count; }
static void sink(String[] grid, boolean[][] seen, int r, int c) { if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length()) { return; } if (grid[r].charAt(c) != '1' || seen[r][c]) { return; } seen[r][c] = true; sink(grid, seen, r + 1, c); sink(grid, seen, r - 1, c); sink(grid, seen, r, c + 1); sink(grid, seen, r, c - 1); }
public static void main(String[] args) { System.out.println(countIslands(new String[] {"101", "010", "101"})); }}Say the cost
merge joins 1 → 5 → 9 and 2 → 3 → 7 → 8 into 1 → 2 → 3 → 5 → 7 → 8 → 9. For lists of n and m nodes, what is the cost?
- Name the pattern for a problem in new words, predict a profit and an island count, complete the flip line of reverse, and say the cost of a merge.