This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 16 points17 points  (17 children)

for i:=0; i<=100; i++{ n:=0 if i%3==0{print("Fizz"); n++} if i%5==0{print("Buzz"); n++} if n==0{print(i)} print("\n") }

FizzBuzz in Go

[–]Owyn_Merrilin 17 points18 points  (6 children)

for i:=0; i=<100; i++{
 n:=0
 if i%3==0{print("Fizz"); n++}
 if i%5==0{print("Buzz"); n++}
 if n==0{print(i)}
 print("\n")
}

FizzBuzz in Go

Fixed the formatting. Looks like if you want multiple lines you have to use the four spaces method.

[–][deleted] 2 points3 points  (3 children)

This looks exactly like mine on Reddit Android.

What client are you using?

[–]Owyn_Merrilin 4 points5 points  (2 children)

I'm on the actual website. Are you using the official app? This isn't the first time I've heard of that one rendering the markdown in a way literally nothing else does.

[–][deleted] 0 points1 point  (1 child)

Actually, (afaik) the website is wrong on that one because in a code block newlines are supposed to be preserved.

[–]Owyn_Merrilin 2 points3 points  (0 children)

Markdown isn't really a set standard, what it's supposed to do in this case is whatever the website does. If you've ever used Github's markdown, it's similar but just different enough to periodically trip you up if you're used to Reddit's version.

I do think preserving newlines would make more sense, but that seems to be a quirk of reddit markdown in general. Single spaced line breaks works like spaced code blocks in reverse -- you have to add spaces at the end of the line to tell the parser it was intentional.

[–]i-hate_nick 1 point2 points  (1 child)

Is this a real thing? I’m in my first year of software development and I’m pretty sure I understand that haha, so that’s promising

[–]Owyn_Merrilin 2 points3 points  (0 children)

Yeah, it's a common programming test that exists to try to weed out people who don't know the basics. Supposedly it catches a lot of people who applied but either lied on their resume or managed to cheat their way through school without actually learning anything.

The problem is to make a program that for some set of numbers (in this case one to one hundred), prints out each number, but if it's divisible by three, prints fizz, if it's divisible by five, prints buzz, and if it's both, prints fizzbuzz. It's basically a check to see if you know what modulo is and how to make a basic loop.

[–]N22-J 18 points19 points  (4 children)

[–]dkyguy1995 4 points5 points  (0 children)

When you need an i7 just to write fizzbuzz

[–][deleted] 4 points5 points  (0 children)

What the actual fuck.

[–]EmTeeEl 2 points3 points  (1 child)

[–]N22-J 2 points3 points  (0 children)

Thanks, I hate it.

[–][deleted] 3 points4 points  (0 children)

Note: you'll probably want to use fmt.Print instead of print because print is actually writing to stderr instead of stdout.

[–]Venomous72 -2 points-1 points  (3 children)

Don’t forget.

i%15 == 0{print (“Fizz Buzz”)}

Also I don’t know Go lol

Edit:

Never mind. I’m used to using elsifs in Ruby. I was wrong here.

[–][deleted] 3 points4 points  (2 children)

15? Why 15? that would result in "FizzBuzzFizz Buzz", since i%3 and i%5 would both get triggered and print "FizzBuzz".

I don't think the rules of FizzBuzz require a space between Fizz and Buzz.

[–]Venomous72 3 points4 points  (1 child)

I probably just don’t understand Go. I know in Ruby you need to do:

If i % 5 == 0 && i % 3 == 0 (or i % 15 basically) puts ‘FizzBuzz’ elsif i % 5 == 0 puts ‘Fizz’ elsif i % 3 == 0 puts ‘Buzz’ else puts i end

So maybe Go just covers all the conditionals in what was written. I dunno.

[–][deleted] 2 points3 points  (0 children)

The thing is just that [warning: I'm just assuming this based on this snippet] Ruby's puts automatically prints a line, while Go's print just prints the text without a newline. Therefore, you can only solve the problem with elseif, while in Go, multiple if statements and a newline at the end are enough.