Sorry, forgot to attach the test suite. wanp...@gmail.com <wanpee...@gmail.com> 於 2020年8月5日 週三 下午11:21寫道:
> I was working on a exercism problem named Raindrops. > > Problem description: > Convert a number to a string, the contents of which depend on the number's > factors. > > If the number has 3 as a factor, output 'Pling'. > If the number has 5 as a factor, output 'Plang'. > If the number has 7 as a factor, output 'Plong'. > If the number does not have 3, 5, or 7 as a factor, just pass the number's > digits straight through. > > I came out with two version. > > ; version 1 > (define (convert n) > (define pling (divides? 3 n)) > (define plang (divides? 5 n)) > (define plong (divides? 7 n)) > (if (or pling plang plong) > (string-append (if pling "Pling" "") > (if plang "Plang" "") > (if plong "Plong" "")) > (number->string n))) > > ; version 2 > (define (convert n) > (define table '((3 . "Pling") (5 . "Plang") (7 . "Plong"))) > (define result (for/list ([(k v) (in-dict table)] #:when (divides? k n)) > v)) > (if (empty? result) (number->string n) > (string-append* result))) > > (require math/number-theory) > > I thought version 1 would be faster, but it turned out to be wrong. > Running with raco test got following timing information. > > version 1 > cpu time: 9 real time: 9 gc time: 9 > version 2 > cpu time: 0 real time: 0 gc time: 0 > > Then I ran both version in DrRacket, both output following result. > cpu time: 0 real time: 0 gc time: 0 > > It's strange, isn't it? > > -- > You received this message because you are subscribed to a topic in the > Google Groups "Racket Users" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/racket-users/LhCM51vnywg/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > racket-users+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/racket-users/46592171-c357-4897-af1a-bea91c838cacn%40googlegroups.com > <https://groups.google.com/d/msgid/racket-users/46592171-c357-4897-af1a-bea91c838cacn%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- - sleepnova 呼叫小黃創辦人 & CEO -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CABa2-7MsirGY2p7ib%3D7a6Nb%3D49%2BW%3DK3uReJvepc_fqs0NbSmQw%40mail.gmail.com.
raindrops-test.rkt
Description: Binary data