Haskell Program for Levenshtein distance [closed] - haskell

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I need a program in Haskell that computes the Levenshtein distance.

You need to calculate the Levenshtein distance (also called the edit distance) which is defined as follow for strings a and b: (taken from Wikipedia):
Because the values of lev(i,j) only depends on previous values, we can take advantage of Haskell's lazyness to intialize an Array where the value of element at position (i, j) is a function of previous values of the same array! See Dynamic programming example to see examples of how this can be done.
Here goes an basic implementation for lev:
import Data.Array
lev :: String -> String -> Int
lev x y = c ! (m, n)
where
c = listArray ((0, 0), (m, n)) [compute i j | i <- [0 .. m], j <- [0 .. n]]
compute 0 j = j
compute i 0 = i
compute i j
| x !! (i - 1) == y !! (j - 1) = c ! (i - 1, j - 1)
| otherwise = 1 + (minimum $ map (c !) [(i , j - 1),
(i - 1, j),
(i - 1, j - 1)])
m = length x
n = length y
This code can be further optimized, but should give you a good idea to get started.
After computing the Levenshtein, you just need to compare it with the edit cost bound k.

Related

L={a^nb^m , m%n=0} is a context-free language? [closed]

Closed. This question is not about programming or software development. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 days ago.
Improve this question
I tried using context-free pumping lemma but i can't find a string that works, i also tried to build a PDA but it also didn't work.
using the pumping lemma definition s=uvwxy
i tried with the string a^n a b^n^2 bb
in the case vx is composed only by "a" with i=2 (uvvwxxy)
in the case vx is composed only by "b" with i=0 (uwy)
but i can't find a working case for vx composed by both "a" and "b" (example vx="abb")
In conclusion i think that L is recursive and not context-free but i can't prove it
One way to see this is to construct a context-free grammar that generates the language. Here's an example of such a grammar:
S -> aSBB | ε
B -> bB | ε
The nonterminal symbol S generates a sequence of a's, followed by any number of pairs of b's. The nonterminal symbol B generates any number of b's, but only in multiples of n, since S can only generate a sequence of a's of length n.
Proof using the pumping lemma for context-free languages:
Assume L is context-free. Then, there exists a pumping length p such that any string s in L with |s| >= p can be written as s = uvwxy, where |vwx| <= p, |vx| >= 1, and for all i >= 0, uv^iwx^iy is in L.
Consider the string s = a^pb^p. Since p is the pumping length, we can write s as s = uvwxy, where |vwx| <= p and |vx| >= 1. There are three cases to consider:
vx contains only a's: In this case, we can pump up vx by setting i = 2. This gives us a^(p + |vx|)b^p, which is not in L since (p + |vx|) % n != 0.
vx contains only b's: In this case, we can pump down vx by setting i = 0. This gives us a^p(b^p - |vx|), which is not in L since (b^p - |vx|) % n != 0.
vx contains both a's and b's: In this case, we can pump up the a's in vx and pump down the b's in vx simultaneously by setting i = 2. This gives us a^(p + k)b^(p - l), where k > 0 and l > 0. Since p % n = 0, we have (p + k) % n = 0 if and only if k % n = 0. Similarly, (p - l) % n = 0 if and only if l % n = 0. Thus, we need both k and l to be divisible by n, which is impossible since k + l = |vx| <= p < n.
Therefore, we have a contradiction in all cases. Hence, L is not a context-free language.

How to calculate a polynomial equation in haskell [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Basically I need to calculate a polynomial in haskell based on a value x, and the values of the coeficients need to be stored in a list of tupples.
So for example the polynomial f(x) = a·xn + a1·xn−1 + ... + an−1·x + an will be represented in a list of tuples like f = [(a0, n), (a1, n-1), ... , (an-1, 1), (an, 0)] , so if I want to calculate
2*x^2 + 3*x + 3 for x=20 I will need the list [(2,2), (3,1) , (0,3)].
Thanks a lot in advance and sorry if I explained this exercise in a messy way :)
If you want a basic solution, try using explicit recursion:
evaluate :: [(Int, Int)] -> Int -> Int
evaluate [] _x = .... -- TODO (1)
evaluate ((a,n) : rest) x = .... -- TODO (2)
where
result = evaluate rest x
Above, in (1) we need to specify what is the result of evaluating an "empty" polynomial (with no coefficients at all). This is the base case of our recursion.
Instead, (2) is the recursive step. Here, we split the coefficients-pairs into the first (a,n), and the rest of the list rest. We then recursively define result = evaluate rest x to evaluate the polynomial "without the first coefficient", that is a1·xn−1 + ... + an−1·x + an.
Then, in line (2) we need to combine this result with the first monomial, evaluated in x.
You should now be able to fill the dots.
Easy: just fold over the list. The accumulator function should add the current term (solved for x) to the accumulate. The seed needs to be 0, of course.
something like this
> peval p x = foldr (\(a,n) s -> s+(a*x^n)) 0 p
> poly = peval [(2,2),(3,1),(3,0)]
> map poly [0..5]
[3,8,17,30,47,68]

Inverse of a pow(a,b,n) function in python decryption code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
I am working on a python decryption code using an encryption code which is already available.
In the encrytpion code, I have
pow (b, xyz, abc)
A number gets encrypted and is passed onto an array.
Now while decrypting, i need to get the value of "b" (from the pow function above) as i have the value in Array.
Using modulus gives the values in range and not the exact value and that is needed for my decryption logic to work.
How to continue with this?
First you factorise 928108726777524737. It has 2 prime factors, call them P and Q.
Then you need to find a value d such that d * 65539 mod (P-1)(Q-1) == 1 (use the Extended Euclidian Algorithm for this).
Once you have done that then given c = pow (b, 65539, 928108726777524737) you can calculate back to b with pow(c, d, 928108726777524737)
To help you a bit more P=948712711, Q=978282167 giving d=872653594828486879
>>> c = pow(99, 65539, 928108726777524737)
>>> pow(c, 872653594828486879, 928108726777524737)
99
Of course in real life you would start with the prime factors and make them a lot larger than this in which case it would be impractical to reverse the process without already knowing the factors. For small values such as this is it is easy to factorise and calculate the inverse.
Calculation of d:
def egcd(a, b):
x,y, u,v = 0,1, 1,0
while a != 0:
q, r = b//a, b%a
m, n = x-u*q, y-v*q
b,a, x,y, u,v = a,r, u,v, m,n
gcd = b
return gcd, x, y
First find the prime factors:
>>> P, Q = 948712711, 978282167
>>> P*Q
928108726777524737
>>> egcd(65539, (P-1)*(Q-1))
(1, -55455130022042981, 3916)
We want the middle value x:
>>> gcd, x, y = egcd(65539, (P-1)*(Q-1))
But we have to make it positive which we can do by adding on the (P-1)*(Q-1) value:
>>> x + (P-1)*(Q-1)
872653594828486879

The best way to map correlation matrix from [-1, 1] space to [0, 1] space [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
SO is warning me my question is likely to be closed, I hope they're wrong :)
My question: let you have a correlation matrix; you would like correlations which are next to 1 and -1 go towards 1, while those next to 0 stay there.
The simplest way is to use absolute values, e.g. if Rho is you correlation matrix then you will use abs(Rho).
Is there any way which is theoretically more correct than the one above?
As an example: what if I use Normal p.d.f. instead of absolute value?
Adjusted Rho = N(Rho, mu = 0, sigma = stdev(Rho))
where N is the Normal p.d.f. function.
Have you any better way?
What are strengths and weaknesses of each method?
Thanks,
Try this.
x <- runif(min = -1, max = 1, n = 100)
tr <- (x - min(x))/diff(range(x))
plot(x)
points(tr, col = "red")
You could also use a logit link function that guarantees the values to be between 0 and 1. But given that you're limited to values between -1 and 1, you would get only values in the range of ~[0.3, 1].

Heavy tail distribution - Weibull [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I know that the Weibull distribution exhibits subexponential heavy-tailed behavior when the shape parameter is < 1. I need to demonstrate this using the limit definition of a heavy tailed distribution:
for all
How do I incorporate the cumulative distribution function (CDF) or any other equation characteristic of the Weibull distribution to prove that this limit holds?
The CDF of the Weibull distribution is 1 - exp(-(x/lambda)^k) = P(X <= x).
So
P(X > x) = 1 - CDF = exp(-(x/lambda)^k),
and
lim exp(lambda * x) * P(X > x) = lim exp(lambda x) * exp( - (x/lambda)^k)
= lim exp(lambda x - x^k/lambda^k)
Since k<1, and x is large, and lambda>0, lambda x grows large faster than x^k/lambda^k (the monomial with the greater exponent wins). In other words, the lambda x term dominates the x^k/lambda^k term. So lambda x - x^k/lambda^k is large and positive.
Thus, the limit goes to infinity.

Resources