Question

In: Computer Science

using python #You've been sent a list of names. Unfortunately, the names #come in two different...

using python

#You've been sent a list of names. Unfortunately, the names
#come in two different formats:
#
#First Middle Last
#Last, First Middle
#
#You want the entire list to be the same. For this problem,
#we'll say you want the entire list to be Last, First Middle.
#
#Write a function called name_refixer. name_refixer should have two
#parameters: an output filename (the first parameter) and the
#input filename (the second parameter). You may assume that every
#line will match one of the two formats above: either First Middle
#Last or Last, First Middle.
#
#name_refixer should write to the output file the names all
#structured as Last, First Middle. If the name was already structured
#as Last, First Middle, it should remain unchanged. If it was
#structured as First Middle Last, then Last should be moved
#to the front and a comma should be added after it.
#
#The names should appear in the same order as the original file.
#
#For example, if the input file contained the following lines:
#David Andrew Joyner
#Hart, Melissa Joan
#Cyrus, Billy Ray
#
#...then the output file should contain these lines:
#Joyner, David Andrew
#Hart, Melissa Joan
#Cyrus, Billy Ray


#Add your code here!

#The code below will test your function. You can find the two
#files it references in the drop-down in the top left. If your
#code works, output_file.txt should have the text:
#Joyner, David Andrew
#Hart, Melissa Joan
#Cyrus, Billy Ray
name_refixer("output_file.txt", "input_file.txt")
print("Done running! Check output_file.txt for the result.")

#If you accidentally erase input_file.txt, here's its original
#text to copy back in (remove the pound signs):
#David Andrew Joyner
#Hart, Melissa Joan
#Cyrus, Billy Ray

Solutions

Expert Solution

def name_refixer(outputFile,inputFile):
first=True
with open(outputFile,'w') as fout, open(inputFile) as fin:
for line in fin:
name=''
if len(line.strip().split(','))==2:
name=line.strip()
else:
first,middle,last=line.split()
name='{}, {} {}'.format(last,first,middle)
if first:
fout.write(name)
first=False
else:
fout.write("\n"+name)


Related Solutions

Python A good friend of yours sent you a long list of recipe names in a...
Python A good friend of yours sent you a long list of recipe names in a text file (where each line is a recipe that also includes information about how long it takes to do the recipe). You only want to know about the recipes that involve chocolate and are fast (i.e., take 15 mins or less to prepare). Write the program for this! Write a program to -       + Find recipes that involve chocolate that can be done...
Using LIST and FUNCTION Write a program in Python that asks for the names of three...
Using LIST and FUNCTION Write a program in Python that asks for the names of three runners and the time it took each of them to finish a race. The program should display who came in first, second, and third place.
This is all in Python. Also, the names come from a .txt file that i have...
This is all in Python. Also, the names come from a .txt file that i have created with 3 names on it(Jim,Pam,Dwight). Main Function. Write a program where the user enters a name. Using the read function, check to see if the name is in the text document. If it is, respond back to the user the name is found within the list. Read Function. The read function should return the contents of the text document as a list. EXAMPLE...
The italicized list below includes scientific names (binomial names) for 5 different organisms. To complete this...
The italicized list below includes scientific names (binomial names) for 5 different organisms. To complete this assignment prepare a document using the textbook (Chapter 1, Diversity of Life) and credible websites as sources (refer to Introductory assignments: Website Credibility for guidelines) that (1) explains the concept and importance of a scientific name- 5 pts., (2) briefly describes each of the following organisms, including the common name- 2 pts. each, (3) assigns each organism to the appropriate domain and kingdom- 1...
Write a program that uses Python List of strings to hold the five student names, a...
Write a program that uses Python List of strings to hold the five student names, a Python List of five characters to hold the five students’ letter grades, and a Python List of four floats to hold each student’s set of test scores. The program should allow the user to enter each student’s name and his or her four test scores. It should then calculate and display each student’s average test score and a letter grade based on the average....
Python: How would I write a function using list comprehensions to list of integers into two...
Python: How would I write a function using list comprehensions to list of integers into two halves with the evens first, followed by odds? And on top of that, the numbers should be listed in their original order.
Declare a Python list named famfri with the first names of five friends or family members....
Declare a Python list named famfri with the first names of five friends or family members. Lists and tuples are examples of what kind of Python construct? Write a Python for loop that prints the names of the friends or family, one per line, without referencing a range or any literal numeric values. Write Python code examples of statements to remove the name at position 2 from the list in the first problem, and to remove one of the remaining...
Create a table of the different classes of neurotransmitters . List names of neurotransmitters, their main...
Create a table of the different classes of neurotransmitters . List names of neurotransmitters, their main functions and implications in human disease.
Create a table of the different classes of neurotransmitters. List names of neurotransmitters, their implications in...
Create a table of the different classes of neurotransmitters. List names of neurotransmitters, their implications in human disease,  and their main functions .
Suppose you've been asked to provide a "Top 5" list for families that includes suggestions or...
Suppose you've been asked to provide a "Top 5" list for families that includes suggestions or "things they need to know" to improve/enhance communication in their family. Drawing on the information from this class, create your bullet point list of "Top 5 Tips" - BE SURE TO INCLUDE ALL REQUIRED ELEMENTS: With each recommendation, provide a "rationale" for WHY families should consider this element Please ensure that the recommendations apply concepts and are not "common sense" recommendations that families would...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT