Question

In: Computer Science

Implement function get_contact(contacts, name) that returns a string. The contacts and the name parameter are both...

Implement function get_contact(contacts, name) that returns a string. The contacts and the name parameter are both type string. This function checks for the name string in the contacts string and returns that person’s contact information. If the person is not found, the function returns, “name not in contact”. Assume input is always valid. Assume the same name is not repeated in contacts.

[You may use split(), range(), len() ONLY and no other built-in function or method]


Examples:

              contacts = “Frank 703-222-2222 Sue 703-111-1111 Jane 703-333-3333”

              name = “Sue”

              print(get_contact(contacts, name))

              returns:

              703-111-1111

              name = “Sam”

print(get_contact(contacts, name))

              returns:

              name not in contact

Solutions

Expert Solution

Below is the function that returns the number of the corresponding name provided.

Here we split the contact string into a list on the basis of spaces. Then we search for the name in the list by iterating through it. If the name is found, the next element to the name in the list is the contact number. So we return the number. If name is not found in the list, we return the string 'name not found in the list'.

def get_contact(contacts, name): # declare the function
    contact_list = contacts.split(' ') #split string based on space
    for i in range(len(contact_list)): #loop until length of contact list
        if name == contact_list[i]: #if name matches
            return contact_list[i+1] #return number of person (next element to name)
    return 'name not in contact' #if out of loop return name not in list

If we supply the following input to the above function:

contacts = 'Frank 703-222-2222 Sue 703-111-1111 Jane 703-333-3333' #contact list
name = 'Sue' #name
print(get_contact(contacts, name)) #print number by calling function

what we get as output is:

when we supply input as:

contacts = 'Frank 703-222-2222 Sue 703-111-1111 Jane 703-333-3333' #contact list
name = 'Sam' #name
print(get_contact(contacts, name)) #get number

we get output as:

Complete program for your reference:

def get_contact(contacts, name): # declare the function
    contact_list = contacts.split(' ') #split string based on space
    for i in range(len(contact_list)): #loop until length of contact list
        if name == contact_list[i]: #if name matches
            return contact_list[i+1] #return number of person (next element to name)
    return 'name not in contact' #if out of loop return name not in list

contacts = 'Frank 703-222-2222 Sue 703-111-1111 Jane 703-333-3333'
name = 'Sue'
print(get_contact(contacts, name))

For any doubts/clarifications, reach out in the comments section. If you find it useful, kindly upvote. Thanks.


Related Solutions

Implement function get_contact(contacts, name) that returns a string. The contacts and the name parameter are both...
Implement function get_contact(contacts, name) that returns a string. The contacts and the name parameter are both type string. This function checks for the name string in the contacts string and returns that person’s contact information. If the person is not found, the function returns, “name not in contact”. Assume input is always valid. Assume the same name is not repeated in contacts. [You may use split(), range(), len() ONLY and no other built-in function or method] Examples:               contacts = “Frank...
4. Implement the function read_info_file that consumes a file name (string) as its parameter and returns...
4. Implement the function read_info_file that consumes a file name (string) as its parameter and returns a list of strings - one element for each line in the file. These lines should have all the whitespace removed from both ends of the line. a. See the formatting of the individual_info data file. Consider how a file can be read into the program. In Python language
(3) Implement the getNumOfNonWSCharacters() method. getNumOfNonWSCharacters() has a string as a parameter and returns the number...
(3) Implement the getNumOfNonWSCharacters() method. getNumOfNonWSCharacters() has a string as a parameter and returns the number of characters in the string, excluding all whitespace. Call getNumOfNonWSCharacters() in the main() method. (4 pts) Ex: Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! You entered: We'll continue our quest in space....
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a function named "replacement" that takes a string as a parameter and returns an identical...
Write a function named "replacement" that takes a string as a parameter and returns an identical string except with every instance of the character "w" replaced with the character "v" My code: function replacement(word){ var str=word; var n=str.replace("w","v"); return n; } Syntax Error: function replacement incorrect on input Not sure how to fix? Can't use a loop for answer
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of the parameter le This function should return a dictionary that stores all of the parameter le data of the SIR model in a format that we will describe further below.
4.31 Implement function duplicate() that takes as input the name (a string) of a file in...
4.31 Implement function duplicate() that takes as input the name (a string) of a file in the current directory and returns True if the file contains duplicate words and False otherwise. duplicate('Duplicates.txt') True duplicate('noDuplicates.txt') False Please solve using Python Language and without using str.maketrans please. Just simple programming, Thank youuuuu!!!!!
Python Implement function noVowel() that takes a string s as input and returns True if no...
Python Implement function noVowel() that takes a string s as input and returns True if no char- acter in s is a vowel, and False otherwise (i.e., some character in s is a vowel). >>> noVowel('crypt') True >>> noVowel('cwm') True >>> noVowel('car') False
Implement function noVowel() that takes a string s as input and returns True if no char-...
Implement function noVowel() that takes a string s as input and returns True if no char- acter in s is a vowel, and False otherwise (i.e., some character in s is a vowel). >>> noVowel('crypt') True >>> noVowel('cwm') True >>> noVowel('car') False Implement function allEven() that takes a list of integers and returns True if all integers in the list are even, and False otherwise. >>> allEven([8, 0, -2, 4, -6, 10]) True >>> allEven([8, 0, -1, 4, -6, 10])...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT