# Name: # Kerberos: # homework_2a.py import math import random ##### Template for Homework 2a, exercises 3.1-3.2 ###### # ********** Exercise 3.1 ********** # Define your rock paper scissors function here def rps(player1, player2): ''' Takes in two strings (rock, paper, or scissors) as input, and gives the result of the game. ''' # Validate the input (must be one of 'rock') if (player1 != 'rock' and player1 != 'paper' and player1!= 'scissors') or \ (player2 != 'rock' and player2 != 'paper' and player2!= 'scissors'): return 'Invalid input.' if player1 == player2: return 'Tie!' elif player1 == 'rock' and player2 == 'scissors' or\ player1 == 'scissors' and player2 == 'paper' or\ player1 == 'paper' and player2 == 'rock': return 'Player 1 wins!' else: return 'Player 2 wins!' # Test Cases for Exercise 3.1 assert rps('rock','scissors') == 'Player 1 wins!' assert rps('rock','paper') == 'Player 2 wins!' assert rps('rock','rock') == 'Tie!' assert rps('nothing','err') == 'Invalid input.' # *********** Exercise 3.2 *********** ## 1 - multadd function def multadd(a, b, c): ''' Takes in three numbers, and returns a * b + c. ''' return a * b + c ## 2 - Equations angle_test = multadd(math.cos(math.pi/4), 1.0/2, math.sin(math.pi/4)) print "sin(pi/4) + cos(pi/4)/2 is:" print angle_test ceiling_test = multadd(2, math.log(12, 7), math.ceil(276.0/19)) print "ceiling(276/19) + 2 log_7(12) is:" print ceiling_test ## 3 - yikes function ## Using math.pow def yikes(x): ''' Takes in a number, and returns x*e^(-x) + (1 - e^(-x))^(1/2). ''' return multadd(x, math.pow(math.e, -x), math.sqrt(1 - math.pow(math.e, -x))) # Using **. def yikes2(x): ''' Takes in a number, and returns x*e^(-x) + (1 - e^(-x))^(1/2). ''' return multadd(x, math.e**(-x), math.sqrt(1 - math.e**(-x))) # Test Cases x = 5 print "yikes(5) =", yikes(x) print "yikes2(5)=", yikes2(x)