HW_SOURCE_FILE=__file__
defmobile(left, right):"""Construct a mobile from a left arm and a right arm."""assert is_arm(left),"left must be a arm"assert is_arm(right),"right must be a arm"return['mobile', left, right]defis_mobile(m):"""Return whether m is a mobile."""returntype(m)==listandlen(m)==3and m[0]=='mobile'defleft(m):"""Select the left arm of a mobile."""assert is_mobile(m),"must call left on a mobile"return m[1]defright(m):"""Select the right arm of a mobile."""assert is_mobile(m),"must call right on a mobile"return m[2]defarm(length, mobile_or_planet):"""Construct a arm: a length of rod with a mobile or planet at the end."""assert is_mobile(mobile_or_planet)or is_planet(mobile_or_planet)return['arm', length, mobile_or_planet]defis_arm(s):"""Return whether s is a arm."""returntype(s)==listandlen(s)==3and s[0]=='arm'deflength(s):"""Select the length of a arm."""assert is_arm(s),"must call length on a arm"return s[1]defend(s):"""Select the mobile or planet hanging at the end of a arm."""assert is_arm(s),"must call end on a arm"return s[2]defplanet(size):"""Construct a planet of some size."""assert size >0"*** YOUR CODE HERE ***"return['planet', size]defsize(w):"""Select the size of a planet."""assert is_planet(w),'must call size on a planet'"*** YOUR CODE HERE ***"return w[1]defis_planet(w):"""Whether w is a planet."""returntype(w)==listandlen(w)==2and w[0]=='planet'defexamples():
t = mobile(arm(1, planet(
Fall 2020 Berkeley cs61a hw03答案HW_SOURCE_FILE=__file__def mobile(left, right): """Construct a mobile from a left arm and a right arm.""" assert is_arm(left), "left must be a arm" assert is_arm(right), "right must be a arm" return ['mobi