Question

Please write the following code as simple as possible in python: You will need to define...

Please write the following code as simple as possible in python:

You will need to define a function with four arguments. Here is what I used:

> def find_matches(file1.txt, output1.txt, strings, value):

file1.txt will contain a list of various strings. The program must copy from the first argument, and it should be written in the second argument (the second file, "output1.txt"). The third and fourth arguments will determine which specific strings will be copied over to the second file. For example, the third argument will have specific strings (i.e., "abc"), and the fourth argument will either have True or False. If the argument presented is "abc" and True, then every word with the string "abc" must be copied over to the second file. In addition, the function should return the number of words copied from the list. It is important to note that the information within the second file should be erased every time the script is ran. The code should not include .replace or a .find. Please refer to the example below.

Input:

def find_matches(file1.txt, output1.txt, "bc", True)

first argument ("the information in file1.txt):

> abcdef

> ed

second argument (the new file created output1.txt)

> abcdef

*if the function was false, then the output1.txt should have stored line (2) rather than line (1)

the function should lastly output because only one item was copied:

> 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python code:

#defining find_matches function

def find_matches(file1,output1,strings,value):

  #opening file file1

  f=open(file1,"r")

  #reading file1 to list listfile1

  listfile1=f.readlines()

  #closing file

  f.close()

  #opening file output1

  f=open(output1,"w")

  #initializing count

  count=0

  #looping through each element in listfile1

  for i in listfile1:

    #checking if the value should be written or not

    if(strings in i and value==True):

      #writing value to output file

      f.write(i)

      #incrementing count

      count+=1

    #checking if the value should be written or not

    if(strings not in i and value==False):

      #writing value to output file

      f.write(i)

      #incrementing count

      count+=1

  #closes the file.

  f.close()

  #returning count

  return count

#calling find_matches function and printing result

print(find_matches("file1.txt","output1.txt","bc",True))

Screenshot of code:

1 2 3 4. л 6 7 8 9 10 11 12 13 14 15 16 #defining find_matches function def find_matches (filel, outputi, strings, value): #o

file1.txt

file1.txt 1 abcdef ed 2

output1.txt

output1.txt 1 abcdef

Output:

1

Add a comment
Know the answer?
Add Answer to:
Please write the following code as simple as possible in python: You will need to define...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Python 3 Write code to define a function that uses three arguments, and returns a...

    In Python 3 Write code to define a function that uses three arguments, and returns a result. The function returns True if the first argument is more than or equal to the second argument and less than or equal to the third argument, otherwise it returns False.

  • python

    pythonComplete the below function that takes the name of two files, inFilename andoutFilename as arguments and reads the text from the inFilename. In this fileeach line contains the Turkish Republic Identity Number (TCNO), name, surnameand telephone number of a person. Your function should sort all personsaccording to their TCNO, write the sorted data into outFilename. If the fileinFilename does not exist, then the function must create an empty file namedoutFilename.For example, if the function is called such asreadText("in.txt", "out.txt")and in.txt...

  • Command line input In C++ it is possible to accept command line arguments. Command-line arguments are...

    Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • write a code that includes the following:(PYTHON NOT JAVA) # This program uses the writelines method...

    write a code that includes the following:(PYTHON NOT JAVA) # This program uses the writelines method to save # a list of strings to a file. def main(): # Create a list of strings. # Open a file for writing. # Write the list to the file. # Close the file. # Call the main function.

  • For this problem, you have to use following hash function: key modulo the number of buckets....

    For this problem, you have to use following hash function: key modulo the number of buckets. Input format: This program takes a file name as argument from the command line. The file is either blank or contains successive lines of input. Each line contains a character, either ‘i’ or ‘s’, followed by a tab and then an integer, the same format as in the Second Part. For each of the lines that starts with ‘i’, your program should insert that...

  • PYTHON Hello can someone create the following functions? I'm not sure how to start on them....

    PYTHON Hello can someone create the following functions? I'm not sure how to start on them. makeDictionary Define a function named makeDictionary with two parameters. The first argument passed the function must be a list of strings to be used as keys, while the second is a list of the same length containing values (of some type). This function must return a dictionary consisting of the keys paired with the corresponding values. For example, makeDictionary(['apno','value'],['REP-12','450']) must return this dictionary: {'apno':...

  • in Python, define a function named filterOut with three parameters. The first argument passed to the...

    in Python, define a function named filterOut with three parameters. The first argument passed to the function should be a list of dictionaries (the data), the second a string (a dictionary key), and the third another string (a dictionary value). This function must return a list of all the dictionaries from the input list which do NOT contain the indicated key:value pairing. in Python, define a function named filterInRange with four parameters. The first argument passed to the function should...

  • IN PYTHON CODE Question #1 Write a function capital that has one argument: strlist that is...

    IN PYTHON CODE Question #1 Write a function capital that has one argument: strlist that is a list of non-empty strings. If each string in the list starts with a capital letter, then the function should return the value True. If some string in the list does not start with a capital letter, then the function should return the value False You may use any of the string functions that are available in Python in your solution, so you might...

  • ### Question in python code, write the code on the following program: def minmaxgrades(grades, names): """...

    ### Question in python code, write the code on the following program: def minmaxgrades(grades, names): """ Computes the minimum and maximujm grades from grades and creates a list of two student names: first name is the student with minimum grade and second name is the student with the maximum grade. grades: list of non-negative integers names: list of strings Returns: list of two strings """ def main(): """ Test cases for minmaxgrades() function """ input1 = [80, 75, 90, 85,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT