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

In python. Write a program that takes 2 string inputs and calculates the Hamming Distance. Hamming...
In python. Write a program that takes 2 string inputs and calculates the Hamming Distance. Hamming distance between two strings is the number of positions at which the corresponding symbols are different. The program should output an integer representing this distance. For example a = XXWWZZ b = ZZWWXX answer = 4 More examples: "Phone" and "PHOONE" = 3 "God" and "Dog" = 2 "Dog" and "House" = 4
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....
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2...
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2 of the same length n. What is the complexity of your algorithm?
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2...
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2 of the same length n. What is the complexity of your algorithm?
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,...
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...
USE Python 2.7(screen shot program with output) the task is: takes in a list of protein...
USE Python 2.7(screen shot program with output) the task is: takes in a list of protein sequences as input and finds all the transmembrane domains and returns them in a list for each sequence in the list with given nonpolar regions and returns the lists for those. 1. This code should call two other functions that you write: regionProteinFind takes in a protein sequence and should return a list of 10 amino acid windows, if the sequence is less than...
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...
* 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT