Month / February, 2015

Building Things is Really Constructive

Last time we discussed the current state of my game, I expounded on the ways I was handling player health and the game’s current state. I laid out how I was move code from all over the place into two functions for hurting and healing the player. And also I discussed how I was handling letting the game know that it will be starting or ending.

This time I want to discuss how I go about building the various levels of the game.

Continue Reading

Calculating Expected Offspring

Onward and upward my loyal followers! Today we explore the exciting, invigorating, and joyous world of expected values! An expected value is the average case of a random variable over time. As in, if we were to repeat some experiment over and over and record all the values for a specific variable, what is the average of those values.

Continue Reading

UITabBarController Is Different

This should come as no surprise, but apparently UITabBarController has a different behavior than most view controllers. The life cycle may overall be the “same” between it and other view controllers, but the order it executes is not.

That is, when you create a subclass of UITabBarController and provide your own custom initializer, you will notice that the viewDidLoad method is called in an unexpected way. That is, as soon as you call [super init] (or other initializer on UITabBarController), it will call loadView during that initialization which will then lead to your viewDidLoad being called. This is likely not what you would expect because most (all?) other UIViewController subclasses do not instantiate their view during the initialization process. As such, if you provided a custom initializer and expected to do some setup before the view is loaded, and then once the view is loaded add your contained view controllers, this will break your logic.

The solution is to actually move your setup code out of the standard viewDidLoad method and into a special setup method that is called at the end of your custom initializer. This strikes me as a “code smell” that Apple should have never let through. Likely though, this is because the UITabBarController needs to add a UITabBar to the UIViewController’s view which requires that the view exist. Which is what fires loadView.

Ugh.

Counting Point Mutations

Welcome back after the standard 5 day wait between blog posts. Oh, you hadn’t noticed that I had been spacing these out evenly? Sorry to have ruined that for you. And just the same I’m about to educate you on how to calculate the Hamming Distance between DNA strings. So join me after the break for this wonderful adventure!

Continue Reading

Getting gamey up in here

I hope all my readers have enjoyed the posts on Project Rosalind solutions. They have been a blast to write and solve. But, it has definitely come at the cost of writing about the game. Time to fix that for a bit.

In the time since I have last written a bunch has happened in the game. In fact, I would say the game has started to come into its own and really gotten me excited. As such, this will probably be a multipart post to cover all the stuff.

This time I want to talk about handling player health and game over. For sure this is stuff is likely going to change over time, but here is the state they are in.

Continue Reading

Translating RNA into Protein

Okay, so I kind of lied that the last problem was going to be really simple. But this time, I swear it is true! This time all we need is a new data structure that is provided by Haskell and we are done. Promise.

Problem Statement

In this problem we will be given an RNA string that we need to translate into a Protein string. RNA can be broken up into chunks of 3 bases, and then those chunks of 3 bases each define a specific protein, or a stop code. This mapping can be found on Wikipedia.

The solution

So to solve this, there are three steps

  1. Setup the RNA to Protein look up table
  2. Break the RNA string into chunks of 3
  3. Look up each chunk of 3 RNA bases in the RNA to Protein look up table.

The look up table

In programming the concept of a look up table is extremely common. There are a few commonly used names for these data structures: hash, dictionary, look up. No matter what though, the basic concept is that given a specific value (called a key) it will relate to one and only one other value (called a value). Now, the value could of course be a list of things, but that list is still just one value.

In Haskell this kind of functionality could be hand written, or you can use the standard Data.Map module, and it is called Map. This data type takes a list of 2-tuples where the first value is the key and the second is the value. It is possible to create a Map from a list using the fromMap function in the module. Knowing that, we can copy the RNA codon table and create the necessary Map pretty simply.

import qualified Data.Map as M

rnaCodonTable = M.fromList [([U,U,U], "F"),([C,U,U], "L"),([A,U,U], "I"),([G,U,U], "V"),
                            ([U,U,C], "F"),([C,U,C], "L"),([A,U,C], "I"),([G,U,C], "V"),
                            ([U,U,A], "L"),([C,U,A], "L"),([A,U,A], "I"),([G,U,A], "V"),
                            ([U,U,G], "L"),([C,U,G], "L"),([A,U,G], "M"),([G,U,G], "V"),
                            ([U,C,U], "S"),([C,C,U], "P"),([A,C,U], "T"),([G,C,U], "A"),
                            ([U,C,C], "S"),([C,C,C], "P"),([A,C,C], "T"),([G,C,C], "A"),
                            ([U,C,A], "S"),([C,C,A], "P"),([A,C,A], "T"),([G,C,A], "A"),
                            ([U,C,G], "S"),([C,C,G], "P"),([A,C,G], "T"),([G,C,G], "A"),
                            ([U,A,U], "Y"),([C,A,U], "H"),([A,A,U], "N"),([G,A,U], "D"),
                            ([U,A,C], "Y"),([C,A,C], "H"),([A,A,C], "N"),([G,A,C], "D"),
                            ([U,A,A], "Stop"),([C,A,A], "Q"),([A,A,A], "K"),([G,A,A], "E"),
                            ([U,A,G], "Stop"),([C,A,G], "Q"),([A,A,G], "K"),([G,A,G], "E"),
                            ([U,G,U], "C"),([C,G,U], "R"),([A,G,U], "S"),([G,G,U], "G"),
                            ([U,G,C], "C"),([C,G,C], "R"),([A,G,C], "S"),([G,G,C], "G"),
                            ([U,G,A], "Stop"),([C,G,A], "R"),([A,G,A], "R"),([G,G,A], "G"),
                            ([U,G,G], "W"),([C,G,G], "R"),([A,G,G], "R"),([G,G,G], "G")]

Break the chain

Again, this is a pretty straightforward part to solve. Haskell has another really useful module called Data.List.Split that provides some great convenience functions for breaking lists up in numerous ways. This time we will use the chunksOf function to break the RNA string into groups of three.

import qualified Data.List.Split as S

stringToRNAChunks :: String -> [[RNANucleotide]]
stringToRNAChunks = S.chunksOf 3 . map charToRNANucleotide

Look up all the things!

Next, we need to look up each of the RNA chunks in the codon table. We do have to consider a few situations though. We need to consider that there may be no value for the chunk we are looking up. To handle that Haskell’s lookup function returns a Maybe value. Maybe has two possible values: Nothing or Just x where x is some specific value. Therefore, we can look up a chunk and if the result is Nothing we can ignore the result.

import qualified Data.Maybe as Mb

lookupProteins :: [[RNANucleotide]] -> [String]
lookupProteins = foldr (\rs acc -> if Mb.isJust rs then (Mb.fromJust rs):acc else acc) [] . map (\c -> M.lookup c rnaCodonTable)

Putting it together

Finally, we need to put this all together. But one last step — we need to strip out any Start and Stop, then we can just concatenate all of the Protein letters together.


See, wasn’t that easier?