【每日一题022】Names scores
drracket吧
全部回复
仅看楼主
level 11
Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938
53 = 49714.
What is the total of all the name scores in the file?
------------------------------
name.txt 链接
网盘链接
2014年04月14日 07点04分 1
level 11
;;;racket
;;;drracket
;;;每日一题022
(define names
(open-input-file (build-path (current-directory) "names.txt")))
(define file-end #f)
(define all-name-lst '())
(define (read-names port)
(let loop ()
(cond (file-end all-name-lst)
(else
(let ((temp-name (read-name port)))
;(displayln temp-name)
(set! all-name-lst (cons temp-name all-name-lst))
(loop))))))
(define (read-name port)
(let loop ((r '()))
(let ((temp (read-char port)))
(cond ((eof-object? temp)
(set! file-end #t)
(name-string (reverse r)))
((equal? temp #\,)
(name-string (reverse r)))
((equal? temp #\")
(loop r))
(else
(loop (cons temp r)))))))
(define (name-string lst)
(list->string lst))
(set! all-name-lst (sort (read-names names) string<=?))
;all-name-lst
(define (prog022)
(let loop ((i 1) (lst all-name-lst) (r 0))
(cond ((null? lst) r)
(else
(loop (add1 i)
(cdr lst)
(+ r (* (name-value (car lst)) i)))))))
(define (name-value str)
(let ((sub-value (sub1 (char->integer #\A)))
(lst (string->list str)))
(foldr + 0
(map (lambda (x) (- (char->integer x) sub-value))
lst))))
(prog022)
;871198282
(close-input-port names)
2014年04月26日 09点04分 2
level 11
;;;每日一题022
;;;用排序库函数~~~
(define names
(open-input-file (build-path (current-directory) "names.txt")))
(require data/splay-tree)
(require data/order)
(define file-end #f)
(define all-name-splay
(make-splay-tree (order 'string-order string? string=? string<?)))
(define (read-names port)
(let loop ()
(cond (file-end all-name-splay)
(else
(let* ((temp-name (read-name port))
(value (name-value temp-name)))
;(displayln temp-name)
(splay-tree-set! all-name-splay
temp-name
value)
(loop))))))
(define (read-name port)
(let loop ((r '()))
(let ((temp (read-char port)))
(cond ((eof-object? temp)
(set! file-end #t)
(name-string (reverse r)))
((equal? temp #\,)
(name-string (reverse r)))
((equal? temp #\")
(loop r))
(else
(loop (cons temp r)))))))
(define (name-string lst)
(list->string lst))
(define (name-value str)
(let ((sub-value (sub1 (char->integer #\A)))
(lst (string->list str)))
(foldr + 0
(map (lambda (x) (- (char->integer x) sub-value))
lst))))
(read-names names)
(close-input-port names)
(define (prog022)
(define first-splay (splay-tree-iterate-first all-name-splay))
(let loop ((i 1) (sum 0) (iterate first-splay))
(cond (iterate
(loop (add1 i)
(+ sum
(* i
(splay-tree-iterate-value all-name-splay iterate)))
(splay-tree-iterate-next all-name-splay iterate)))
(else
sum))))
(prog022)
;871198282
2014年04月26日 10点04分 3
1