A.I, Data and Software Engineering

Fun coding challenge: print the American flag

F

Printing is an important part when programming. Printing the US flag to the system console was a small part of “the shape game” for kids, in which the players will select the correct name for a given shape.

The flag of the united states

Basic Requirements:

You are required to print the American flag to the console using just “*” and “=”. The number of stars varies depending the line (row). A sample output:

Fun facts of students’ solutions

funny look

Most of the students provided solutions which the flag is hardcoded. It means they fixed (data or parameters) in a program in such a way that they cannot be altered without modifying the program. A typical solution (in JAVA) looks like this:

Or partly hard-coded 😀 . Printing the first few rows are tricky as they are mixed “*” and “=”. Therefore, they decided to store the two lines (of the flag) in a variable.

The advanced requirements:

The previous code works perfectly but it is not flexible. Now let make it a bit more challenging by some new requirements:

  1. No more hard-coded
  2. Make the flag scalable with the width and height (as integers)
  3. Make the code as short as possible.

Give yourself 5 mins before going to the following solution.

Print flag Using Java

We use loops to print line by line. First, we print star patterns, then we print “=”, finally we print new line.

The default width and height are 46 and 15 respectively. The star pattern is 6* on even index lines (if the first line has index 0) and 5* on odd index lines. Between 2 stars, we have a space (” “). Therefore, all the stars are bounded in 9×12 rectangle. We print a star only when the sum of the row and column indices are ODD.

An easy to understand version:

A crazy version with only ONE print line.

For scalability, we create a new function, printUSAFlag, that takes 2 input params height and width. The indices i and j are controlled with ratio to the original width and height. Now we try to shorten the code with the same logic by replacing if-statements with the ternary operator. Syntax:

If condition is true, the value of expr1 is returned; if condition is false, the value of expr2 is returned.

Print with Python

With the same logic, we can implement with Python.

Want to play with python a little more with python generator?

Print Flag with Kotlin

If you are new to Kotlin, you can read this quick tutorial.

Now we can test with any width and height.

2 comments

A.I, Data and Software Engineering

PetaMinds focuses on developing the coolest topics in data science, A.I, and programming, and make them so digestible for everyone to learn and create amazing applications in a short time.

Pin It on Pinterest

Newsletters

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

Petaminds will use the information you provide on this form to be in touch with you and to provide updates.
%d bloggers like this: