DanaFosmer.com
  • Test Engineering
  • Electrical Engineering
  • Projects and Code
  • Archives
  • About

Python Problem 3

12/29/2022

0 Comments

 

Problem Description

This problem called the LCD Display. The idea is to take a number as an input and output the same number written with vertical and horizontal dashes. So, input looks like this, where the first number is the segment size and the second number is the number to convert:
Picture
The output looks like this, in dashes with each segment being made of two dashes (the first number of the input)
Picture

Solution

This seemed kind of silly but still a couple tricky things to solve.
I couldn't think of a way to just create each digit and stitch the strings together (I don't think there is a way to do that). So, I broke it apart into five parts that could be written as normal strings and added together.
Picture
Then, there's a method for each portion that will decide what blanks or dashes are needed for the current number. For example, here is the build_top() method. If you need a 1 or 4, it's blank, otherwise it's a line.
def build_top(self):
        for i in self.number_to_convert:
            if i == "1" or i == "4":
                self.top_string += " "*(self.segment_size+2)+ " "
            else:
                self.top_string += " " + "-"*self.segment_size + "  "
        return self.top_string
The main function does the following:
  1. Read in the number
  2. Call build_top(), build_upper(), build_middle(), build_lower(), build_bottom()
  3. Concatenate all the strings
  4. Write to a file
Note there is a loop for the upper and lower part as it is made of multiple strings. Here's the code:
def execute_main():
    input_file = fr.FileReader("inputFile.txt")
    raw_input = input_file.file_read_all_str_list()

    #calculate position strings
    exe_problem2 = Problem3(raw_input[0], raw_input[2:])
    top = exe_problem2.build_top()
    
    for i in range(int(raw_input[0])):
        upper = exe_problem2.build_upper()
        upper += "\n"+ upper
                
    middle = exe_problem2.build_middle()

    for i in range(int(raw_input[0])):
        lower = exe_problem2.build_lower()
        lower += "\n" + lower

    bottom = exe_problem2.build_bottom()

    #combine position strings
    output_string = top + "\n" + upper + "\n" + middle + "\n" + lower +"\n" + bottom

    output_file = fw.FileWriter("PyProblem3-Output.txt")
    output_file.file_write_all_str(output_string)
See the full code on my Github
0 Comments



Leave a Reply.

    Archives

    December 2022
    March 2016
    March 2014
    July 2011

    Categories

    All
    Python

  • Test Engineering
  • Electrical Engineering
  • Projects and Code
  • Archives
  • About