Final challenge: two days of logins
After this: Follow a set, predict a seen-set program and write a difference by hand.
A login service records the user names that log in each day. This challenge has three short steps, and each one is scored on its own.
- Follow a set through seven operations.
- Predict what a seen-set program prints for today's logins.
- Choose an approach for comparing two days, then write the one line that finds the users who logged in today but not yesterday.
You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 3Trace it
Trace it
An 8-slot set starts empty. Run add(cat), add(dog), add(act), add(cat), remove(dog), add(tac), remove(act). What does slot 0 hold afterwards?
Predict it
What does this print?
logins = "amir bea amir kai bea amir".split() # today's loginsseen = set()repeats = set()for user in logins: if user in seen: repeats.add(user) # logged in more than once seen.add(user)print(len(seen), sorted(repeats))String[] logins = "amir bea amir kai bea amir".split(" "); // today's loginsSet<String> seen = new HashSet<>();Set<String> repeats = new HashSet<>();for (String user : logins) { if (seen.contains(user)) { repeats.add(user); // logged in more than once } seen.add(user);}System.out.println(seen.size() + " " + new TreeSet<>(repeats));Apply it · the approach
today and yesterday are lists of user names, n and m items long, and each list may have repeats. You want the users who logged in on both days. Which approach is best?
Apply it · complete the difference
Now find the users who logged in today but not yesterday. only_today(today, yesterday)onlyToday(today, yesterday) goes through today and keeps each user who is not in yesterday. Write the missing test. yesterday is a set.
def only_today(today, yesterday): result = set() for user in today: ______________________________ # ← keep user only if yesterday lacks them result.add(user) return resultstatic Set<String> onlyToday(Set<String> today, Set<String> yesterday) { Set<String> result = new HashSet<>(); for (String user : today) { ______________________________ // ← keep user only if yesterday lacks them result.add(user); } } return result;}- Follow a set through adds and removes, predict a seen-set program and write a difference by hand.