views:

213

answers:

6

Create a recursive function to print a multidigit number vertically. For example, 2378 should be printed

2
3
7
8

Test your program with numbers of different length.

+4  A: 

You might find that 2378 % 10 == 8 is useful...

Austin Hyde
+3  A: 

in haskell because thats probably not what your assignment is in and it will not be helpful

print_num = print . unlines . map show . vertnums
vertnums n =
    let (d,m) = divMod n 10
    in  case d of
        0 -> [m]
        x -> vertnums x ++ [m]

more code golf

toString c = [c]
print_num = print . unlines . map toString . show

this also works, i was surprised

print_num = print . unlines . map return . show
barkmadley
Thanks for your help but, I am using C language
Diya Roy
+2  A: 

In quick-and-dirty python:

def recursive(x):
    if x > 10:
        print recursive(x/10)
    return x%10
machielo
+3  A: 

Ruby, 5 lines ... Oh wait, this isn't code-golf

#!/usr/bin/ruby
def rp n
  rp n / 10 if n >= 10
  puts n % 10
end
rp ARGV[0].to_i

Clojure, your instructor will love it, trust me

 (defn rp [n]
   (if (>= n 10)
     (rp (quot n 10)))
   (println (rem n 10)))

nroff, a job-interview must-have, oh wait...

.pl 1
.de f
. if \\$1>=10 \{\
.   nr t \\$1/10
.   f \\nt
. \}
. nr t \\$1%10
\\nt
. br
..
.f 2378
DigitalRoss
+1 for code golf
bguiz
+3  A: 

PostScript

    /print_vertically {

        % Init all stuff
        /Palatino-Roman 12 selectfont % Long live Hermann Zapf!
        24 720 moveto % Sounds like a safe place to begin printing, for A4 or letter
        0 14.4 rmoveto % little trick to start 1 line higher than we will print

        doprint_vertically % call the main routine

        showpage % We're done!

       } def

    % The main routine
    /doprint_vertically {
        currentpoint exch pop 14.4 sub 24 exch moveto % move down one line
        dup 10 mod 1 string cvs show % compute remainder mod 10 and print 10
        10 idiv dup 0 ne % compute quotient
        { doprint_vertically } % if quotient != 0, recurse
        { pop } % else, clean up the stack
        ifelse % Never got to devise a satisfactory coding style for PostScript...
    } def

SO's pretty printer is really bad at displaying PostScript code... not really surprising ;-)

Arthur Reutenauer
+1 - Brilliant! LOL!
TrueWill
Glad you enjoyed it :-) Actually I botched it, it prints upside-down, but I don't think many people will notice!
Arthur Reutenauer
Oh cool, brings back memories. For some reason I once decided to write a postscript program that output the traditional reactance-vs-frequency chart, in colors of course...
DigitalRoss
I once wrote a PostScript program to print the Mandelbrot set ;-) ...
Arthur Reutenauer
So many years since my PostScript synapses fired :) I believe you can do this more simply using the 'forall operator after converting the number to a string using 'cvs : forall pushes each character in the string on the top of the stack as a number between 0/255 : subtracting 48 should get you the digit number itself (for ASCII coded numbers); hopefully, you could then call 'cvs on that to get a string suitable for 'show, then do your translation down the page. Take all this with a "big grain of salt" : those synapses may be much more compromised than just "rusty." :) best,
BillW
@BillW: right, I didn't think of forall. But then it wouldn't be recursive!
Arthur Reutenauer
Inspiration! You've created a monster. I added `nroff` to my answer. :-)
DigitalRoss
I really should try to write a version of that program in my old friend's Unlambda (http://www.madore.org/~david/programs/unlambda/), but I've never come close to mastering the language.
Arthur Reutenauer
+3  A: 

Brainfuck:

[>,----------][<][>[++++++++++.[-]++++++++++.----------]]

That's about as recursive as I can get it.

Austin Hyde
Great! I wanted to do it in Brainfuck too, originally!
Arthur Reutenauer
That being said, I'm not able to do anything with your code... I'm using <a href="http://www.muppetlabs.com/~breadbox/software/tiny/bf.asm.txt">Brian Raiter's compiler</a>.
Arthur Reutenauer
No guarantees. I only worked it out on paper first. I (think) it should (theoretically) work...
Austin Hyde
Writing it out on paper would be what I'd expect from a true Brainfuck programmer, too :-) What I observe when compiled is that it exists right away, without waiting for my input.
Arthur Reutenauer
That's odd... I can't really see any reason it would. Maybe adding a `>,` primer to the beginning?
Austin Hyde
Well... now it waits for my input, but it exists right away all the same, without printing anything. Have you tested it? Brian's compiler for Linux is really great. And really, really lightweight ;-)
Arthur Reutenauer
Exits! Sorry for the repeated typo.
Arthur Reutenauer
Nope, haven't tested it, mostly because I don't have a bf compiler. I'll try out Brian's compiler, but I'm using Mac OS X. How would I compile it on mac? or will I have to boot up a VM?
Austin Hyde
I think you have to use Linux, because Brian's compiler produces ELF binaries. It compiles fine on the Mac (that's what I'm using at home, too), but then you can't run it ;-)
Arthur Reutenauer
Ok, I've compiled the compiler, and I've compiled the brainfuck, and ... apparently, newline on linux isn't 10.
Austin Hyde
Hmm... it really should be, at least I didn't have any problem with programs like "++++++++++." (they really displayed a blank line). But keep going! And good luck :-)
Arthur Reutenauer