From 44fa83f03350a57c4a944c58274691f38347c995 Mon Sep 17 00:00:00 2001 From: shironeko Date: Sun, 15 Nov 2020 17:45:12 +0800 Subject: [PATCH] use vectors --- lotte-cli.scm | 12 ++++++------ lotte/main.scm | 51 ++++++++++++++++++++++---------------------------- 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/lotte-cli.scm b/lotte-cli.scm index dfef3b5..5a2d493 100755 --- a/lotte-cli.scm +++ b/lotte-cli.scm @@ -49,12 +49,12 @@ (iter '())) (define (print-winners winners) - (if (not (null? winners)) - (let ((winner (car winners))) - (if (not (null? winner)) - (begin - (format #t "~A score: ~A\n" (car winner) (cdr winner)) - (print-winners (cdr winners))))))) + (define (iter i) + (if (< i (vector-length winners)) + (let ((winner (vector-ref winners i))) + (format #t "~A score: ~A\n" (car winner) (cdr winner)) + (iter (1+ i))))) + (iter 0)) (define opt (cdr (command-line))) (call-with-input-file diff --git a/lotte/main.scm b/lotte/main.scm index fb49eac..34f54e8 100644 --- a/lotte/main.scm +++ b/lotte/main.scm @@ -23,36 +23,29 @@ (define (lotte lotte-name lucky-num num-of-winners users) ; User's score is the smallest value obtained by ; hash(num from 1 to lucky-num + ' ' + lotte-name + ' ' + username) - (define (get-score user) + (define (score-user user) (define (iter num score) (if (zero? num) score - (iter (1- num) (string-min score (hash-strs (number->string num) - lotte-name user))))) - (iter lucky-num max-score)) - (define (iter winners-rev len users-left) - ; Insert new winner into the list sorted from highest to lowest - (define (insert-winner winner winners-rev) - (define (iter winners winners-left) - (if (null? winners-left) - (reverse (cons winner winners)) - (if (stringstring num) lotte-name user) + score)))) + (cons user (iter lucky-num max-score))) + (define (iter winners users-left) + ; Insert user into the winners list sorted from lowest to highest if they + ; score smaller than any of them + (define (insert-user! user) + (define (iter i) + (if (< i (vector-length winners)) + (if (string