]> gitweb.factorcode.org Git - factor.git/commitdiff
Fix common Project Euler word alpha-num
authorAaron Schaefer <aaron@elasticdog.com>
Sun, 3 Feb 2008 22:18:10 +0000 (17:18 -0500)
committerAaron Schaefer <aaron@elasticdog.com>
Sun, 3 Feb 2008 22:18:10 +0000 (17:18 -0500)
extra/project-euler/022/022.factor
extra/project-euler/common/common.factor

index e9b0b5fbcf78509698bc919a8671ef8482659086..9c8866b736c32b9d66810bdb2e702cf937e4a2a4 100644 (file)
@@ -1,7 +1,6 @@
 ! Copyright (c) 2007 Aaron Schaefer.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: io.files kernel math math.parser namespaces sequences sorting splitting
-    strings system vocabs ascii ;
+USING: ascii io.files kernel math project-euler.common sequences sorting splitting ;
 IN: project-euler.022
 
 ! http://projecteuler.net/index.php?section=problems&id=22
@@ -31,9 +30,6 @@ IN: project-euler.022
     "extra/project-euler/022/names.txt" resource-path
     file-contents [ quotable? ] subset "," split ;
 
-: alpha-value ( str -- n )
-    [ string>digits sum ] keep length 9 * - ;
-
 : name-scores ( seq -- seq )
     dup length [ 1+ swap alpha-value * ] 2map ;
 
@@ -43,9 +39,9 @@ PRIVATE>
     source-022 natural-sort name-scores sum ;
 
 ! [ euler022 ] 100 ave-time
-! 59 ms run / 1 ms GC ave time - 100 trials
+! 123 ms run / 4 ms GC ave time - 100 trials
 
 ! source-022 [ natural-sort name-scores sum ] curry 100 ave-time
-! 45 ms run / 1 ms GC ave time - 100 trials
+! 93 ms run / 2 ms GC ave time - 100 trials
 
 MAIN: euler022
index 50adbe4953d210b4fa1faee93e9d34317bbe400d..99bb3169c468419b4285b7e00bac2bb80dcccbac 100644 (file)
@@ -1,6 +1,6 @@
 USING: arrays combinators.lib kernel math math.functions math.miller-rabin
     math.matrices math.parser math.primes.factors math.ranges namespaces
-    sequences sorting ;
+    sequences sorting unicode.case ;
 IN: project-euler.common
 
 ! A collection of words used by more than one Project Euler solution
@@ -8,6 +8,7 @@ IN: project-euler.common
 
 ! Problems using each public word
 ! -------------------------------
+! alpha-value - #22, #42
 ! cartesian-product - #4, #27, #29, #32, #33
 ! collect-consecutive - #8, #11
 ! log10 - #25, #134
@@ -52,6 +53,9 @@ IN: project-euler.common
 
 PRIVATE>
 
+: alpha-value ( str -- n )
+    >lower [ CHAR: a - 1+ ] sigma ;
+
 : cartesian-product ( seq1 seq2 -- seq1xseq2 )
     swap [ swap [ 2array ] map-with ] map-with concat ;