Question

OpenSooq Assignment - Software Engineer Given two strings input of printable text, a and b ....

OpenSooq Assignment - Software Engineer
Given two strings input of printable text, a and b .
let the edit distance between a and b is least number of operations needed to transform a to
b, write php code that calculate edit distance using:
1. hamming distance that only have substitute operations (ex. substitute letter
X with letter Y).
2. Levenshtein distance: that have 3 possible operators: insert, delete or
substitution operations
the function should consider all possibilities but should not consider the same part twice, and
report the smallest possible distance
hamming_dis(a, b) returns integer levenshtein_dis(a,
b) returns integer
Your code should use the following:
1. OOP
2. two classes one for each mode hamming or levenshtein
2. one public static helper that creates an instance, initialise the state,
do the calculation and return the result
3. define protected/private properties as needed to hold input
strings and intermediate data that you use
5. private/protected methods that you call recursively if needed
6. those methods should take offset from the beginning of input texts
instead of passing substrings
7. document/comment your code
8. write another file that requires your file as a library and perform some tests on
it
9. write another command line tool that prompt for two input strings and
display the levenshtein distance
10. write a web page with a form of two fields and returns the
levenshteindistance, provide instructions to run it using PHP's builtin web server
from command line
example levenshtein distances a:
"this is a test"
b: "this is test"
levenshtein: 2 operations (remove a and next space)
a: "this is test" b:
"the is test"
levenshtein: 2 operations (replace i with e in this and remove s)

in python code please

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

//Import the necessary libraries
import java.util.*;
import java.util.Arrays;
//creating class
class edit_dist
{
   //Function to calculate Hamming Distance
   public static int hamming_Distance(String s1, String s2)
   {
       int count=0;
       for(int i=0;i<s1.length();i++)
       {
           if(s1.charAt(i)!=s2.charAt(i))
           {
               count++;
           }
       }
       return count;
   }
   //Function to calculate Levenshtein Distance  
   public static int levenshtein_Distance(String s1, String s2)
   {
       //initilize the lengths
       int len_s1=s1.length();
       int len_s2=s2.length();
       //Initilize the arrya of matrix for memorization
       int dp[][]=new int[len_s1+1][len_s2+1];
       for(int i=0;i<=len_s1;i++)
       {
           for(int j=0;j<=len_s2;j++)
           {
               if(i==0)
               {
                   dp[i][j]=j;
               }
               else if(j==0)
               {
                   dp[i][j]=i;
               }
               else
               {
                   dp[i][j]=minimum(dp[i-1][j-1]+(s1.charAt(i-1)==s2.charAt(j-1)?0:1),dp[i-1][j]+1, dp[i][j-1]+1);
               }
           }
       }
       return dp[len_s1][len_s2];
   }
   //Supporting Function for Levenshtein Distance
   public static int minimum(int... num)
   {
       return Arrays.stream(num).min().orElse(Integer.MAX_VALUE);
   }
   //Main Function
   public static void main(String args[])
   {
       //Hamming Distance

       String s1="hamming distance";
       String s2="hammanh distamcs";
       System.out.println("String 1:"+s1);
       System.out.println("String 2:"+s2);
       System.out.println("Hamming Distance : "+hamming_Distance(s1,s2));

       //Levenshtein Distance

       s1="this is test";
       s2="the is test";
       System.out.println("String 1:"+s1);
       System.out.println("String 2:"+s2);
       System.out.println("Levenshtein Distance : "+levenshtein_Distance(s1,s2));
   }
}

Add a comment
Know the answer?
Add Answer to:
OpenSooq Assignment - Software Engineer Given two strings input of printable text, a and b ....
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
  • Write a function rm_multiple() that takes two parameters as input (a list of strings, and a...

    Write a function rm_multiple() that takes two parameters as input (a list of strings, and a list of strings to remove from the list) and returns a new list with the items removed. # Define function in this cell def rm_multiple(strings,remove_list): l = [10,20,40,50] #use to delete an element in the list for string in strings: strings= l if string not in remove_list: lst.append(string) # Edit the code here to call your function in this cell items = [10,20,40,50] to_remove...

  • /* Write a recursive function named editDistance that accepts two string    * parameters and returns...

    /* Write a recursive function named editDistance that accepts two string    * parameters and returns the "edit distance" between the two strings as an    * integer. Edit distance (also called Levenshtein distance) is the minimum    * number of "changes" required to get from s1 to s2 or vice versa. A "change"    * is a) inserting a character,    * b) deleting a character, or    * c) changing a character to a different character.    *...

  • Implement the function hasDuplicates. Input will be given as a line containing a positive integer n,...

    Implement the function hasDuplicates. Input will be given as a line containing a positive integer n, followed by n rows, each containing a string. The output should be either the word true if there are any duplicates among these strings or false if there are not. Your code should work well on long lists of strings. Use the following set-up to write the code in JAVA import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) {...

  • 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...

  • Your assignment is to write a grade book for a teacher. The teacher has a text file, which includ...

    Your assignment is to write a grade book for a teacher. The teacher has a text file, which includes student's names, and students test grades. There are four test scores for each student. Here is an example of such a file: Count: 5 Sally 78.0 84.0 79.0 86.0 Rachel 68.0 76.0 87.0 76.0 Melba 87.0 78.0 98.0 88.0 Grace 76.0 67.0 89.0 0.0 Lisa 68.0 76.0 65.0 87.0 The first line of the file will indicate the number of students...

  • 6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you...

    6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you will practice working with arrays. Your program will read lines of text from the keyboard and use an array to track the number of times each letter occurs in the input text. You will use the contents of this array to generate a histogram (bar graph) indicating the relative frequencies of each letter entered. Test cases are available in this document. Remember, in addition...

  • Problem: Implement an interface that manipulates a list of strings. You will be provided with the...

    Problem: Implement an interface that manipulates a list of strings. You will be provided with the following files (see below): • StringList.h containing a class declaration, set up for a linked list representation. • Driver.cpp containing a main function you can use to test your implementation. You will be responsible for providing the StringList.cpp file, including the implementation of the StringList member functions (described below): StringList and ~StringList: creates an empty list, and deallocates all the nodes in the list,...

  • Java programming only Create a Java program that inputs a grade from the user. The grade input from the user will be an...

    Java programming only Create a Java program that inputs a grade from the user. The grade input from the user will be an integer. Once the input is stored, use an if-else-if block of code to determine the letter grade of the inputted integer grade. Do not use a bunch of if statements by themselves to solve this problem. You will print an error message for inputs greater than 100 and for inputs less than 0. Both errors must be...

  • Design a function named max that accepts two integer values as arguments and returns the value...

    Design a function named max that accepts two integer values as arguments and returns the value that is the greater of the two. For example, if 7 and 12 are passed as arguments to the function, the function should return 12. Use the function in a program that prompts the user to enter two integer values. The program should display the value that is the greater of the two. Need Variable lists, Psuedocode, ipo chart, Flow Chart, and a python...

  • Python 3.5 Code Part A: 10 points A tick is a short line that is used...

    Python 3.5 Code Part A: 10 points A tick is a short line that is used to mark off units of distance along a line. Write a function named drawTick() that uses a turtle parameter to draw a single tick of specified length perpendicular to the initial orientation of the turtle. The function drawTick() takes two parameters: 1. a turtle, t, that is used to draw 2. an integer, tickLen, that is the length of the tick When drawTick() is...

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
ADVERTISEMENT