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:


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’!


Comment this

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s