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

Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
This is python: #Write a function called count_positive_evens. This function #should take as input a list...
This is python: #Write a function called count_positive_evens. This function #should take as input a list of integers, and return as #output a single integer. The number the function returns #should be the count of numbers from the list that were both #positive and even. # #For example: # # count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1 # count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6 # count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0 # #0...
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters,...
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters, a list of strings, and a string. This function #should return a list of all the indices at which the #string is found within the list. # #You may assume that you do not need to search inside the #items in the list; for examples: # # search_for_string(["bob", "burgers", "tina", "bob"], "bob") # -> [0,3] # search_for_string(["bob", "burgers", "tina", "bob"], "bae") # -> []...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at...
Sovle with python 3.8 please. 1, Write a function called same_without_ends that has two string parameters....
Sovle with python 3.8 please. 1, Write a function called same_without_ends that has two string parameters. It should return True if those strings are equal WITHOUT considering the characters on the ends (the beginning character and the last character). It should return False otherwise. For example, "last" and "bask" would be considered equal without considering the characters on the ends. Don't worry about the case where the strings have fewer than three characters. Your function MUST be called same_without_ends. You...
write a javascript function called cal_modes that will take a List of numbers or strings as...
write a javascript function called cal_modes that will take a List of numbers or strings as input and returns a List of the most frequent values. If there's only one most-frequent value, it returns a single-element List. example cal_modes([3,4,5,5]) should return [5] cal_modes([8.9, 1, 1]) should return [1] cal_modes([2.5, -2, -2, 2.5]) should return [2.5] cal_modes([3,3,4,4]) should return [3,4] cal_modes([3,4,5]) should return [3,4,5], because all occur with equal frequency cal_modes(["hi", "what", "where", "hi"]) should return ["hi”]
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...
1. Implement the function calculate_score that consumes three parameters, two strings and a list. The strings...
1. Implement the function calculate_score that consumes three parameters, two strings and a list. The strings will each be ONE character and will represent a nucleotide. The list will be a nested int list representing a 4x4 score matrix. This function will return the value (int) from the nested int list at the location of the two referenced nucleotides. a. An example call to calculate_score would be calculate_score(“A”, “T”, score_matrix). If we look at the alignment score table in the...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT