Codington
0 of 6 problems solved • 0%

Exercise prompt

Parse Stock Movements

Example

A movement is easier to apply once its fields have consistent types.

import csv from io import StringIO row = next(csv.DictReader(StringIO("item,delta\n soap ,+2"))) movement = (row["item"].strip(), int(row["delta"])) print(movement) # ('soap', 2)

Applying this conversion to an entire delivery file also requires deciding which rows have usable fields.

Your task

Implement parse_movements(text). Read the item and delta fields of each CSV row. Strip whitespace from the item, convert the delta to an integer, and return a list of (item, delta) tuples in file order. Skip rows with blank item names or delta strings that cannot be converted to integers. Both columns exist in every row.

parse_movements("item,delta\ntea,-1\ncoffee,?\nmilk,0") # [("tea", -1), ("milk", 0)]