In: Computer Science
using python
1.
#Write a function called multiply_file_by_index. This
function
#should take two parameters, both strings. The first string
is
#the filename of a file to which to write (output_file), and
#the second string is the filename of a file from which to
read
#(input_file).
#
#In the input file, there will be an integer on every line.
#To the output file, you should write the integer from the
#original file multiplied by the line number on which it
#appeared. You should assume that the first line of the file
#is line 1 (which is different from a list, where the first
item
#is at index 0).
#
#For example, if the input file contained this text:
#1
#4
#3
#7
#6
#
#Then the output file would contain this text:
#1
#8
#9
#28
#30
#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:
#1
#8
#9
#28
#30
multiply_file_by_index("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):
#1
#4
#3
#7
#6
2.
#Write a function called search_inside_files.
search_inside_files
#should have two parameters: a filename and a string to search
for.
#search_inside_files should return a list of line numbers as
integers
#where the string appeared. Note that the string at that index
does
#not need to BE the search string, but rather must just contain
it.
#You should assume that the first line in a file is line 1, not
line
#0.
#
#For example, if the files contents was:
#cat
#cats
#dog
#dogs
#catsup
#
#Then search_inside_files("input_file.txt", "cat") would
return
#[1, 2, 5], because "cat" appears on lines 1, 2, and 5.
#
#Make sure the list you return is sorted from lowest line number
to
#highest.
#Add your code here!
#The code below will test your function. You can find the file
it
#references in the drop-down in the top left. If your code
works,
#this should print:
#[1, 2, 5]
#[3, 4]
#[2, 5]
#[5]
#[]
print(search_in_files("input_file.txt", "cat"))
print(search_in_files("input_file.txt", "dog"))
print(search_in_files("input_file.txt", "cats"))
print(search_in_files("input_file.txt", "sup"))
print(search_in_files("input_file.txt", "aardvark"))
3.
#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 First Middle Last.
#
#Write a function called name_fixer. name_fixer 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_fixer should write to the output file the names all
#structured as First Middle Last. If the name was already
structured
#as First Middle Last, it should remain unchanged. If it was
#structured as Last, First Middle, then Last should be moved
#to the end after a space and the comma removed.
#
#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:
#David Andrew Joyner
#Melissa Joan Hart
#Billy Ray Cyrus
#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:
#David Andrew Joyner
#Melissa Joan Hart
#Billy Ray Cyrus
name_fixer("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
1. multiply_file_by_index :-
def multiply_file_by_index(output_file, input_file):
"""
function to multiply line number and integer which
contain in that line and write result to an output file.
:param output_file: filename of output file where result to be written
:param input_file: filename of input file from where content to be read.
:return: nothing. just write result to output file
"""
# open both input and output files
# opening output write in write mode ("w") and input file in read mode ("r")
with open(output_file, "w") as output_file, open(input_file, "r") as input_file:
line_number = 0 # variable to hold current line number of input file.
for line in input_file: # loop through each line of input file.
line_number = line_number + 1 # increment line number by 1.
integer = int(line) # get line content from file and convert to integer
new_integer = line_number * integer # multiply line number and integer
output_file.write(str(new_integer) + "\n") # convert new_integer to string and write to output file
# calling multiply_file_by_index() to check it's working
multiply_file_by_index("output_file.txt", "input_file.txt")
print("Done running! Check output_file.txt for the result.")
Sample Output :-
input_file.txt :-
output_file.txt :-
Please refer to the Screenshot of the code given below to understand indentation of the Python code.
2. seach_in_files :-
def search_in_files(input_file, search):
with open(input_file) as input_file:
number_list = list()
line_number =0
for line in input_file:
line_number = line_number+1
if line.__contains__(search):
number_list.append(line_number)
return number_list
print(search_in_files("input_file.txt", "cat"))
print(search_in_files("input_file.txt", "dog"))
print(search_in_files("input_file.txt", "cats"))
print(search_in_files("input_file.txt", "sup"))
print(search_in_files("input_file.txt", "aardvark"))
Sample Output :-
input_file.txt :-
Please refer to the Screenshot of the code given below to understand indentation of the Python code :-
3. name_fixer :-
def name_fixer(output_file, input_file):
with open(output_file, "w") as output_file, open(input_file, "r") as input_file:
for line in input_file:
if line.__contains__(","):
last_name = line[0:line.index(",")]
first_middle_name = line[line.index(",") + 2:line.index("\n")]
output_file.write(first_middle_name + " " + last_name + "\n")
else:
output_file.write(line)
name_fixer("output_file.txt", "input_file.txt")
input_file.txt :
Sample output :-
output_file.txt :-
Please refer to the Screenshot of the code given below to understand indentation of the Python code :-