SECTION 12 / 28

Count sorted groups and flush the last run

2 min read
On this page 1 headings
This miniature example has three Chennai entries and two Madurai entries. The full dataset counts are shown by the lab.
This miniature example has three Chennai entries and two Madurai entries. The full dataset counts are shown by the lab.
REAL COURSE DATA · SORT, GROUP AND VERIFY

Count equal-city runs

Follow the selected procedure. Change a field, tie rule or weekday and explain why the result changes.

Ready

CURRENT RECORD / VALUE

Ready to begin

No records have been processed. Start with Next step.

What we remember

Processed
0

Predict the next change. Keep track of which list level, field and position convention the procedure uses.

Result = []
CurrentKey = empty; Count = 0
foreach X in RecordsSortedByKey:
    if CurrentKey is not empty AND key(X) != CurrentKey:
        Result = Result ++ [[CurrentKey,Count]]
        Count = 0
    CurrentKey = key(X)
    Count = Count + 1
if CurrentKey is not empty:
    Result = Result ++ [[CurrentKey,Count]]

Sorting gathers equal keys into runs. Keep the current key and a count. On a key change, save the previous group, reset the count and begin the new group. Save the final run after the loop because no following change will trigger it. An empty input produces [].

Actual city counts include Chennai 8, Bengaluru 5, Madurai 5, Trichy 3 and Vellore 3. Singleton cities include Ambur, Nagercoil, Salem and Theni. The procedure works because sorting grouped equal cities; it does not require a geographic ordering.

Change the unit before changing the key

REAL COURSE DATA · SORT, GROUP AND VERIFY

Count sorted bill summaries by customer or shop

Follow the selected procedure. Change a field, tie rule or weekday and explain why the result changes.

Ready

CURRENT RECORD / VALUE

Ready to begin

No records have been processed. Start with Next step.

What we remember

Processed
0

Predict the next change. Keep track of which list level, field and position convention the procedure uses.

The source applies the same idea to item-level [Customer,Shop,Category] triples. Sorting those by customer counts item rows per customer; sorting by shop counts item rows per shop. Our complete 30-record bill-summary lab counts BILLS instead: Srivatsan 4 and Ahmed 4, for example. Bill counts, item-row counts and purchased quantities are different units. Full item counting requires original item rows, not category summaries.

Try it yourself

Why save a group after the loop?

Show explained answer

The last run has no next key change, so otherwise it would never be emitted.

Try it yourself

Does four Srivatsan bill summaries mean four purchased items?

Show explained answer

No. Each bill can contain many rows and quantities. The unit here is bills.

Try it yourself

What result should the grouping procedure return on []?

Show explained answer

[], without inventing a group for an unset key.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑