Question

In: Computer Science

Python Programming 1. Write a function name split_name which takes one parameter called name. If the...

Python Programming

1. Write a function name split_name which takes one parameter called name. If the name parameter value is a name in the form LastName, FirstName(e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName(e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element.

2. Repeatedly prompt the user for a name (which the user may give in Last-name, FirstName form or FirstName LastName form). If the user’s response is an empty string (i.e., they simply press Return) cease looping. If the user’s response is not the empty string, call the split_name function you wrote and append the returned value to a list (initially empty).

3. Using the sorted function (which can take a single parameter, a list) and returns a copy of the list in sorted order) sort the list of names once the loop has finished. Print the sorted list of names, one name per line in LastName, FirstName form.

4. Observe the sorted list of names, are they sorted alphabetically by first name or last name? Leave your answer in a comment within the Python file you submit. Also, how do you think we could potentially modify the program to sort by last name instead of first (or first name instead of last, whichever is opposite of what you observed)? Leave your answer in a comment within the Python file you submit.

5. At the top of your file must be a comment that contains a brief explanation of how you determined which portion of each name was the first name vs. the last name.

Solutions

Expert Solution

Here is the full codes:

# 1)
# If the input strings contains comma, we consider text before comma as last name and
# text after comma as first name, else if there is no comma in the string, we suppose
# name has been put as first name and last name
def split_name(name):
    if ',' in name:
        name = name.split(',')
        FirstName = name[-1].replace(" ", "")
        LastName = name[0].replace(" ", "")
    else:
        name = name.split(' ')
        FirstName = name[0].replace(" ", "")
        LastName = name[-1].replace(" ", "")
        
    return(FirstName,LastName)

# calling the function
print(split_name('Grounds, Nic'))
print(split_name('Nic Grounds'))



# 2)
name_list = list()
while(True):
    name = input('Please enter your first name and last name: ')
    if(len(name) == 0):
        break
    else:
        FirstName,LastName = split_name(name)
        name_list.append((FirstName,LastName))

# 3) Print the sorted list of names, one name per line in LastName, FirstName form.
name_list_show = sorted(name_list)
for loop1 in range(len(name_list_show)):
    print(name_list_show[loop1][1],name_list_show[loop1][0])


# 4) Above code has printed alphabetically by last name
# here is the code to print by first name
sort_firstname = sorted(name_list, key = lambda x: x[0])
sort_firstname

Here is the output:

5) # If the input strings contains comma, we consider text before comma as last name and
# text after comma as first name, else if there is no comma in the string, we suppose
# name has been put as first name and last name.


Related Solutions

Write a python function image compress() that takes one argument called filename, which is the name...
Write a python function image compress() that takes one argument called filename, which is the name of a file that contains a N × N (N-pixel by N-pixel) “grayscale bitmap image”. A “grayscale bitmap image” is an image of the following form where every pixel contains a grayscale color value between 0 − 255 (inclusive). Colour value 0 means that pixel should appear completely black and color value 255means completely white. Any other value in between stands for different shades...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and returns a new sequence that is twice the length of the parameter sequence but that contains the contents of the original in palindrome form. For example, if the sequence "super" is passed into the function, the function will return "superrepus".
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' 1. Write a function called compute_discount which takes a float as the cost and a...
'PYTHON' 1. Write a function called compute_discount which takes a float as the cost and a Boolean value to indicate membership. If the customer is a member, give him/her a 10% discount. If the customer is not a member, she/he will not receive a discount. Give all customers a 5% discount, since it is Cyber Tuesday. Return the discounted cost. Do not prompt the user for input or print within the compute_discount function. Call the function from within main() and...
solve with python 3.8 please 1,Write a function called make_squares_coordinate_pair that has one parameter - an...
solve with python 3.8 please 1,Write a function called make_squares_coordinate_pair that has one parameter - an int. The function should return a tuple containing that int and its square. Suppose you were to call your function like this: print(make_squares_coordinate_pair(5)) This should output: (5, 25) Your function MUST be called make_squares_coordinate_pair. You can write code in the main part of your program to test your function, but when you submit, your code must ONLY have the function definition! 2, Write a...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and returns a double. When the variable argument phone contains the character a or A print the word Apple and return 1099.99 When phone contains anything else return a 0.0
using python 1. #Write a function called multiply_by_index. multiply_by_index #should have one parameter, a list; you...
using python 1. #Write a function called multiply_by_index. multiply_by_index #should have one parameter, a list; you may assume every item #in the list will be an integer. multiply_by_index should #return a list where each number in the original list is #multipled by the index at which it appeared. # #For example: # #multiply_by_index([1, 2, 3, 4, 5]) -> [0, 2, 6, 12, 20] # #In the example above, the numbers 1, 2, 3, 4, and 5 appear #at indices 0,...
C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT