Avatar of craigmaloney
0
0
Genius
0
0

craigmaloney's solution

to Raindrops in the Scheme Track

Instructions
Test suite
Solution

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.

Examples

  • 28's factors are 1, 2, 4, 7, 14, 28.
    • In raindrop-speak, this would be a simple "Plong".
  • 30's factors are 1, 2, 3, 5, 6, 10, 15, 30.
    • In raindrop-speak, this would be a "PlingPlang".
  • 34 has four factors: 1, 2, 17, and 34.
    • In raindrop-speak, this would be "34".

Source

A variation on a famous interview question intended to weed out potential candidates. http://jumpstartlab.com

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

raindrops-test.scm

;; 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
      )
    )
  )

What can you learn from this solution?

A huge amount can be learnt 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.

  • What compromises have been made?
  • Are there new concepts here that I could read more about to develop my understanding?