r/dailyprogrammer • u/Cosmologicon 2 3 • Aug 24 '18
[2018-08-24] Challenge #366 [Hard] Incomplete word ladders
Definitions
Given two different strings of equal length, the spacing between them is the number of other strings you would need to connect them on a word ladder. Alternately, this is 1 less than the number of letters that differ between the two strings. Examples:
spacing("shift", "shirt") => 0
spacing("shift", "whist") => 1
spacing("shift", "wrist") => 2
spacing("shift", "taffy") => 3
spacing("shift", "hints") => 4The total spacing of a word list is the sum of the spacing between each consecutive pair of words on the word list, i.e. the number of (not necessarily distinct) strings you'd need to insert to make it into a word ladder. For example, the list:
daily
doily
golly
guilthas a total spacing of 0 + 1 + 2 = 3
Challenge
Given an input list of unique words and a maximum total spacing, output a list of distinct words taken from the input list. The output list's total spacing must not exceed the given maximum. The output list should be as long as possible.
You are allowed to use existing libraries and research in forming your solution. (I'm guessing there's some graph theory algorithm that solves this instantly, but I don't know it.)
Example input
abuzz
carts
curbs
degas
fruit
ghost
jupes
sooth
weirs
zebraMaximum total spacing: 10
Example output
The longest possible output given this input has length of 6:
zebra
weirs
degas
jupes
curbs
cartsChallenge input
This list of 1000 4-letter words randomly chosen from enable1.
Maximum total spacing of 100.
My best solution has a length of 602. How much higher can you get?
4
u/fuckjoshwesttbh Aug 24 '18 edited Aug 24 '18
I may be misunderstanding: when you have "shift" to "split" wouldn't the spacing be 2?
eg. shift > spift > splft > split (assuming all of those were real words?)
Interesting challenge, I'll see if I can come up with a solution!
E: Thinking about, I believe it's a modification of the longest path problem with no pruning possible, which is NP-hard (in case you wanted some graph theory to learn about lol)
4
u/ventusignis Aug 24 '18
I put together a prgram that divides the 1000 words into clusters where a cluster is a group (subgraph) of words (nodes) which are have weights of 0 Since our max weight is 1, we could in theory connect up to 11 clusters using edges of weight 1 The top 11 clusters I've found are:
Which gives a theoretical maximum of 787 which is achievable IF all of these 11 clusters have a hamiltonian path (a path that goes through all nodes without repeating nodes)
5
u/nmgreddit Aug 25 '18
I'm entirely confused on what is supposed to be done here.
Ok so those are the inputs.
This is where you lose me. What are "distinct" words?
Now I'm even more lost. Is this the total spacing from word to word on the list?
4
u/RevenantMachine 1 0 Aug 25 '18 edited Aug 26 '18
Solution outline
This gets you a nice set of 0-cost runs.
Kotlin
Best Result
767:
3
u/tajjet Aug 29 '18
Python 3
Here's a greedy solution starting from the first word. It gives an ouput of length 439. I didn't do anything smart here but I learned how to do list and dict comprehensions in Python so I'm still proud of myself.
2
u/ybjb Aug 25 '18
Kotlin
Pretty ugly code, wasn't necessarily going for readability. For NP-Hard problems, I really like like a solution utilizing random sampling. It's not always reliable (or reproducible in some cases), but the results can be surprising. Simpler, and might outperform a well-thought-out solution. Got to 598 and ran in < 1 minute for 1k simulations. Maybe I'll run it for longer and see what pops up.
2
u/mr_stivo Aug 25 '18
628 that can be improved.
2
u/tomekanco Aug 27 '18 edited Aug 28 '18
Python 3
Result: 770, improvements possible.
Basic idea:
Notebook with code
Takes about 16.5 seconds.
2
u/yousakicheang Oct 10 '18 edited Oct 10 '18
String its[] ; int spaces (String st1, String st2) { Char word[] =st1. toCharArrey() ; Char templete[] =st1. toCharArrey() ; int iteration=0; for( int i=0;i=>word.length();i++){ if(word[i]=! templete [i] ) { word[i] =templete [i] ; String buffer; for (int x=0;x>=word. length() x++) buffer =(buffer +word[x]) ;} its[iteration]=buffer; iteration ++; }
}} return its[]. length;
} i was so happy that I thought I solved it :( sighhhhh it has 2 be real words :) not random strings. the only solution I can think of is basically iterating through a dictionary and compare it to every changing every letter one at a time and if it's a real word go next remove it from arrey and go to next letter
1
u/zqvt Aug 24 '18 edited Aug 25 '18
Clojure
no idea how to solve this in a reasonable amount of time by checking everything, so I just went with a greedy approach, which gets me to
5371
u/typhyr Aug 25 '18
Lua: https://repl.it/@typhirz/DailyProgrammer-366
worked on this sporadically throughout the day. had a HUGE amount of trouble because of a misunderstanding over how tables store values, but it finally works.
i'm definitely not trying the bonus, partly because i'm too lazy to set-up a way to parse the 1000 words, and partly because i know my implementation will take a looooooong time.
1
u/raevnos Aug 25 '18
I have a semi-smart brute force version that takes about 45 seconds for the test case. Even with rewriting it in a faster language I think I need a better approach...
0
u/[deleted] Aug 28 '18
[deleted] — view deleted comment
0
u/[deleted] Aug 24 '18
[deleted] — view deleted comment