27 ms·
I do not know Ruby, but I wanted to see what happened when I clicked next. Without writing any code I clicked "Grade it!" This provided me with the unit test ou
by absconditus 15y ago
I do not know Ruby, but I wanted to see what happened when I clicked next. Without writing any code I clicked "Grade it!" This provided me with the unit test output. I looked at it for a moment and then guessed at a solution that might work using copy and paste and based on the comment in the code. Here it is:
puts ["1", "2", "fizz", "4", "buzz", "fizz", "7", "8", "fizz", "buzz", "11", "fizz", "13", "14", "fizzbuzz", "16", "17", "fizz", "19", "buzz", "fizz", "22", "23", "fizz", "buzz", "26", "fizz", "28", "29", "fizzbuzz", "31", "32", "fizz", "34", "buzz", "fizz", "37", "38", "fizz", "buzz", "41", "fizz", "43", "44", "fizzbuzz", "46", "47", "fizz", "49", "buzz", "fizz", "52", "53", "fizz", "buzz", "56", "fizz", "58", "59", "fizzbuzz", "61", "62", "fizz", "64", "buzz", "fizz", "67", "68", "fizz", "buzz", "71", "fizz", "73", "74", "fizzbuzz", "76", "77", "fizz", "79", "buzz", "fizz", "82", "83", "fizz", "buzz", "86", "fizz", "88", "89", "fizzbuzz", "91", "92", "fizz", "94", "buzz", "fizz", "97", "98", "fizz", "buzz"]
I passed.
- rwolf 15y agoMy favorite answer. You can even leave out the "" around the numbers. I'm surprised the lower case fizz and buzz passed--the directions definitely say "Fizz" and "Buzz" (I got a 503, so I haven't been able to get to the unit tests).
- cheald 15y agoOr even more tersely: puts %w(1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17 fizz 19 buzz fizz 22 23 fizz buzz 26 fizz 28 29 fizzbuzz 31 32 fizz 34 buzz fizz 37 38 fizz buzz 41 fizz 43 44 fizzbuzz 46 47 fizz 49 buzz fizz 52 53 fizz buzz 56 fizz 58 59 fizzbuzz 61 62 fizz 64 buzz fizz 67 68 fizz buzz 71 fizz 73 74 fizzbuzz 76 77 fizz 79 buzz fizz 82 83 fizz buzz 86 fizz 88 89 fizzbuzz 91 92 fizz 94 buzz fizz 97 98 fizz buzz)
- tzs 15y agoI too don't know Ruby and passed. My approach: 1. Google to see if Ruby has a for loop. It does. "for i in 1..100" looks like a good start. 2. Google for a switch or case statement. Don't like what I see. Google for an if/else. Looks good. Guess "if i%15 == 0 ... elsif ... end". 3. Google to make sure the mod operator is indeed %. Done.
- tptacek 15y agoThat "for i in 1..100" loop would get you dinged at lots of Ruby shops, since it's not idiomatic. "100.times { }" is the idiom.
- ryanbraganza 15y ago100.times is also wrong (unless you i+1 in your block all the time) Cleaner, I think is "1.upto(100){ ... }"
- tzs 15y agoIt's actually kind of fun to write a program in a language you don't know, just learning by Googling as you go. I did that when I wanted to write some add-ons for Warhammer Online. Someday, I'm going to have to properly learn Lua and its idioms and conventions and then go back and look at my Warhammer stuff and see just how horrible I did. If anyone wants to laugh at my code, here it is: https://github.com/tzs/WAR-Whom https://github.com/tzs/WAR-Whom I also did the same thing with JavaScript. I wanted a calculator for Talisman building in Warhammer Online. At first I was going to do it as an iPhone application. Then I decided a web page would make more sense, and so was going to write it as a Perl CGI application. Then I decided it would make more sense to do it in JavaScript so that my server wasn't doing the work. That code is here: https://github.com/tzs/WAR-TalismanCalculator https://github.com/tzs/WAR-TalismanCalculator These were fun experiments, but overall I think I prefer to learn the way I learned C. I got K&R, read it, then went to the computer center (this was before personal computers were common), sat down at a terminal, and started hacking out some simple programs, with my K&R at my side. A couple sessions of that cleared up the few things I had misunderstood. Unfortunately, I've never found a book as good as K&R for any other language I've wanted to learn. I remember reading an article about "Digital Natives" and "Digital Immigrants". The thesis of the article was that people who grow up in the digital world have a fundamentally different approach to things like learning than do those of us who grew up pre-digital. They are the natives--they grew up with computers, and multitasking, and access to vast amounts of online knowledge. They don't want to learn things by sitting down with a book and reading it through chapter by chapter, and then go out and apply that knowledge. They expect that for any task they wish to accomplish, they can learn what they need when they need it by going to Wikipedia, or Googling, or getting help on IRC, and so on. When I set out to write my Warhammer Online add-on and the talisman calculator, I was consciously trying to act more like a digital native than a digital immigrant, to see how well it worked. It worked out better than I expected, but not enough to convince me that the digital native approach is better than the digital immigrant approach.
- joelly 15y agoIf the question said 'generate', you passed; if it said 'calculate', you failed.