DEV Community

armstrca
armstrca

Posted on

Flying through Intro to Ruby until I hit a wall

I did a significant chunk of the Codecademy Intro to Ruby course before Tech Prep started, and as such much of the Instructure Intro to Ruby course came pretty quickly to me. Until FizzBuzz.

I spent much longer on FizzBuzz than I'd care to admit, but I was very reluctant to ask questions about it because I felt like I wanted to push through and figure out the problems myself so that I understood both the problem and the solution better. Eventually, though, I caved a little bit and went to the Ask GPT for some help, strictly specifying that I wanted help about how to figure out the solution rather than just be given the solution itself.
As I recall (I should've written this closer to when I actually did FizzBuzz), it turned out, happily, that my code was overcomplicated, and that I mostly just needed to rearrange my conditionals so that numbers that qualified for FizzBuzz rather than just Fizz or Buzz would actually be properly generated. I'm very interested to see if there might be an even more simple/elegant solution, but I'm happy with this:

x = 0

while x < 30
    x = x + 1
    if (x % 3 == 0) && (x % 5 == 0)
        pp "FizzBuzz"
    elsif x % 3 == 0
        pp "Fizz"
    elsif x % 5 == 0
        pp "Buzz"
    else
        pp x
    end
end
Enter fullscreen mode Exit fullscreen mode

Similarly, the last exercise of that lesson, "Multiples", also gave me some trouble. It was easy enough to get the right outputs when the inputs were non-zero, but it took a while to figure out what to do with 0. My solution feels a little cheap/brute force to me, though, and I wonder if there's a better way to deal with a 0 input that makes more use of the concepts emphasized in that lesson.

n = [3, 10, 15].sample
if n == 0
    10.times do
        pp n
    end
else
    n.step(n*10, n) do |n|
    pp n
    end
end

Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
quitebearish profile image
Jared Bears • Edited

Hey!

Sometimes a cheap/brute force solution is perfectly acceptable if it gets the job done! From a time complexity angle, your solution comes out just as efficient as anyone else's, although the code itself could perhaps be a bit cleaner. There's never going to be one solution, the most important thing is being able to justify your solution!

Here is my solution to the above two problems:

FizzBuzz

# write your program here
i = 0
30.times do
    i += 1
    output = ""
    if i % 3 == 0
        output.concat("Fizz")
    end

    if i % 5 == 0
        output.concat("Buzz")
    end

    if output == ""
        pp i
    else
        pp output
    end
end
Enter fullscreen mode Exit fullscreen mode

Multiples

n = [3, 10, 15].sample
# write your program here
1.upto(10) do |i|
    pp n * i
end
Enter fullscreen mode Exit fullscreen mode
Collapse
 
samuellubliner profile image
Samuel Lubliner

Hi, nice solutions! FizzBuzz was tricky. It's interesting to see how other people approach the code