Question

In: Computer Science

using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings....

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

Solutions

Expert Solution

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 :-


Related Solutions

Define a Python function named matches that has two parameters. Both parameters will be lists of...
Define a Python function named matches that has two parameters. Both parameters will be lists of ints. Both lists will have the same length. Your function should use the accumulator pattern to return a newly created list. For each index, check if the lists' entries at that index are equivalent. If the entries are equivalent, append the literal True to your accumulator. Otherwise, append the literal False to your accumulator. Hint: Since you must use the same index with each...
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,...
Write a function in R named counts. This function should take as parameters a numeric vector...
Write a function in R named counts. This function should take as parameters a numeric vector x and also a number indicating a number of bins n. The function will consider the range [min(x),max(x)], and then consider a parti- tion of this interval into n non-overlapping equally sized half open intervals: I1 = [min(x),b1),I2 = [b1,b − 2),...,In = (bn−1,max(x)]. Note that since the intervals are equally sized, the value of bi is constrained. The function will then return a...
#Write a function called wish_list. wish_list should have #four parameters, in this order: # # -...
#Write a function called wish_list. wish_list should have #four parameters, in this order: # # - a list of strings, representing a list of items on a # wish list # - a string, representing a particular item # - a float, representing the cost of this item # - a float, representing your budget # #If the item is on the list and you can afford it (cost is #less than or equal to budget), return the string, #"You...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search,...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search, and string to search #for. The function should return everything in the first #string *after* the *second* occurrence of the search term. #You can assume there will always be at least two #occurrences of the search term in the first string. # #For example: # after_second("11223344554321", "3") -> 44554321 # #The search term "3" appears at indices 4 and 5. So, this #returns everything...
IN PYTHON Create a function called biochild.  The function has as parameters the number m...
IN PYTHON Create a function called biochild.  The function has as parameters the number m and the lists biomother and biofather.  The biomother and biofather lists contain 0’s and 1’s.  For example: biomother = [1,0,0,1,0,1] and biofather = [1,1,1,0,0,1]  Both lists have the same length n.  The 0's and 1's represent bits of information (remember that a bit is 0 or 1).  The function has to generate a new list (child).  The child...
#Write a function called solve_right_triangle. The function #solve_right_triangle should have three parameters: opposite, #adjacent, and use_degrees....
#Write a function called solve_right_triangle. The function #solve_right_triangle should have three parameters: opposite, #adjacent, and use_degrees. opposite and adjacent will be #floats, and use_degrees will be a boolean. use_degrees #should be a keyword parameter, and it should have a #default value of False. # #The function should return a tuple containing the #hypotenuse and angle of the right triangle (in that order). #If use_degrees is False, the angle should be in radians. #If use_degrees is True, the angle should be...
Write a method that concatenates two strings passed as parameters.    Version #1 - assign to...
Write a method that concatenates two strings passed as parameters.    Version #1 - assign to the 1st parameter the concatenation of both parameters. Perform the concatenation using the + operator. We are assuming reference parameter work that way (they don't).    Version #2 - fine. return the value of the 1st param after concatenating to it the second parameter then.    Version #3 - Let's do it the proper way now, I say. Create a temporary String reference variable...
Write a function called is_equal(). This function will take as an argument two integers. Call the...
Write a function called is_equal(). This function will take as an argument two integers. Call the is_equal function and supply two integers. You do not need to get the integers from the keyboard. Compare the two integers. If the two integers are equal, then print "equal". If the two integers are not equal, print "not equal". Call the function from your main code to execute the function. Sample output: equal Sample output: not equal
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT