FizzBuzz Station 4

Posted by Oliver on February 28, 2008

Uh oh! I overthought fizzbuzz:


The following Ruby snippets aren’t quite the same as the automaton above (this Haskell version is actually closest to that), but here’s a couple of Regexp solutions in a style that I haven’t seen before. They have a kind of Turing-tape flavor to them.

puts (1..100).map { |n| '1'*n+":#{n}\n" }.join.
  gsub(/^(1{5})*:/,'\0Buzz').gsub(/^(1{3})*:/,'Fizz').gsub(/.*:|(z)\d+/,'\1')
puts (1..100).map { |n| 'x'*n+"#{n}\n" }.join.
  gsub(/^(xxx)*\d/,'Fizz').gsub(/[05]$/,'Buzz').gsub(/^x*|\d*(.+?)\d*/,'\1')

I’m not much of a golfer, so please let me know in the comments if you tighten them up any.

Trackbacks

Trackbacks are closed.

Comments

Comments are closed.

  1. Anonymous Tue, 04 Mar 2008 11:47:41 PST

    Brainfuck: http://koizuka.nowa.jp/entry/146a3d3b33

    >++++++++++[<++++++++++>-]>>>+++>+++++>+<<<<<<
    [
    >>>+
    >- [<<+>>-]<<<+>[<]>>>+<<<[>>>+++>>[]<<<<< +++++++[>++++++++++<-]>.<+++++++[>+++++<-]>.<++++[>++++<-]>+..[-]<]>>
    >>- [<<<+>>>-]<<<<+>[<]>>>>+<<<<[>>>>+++++>[]<<<<< +++++++++++[>++++++<-]>.<+++++++[>+++++++<-]>++.+++++..[-]<]>>
    >>>
    [-
    <<<[<+>>>>+<<<-]<[>+<-]>>>>
    [
    >++++++++++<
    [>-[>+>+<<-]>[<+>-] >[<]>< [>><<<++++++++++>-]<<-]
    >———- >>>[<<<<+>>>>-] <<<<
    >>>>>+> >>] <[>+< <[>+<-] <<<<< [>>>>>+<<<<<+] <
    ]
    >>>>>
    [ <++++++[>>++++++++<<-]>> . ] >[<+> >[<+>-] <<<-]
    <<<<<
    ]+
    <<<<<
    +++++++++++++.—.[-]
    <-]

  2. Vidar Hokstad Sat, 15 Mar 2008 14:36:36 PDT

    I’m a sucker for nice diagrams – did you do it manually or is it generated with a tool?

  3. oliver Sun, 16 Mar 2008 17:22:25 PDT

    Thanks, Vidar! I make most of my illustrations, including this one, with Omnigraffle — in fact, it’s one of the reasons that I switched back to the Mac. Nothing automated, just lots of tweaking — which I enjoyed, because I’m trying to learn the rudiments of graphic design, and I usually learn things better if I do them by hand before trying to automate them.

  4. Vidar Hokstad Mon, 17 Mar 2008 18:10:40 PDT

    … it’s actually the only diagram editor I actually like using. It seemed like that type of visualization of a state machine would be pretty interesting to automate, though, so I was curious if anyone had done it :)