Write a function called Cutter with the following specifications: The function t
ID: 3801141 • Letter: W
Question
Write a function called Cutter with the following specifications: The function takes two input parameters, a list of numbers and a number called the 'cut' The function loops through the lists and subtracts from each element the 'cut' The function rectums the updated list For example: cutter ([10, 20, 30, 40], 5) should return [5, 15, 25, 35] cutter [1, 9, 2], 2) should return [-1, 7, 0] Write a function maxplus(num1 num2, nun3) that takes three different numbers and return the maximum number incremented by one. maxPLus(5, 18, 7) should return 11 (the maximum 10 +1) maxPLus(300, 150, 200) should return 301 (the maximum 300 +I) You are planning to write a function called readCC_+. The function main objective is to create a list of countries and a list of Capitals and that is where the name readCC comes from (read Countries and Capitals). Before writing the function, you found a text file that has a list of countries and their capitals. The file is called 'capitals txt'. The first few lines are the following: 1-Abu Dhabi United Arab Emirates 2-Abuja Nigeria 3-Accra-Ghana 4-Addis Ababa Ethiopia You can notice that it follows the format: #-capital-country where m is a number). write the faction readCC such that it has the following features: It has one input parameter, which is the filename It opens the file and reads the file line by line until the end of file Using the data read from the file, it creates two lists one containing the list of capitals and the second list of countries It counts how many countries/capitals were found in the file It prints the result of counting to the terminal It returns the two list of capitals and list of countriesExplanation / Answer
1) Cutter.py
def Cutter(list,cut):
rList=[];
i=0
for n in list: # Iterating List and negating cut from that
v=n-cut;
rList.insert(i,v)
i=i+1
print rList
Cutter([10,20,30,40],5) # Passing List and cut into Cutter Function
Cutter([1,9,2],2) # Passing List and cut into Cutter Function
Output:
python Cutter.py
[5, 15, 25, 35]
[-1, 7, 0]
2) MaxPlus.py
def maxPlus(a,b,c):
big=0
if (a>b and a>c): # Checking max between a and c
big=a
elif (b>a and b>c): # Checking max between b and c
big=b
else:
big=c
max=big+1
print "The maximum is:" + str(max) # Converting integer into string
maxPlus(5,10,7)
maxPlus(300,150,200)
Output
python MaxPlus.py
The maximum is:11
The maximum is:301
3) ReadCC.py
def readCC(f):
fl = open(f,'r+') # Opening file in read mode
coList=[] # Initial list as empty
cpList=[]
i=0
for line in iter(fl): #Iterating each line from file till EOF using Iter
data = line.split("-"); # Splits String and store into array
cpList.insert(i,data[1]) #Inserting each value into List
coList.insert(i,data[2].strip())
i=i+1
print "Count is:"+str(i)
print "Countries are:"
print coList
print "Capitals are:"
print cpList
fl.close()
readCC("capitals.txt")
Output:
python Capitals.py
Count is:4
Countries are:
['United Arab Emirates', 'Nigeria', 'Ghana', 'Ethiopia']
Capitals are:
['Abu Dhabi', 'Abuja', 'Accra', 'Addis Ababa']
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.