Comparing two sequences
Some questions hand you two strings or lists and ask how they relate. The usual move is to walk both with the same index:
for i in range(len(a)): if a[i] != b[i]: ...
Before you do, ask what happens when they have different lengths. b[i] raises IndexError once i passes the shorter one. Loop to min(len(a), len(b)) for the shared part, then deal with the leftover tail on purpose.
Other handy shapes:
zip(a, b)pairs items up but stops at the shorter sequence, so extra items are silently dropped.- For a running comparison (who is ahead?), update running totals in one loop and compare them after each step.
- When you must find where two sequences first disagree, stop at the first mismatch: that position, not the whole scan, is the answer.
Watch i step through both strings, and diff count mismatches.
See it run
a = "cart" b = "card" diff = 0 for i in range(len(a)): if a[i] != b[i]: diff = diff + 1
Two strings, compared position by position with one shared index.
Practice comparing two sequences
Exercise 1
Two spellings of a street name are compared. Write solution(a, b) returning the first position where they differ.
Rules:
- If both strings have a character at position
iand they differ,iis a candidate; return the smallest suchi. - If there is no such position but the lengths differ (one string is the start of the other), return the length of the shorter string.
- If the strings are identical, return
-1. - Upper and lower case letters are different characters.
Constraints: 0 ≤ len(a), len(b) ≤ 1000.
Example: solution("cart", "card") returns 3, and solution("ab", "abc") returns 2.
Exercise 2
Two queues merge into one at a door. Write solution(first, second) returning a new list where people alternate: first's person 1, second's person 1, first's person 2, second's person 2, and so on.
Rules:
- Always take from
firstbeforesecondat each step. - When one list runs out, the rest of the other list follows in its original order.
- Either list may be empty.
Constraints: 0 ≤ len(first), len(second) ≤ 1000. Items can be any values.
Example: solution([5, 6, 7, 8], [1, 2]) returns [5, 1, 6, 2, 7, 8].
Exercise 3
Two teams play rounds. a[i] and b[i] are the points team A and team B scored in round i (equal lengths). After every round, compare the running totals: the team with the larger total is the leader; equal totals mean nobody leads.
A lead change happens when a round ends with a leader who is different from the most recent earlier leader. Rounds where nobody leads are ignored, and the very first leader is not a change.
Return the number of lead changes, as an integer.
Constraints: 0 ≤ len(a) = len(b) ≤ 10^4, points between 0 and 100.
Example: solution([3, 0, 2], [1, 4, 0]) returns 1: totals 3-1 (A leads), 3-5 (B leads: change), 5-5 (nobody).
Exercise 4
A shelf list full was copied, and the copy short lost exactly one item: short is full with one position deleted, all other items in the same order. Find which position was deleted.
Return the smallest index i in full such that deleting full[i] gives exactly short. (With equal neighbours several positions work; the smallest is wanted.)
Rules:
len(full) = len(short) + 1and a valid answer always exists.- Items can be any values that compare with
==.
Constraints: 1 ≤ len(full) ≤ 1.2·10^4. Checking every deletion by rebuilding the list is too slow.
Example: solution([4, 7, 9], [4, 9]) returns 1, and solution([5, 5, 5], [5, 5]) returns 0.
The explanation and tasks are public. Running this lesson requires an account with course access.
Practice
Loading Python…Visualizer Watch your code run line by line
Run your code, then choose Play to follow its execution.