Convert a number to a string, the contents of which depend on the number's factors.
A variation on a famous interview question intended to weed out potential candidates. http://jumpstartlab.com
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
;; Load SRFI-64 lightweight testing specification
(use-modules (srfi srfi-64))
;; Suppress log file output. To write logs, comment out the following line:
(module-define! (resolve-module '(srfi srfi-64)) 'test-log-to-file #f)
(add-to-load-path (dirname (current-filename)))
(use-modules (raindrops))
(test-begin "raindrops")
(test-equal "test-1"
"1"
(convert 1))
(test-equal "test-3"
"Pling"
(convert 3))
(test-equal "test-5"
"Plang"
(convert 5))
(test-equal "test-7"
"Plong"
(convert 7))
(test-equal "test-6"
"Pling"
(convert 6))
(test-equal "test-9"
"Pling"
(convert 9))
(test-equal "test-10"
"Plang"
(convert 10))
(test-equal "test-15"
"PlingPlang"
(convert 15))
(test-equal "test-21"
"PlingPlong"
(convert 21))
(test-equal "test-25"
"Plang"
(convert 25))
(test-equal "test-35"
"PlangPlong"
(convert 35))
(test-equal "test-49"
"Plong"
(convert 49))
(test-equal "test-52"
"52"
(convert 52))
(test-equal "test-105"
"PlingPlangPlong"
(convert 105))
(test-equal "test-12121"
"12121"
(convert 12121))
(test-end "raindrops")
(define-module (raindrops)
#:export (convert))
(define (factor number)
(define (*factor divisor number)
(if (>= divisor number)
(list number)
(if (= (remainder number divisor) 0)
(cons divisor (*factor (+ 1 divisor) number ))
(*factor (+ divisor 1) number))))
(*factor 1 number))
(define (convert number)
(let ((outstring "")
(factors (factor number)))
(if (memq 3 factors)
(set! outstring (string-append outstring "Pling")))
(if (memq 5 factors)
(set! outstring (string-append outstring "Plang")))
(if (memq 7 factors)
(set! outstring (string-append outstring "Plong")))
(if (string=? outstring "")
(number->string number)
outstring
)
)
)
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments