In: Computer Science
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.
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.