Skip to main content

FizzBuzz - part 1

 What is FizzBuzz

FizzBuzz is a simple programming exercise that is frequently used as a white board problem during interviews.  I, personally, have never used this problem or had anyone ask it of me during an interview so your mileage  may vary.

I was inspired to write this sequence after getting Poly Bridge 2 as a Christmas gift.  As I was watching some of the truly intricate bridge designs on YouTube, I also ran across a FizzBuzz video (no, I have no idea why Google put them together as a recommendation).  What, I thought to myself, would happen if I took the simple FizzBuzz to the same levels as Tyler and Arglin Kampling.

I plan to discuss a progression of FizzBuzz from what I would expect from an entry level developer to an enterprise architect.  Since I'm playing both sides of this, I don't expect anyone else will make the same choices as I do - what's important is the journey.

The initial solution:

package dev.boundary.waters.FizzBuzz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MySpringBootApplication {

/**
* A canonical FizzBuzz implementation.
* Write a program that prints the numbers from 1 to 100. 
* But for multiples of three print “Fizz” 
         * instead of the number.
* For the multiples of five print “Buzz”. 
* For numbers which are multiples of both three and 
         * five print “FizzBuzz”.
*/
public static void fizzBuzz() {
for(int i = 1; i < 101; i++) {
boolean isFactorThree = ((i % 3) == 0);
boolean isFactorFive = ((i % 5) == 0);
if(isFactorThree && isFactorFive) {
System.out.println("FizzBuzz");
} else if(isFactorThree) {
System.out.println("Fizz");
} else if(isFactorFive) {
System.out.println("Buzz");
} else {
System.out.println(i);
}
}
}
    /**
     * A main method to start this application.
     * The spring boot call is commented out for now.
     */
    public static void main(String[] args) {
        fizzBuzz();
        // SpringApplication.run(MySpringBootApplication.class, args);
    }
}

What's important in the solution:

  • This is a basic program that has:
    1. Input (ranging from 1 to 100)
    2. Processing ( the FizzBuzz rules)
    3. Output
  • The programmer needs understand operators in order to determine the remainder when dividing by 3 and 5.
  • They also need to understand that you have to check the AND condition before the other two options.
  • The input and output are currently hard coded, which in general is a poor choice.

What's less important in the solution:

  • First, because I'm planning on extending this solution, all of the references to SpringBoot and Apache Camel in the project is just so I don't have to re-generate my solution for other parts of the discussion.
  • Did they factor out the boolean modulus operators?  As developers get more experience, they find that naming things for the intent (i.e. is it divisible by 3 or 5) is important to communicate to the reader.  But on a white board where space isn't infinite, putting it in the if statement would be fine.
  • All of the other things that could be put in the solution.  I will discuss what I would put in, but it's going to go past what a white board could hold and I would do hand wavy lists if I were being asked what more to add.+

Comments

Popular posts from this blog

Spring Boot native builds when internet downloads are blocked made simple

 No direct access to the internet If you work at a company that controls their software bill of materials, it's quite common to be blocked from directly downloading from: Maven Central Docker hub GitHub (the public parts) Getting the bits Maven Maven is first, because without it, you won't be able to compile your Spring Boot application, let alone move on to turning it into a native docker image. I will be showing changes need to work with artifactory, but you should be able to adapt it to other mirror solutions.  repositories {   maven {     name = "central"     url = "https://artifactory.example.com/central"     credentials {       username = "${project.ext.properties.artifactory_username}"       password = "${project.ext.properties.artifactory_apikey}"     }   } } With this configuration change, you should be able to download your plugins and dependencies, allowing you to compile and ...

Kotlin Notebook when you're blocked from Maven Central

 TLDR; If you are blocked getting to maven central when first using Kotlin Notebooks because of company firewalls, you can use a tool like Fiddler Tool to redirect to a different network location. Kotlin Notebooks Kotlin Notebooks are a JDK based environment that brings the Python based Jupyter Notebooks  expressiveness to IntelliJ. From the blog post announcing the plugin, it looks like this: At home, the installation of jar files looked like this: I played around with it at home, but I couldn't use it at work.  Many companies, mine included, do not allow software components to be used when downloaded directly from the internet. In my companies case, we use a product called Artifactory, which allows you to mirror the content from Maven Central while still applying policies like CVE scanning, tracking, etc. The way it should work IntelliJ, as one of the leading IDE's, generally supports this quite well.  In fact, there is a whole setting page dedicated to dealing wi...

Active vs. Passive Log4jShell remediation

 Log4jShell  All computer professionals should be aware of the Log4jShell ( CVE-2021-44228 ) and it follow on defects.  There is no shortage of opinions and lessons to be be learned: The difficulty of performing safe interpretation The problems when assumptions are not clearly documented.  I, for one, was completely shocked to find out that a logging system would actually attempt to do variable substitution in an actual message. The difficulty of finding and resolving issues with such a common library that is not provided by an OS package manager. IT'S A LOG4J CHRISTMAS One of my favorite podcasts, Security Now - episode 850 , discussed an analysis by Google regarding the depth of log4j dependencies.  From the show notes : One contributing reason is because Log4j is, more often than not, an indirect dependency. Java libraries are built by writing some code which uses functions from other Java libraries, which are built by writing some code which uses functions f...