id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

peter@norvig.com > balanced_mobile
prev  |  next  |  chance

This exercise involves mobiles. Not the third-largest city in Alabama, nor the cellular phone network, but rather the kinetic hanging sculptures popularized by Alexander Calder. You will be asked to say whether a given mobile is "balanced" or not. For the purpose of this exercise a mobile is defined as either (a) a piece: a single decorative hanging piece of metal or cardboard or whatever; or (b) a rod: a rigid bar which has zero or more mobiles (pieces and/or rods) hanging off of it, each on a string. Each mobile has a fixed point from which it is hung by a string from the rod above (or from the ceiling). _________________________________________________________________________________________ We will represent a piece as a Python str. For example, "whale" is a piece. The weight of a piece is the length of the str; "whale" has weight 5. We will represent a rod as a list of (distance, mobile) pairs. The distance is the distance to the fixed point, and can be a positive or negative number. The total weight of a rod is the sum of the weights of the sub-mobiles hanging from the rod. The rods and strings are thin and light; their weight is negligible and can be ignored. Thus, the total weight of a mobile is equal to the to total weight of all the pieces that comprise the mobile. _________________________________________________________________________________________ Now what about the notion of "balanced"? A piece is always balanced; it just hangs there. A rod is balanced if all of its hanging sub-mobiles are balanced and if the total torque on the rod is zero. Don't panic -- the concept of torque is not complicated. The torque applied to a rod by a hanging mobile is just the total weight of the mobile times the distance of the mobile to the fixed point of the rod. Consider this mobile: [(-4, "CAT"), (3, "FROG")]. The torque applied by "CAT" is -12, because it is distance -4 from the fixed point and "CAT" has weight 3. The torque applied by "FROG" is 3 * 4 = 12, so the total torque is -12 + 12 = 0, so the mobile is balanced. _________________________________________________________________________________________ Note: This exercise is adapted from one in SICP (Structure and Interpretation of Computer Programs). Here is a picture of a mobile that is used for some of the test cases: http://themobilefactory.files.wordpress.com/2010/10/hellohangingmobile.jpg Sorry you have to copy and paste it; codingbat doesn't let me do anything else. Also, probably a dictionary of the form {-4: "CAT", 3: "FROG"} would be a better representation of a rod than the list [(-4, "CAT"), (3, "FROG")], but codingbat can't handle dictionaries in test examples.


...Save, Compile, Run (ctrl-enter)

def balanced_mobile(mobile):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Post-solution available

Copyright Nick Parlante 2017 - privacy