Skip to content

Instantly share code, notes, and snippets.

@chenghan
Last active February 5, 2022 20:17

Revisions

  1. chenghan revised this gist Jun 10, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    import sys

    salesTotal = 0
    oldKey = None

    @@ -10,7 +12,7 @@
    thisKey, thisSale = data
    if oldKey and oldKey != thisKey:
    print oldKey, "\t", salesTotal
    oldKey = thisKey;
    oldKey = thisKey
    salesTotal = 0

    oldKey = thisKey
  2. chenghan created this gist Nov 13, 2013.
    20 changes: 20 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    salesTotal = 0
    oldKey = None

    for line in sys.stdin:
    data = line.strip().split("\t")
    if len(data) != 2:
    # Something has gone wrong. Skip this line.
    continue

    thisKey, thisSale = data
    if oldKey and oldKey != thisKey:
    print oldKey, "\t", salesTotal
    oldKey = thisKey;
    salesTotal = 0

    oldKey = thisKey
    salesTotal += float(thisSale)

    if oldKey != None:
    print oldKey, "\t", salesTotal