(Code) Golfing at the office

Engineering

Learn from our challenges and triumphs as our talented engineering team offers insights for discussion and sharing.

(Code) Golfing at the office

Engineering

Some doctors watch Grey’s Anatomy to relax, and some engineers code for fun when they’re not coding. Well, those engineers are us.

Ben P. decided to kick off an internal game of code golf , which we’re hoping will be the first of many. The objective is to solve a certain problem with the fewest number of characters.

SPOILER ALERT: solutions are at the bottom of this post.

——-
Problem 1: implement FizzBuzz:

Print the integers 1 to 100,
but replace multiples of 3 with “Fizz”
and multiples of 5 with “Buzz”
and multiples of both with “FizzBuzz”.
(each output should be on a new line)

 

Language: Ruby
Par: 69 characters

A couple points for clarification: the output needs to be exactly as specified – no extraneous quotation marks.  You also can’t rely on the user having an internet connection or anything other than ruby 1.8.7 installed (including bash).
——

Ben gave the team a 4-day deadline. The next day, he announced updates: Alex P. was at 34 characters “although it might break the rules.” Gareth was at 63 characters with a normal ruby program.

The winning code was 57 characters long. Below is a 58-character solution we found that does some interesting things, and the internet record is 50 characters. Can you beat that?

Happy hacking!

 

58- character solution:

1.upto(?d){|x|puts (y=’Fizz’*1[x%3]+’Buzz’*1[x%5])[1]?y:x}