In: Computer Science
def myLength(mylist):
count = 0
for index in range(len(myList)):
print(myList[index])
count = count + 1
return count
def myLength2(mylist):
count = 0
for element in myList:
print(element)
count = count + 1
return count
Use these two functions as examples to write your Python
function below!
Function 1: Write a function called myCount that takes in two
parameters (a list and an item) like this:
def myCount(myList,item):
The function should returns an integer representing how many times
the given item is in the given list. Could be from zero up to
infinity. You cannot use the list method list.count() to impelement
this function.
Function 2: Write a function called myIndex that takes in two
parameters (a list and an item) like this:
def myIndex(myList,item):
The function should return an integer representing the index of the
first occurrence of the given item in the given list. Returns from
zero up to inifinity if the item is found. Returns -1 if it is not
in the list.You cannot use the list method list.index() to
impelement this function.
Function 3: Write a function called contains that takes in two
parameters (a list and an item) like this:
def contains(myList,item):
The function should return True if the item is present in the list
and False if it is not. You cannot use the operator "in" to
implement this function, like:
if item in myList: #not allowed!
Instead, you should write a loop to look through each element, one
at a time, to determine if an item is in the list. This is
allowed:
for element in myList: #allowed!
Function 4: Write a function called myInsert that takes in three
parameters (a list, an index position, and an item) like
this:
def myInsert(myList,index,item):
The function should return a new list consisting of everything from
the old list but with the new item inserted at the given index. If
index is not valid (less than zero), then the function returns the
original list with no changes. You cannot use the list method
list.insert() to impelement this function.
Function 1 -
def myCount(myList,item):
count=0
for element in myList:
if element==item:
count=count+1
return count
print(myCount([1,4,5,1,1,5,'apple','cat','cat'],1))
Explanation :
1.We define a function named myCount which accepts myList and item as parameters.
2.we define a variable count with value 0.
3.we then iterate through element in the myList and check if myList element is equal to item . if yes then we increase count value by1 which represents nothing but the count of item in myList. After running for loop , return the count value.
4.Finally call the function by passing arguments , i have used myList = [1,4,5,1,1,5,'apple','cat','cat'] and item =1 in above example. It return value 3 since 1 has 3 occurences in myList.
Function 2 -
def myIndex(myList,item):
count=0
for index in range(len(myList)):
if myList[index]==item:
return index
else:
count=count+1
if count==len(myList):
return -1
print(myIndex([1,4,5,1,1,5,'apple','cat','cat'],'cat'))
Explanation :
1.We define a function named myIndex which accepts myList and item as parameters.
2.we define a variable count with value 0.
3.we then use for loop with iterations equal to len(myList) using range function.Here index takes value from 0 till len(myList) minus 1. if len(myList) = 5 then index takes value from 0 till 4.
if mylist value at index equal to item then we return index else we increment count by 1.
4.After the completion of for loop , if count value is equal to len(myList) it means no item was found in the list.Hence we return -1.
5.Finally call the function by passing arguments , i have used myList = [1,4,5,1,1,5,'apple','cat','cat'] and item ='cat' in above example. It return value 7 since first occurence of item 'cat' is at 7th index.
Function 3 -
def contains(myList,item):
count=0
for element in myList:
if element==item:
return True
else:
count=count+1
if count==len(myList):
return False
print(contains([1,4,5,1,1,5,'apple','cat','cat'],3))
Explanation :
1.We define a function named contains which accepts myList and item as parameters.
2.we define a variable count with value 0.
3.we then iterate through element in the myList and check if myList element is equal to item . if yes then we return True else we increase count value by1 .
4.After the completion of for loop , if count value is equal to len(myList) it means no item was found in the list.Hence we return False.
5.Finally call the function by passing arguments , i have used myList = [1,4,5,1,1,5,'apple','cat','cat'] and item =3 in above example. It return False since f3 is not present in myList.
Function 4 -
def myInsert(myList,index,item):
new_list=[]
if index<0:
return myList
for i in range(len(myList)):
if i != index:
new_list.append(myList[i])
else:
new_list.append(item)
new_list.append(myList[i])
return new_list
print(myInsert([1,4,5,3,'apple','cat'],4,6))
Explanation :
1.We define a function named myInsert which accepts myList,index and item as parameters.
2.we define am empty list named new_list
3.We check if index is less than zero then we return original list which is myList.
3.we use for loop with iterations equal to len(myList) using range function.Here i takes value from 0 till len(myList) minus 1. if len(myList) = 5 then i takes value from 0 till 4.
if i is not equal to index passed in function then we append myList element at index i to new_list
4.But if i equal to index passed in function then we append item passed in function to new_list and after that we append myList element at index i to new_list since we have to insert new element not replace new element with old element in list.
5.Finally call the function by passing arguments , i have used myList =[1,4,5,3,'apple','cat'] , index=4 and item =6 in above example. It return new_list = [1, 4, 5, 3, 6, 'apple', 'cat'] we have insert item 6 at index 4.