ACSL Junior Asterisks Problem

Published October 27, 2024

This article discusses and contains the Python code solution for the American Computer Science League (ACSL) Junior Division 2002-2003 Contest 1 Programming Problem Asterisks.

ACSL Junior 2002-2003 Contest 1 Asterisks problem ACSL Junior 2002-2003 Contest 1 Asterisks problem

Asterisks

The ACSL Junior 2002-2003 Contest 1 Asterisks problem problem was the programming problem in ACSL Junior Division 2002-2003 Contest 1.

First, read the Asterisks problem. Make sure you understand the problem description. Read it many times, if necessary. Then, write your algorithm on paper before coding anything. For your algorithm, you can write either pseudocode or a diagrammatical flowchart.

Programming Solution Logic

How to print asterisks multiple times

From the problem description, we see that the output has to be patterns drawn by repeating asterisks n number of times.

To draw a horizontal line with, say, 5 asterisks, we do this:

print('*' * 5)

Output:

*****

To get an output like this: * * * * *, we just put a space after '* ':

print('* ' * 5)

Output:

* * * * *

Next, let us review the four kinds of input.

Four Types of Input

Input Type 1: S, Height

Input Type 2: H, Height

Input Type 3: L, Height

Input Type 4: B, Length, Width

Sample Input

S, 3
H, 5
L, 5
B, 4, 5

Sample Output

* * * 
* * * 
* * * 

* * * * * 
  * * * * 
    * * * 
      * * 
        * 


* 
* * 
* * * 
* * * * 
* * * * * 

* * * * * 
* * * * * 
* * * * * 
* * * * * 

Algorithm

1. Read input and store the letter in variable line.
2. Check the first character.
3. if first character is 'S' then
    3.1 split line on ', ' and store the integer in height.
    3.2 call figure_s(height) and print the value returned.
4. else if first character is 'S' then
    4.1 split line on ', ' and store the integer in height.
    4.2 call figure_h(height) and print the value returned.
5. else if first character is 'L' then
    5.1 split line on ', ' and store the integer in height.
    5.2 call figure_l(height) and print the value returned.
6. else if first character is 'B' then
    6.1 split line on ', ' and store the two integers in length and width respectively.
    6.2 call figure_b(length, width) and print the value returned.

My Python solution

This is my Python solution:

'''
ACSL Junior 2002-2003 Contest 1 ASTERISKS
Blog Post: https://aruljohn.com/blog/acsl-asterisks/
'''

# Figure S
def figure_s(height):
    figure = ''
    for i in range(height):
        figure += '* ' * height + '\n'
    return figure

# Figure H
def figure_h(height):
    figure = ''
    for i in range(height, 0, -1):
        spaces = height - i
        figure += '  ' * spaces + '* ' * i + '\n'
    return figure

# Figure L
def figure_l(height):
    figure = ''
    for i in range(height + 1):
        figure += '* ' * i + '\n'
    return figure

# Figure B
def figure_b(length, width):
    figure = ''
    for i in range(length):
        figure += '* ' * width + '\n'
    return figure

# Decide which figure and draw it
def draw_figure(line):
    # If the format is S, Height
    if line.startswith('S'):
        _, height = line.split(', ') # ignore the first character; we don't need it
        print(figure_s(int(height)))
    elif line.startswith('H'):
        _, height = line.split(', ') # ignore the first character
        print(figure_h(int(height)))
    elif line.startswith('L'):
        _, height = line.split(', ') # ignore the first character
        print(figure_l(int(height)))
    elif line.startswith('B'):
        _, length, width = line.split(', ') # ignore the first character
        print(figure_b(int(length), int(width)))


'''
SAMPLE INPUT
S, 3
H, 5
L, 5
B, 4, 5

SAMPLE OUTPUT
<< Figure S >>
<< Figure H >>
<< Figure L >>
<< Figure B >>
'''

# Main function
if __name__ == '__main__':
    for i in range(4):
        line = input()
        draw_figure(line)

Output

* * * 
* * * 
* * *

* * * * * 
  * * * * 
    * * * 
      * * 
        * 


* 
* * 
* * * 
* * * * 
* * * * * 

* * * * * 
* * * * * 
* * * * * 
* * * * * 

Decoding the program

For every line of input in the main function, call the draw_figure() function and pass in the line as parameter.

In draw_figure(), depending on the first character, any of the four functions can be called. Each function returns a string with the figure drawn in asterisks.

figure_s(height) returns a multi-line string square of side size height.

figure_h(height) returns a multi-line string triangle of height size height. The triangle is in the shape of quadrant 3, aligned to the right side. Each line contains an increasing number of spaces followed by a decreasing number of '* '.

figure_l(height) returns a multi-line string triangle of height size height. The triangle is in the shape of quadrant 1, aligned to the left side. Each line contains an increasing number of of '* '.

figure_b(length, width) returns a multi-line string rectangle of height size length and width size width.

Useful Python code snippets

There are many code snippets that can be used in competitive programming contests. These are a few, relevant to this problem. We will use the Python shell to run these individual snippets.

Repeat a string n times

We use this code to repeat string * 5 times:

'* ' * 5 

Draw a square or rectangle with character *

To draw a square or rectangle using a single character '*', use a nested for loop or a single for loop with '* ' * n.

To draw a triangle using character *

To draw a triangle using a single character '*', depending on the shape of the triangle, you may have to include spaces as fillers before the non-space characters.

Separate functions for each rule or type

You can get away with a multiple if-elif-else loop in the main function, and putting all the logic in just that main function. But the more manageable way to do it is by writing separate functions for each related task or functionality.

Group all related lines of code in a separate function.

If your code is going to have have more than 15 lines of code in a function, try to break up that function into multiple functions by splitting them based on functionality, and call them appropriately. This is also easier to debug and handle.

Use the Python shell

If you want to debug and see what string or numerical functions work, just run them in the Python shell.

Combine common code and DRY

Wherever possible, see if you can combine common code that repeats itself. DRY stands for don't repeat yourself. You can either use repeating code in a function with appropriate parameters. If you don't have time, or it's too late to make a clean fix, just leave it as it is, as long as it is working properly.

Running the Python code

Run the Python code from your Terminal. You do not need to use any input file because I added the input as a string in the main function.

When the program runs, this will be the output:

$ python asterisks.py 
S, 3
H, 5
L, 5
B, 4, 5

* * * 
* * * 
* * * 

* * * * * 
  * * * * 
    * * * 
      * * 
        * 


* 
* * 
* * * 
* * * * 
* * * * * 

* * * * * 
* * * * * 
* * * * * 
* * * * * 

Conclusion

If there is anything you would like me to add to this article, feel free to comment below or contact me. Thanks.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: October 27, 2024.     This post was originally written on October 26, 2024.