Menu
CoddyTech

最終課題:株式台帳

Coddyの「C開発者のためのKotlin」コースのレッスン 47/48。

challenge icon

チャレンジ

簡単

入力は、名前と数量の行が0行以上です。名前は小文字のASCII文字、数量は符号付き整数で、中間合計はIntの範囲内に収まります。名前ごとに集計し、合計が0以下のものを除外して、名前のアルファベット順に並べたname:total形式の行を返します。残るものがなければemptyを返します。

編集可能なファイルを実装し、Exercise.solve(input: List<String>)から結果を返してください。編集可能なメソッド内で出力しないでください。表示されているロック済みのMain.ktは、すべての入力行を読み取り、返されたテキストを最後に改行を1つ付けて出力します。Text.ktには、解析と書式設定のヘルパーが用意されています。より狭い上限が指定されていない限り、すべての算術結果はIntの範囲内に収まります。

編集可能なファイル:Exercise.kt、Text.kt。mainがあるのはMain.ktだけです。このファイルはロックされており、最後に配置されています。すべてのファイルは同じパッケージ内で一緒にコンパイルされます。

この完全なCのリファレンス実装を、KotlinのファイルAPIに移植してください。これは同じ有効な入力の契約を実装しており、すべての合計は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;
}

自分で試してみよう

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

C開発者のためのKotlinのすべてのレッスン

自分で練習してみよう: Kotlinプレイグラウンド