Python: Problem Set 7

Problem 7 simulates the growth of a virus in a subject. Due to odds of multiplying or self healing, the virus population varies greatly up and down. In this simulation, the graph shows that this virus does not exceed 15% of host cells. This is due to the inverse relationship between the number of virus cells and their likelihood to reproduce: self.maxBirthProb * (1 – popDensity). This would not be so bad for the virus if it were not for the probability of virus cells to cleanse themselves (5%).

virus simulation

Problem 7b computationally shows the odds of rolling a Yahtzee, five six-sided die landing on the same number. Mathematically we know this is 6/7776, the number of desired outcomes over the number of possible outcomes. By simulating a number of trials (20) with increasing numbers of dice rolls, we can see that as our rolls exceed 100K and grow to 2^20, the mean of the resulting ratios becomes very close to the number we expect, 0.00077.


Python: Problem Set 6

Problem set 6 involves modeling robots that clean rooms. We have room object with tiles to be cleaned, robots which “move” and mark tiles as cleaned. All robots begin at random positions in the room, pointed in a random direction. Standard robots move in the same direction until reaching a wall, when they pick a new direction. Random-walking-robots pick a new direction on each step. This looks like spinning when visualized with Tkinter. The visualization module is provided as a helper file with the lesson, not made by me. This came in handy for figuring out that I was not flooring() the robots position either on passing or getting the variable for cleaning. It was clear once I could see that robots would clean if they were moving straight along one axis, but not if they were in a decimal position “between” the tiles. They could clean the grout, but not the tile :)

The second part to the problem is to measure how long it takes both types of robots to clean a room. Graphing is done in Pylab. The random walking robot takes more than twice the time to clean 80% of the tiles in a square room.

Python: Problem Set 5

Problem Set 5 makes use of Google’s RSS feed to search for keywords and return stories matching those words. The exercise I believe is the first to begin using classes and inheritance. In order to avoid code duplication, a generic WordTrigger is created to look for for a “word” inside a “text.” Then, we create subclasses of triggers to look at the title, summary, or subject fields. Later on, it creates AND/OR functions to make more useful searches.

My functions passed the, but when I try using provided main_thread’s implementation that pulls RSS stories from google (stories = process(“”)), only the title trigger seemed to work. I suspect the provided may not be up to date with the RSS feed. On the other hand, looks like it is mapping the description element to “summary”, so the error is not obvious to me.

Python: Problem Set 4

Problem set 4 is to implement a Caesar Cipher including encryption and decrpytion of a text string. For example, encrypting a text by 1 and decrypting it:

>>> import ps4
Loading word list from file…
55909 words loaded.
>>> s = ps4.apply_shift(“The files are *in* the computer”, 1)
>>> s
>>> ps4.find_best_shift(ps4.wordlist, s)
>>> ps4.apply_coder(s, ps4.build_decoder(1))
The files are *in* the computer

On a multiple-shift decryption, my control flow only allows the function to decrypt text from left to right. That way we keep track of our position in the text as we shift our way across it, while also counting the number of shifts to find each valid word according to the dictionary. Until I implemented this, my valid_word checker might find a word farther down the string, move the ‘cursor’ and get completely derailed. This is obvious around line 475.

Another ‘ah-ha’ moment was trying to keep track of the counter through a recursive function. To do so, I turned the ‘start’ variable in the same function as above into an array instead of an int. start[0] functions as the cursor, start[1] as the number of shifts, and further tuples track where shifts started and how many occurred. I am not entirely sure this is allowed per the instructions, but the means are currently justifying the end result.


Python: Problem Set 3

This problem set involves a word game somewhat like Scrabble. In Part A, the player is dealt a hand of semi-random letters and has to make as many valid words as she can. Some letters are weighted higher or lower when it comes to scoring. Some aspects of the game code are provided to begin with. The rest of the functions are left up to us to complete, then put together into a looping game where you can choose to start a New Game, Replay, or Exit. In Part B, a computer player is added, which will pick one or more valid words until no more can be made with the remaining letters.

Key take-aways from lecture:

  • floats are dangerous, e.g. 0.1 can only be approximated in binary
  • print(num) may show ‘0.1’ but use repr(num) to see what is actually being stored
  • When debugging, ask, “How could it have done what it did?”
  • It’s a better starting point than, “Why didn’t it do what I want?”
  • Use print statements in a manner similar to bisection search. Check here, then there.
  • Try to test against small input values and make tests repeatable

Part A

  • Scoring Words
  • Dealing with Hands
  • Word Validation
  • Playing a Hand
  • Playing a Game

Part B

  • Computer Chooses (best scoring first word and so on)
  • Computer’s Turn to Play
  • You or the Computer

Python: Problem Set 2

Problem set 2 involves two different problems. The first uses successive approximation to find the root of a function. If, like me, you need a refresher on the derivative of a polynomial, try The problem is actually broken down into distinct functions to help us get to the final answer.

The second problem is a bit more fun for those that prefer letters over number crunching. Implement your own game of hangman!

Successive Approximation


Python: Problem Set 1

This week’s problem set involved calculating credit card payments and interest rates over time. The objective comes with three sub-tasks: calculating payments over a fixed period, finding the amount required to pay debt in 12 months through exhaustive enumeration, and improving this second task using binary search.

concepts covered in lecture:

  • Exhaustive enumeration often the right solution
  • Break exits only the current loop
  • If the answer falls outside the range tested, answer won’t be found
  • E.g. range(0, 100) when answer is negative

Paying the Minimum

Paying off debt in 12 months

(brute force method)

(binary search method)

If you made it this far, thank you for reading. I am aware I should be using decimal types, no floats, for much of the calculation here. I spent too much time trying to get decimal to work and failing, so I used float formatting and moved on.

Critiques and questions are appreciated.