Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Python Tutorial 2

The document describes functions to: 1) Find the intersection of two lists containing no duplicate values 2) Print pairs of integers from two lists that sum to a given integer n 3) Print the indexes of pairs in a list that sum to a given integer n

Uploaded by

queen setilo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Python Tutorial 2

The document describes functions to: 1) Find the intersection of two lists containing no duplicate values 2) Print pairs of integers from two lists that sum to a given integer n 3) Print the indexes of pairs in a list that sum to a given integer n

Uploaded by

queen setilo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Write a function intersect() that takes two lists, each containing no duplicate

values, and Returns a list Containing Values that are present in Both lists(I,e., the
intersection of the two input lists).

>>> intersect([3, 5, 1, 7, 9], [4, 2, 6, 3, 9]) [3, 9]

Syntax:
def intersect(listA, listB):
listed = []
for list1 in listA:
for list2 in listB:
if list1 == list2:
listed.append(list1)
return listed
listA=input("Enter list A:").split(",")
listB=input("Enter list B:").split(",")
print(intersect(listA, listB))

Output:
Implement the function pair() That Takes as input two lists of Integers and one
integer n and prints the pair of integers, one from the first input list and the other
from the second input list, that add up to n. Each pair should be printed.

>>> pair ([2, 3, 4], [5, 7, 9, 12], 9)


27
45

Syntax:

listA=[int(i) for i in input("Enter list


A:").split(",")]
listB=[int(i) for i in input("Enter list
B:").split(",")]
n =int(input("enter integer:"))
def pair(listA, listB, n):
for list1 in listA:
for list2 in listB:
if list1 + list2 == n :
print(list1,list2)

pair(listA,listB,n)

Output:
Implement the function pairSum() that takes as input a list of distinct integers 1st
and an integer n, and prints the indexes of all pairs of values in 1st that sum up to n.

>>>pairSum([7, 8, 5, 3, 4, 6], 11) 04 13 25

Syntax:
list=[int(i) for i in input("enter the list of distinct
integer:").split(",")]
n=int(input("Enter integer:"))
def pairSum(list, n):
for x in range(0, len(list)):
for y in range( x + 1, len(list)):
if list[x] + list[y] == n :
print(x,y)

pairSum(list,n)

Output:
Write function pay() that takes as input an hourly wage and the number of hours an
employee worked in the last week. The Function should compute and return the
employee’s pay. Overtime work should be paid in this way: Any hours beyond 40
but less than or equal 60 should be paid at 1.5 times the regular hourly wage. Any
hours beyond 60 should be paid at 2 times the regular hourly wage.
>>> pay (10, 35) 350
>>>pay (10, 45) 475
>>> pay (10,61) 720

Syntax:
hourly_wage=int(input("Enter hourly wage:"))
hour=int(input("Enter number of hours:"))

def pay(hourly_wage,hour):
if hour <= 40:
return hourly_wage * hour
elif 40 < hour <= 60:
return 1.5 * (hour - 40) * hourly_wage + 40 *hourly_wage
else:
return 2 * (hour - 60) * hourly_wage + 20 *1.5 *
hourly_wage + 40 * hourly_wage

print(pay(hourly_wage, hour))

Output:

(a). (b).

(c).
Write function case() that takes a string as input and returns ‘capitalized’, ‘not
capitalized’, or ‘uknown’, depending on whether the string starts with an uppercase
letter, lowercase letter, or something other than a letter in the English alphabet,
respectively.

>>> case (‘Android’)


‘capitalized’
>>> case(‘3M’)
‘unkown’

Syntax:
word=input("enter word:")
def case(word):
if word[0] >= "A" and word [0] <= "Z":
return "capitalized"
elif word[0] >= "a" and word [0] <= "z":
return "not capitalized"
else:
return "unknown"

print(case(word))

Output:

(a). (b).
Implement function leap() that takes on input argument--- a year --- and returns
True if the year is a leap year and False otherwise. (A year is a leap year if it is
divisible by 4 but no by 100, unless it is divisible by 400 in which case it is a leap
year. For example, 1700, 1800 and 1900 are not leap years but 1600 and 2000 are.)

>>> leap(2008)
True
>>> leap(1900)
False
>>>leap (2000)
True

Syntax:
year=eval(input("enter year:"))
def leap(year):
if (year % 4 == 0) & ((year % 100 != 0) | (year %
400 == 0)):
return True
return False
print(leap(year))

Output:

(a). (b).

(c).
Rock, Paper, Scissors is a two- player game in which each player chooses one of
three item s. If both player choose the same item, the game is tied. Otherwise, the
rules that determine the winner are:

(a) rock always beats Scissors (Rock crushes Scissors)


(b) Scissors always beats Paper (Scissors cut Paper)
(c) Paper always beat Rock (Paper covers Rock)

Implement function rps() that takes the choice (‘R’, ‘P’, or ‘S’) of player 1 and the
choice of player 2, and returns -1 if player 1 wins, 1 if player 2 wins, or 0 if there is
a tie.

>>> rps(‘R’, ‘P’) 1


>>> rps( ‘R’ , ‘S’) -1
>>> rps( ‘s’ , ‘S’) 0

Syntax:
player1=input("player 1:")
player2=input("player 2:")

def rps(player1,player2):
if (player1 == "S" and player2 == "P") | (player1 == "R" and
player2 == "S") | (player1 == "P" and player2 == "R"):
return "1"
elif player1 == player2:
return "0"
else:
return "-1"

print(rps(player1,player2))

Output:

(a). (b). (c).

You might also like