Tag Archives: genetic

CodeJam Online Competition for Veterans 2013 B: Baby Height

This was supposed to be posted last night, but I was trying to make it work properly. This problem reproduces a few Python division problems and took me some time to figure it out. Actually I only knew about this when I checked the winner’s solution (also written in Python).

Without the “gotcha”, the problem is quite easy to solve, if you know how to work with feet and inches. As a metric system native, it was my first time. 😀 It’s called Baby Height, and it says, yo!

Every parent wants to know how tall their child will grow.
Dr. Spaceman’s algorithm, which we describe below
Accurately calculates, with errors very low,
Adult height of any child, with just genetics, yo!

Take the mother’s height and add it to the father’s height.
For a girl, subtract five inches; this I will highlight.
For a boy, you add five inches, or it won’t be right.
Then divide by two, and get your target in plain sight.

Dr. Spaceman is convinced that target is precise.
Plus or minus four, in inches, truly will suffice.
When a parent asks the question, looking for advice,
Dr. Spaceman’s answer is this range, and it’s concise.

Also, a minor problem gotcha is found on the Output rules:

Output

For each test case, output one line containing “Case #x: A to B“, where x is the case number (starting from 1), A is the smallest and B is the largest baby height, according to Dr. Spaceman’s algorithm. If the algorithm produces a range whose endpoints fall on fractional inches, your program should shrink the range until both endpoints can be expressed in whole inches.

I’ve overlooked that at first. 😛 Anyway, here’s the final code, validated:

from __future__ import division                            #Get python3 revised division operator

def asInches(strFeetInches):                               #Function converting Foot/inches string to inches
        nums = strFeetInches[:-1].split("\'")              #Split string on ' and remove " marks
        return int(nums[0])*12 + int(nums[1])              #1 foot = 12 inches, convert

for case in range(1,int(raw_input())+1):                   #For all test cases
        vector = []
        boy = False
        father = -1
        mother = -1

        for i in raw_input().split(" "):                   #Split the case values by spaces
                if i == "B":                               #Is it a boy?
                        boy = True
                elif i == "G":                             #Is it a girl?
                        boy = False
                elif father == -1:                         #If father is not yet known, get it
                        father = asInches(i)               #Store his height in inches
                else:                                      #Father is known, so get mother this time
                        mother = asInches(i)               #Store in inches

        height = (mother + father + (5 if boy else -5))/2  #Baby height math
        inc = 4.0                                          #Range default value

        lower = height-inc                                 #Get lower range
        if lower != int(lower):                            #If number is a fraction
                lower += 0.5                               #Reduce range size

        higher = height+inc                                #Get higher range
        if higher != int(higher):                          #If number is a fraction
                higher -= 0.5                              #Reduce range size

        print "Case #%d: %1d'%1d\" to %1d'%1d\"" % (case, lower/12, lower%12, higher/12, higher%12)

This problem represents what CodeJam is about. Not only knowing how to code, but also knowing about programming “gotchas” and avoiding them.

Happy jammin’!

Advertisement

JenPop: genetic populations in your hands

I’ve started developing a new hobby project (yeah, why not finish the other projects first?), that can create entire generations of nodes based on a single parent, according to the rules and score methods you provide! It’s JenPop (if you didn’t guess, J is for Java :D, Gen is for genetic, Pop is for population) and I already have code to throw at you!

At the moment, only 2 features are available:

  • Create generations and children (of any object) based on the rules you provide, from a parent class extending GeneticNode;
  • Get node scores and best nodes based on the rules you provide;

And as a bonus, a sample of Tic Tac Toe implementing the above features, that will never lose. Can’t lose even when playing against itself! 😀 For the moment, the only way you can download and use it is by downloading the tarball from my Google Drive, but I’ll host the project online as soon as I decide where to.

As the next step, I’m aiming into being able to process the best “path” to use after calculating nodes from many generations, looking at the possibilities from the current state. After that, I think I’ll implement some cross-over methods, so that you can “diverse” your population, it’s useful for some problems.

Needless to say, I’m quite happy with the results at this moment.

See ya!