Question

In: Computer Science

Python(please take a screen shot!): 1. hamming distance: write a function distance that take two bits...

Python(please take a screen shot!):

1. hamming distance: write a function distance that take two bits strings, you can assume each strings only contains 0's and 1's. (Note: the two strings might have the same length or not!)

for example:

hamming('010001001111', '01010100') should return 5(1 bit changed plus 4 bits "lost" from the end). 

2. write a main function that ask user for two file names, open files and read the 1st line of each, and compares them using Hamming Distance. Just assume each of the 1st line only contains 0's and 1's.

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

(when create two files to test the main function, make sure the file saved as (.txt) !)

Thanks so much !!!

Solutions

Expert Solution

Python Code:

IDE: repl online python editor

def hamming(x, y):
  """Calculate the Hamming distance between two bit strings"""

  count, z = 0, int(x,2)^int(y,2)

  while z:
    if z&1: 
      count += 1

    z >>= 1
    
  return count

def main():
  # Open files
  with open('file1.txt') as f1: # Opening file 1
    with open('file2.txt') as f2: # Opening file 2
      # read files line by line

      line_f1 = f1.readline()
      line_f2 = f2.readline()

      while True:
        if line_f1 == '':   # break loop if file1 is at eof (end of file)
          break

        # Hamming distance
        print('Hamming distance between', line_f1, 'and', line_f2, 'is:', hamming(line_f1, line_f2))

        line_f1 = f1.readline()
        line_f2 = f2.readline()


if __name__ == '__main__':
  main()

file1.txt:

010001001111
100101010101
1001010110
10101010111111

file2.txt:

01010100
0011010101100101
100101001110
1010110111

Output:

Code Screenshot:


Related Solutions

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....
python Write a program that prints your name 100 times to the screen. Write a function...
python Write a program that prints your name 100 times to the screen. Write a function that takes a string s and an integer n as parameters and prints the string s a total of n times (once per line). Write a for loop that prints all the integers from 3141 to 5926, skipping every other one. Write a for loop that prints every 5th integer from 5926 down to 3141. Write a program (using a for loop) to print...
write a python program that include a function named activity_selection() and take in two arguments, first...
write a python program that include a function named activity_selection() and take in two arguments, first one would be the number of tasks and the second argument would be a list of activities. Each activity would have an activity number, start time and finish time. Example activity_selection input and output: activity_selection (11, [[1, 1, 4 ], [2, 3, 5], [3, 0, 6], [4, 5, 7], [5, 3, 9], [6, 5, 9],[7, 6, 10], [ 8, 8, 11], [ 9, 8,...
In python i want to create a function. The function will take in two linked lists...
In python i want to create a function. The function will take in two linked lists as the parameters. If one is shorter than the other then the shorter will be the length. I want to take the values from both linked lists and turn them into tuples. I then want these tuples to be put into a new linked list. I want to return that linked list. I want to do this using recursion and no helper functions or...
* Make sure you turn in your code (take a screen shot of your code in...
* Make sure you turn in your code (take a screen shot of your code in R)and answers. Conduct the hypothesis and solve questions by using R. 2) A random sample of 12 graduates of a secretarial school averaged 73.2 words per minute with a standard deviation of 7.9 words per minute on a typing test. What can we conclude, at the .05 level, regarding the claim that secretaries at this school average less than 75 words per minute on...
Please show that the code is working at the end(screen shot of the final result +...
Please show that the code is working at the end(screen shot of the final result + classes and interfaces) and use interfaces. Your program should read from the standard input a sequence of integer values, with each value separated by a space. Your task is to: Build a binary search tree using these values in the order they are entered. Print 3 traversals: pre-, in-, and post-order. Allow the user to insert/delete a value. Once a new tree is generated,...
Python 3 Write the definition of a function that take one number, that represents a temperature...
Python 3 Write the definition of a function that take one number, that represents a temperature in Fahrenheit and prints the equivalent temperature in degrees Celsius. Write the definition of another function that takes one number, that represents speed in miles/hour and prints the equivalent speed in meters/second. Write the definition of a function named main. It takes no input, hence empty parenthesis, and does the following: - prints Enter 1 to convert Fahrenheit temperature to Celsius - prints on...
Language: Python 3 (Please structure answer as basic as possible) Write a function that involves two...
Language: Python 3 (Please structure answer as basic as possible) Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as arguments, the name of a file, myFile, and the case, which will either be “upper” or “lower”. If case is equal to “upper” the function will open the file, convert all characters on each line to upper case, write each line to a new file, named “upperCase.txt”, and return the string “Converted file to upper case.” If...
Write code in C please. #1 Write a function multiples() which will take an integer input...
Write code in C please. #1 Write a function multiples() which will take an integer input and it will print out all the multiples of this number starting from 2 but not including itself. For example, multiples(10) will print 2, 5 and multiples(100) will print 2, 4, 5, 10, 20, 25, 50 #2 Write and test a Fibonacci() function that uses a loop instead of recursion to calculate Fibonacci numbers.
Please note that I tried a screen shot and scanned, and I could not paste this...
Please note that I tried a screen shot and scanned, and I could not paste this on this site because of the browser will not allow, and I called, and was told to type this. The problem and questio Analysis of Variance by hand. The average number of purchases in three different stores are compared to determine if they are significantly different. The following summary statistics is given for each store. State the null and alternative hypothesis, calculate the F-statistics,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT