Write a method isExclusive that accepts a map whose keys and values are strings
ID: 655743 • Letter: W
Question
Write a method isExclusive that accepts a map whose keys and values are strings as a parameter and returns true if no two keys map to the same value (and false if any two or more keys do map to the same value). For example, if the map contains the following key/value pairs, your method would return true: {Mary=Bailey, Stuart=Peter, Jessica=Miller, Amanda=James, Hal=Perkins}. But calling it on the following map would return false, because of two mappings for Perkins and Reges: {Kendrick=Perkins, Stuart=Reges Jessica=Miller, Bruce=Reges, Hal=Perkins}.
Write a method removeEvenLength that accepts a set of strings as a parameter and that removes all of the strings of even length from the set.
Explanation / Answer
Program code:
1)
def isExclusive(mymap):
for i in range(0,len(mymap)):
for j in range(i+1,len(mymap)):
if list(mymap.values())[i]==list(mymap.values())[j]:
return False
return True
mymap={"Kendrick":"Perkins", "Stuart":"Reges" ,"Jessica":"Miller", "Bruce":"Reges", "Hal":"Perkins"}
flag=isExclusive(mymap)
print(flag)
mymap={'Marty':'Stepp', 'Stuart':'Reges', 'Jessica':'Miller', 'Amanda':'Camp', 'Hal':'Perkins'}
flag=isExclusive(mymap)
print(flag)
Sample output:
False
True
2)
def removeEvenLength(myList=[]):
length=len(myList)
strings=""
k=0
while k < len(myList):
strings=myList[k]
if (len(strings)%2)==0:
del myList[k]
k=k+1
for k in myList:
print(k)
newList=['Kendrick', 'Perkins', 'Stuart', 'Jessica', 'Bruce', 'Miller', 'Amanda']
removeEvenLength(newList)
Sample Output:
Perkins
Jessica
Bruce
Amanda
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.