With this problem we start a collection of tasks to be solved in Scheme
. As with tasks in
Asm4004
and BF your solution
is executed and tested on server, which gives us new opportunities for challenges etc.
However unlike these two special languages, Scheme
is more popular and practical.
If you know Scheme
already, you hopefully like it. If not - try it! It emphasizes popular Functional
style in programming! And anyway modern programmer should be quick in grasping languages. (I myself
was hired a couple of times to projects with rare languages with which I only got acquainted weekend before interview).
As a quick "rush-into" please watch the video above - or READ THIS ARTICLE to learn about Scheme version we use on server (TinyScheme-R7) - and about resources for learning Scheme.
Thanks to Kostis K. for the story of multiplication persistence - though we are going to have more difficult problem on this topic in future!
The program should define a function named multiplex
which, for given number, calculates product of all its
digits and continues until reduced to single digit. For example
1937 => 1*9*3*7 = 189
189 => 1*8*9 = 72
72 => 7*2 = 14
14 => 1*4 = 4
More formally: your code should contain something like:
(define (multiplex n)
...
...)
Which, when called, prints number n
and then all consecutive digit products, including the last single-digit
result, separated with spaces, e.g.
1937 189 72 14 4
Your code could define other functions (e.g. for calculating product of digits), but it shouldn't either print anything on itself, or read from console, or enter endless loop. E.g. if you have few lines below the function definition(s) to test this function - don't submit these test lines (or comment them with semicolon) e.g. you may submit something like this:
(define (digit-product n) ...)
(define (multiplex n) ...)
; (multiplex 1937)
; (newline)
Note the last two lines (which obviously are for testing) are commented - otherwise checker will be confused by extra garbage in the output.
Your function shouldn't "read" input - it should accept single parameter (number).
Then it should print answer in a single line. Here are few examples:
(multiplex 7)
should print just 7
(multiplex 77)
should print exactly 77 49 36 18 8
(multiplex 1937)
should print 1937 189 72 14 4
This task is checked by the code you submitted, so you needn't provide usual "answer" - and you are never supposed to know on what input data your program is tested. Instead checker would give some textual verdict if anything went wrong.
Note that (display ...)
and (write ...)
functions (which you may probably use) behave differently.