Introducing the Coding Challenge

Welcome to the Coding Challenge.

Here I will post a coding challenge every few days that you can solve. There will be easy ones and hard ones, pretty mixed.

How does it work?

  • Try to solve the challenge in whatever language you find fitting
    • You can use a new language you always wanted to learn or show off your golfing skills
  • Post a comment containing your code (in ```code here```) and some info on how the code works or what is remarkable
  • Optional: Create a post with the tag #coding-solution
    • In that post walk other users through the process of solving your challenge. That way they can learn and try it out on the next one
  • Optional: Read and review other peoples code, try to find bugs, give feedback and learn

Why even bother?

  • Training is everything, the more you train your mind to think in code, the better you are prepared
  • You may learn a few new tricks from reading other solutions
  • I will send tips for really good solutions (and use the liquid rewards of this post for it too)
  • You may get recruited if someone likes your code (f.e. I am looking for talents)

Challenge #1 – FizzBuzz

This challenge is a pretty basic one, but the devil lies in the detail.

Implement a fizzbuzz function taking an integer n that abides to these specifications:

  • if n is a multiple of 3, print Fizz
  • if n is a multiple of 5, print Buzz
  • if n is a multiple of 3 and a multiple of 5, print FizzBuzz
  • in all other cases print the number

Also create a snippet or main function that calls fizzbuzz for all values from 0 to 100, ascending.

Remarks:

  • If you use a pure functional language, try returning a string instead of using a print Monad (both are allowed)
  • Bonus points for:
    • handling edge cases
    • good coding practices and modularity
    • useful and meaningful tests
    • beautiful (readable, self-documenting) code

Please keep the #coding-challenge tag clean of solutions.

Sort:  
Loading...

Two solutions with Haskell: http://codepad.org/tIR16emr

Screen Shot 2017-08-17 at 9.21.12 PM.png

Thank you for your submission! I really hoped that I get a Haskell one 🐑

Your first solution is pretty concise, not much to say there. The second one is really cool! Abstracting it as rules is smart and that filter reduces logic duplication.

I think you can make this a bit more readable with making rules a constant and extracting the tester in a helper function that has a good name.

Looking forward to you writing a post on how your process was, implementing the second version of it.

Only comment I have is the question asked for 0 to 100 but the solutions are for 1..100. How does either solution work for 0? Dealing with 0 shouldn't be too difficult, though 0/x is going to be 0 and 0 mod x is also 0. I guess it will/should print FizzBuzz for n=0.

True his solution starts at 1, good catch, here is a tip! But yeah it will work with 0, pretty sure.

Hi @ratticus! You have just received a 0.5 SBD tip from @reggaemuffin!


@tipU quick guide | How to check your pending payouts.

I don't know what you mean by

making rules a constant

It's already a constant there.

Since the filter phrase used once and it's a short one, I'd argue it's easier to read it this way.

I implemented/borrowed the second one because I wanted to have a generic solution that is easy to expand or change with new rules. Now, all you need is to update the rules to get a new FizzBuzz.

Loading...
Loading...

Ooh, this is right up my alley! I'll start out with JavaScript (because I already know it) but I might switch to different languages for future challenges just for the fun of it. I'll be looking forward to these!

Looking forward to your solution :)

My Solution in plain old JS5 :)

function fizzbuzz(value) {
    if (value % 5 == 0 && value % 3 == 0) {
        console.log("FizzBuzz")
    } else if (value % 5 == 0) {
        console.log("Buzz")
    } else if (value % 3 == 0) {
        console.log("Fizz")
    } else {
        console.log(value)
    }
};

function TestNumbers() {
for (var i = 0; i < 101; i++) {
    console.log(fizzbuzz(i));
    }
};

TestNumbers();

Looks good to me ;)

Wow, Really wish I could code lol, I have tried a couple of times to learn but just not making considerable progress. I have found a niche in data analysis I am so passionate about though , I need some coding ability to succeed in this area like R,Python and SQL. Perhaps this would pave a way for me into "core coding" :). Would closely follow this and see what I can learn. Great initiative

I suggest you check out the solutions then and learn from it :) That is what this challenge is about, you can see how it can be done and learn to think that way, till you can do it yourself

Yeah I got my pen and notebook ready. Would try to understand the thought process of each solution.

I resteem your post.I am new on steemit.this is my first post.
https://steemit.com/introduceyourself/@cryptomaker/first-introduceyourself-on-steemit
Please go and upvote my first post.

I suggest you don't hijack other posts and ask for upvotes.

Loading...
Loading...

My solution for python:

def fizzbuzz(n):
  multipleOf3 = n % 3
  multipleOf5 = n % 5
  
  if (multipleOf3 == 0 and multipleOf5 == 0):
    return "FizzBuzz"
  elif (multipleOf3 == 0):
    return "Fizz"
  elif (multipleOf5 == 0):
    return "Buzz"
  else:
    return n

for n in range(101):
  print(fizzbuzz(n))

One suggestion I would have is keep the return type of fizzbuzz consistent. So I would probably do a return str(n) to ensure it is always a string. And in python I would follow snake_case with my variables so multiple_of_3 would be the preferred format.

Your solution looks good and I think it it correct, well done :)

lol, snake case for python. I get it! =D
I kinda wonder about these traditions of code styles and methods. It would be cool to tell a story about the history of traditions like "hello world" and such.

Thank you for the feedback. I'm come from Java. Automatically coded in it's coding style. lols