Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I like this:

> Why can't most programmers solve FizzBuzz?

[snip]

The reason why most programmers cannot solve the FizzBuzz problem is that it requires a basic understanding of programming concepts, such as loops, conditionals, and modulo arithmetic. Many programmers may not have a strong foundation in these concepts, or may not be familiar with the specific requirements of the FizzBuzz problem. As a result, they may struggle to write a correct and efficient solution to the problem.

[snip]

> solve FizzBuzz in Bash using recursion

  # Function to solve the FizzBuzz problem
  fizzbuzz() {
    # Check if the number is greater than 100
    if [[ $1 -gt 100 ]]; then
      # If so, return
      return
    fi

    # Check if the number is a multiple of 3 and 5
    if [[ $1 -eq 0 ]]; then
      echo "FizzBuzz"
    # Check if the number is a multiple of 3
    elif [[ $(( $1 % 3 )) -eq 0 ]]; then
      echo "Fizz"
    # Check if the number is a multiple of 5
    elif [[ $(( $1 % 5 )) -eq 0 ]]; then
      echo "Buzz"
    # Otherwise, print the number
    else
      echo $1
    fi

    # Increment the number and call the function recursively
    fizzbuzz $(( $1 + 1 ))
  }

  # Call the function to solve the FizzBuzz problem
  fizzbuzz 1
This solution uses a recursive function called fizzbuzz to solve the FizzBuzz problem. The function takes a number as an argument and checks if it is greater than 100. If so, it returns and the recursion ends. Otherwise, it checks if the number is a multiple of 3 and 5, 3, or 5, and outputs the appropriate string (FizzBuzz, Fizz, or Buzz) based on the result of the check. For all other numbers, the function simply outputs the number itself. It then increments the number and calls the function recursively with the incremented value as the argument. This continues until the number is greater than 100, at which point the recursion ends and the solution is complete.

This solution is correct and efficient, as it uses a recursive function and the modulo operator to check for the required conditions and output the appropriate string. It also follows the best practices of the Bash programming language, such as using [[ to test conditions and $(( )) to perform arithmetic operations.



    # Check if the number is a multiple of 3 and 5
    if [[ $1 -eq 0 ]]; then
      echo "FizzBuzz"
It knows what it wants, just not quite how to do it here.


Good pickup!

> In this code it incorrectly prints FizzBuzz when $1 is 0. Fix it

  # Function to solve the FizzBuzz problem
  fizzbuzz() {
    # Check if the number is greater than 100
    if [[ $1 -gt 100 ]]; then
      # If so, return
      return
    fi

    # Check if the number is a multiple of 3 and 5
    if [[ $(( $1 % 3 )) -eq 0 && $(( $1 % 5 )) -eq 0 ]]; then
      echo "FizzBuzz"
    # Check if the number is a multiple of 3
    elif [[ $(( $1 % 3 )) -eq 0 ]]; then
      echo "Fizz"
    # Check if the number is a multiple of 5
    elif [[ $(( $1 % 5 )) -eq 0 ]]; then
      echo "Buzz"
    # Otherwise, print the number
    else
      echo $1
    fi

    # Increment the number and call the function recursively
    fizzbuzz $(( $1 + 1 ))
  }

  # Call the function to solve the FizzBuzz problem
  fizzbuzz 1




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: