Problem 237991 · easy · Level 02 Linear Data Structures

Packets That Do Not Add Up

py-unpacking · strings · parsing

A weather station sends one packet per line. A packet is a sequence of fields separated by single spaces: first the packet's name, then zero or more integer readings, and last the total the station computed before sending. A packet is corrupted when the readings do not add up to its total.

Write bad_packets(lines) that returns the names of the corrupted packets, in the order they appear.

Examples

Input:  lines = ["north 4 8 15 27", "south 16 23 40", "east 42 42", "west 0"]
Output: ["south"]
Explanation: 4 + 8 + 15 = 27 and 42 = 42 are fine; 16 + 23 = 39, not 40.
             "west" has no readings, so its readings add up to 0, which matches.

Input:  lines = ["a -3 5 2", "b 1 1 1 4", "c 7"]
Output: ["b", "c"]

Constraints

  • 0 <= len(lines) <= 10**4
  • Every line has at least two fields; the name contains no spaces.
  • Readings and totals are integers between -10**6 and 10**6.

Goals

  • Split a line into its first field, its last field and everything in between with starred unpacking
  • Handle records whose middle part is empty
  • Convert the collected fields to numbers in one expression
Starting Python…