This is the Interpreter problem. The idea is it's a computer where a list of commands are read from RAM and processed with a set of instructions and registers.
The input file is a list of commands read into RAM that are then processed according to these rules
I have main do a little bit of the work in this problem. It basically iterates through the commands in RAM, the trouble is some of the commands change the instruction pointer to a new location in RAM. I feel like my logic is a little messy with those tasks. There is a class called RamProcessor that handles each command, but the instance also keeps track of the instruction pointer.
I just treated the commands as regular integers, and then every result was processed as modulo 1000 to keep it to three digits, or roll-over.
See full code on my GitHub
This is the graphical editor problem. It's sort of a text version of the Microsoft Paint program. This one doesn't read in a file, it takes user input of commands to change the "image."
The commands to implement are as follows:
This is another 2D array problem, but again in Python it's a list of lists.
I'm not sure how clean I got the object design aspects of this, I have a class called Interface and a class called Drawing that handle the user input processing and the drawing of the image. Trouble is I kind of have some interface aspects in the main function. It seems to work, but probably could be cleaner.
The Interface class is a while loop that will allow you to continue to read commands until the exit command is given. I also implemented a commend to output a text menu of choices, although that wasn't really in the problem. Here is the menu.
Here is the Interface class, interface method. It's a loop that checks if you made a valid selection and returns your selection. (Here's the bad job by me, the X command is actually handled in the main function. So, that's where the design could be better. Maybe I'll still fix it)
The main function runs a loop that calls the interface and returns the command to be processed
It took a long time to write all the methods to process all the lists (maybe that's why I've posted this with some problems - haha) Here is the method to fill a region in the Drawing class. It determines what color you select as input and changes all of that color to a the new color.
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:
The output looks like this, in dashes with each segment being made of two dashes (the first number of the input)
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.
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.
The main function does the following:
This problem is taken from the game Minesweeper on Windows. The input is a static board of mines and blank locations (* and .)
The objective is to create an output file with the same game board only with the blanks replaced with a count of the adjacent count.
I think the intention of this problem was to solve a 2D array problem. Well, there's not really arrays in Python, so I used a list of lists. (Lists are pretty much arrays)
The tricky thing about this problem was thinking of a way to iterate the list and perform the calculation without writing out of bounds. Here's how I did it:
Couple things, I used list comprehensions to initialize the list of empty lists like this:
Then I could just append to the lists like this and didn't need to know the number of columns of the game board to do it.
This is the more Pythonic way to do this (I think). Adding the index variable and enumerate keyword allows using the lists as iterators in the for loops. You could use nested for loops with counters like i and j along with the range() function. That would look more like C++ but is not the thing. (I guess I could have skipped the elif part, but I like how this reads)
Python Problem 1 is called the 3n+1 problem. This is an actual famous math/CS problem (search for Collatz conjecture). The objective here is to read in an input that consists of a pair of numbers. The first number is the starting point in a consecutive sequence of integers extending to the ending number. Each number is processed through the 3n+1 algorithm keeping track of the cycle time for each input to cycle to n=1.
Here is the calculation, if the number n is even, divide by 2. If the number is odd, multiply by 3 and add 1.
The result of that is the new n, continue until you get to n=1. Keep track of how many cycles it takes to get to n=1
For example, an input might be 1 10
n=1 and done right away. Cycle time is 1
n=2, 2/2=1. Cycle time is 2
The desired output is the input plus the longest cycle time.
1 10 20
This problem is pretty straight forward. I created a class for the problem solution, file input and file output. There is a method that solves the actual 3n+1 calculation from input to n=1 and returns the number of cycles.
The main loop just keeps track of the longest cycle count and formats the output string.
I'm still learning the best way to structure things, but I run with a main function like this:
They call this "dunder name, dunder main" and it's a way to either run your code from a terminal or import it like a script. (dunder is a way to say double underscore out loud - I guess I'm typing - oh well)
Here is an image of running the code like a script. Note that I added a print statement for this to work, the output is supposed to just go to a file
Look at that!
Other notes, I'm using VS Code on Linux for development. I also have been setting up a virtual environment for loading packages and keeping my install clean. I only used native Python for this, but it a good practice.
See the full code on my Github
This is the LabVIEW code to create a trace graph display for an Agilent U2001A power sensor. Here is the write up