Advertisement
Rew35

6.15

Feb 22nd, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. def chicken_rabbit(num_heads, num_legs):
  2. chicken = int((2 * num_heads) - (num_legs / 2))
  3. rabbit = int(num_heads - chicken)
  4. ans_chicken = ""
  5. ans_rabbit = ""
  6. #print(chicken)
  7. #print(rabbit)
  8. if(num_legs%2 != 0):
  9. return ["Invalid inputs received. Program aborted."]
  10. if(chicken < 0 or rabbit < 0):
  11. return ["Invalid inputs received. Program aborted."]
  12. if(chicken == 0):
  13. ans_chicken = "There isn't any chicken in the cage"
  14. elif(chicken == 1):
  15. ans_chicken = "There is 1 chicken in the cage"
  16. elif(chicken > 1):
  17. ans_chicken = "There are {0} chickens in the cage".format(chicken)
  18.  
  19. if(rabbit == 0):
  20. ans_rabbit = "There isn't any rabbit in the cage"
  21. elif(rabbit == 1):
  22. ans_rabbit = "There is 1 rabbit in the cage"
  23. elif(rabbit > 1):
  24. ans_rabbit = "There are {0} rabbits in the cage".format(rabbit)
  25.  
  26. return [ans_chicken, ans_rabbit]
  27.  
  28.  
  29. def unittest_function_pass():
  30. # write assertion statements here to test the function
  31. # enumerate as many test cases you can think of to test your function
  32. # See "Additional Requirements described in the assignment" for details.
  33. assert chicken_rabbit(31 , 94) == ["There are 15 chickens in the cage", "There are 16 rabbits in the cage"]
  34.  
  35.  
  36. def unittest_function_xfail():
  37. # write assertion statements here to test the function on any expected failing cases
  38. # See "Additional Requirements described in the assignment" for details.
  39. assert chicken_rabbit(2, 6) == ["Invalid inputs received. Program aborted."]
  40.  
  41. if __name__ == "__main__":
  42.  
  43. num_heads = int(input("Input number of heads:\n"))
  44. num_legs = int(input("Input number of legs:\n"))
  45. answer = chicken_rabbit(num_heads, num_legs)
  46. # TODO: add print statements below.
  47. if len(answer) == 1:
  48. print(answer[0])
  49. else:
  50. print(answer[0])
  51. print(answer[1])
  52.  
  53. # TODO: complete the unittest_function so your function undergo a rigorous unittest created by yourself
  54. #unittest_function_pass() #if the num legs are more than the num heads
  55.  
  56. # TODO: complete the unittest_function_xfail so your function undergo a rigorous unittest created by yourself
  57. #unittest_function_xfail() #more head than legs
  58.  
  59. # 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