Question

Python problem: write a main function that ask user for 2 file name, open files and...

Python problem:

write a main function that ask user for 2 file name, open files and read the 1st line of each and compare them using hamming function(below is the hamming function I wrote). Just assume the fist line of each file only contains 0s and 1s, such as 0100111101.

The main function may have to do some extra work to remove newline characters or other whitespace from the text read from each file.

This is hamming function that need to be used in the main function:

def hamming(str1, str2):
diffs = 0
smaller_length = len(str1) if len(str1) < len(str2) else len(str2)
for i in range(smaller_length):
if str1[i] != str2[i]:
diffs += 1
  
diffs += abs(len(str1) - len(str2))
return diffs

Homework Answers

Answer #1

Python Code:

def hamming(str1, str2):
    diffs = 0
    smaller_length = len(str1) if len(str1) < len(str2) else len(str2)
    for i in range(smaller_length):
        if str1[i] != str2[i]:
            diffs += 1
      
    diffs += abs(len(str1) - len(str2))
    return diffs

def main():
    inp1=input("Enter the name of 1st file ")
    inp2=input("Enter the name of 2nd file ")
    with open(inp1) as file1, open(inp2) as file2:
        file1line=file1.readline().strip()
        file2line=file2.readline().strip()
        ans=hamming(file1line,file2line)
        print(ans)

if __name__=="__main__":
    main()
        
Know the answer?
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
Similar Questions
You must write a function that works like a small editor. It should open an existing...
You must write a function that works like a small editor. It should open an existing file (let us say this is File1.txt. This file must exist before the function is run), read it line by line. Then write it to another file (let us say this is File2.txt. This file need not exist since Python will create it). However, if a certain line exists in the first file, it should be replaced by another line supplied by you. You...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it. Q2) Write a Python function called lines_of_code that takes a Path object as a parameter, which is...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
** Language Used : Python ** PART 2 : Create a list of unique words This...
** Language Used : Python ** PART 2 : Create a list of unique words This part of the project involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing. Create a test...
convert this code to accept int value instead of float values using python. Make sure to...
convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files Method:----------------------- #define a python user difined method def get_float_val (prompt): is_num = False str_val = input (prompt) #prming read for our while #while is_num == False: (ignore this but it works) old school while not is_num: try: value =...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Write a Python 3 program called “parse.py” using the template for a Python program that we...
Write a Python 3 program called “parse.py” using the template for a Python program that we covered in this module. Note: Use this mod7.txt input file. Name your output file “output.txt”. Build your program using a main function and at least one other function. Give your input and output file names as command line arguments. Your program will read the input file, and will output the following information to the output file as well as printing it to the screen:...