Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def chicken_rabbit(num_heads, num_legs):
- chicken = int((2 * num_heads) - (num_legs / 2))
- rabbit = int(num_heads - chicken)
- ans_chicken = ""
- ans_rabbit = ""
- #print(chicken)
- #print(rabbit)
- if(num_legs%2 != 0):
- return ["Invalid inputs received. Program aborted."]
- if(chicken < 0 or rabbit < 0):
- return ["Invalid inputs received. Program aborted."]
- if(chicken == 0):
- ans_chicken = "There isn't any chicken in the cage"
- elif(chicken == 1):
- ans_chicken = "There is 1 chicken in the cage"
- elif(chicken > 1):
- ans_chicken = "There are {0} chickens in the cage".format(chicken)
- if(rabbit == 0):
- ans_rabbit = "There isn't any rabbit in the cage"
- elif(rabbit == 1):
- ans_rabbit = "There is 1 rabbit in the cage"
- elif(rabbit > 1):
- ans_rabbit = "There are {0} rabbits in the cage".format(rabbit)
- return [ans_chicken, ans_rabbit]
- def unittest_function_pass():
- # write assertion statements here to test the function
- # enumerate as many test cases you can think of to test your function
- # See "Additional Requirements described in the assignment" for details.
- assert chicken_rabbit(31 , 94) == ["There are 15 chickens in the cage", "There are 16 rabbits in the cage"]
- def unittest_function_xfail():
- # write assertion statements here to test the function on any expected failing cases
- # See "Additional Requirements described in the assignment" for details.
- assert chicken_rabbit(2, 6) == ["Invalid inputs received. Program aborted."]
- if __name__ == "__main__":
- num_heads = int(input("Input number of heads:\n"))
- num_legs = int(input("Input number of legs:\n"))
- answer = chicken_rabbit(num_heads, num_legs)
- # TODO: add print statements below.
- if len(answer) == 1:
- print(answer[0])
- else:
- print(answer[0])
- print(answer[1])
- # TODO: complete the unittest_function so your function undergo a rigorous unittest created by yourself
- #unittest_function_pass() #if the num legs are more than the num heads
- # TODO: complete the unittest_function_xfail so your function undergo a rigorous unittest created by yourself
- #unittest_function_xfail() #more head than legs
- # Please note that unittest_function_xfail is ALWAYS expected to trip an error! Because it is testing on expected failing cases
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement