22
\$\begingroup\$

In this challenge you will write a program that takes two newline-separated strings, s1 (the first line) and s2 (the second line), as input (STDIN or closest). You can assume that the length of s1 will always be smaller than 30 and bigger than the length of s2. The program should then output each step in the levenshtein distance from s1 to s2.

To clarify what each step in the levenshtein distance means, the program will print n strings, where n is the levenshtein distance between s1 and s2, and the levenshtein distance between two adjacent strings will always be one. The order doesn't matter. The output should be newline-separated and not include s1, only the in-betweens and s2. The program should also run in under one minute on a modern computer.

Examples:

Input:

Programming
Codegolf

Output:

rogramming
Cogramming
Coramming
Coamming
Codmming
Codeming
Codeging
Codegong
Codegolg
Codegolf

Input:

Questions
Answers

Output:

uestions
Aestions
Anstions
Ansions
Answons
Answens
Answers

Input:

Offline
Online

Output:

Ofline
Online

Input:

Saturday
Sunday

Output:

Sturday
Surday
Sunday

Here is a link to a python script that prints out the distance and the steps.

Additional rules:

This is code-golf so keep you code short; shortest code wins!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ For my edit, I rather presumed that the input would be of the form s1(newline)s2, however, having looked over the question again, I am wondering if instead you intended for the program to select s1 and s2 based on the length of 2 inputed strings, coming in either order, would you mind clarifying this point? That is, do we assume the input is s1 followed by s2, or do we select s1 and s2 based on the length of the two inputs? \$\endgroup\$ Jun 7, 2015 at 17:25
  • \$\begingroup\$ Does an answer have to run in a reasonable amount of time? \$\endgroup\$
    – KSab
    Jun 7, 2015 at 18:30
  • \$\begingroup\$ Camper - Ampere, distance 2, the python script runs forever ... \$\endgroup\$
    – edc65
    Jun 9, 2015 at 13:47
  • \$\begingroup\$ How strict is "take input from STDIN or closest"? Can I write a function that takes the input via function argument? The currently accepted answer does so. \$\endgroup\$
    – nimi
    Mar 5, 2016 at 23:57

2 Answers 2

10
\$\begingroup\$

Haskell, 201 194 bytes

l=length
g[]n u=map(\_->"")n
g(b:c)[]u=(u++c):g c[]u
g(b:c)n@(o:p)u|b==o=g c p(u++[o])|1<2=((u++o:c):g c p(u++[o]))!((u++c):g c n u)
a!b|l a<l b=a|1<2=b
p[a,n]=g a n""
f=interact$unlines.p.lines

Longer than expected. Maybe I can golf it down a little bit ...

Usage example:

*Main> f                     -- call via f
Questions                    -- User input
Answers                      -- no newline after second line!
uestions                     -- Output starts here
Aestions
Anstions
Ansions
Answons
Answens
Answers

It's a brute force that decides between changing and deleting if the initial characters differ.

\$\endgroup\$
4
  • \$\begingroup\$ How long does it take to run? \$\endgroup\$
    – xenia
    Jun 9, 2015 at 5:12
  • \$\begingroup\$ How can I test (maybe ideone)? \$\endgroup\$
    – edc65
    Jun 9, 2015 at 13:49
  • \$\begingroup\$ @Loovjo: shorter strings like your examples are calculated instantly, worst case is about 1:30min. I've interpreted the "should" in "should run in under one minute" not as a strict limit (should vs. must). If this is a must, I can add a "performance pack" for about 20 bytes. \$\endgroup\$
    – nimi
    Jun 9, 2015 at 15:09
  • \$\begingroup\$ @edc65: yes, ideone, but it expects the function to be executed to be called "main". Try: ideone.com/CUgU8W \$\endgroup\$
    – nimi
    Jun 9, 2015 at 15:21
1
\$\begingroup\$

APL (Dyalog 18.0), 66 bytes

{⍺<Ö≢⍵:⍺∇⎕←0~⍨0@(⊃(⍸~p)~⍳⊃⌽⍸p←⍵=⍺↑⍨≢⍵)⊢⍵⋄j←⊃⍸⍺≠⍵⋄⍺≢⍵:⍺∇⎕←⍺[j]@j⊢⍵}

Try it online!

Works correctly now.

A recursive function submission, done with a lot of input from Adám and Bubbler.

Input is taken as <s2> f <s1>.

Explanation

⍺<⍥≢⍵: if the strings have different lengths:

0@(...)⊢⍵ put a zero at the following index:

first element of:

(⍸~p) characters which do not match

~ without

⍳⊃⌽⍸p←⍵=⍺↑⍨≢⍵ all indices before last character match

0~⍨ remove the zero(thereby removing the element)

⍺∇⎕← display the result and recurse

⋄⍺≢⍵: Otherwise, till the strings match:

i←⊃⍸⍺≠⍵ find first non-matching element

⍺[i]@i⊢⍵ and replace the element at that index

⍺∇⎕← display the result, and recurse

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.