Menu
CoddyTech

Final: Stock Ledger

Lesson 47 of 48 in Coddy's Kotlin for C Developers course.

challenge icon

Challenge

Easy

Input is zero or more name quantity rows. Names are lowercase ASCII letters, quantities signed integers, and intermediate totals fit Int. Aggregate by name, discard totals at most zero, and return alphabetically sorted name:total lines. Return empty if none remain.

Implement the editable files and return the result from Exercise.solve(input: List<String>). Do not print inside editable methods. The visible locked Main.kt reads all input lines and prints your returned text with one final newline. Text.kt supplies parsing and formatting helpers. All arithmetic results fit Int unless a narrower bound is stated.

Editable files: Exercise.kt, Text.kt. Only Main.kt has main; it is locked and is the last file. All files compile together in the same package.

Port this complete C reference into the Kotlin file API. It implements the same valid-input contract; all totals fit Int.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdint.h>

struct Entry { char *name; int total; };

static void *resize(void *p,size_t bytes) {
    void *result=realloc(p,bytes);
    if(!result) { free(p); exit(1); }
    return result;
}
static char *read_word(void) {
    int ch;
    do { ch=getchar(); } while(ch!=EOF && isspace((unsigned char)ch));
    if(ch==EOF) return NULL;
    size_t size=0,capacity=32;
    char *word=resize(NULL,capacity);
    do {
        if(size+1==capacity) {
            if(capacity>SIZE_MAX/2) exit(1);
            capacity*=2;
            word=resize(word,capacity);
        }
        word[size++]=(char)ch;
        ch=getchar();
    } while(ch!=EOF && !isspace((unsigned char)ch));
    word[size]='\0';
    return word;
}
static int compare_entries(const void *a,const void *b) {
    return strcmp(((const struct Entry *)a)->name,
                  ((const struct Entry *)b)->name);
}
int main(void) {
    struct Entry *entries=NULL;
    size_t size=0,capacity=0;
    char *name;
    while((name=read_word())!=NULL) {
        char *quantity=read_word(),*end;
        if(!quantity) return 1;
        errno=0;
        long parsed=strtol(quantity,&end,10);
        if(errno==ERANGE || end==quantity || *end!='\0'
           || parsed<INT_MIN || parsed>INT_MAX) return 1;
        free(quantity);
        size_t i=0;
        while(i<size && strcmp(entries[i].name,name)!=0) i++;
        if(i==size) {
            if(size==capacity) {
                size_t next=capacity ? capacity*2 : 8;
                if(next<capacity || next>SIZE_MAX/sizeof *entries) return 1;
                entries=resize(entries,next*sizeof *entries);
                capacity=next;
            }
            entries[size++]=(struct Entry){name,0};
        } else {
            free(name);
        }
        entries[i].total+=(int)parsed;
    }
    if(size>1) qsort(entries,size,sizeof *entries,compare_entries);
    int any=0;
    for(size_t i=0;i<size;i++) {
        if(entries[i].total>0) {
            printf("%s:%d\n",entries[i].name,entries[i].total);
            any=1;
        }
        free(entries[i].name);
    }
    if(!any) puts("empty");
    free(entries);
    return 0;
}

Try it yourself

fun main() {
    val input = generateSequence { readlnOrNull() }.toList()
    
    println(Exercise.solve(input))
}

All lessons in Kotlin for C Developers

Practice on your own: Kotlin playground