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
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.
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])...
A constructor, with a String parameter representing the name of the item.
USE JAVA Item classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.Inventory classA constructor which takes a String parameter indicating the item type, an int parameter indicating the initial...
Python Programcomplete theprocessString(string)function. This function takesin a string as a parameter and prints the...
Python Program complete theprocessString(string)function. This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision(see test cases below).-Assume a sentence always ends with a period (.)or when the string ends. -Assume there is always a blank space character(" ")between each word. -Do not count the blank spaces between words or the periods as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT