r/dailyprogrammer • u/Cosmologicon 2 3 • Aug 22 '18
[2018-08-22] Challenge #366 [Intermediate] Word funnel 2
Challenge
A word funnel is a series of words formed by removing one letter at a time from a starting word, keeping the remaining letters in order. For the purpose of this challenge, a word is defined as an entry in the enable1 word list. An example of a word funnel is:
gnash => gash => ash => ahThis word funnel has length 4, because there are 4 words in it.
Given a word, determine the length of the longest word funnel that it starts. You may optionally also return the funnel itself (or any funnel tied for the longest, in the case of a tie).
Examples
funnel2("gnash") => 4
funnel2("princesses") => 9
funnel2("turntables") => 5
funnel2("implosive") => 1
funnel2("programmer") => 2Optional bonus 1
Find the one word in the word list that starts a funnel of length 10.
Optional bonus 2
For this bonus, you are allowed to remove more than one letter in a single step of the word funnel. For instance, you may step from sideboard to sidebar by removing the o and the final d in a single step. With this modified rule, it's possible to get a funnel of length 12:
preformationists =>
preformationist =>
preformations =>
reformations =>
reformation =>
formation =>
oration =>
ration =>
ratio =>
rato =>
rat =>
atpreformationists is one of six words that begin a modified funnel of length 12. Find the other five words.
Acknowledgement
Thanks to u/duetosymmetry for posting today's challenge on r/dailyprogrammer_ideas!
6
u/skeeto -9 8 Aug 22 '18
C with bonus 1. Finds the longest funnel in about 120ms.
2
u/brickses Aug 22 '18
Why are length 1 words not included in the word list? I would have expected 'ah' or 'at' to reduce to 'a'
3
u/SP_Man Aug 22 '18
Python with bonus 1. Runs in about 200ms.
Output:
3
u/raevnos Aug 22 '18 edited Aug 22 '18
Scheme, with the first bonus (EDIT: Now with faster version of bonus 1)
3
u/marucOG Aug 23 '18
Python 3
Bonus 1
Edit: formatting
3
u/popillol Aug 23 '18 edited Aug 23 '18
Go / Golang does bonus 1 in about
330ms(Edit: got down to ~80ms by adding a very simple check to make sure the length of the word was long enough to have n=10 possible funnels). I built upon my original Trie from the funnel 1 challenge. This actually will return all words with a funnel length of n, so it doesn't stop at the first instance.Still have to think some more about bonus 2 since apparently my recurse function isn't working when n != 1
Output
3
u/mattiadr96 Aug 24 '18
Python 3 with bonus 2
3
u/xXZoulocKXx Aug 25 '18 edited Aug 26 '18
Haskell version with 2 bonuses. It is pretty slow (no memoization used, 1m 32s to find bonus1).
Edit: typo
3
u/zatoichi49 Aug 26 '18 edited Aug 28 '18
Method:
Uses the same dictionary split and find_subwords() function as the previous challenge. To find the longest funnel, add the word to a list and apply the find_subwords() function to each item. Extend the list with the returned subwords. Continue iterating through the list until no more valid words have been added. To calculate the funnel length, take the length of the original word and subtract the length of the word at index -1 (shortest word), adding one to account for the length of the original word. For bonus 1, repeat the above for all words in the dictionary, returning any words with a funnel length of 10.
Python 3 (with Bonus 1):
Output:
3
u/r_jet Oct 07 '18
Java, no bonus
Test, using the sample data
2
u/[deleted] Aug 22 '18
Python 3: My first stab at a recursive function. Some feedback would be nice. I think it is only currently working because finding the longest funnel takes the longest time but not sure.
Output: 4, 9, 5, 1, 2
2
u/zqvt Aug 22 '18 edited Aug 23 '18
Clojure, Bonus1
2
u/jnordwick Aug 22 '18
Quick idea:
You can build the reduction graph on dictionary load: sort list by length, add all words of length 1 with a reduction count of 1, them add reach for of length N with their reduction count being 1 if they cannot be reduced (ie there is no N-1 word in the dictionary that is a legal reduction) or 1 + the largest legal reduction.
Now queries are O(length of query), but construction time is now O(length of dictionary * average length of word). Another drawback is bonus #2 has to build it's own table.
If you want to optimize for startup time instead of online worries, do a BFS through the graph of reductions instead?
2
u/zilmus Aug 23 '18
Python 3 with Bonus 1. Right output in 650ms.
Bonus 2 isn't finished. For this part I used another aproach: Build a graph-like structure and use it with recursion to get the funnel.
2
u/octolanceae Aug 23 '18
C++17
Challenge and Bonus 1. Bonus 1 completes in ~80ms
Output:
2
u/5900 Aug 26 '18
Haskell
Solves both bonuses. It's really slow and super complicated due to caching/monads. I took inspiration from mattiadr's Python solution. Bonus 2 took ~211 seconds to run.
2
u/tetrapod_thief Oct 13 '18
First time posting.
Python 3:
2
u/MustardScroll7 Oct 22 '18
Maybe it's just me, but I find the problem wording confusing. Could someone please help me make sense of this problem? What exactly are we doing?
2
u/Mr_Persons Oct 30 '18
Haskell
2
u/quackyrabbit Nov 07 '18 edited Nov 08 '18
Here's a solution in Racket
2
u/dustinroepsch Dec 29 '18
Python 3
1
u/mr_stivo Aug 22 '18
perl with examples, bonus 1 and bonus 2
I'm happy you guys are back with some new challenges! Thank you!
Code:
Examples:
Optional bonus 1:
Optional bonus 2:
1
u/chunes 1 2 Aug 22 '18 edited Aug 22 '18
Factor with bonus 1.
Output:
1
u/alkasm Aug 22 '18
Just FYI to those reading, I had a very very similar question for one of my onsites at Google. Great question to practice.
1
u/baskets209 Aug 22 '18
F#, Bonus1
1
u/DerpinDementia Aug 22 '18 edited Aug 22 '18
Python 3.5 Challenge
My code makes sue of memoization by saving the funnels generated to save time on executing funnel2() on similar words. This also helps with the bonus. In case of ties, I only return the alphabetically first funnel.
Bonus 1
This is where my ladder dictionary comes in handy. To find the longest funnel, it takes roughly 1.3 seconds (assuming I didn't know before hand the longest funnel is 10 words long).
Bonus 2
Coming soon...
All feedback welcome!
1
u/Scroph 0 0 Aug 22 '18 edited Aug 22 '18
D (dlang) solution with the first bonus. Much like a machine learning algorithm, I just kept toying with it until it started giving the correct results :
Output :
1
u/TheUpriseConvention Aug 23 '18
Python 3.6
Time taken searching for all words (not including dictionary population): ~200μs
1
u/menschmichi Sep 09 '18
NodeJS with Bonus 1: (needs 45 seconds for Bonus 1)
1
u/timbar1234 Sep 11 '18 edited Sep 11 '18
C# with elements of bonus 2
Bonus 2 is a tricky one; this isn't a complete solution as the "reach" of each recursive search is set in code. If you allow it to remove up to 2 chars then it gets two of the 12-word funnel set:
in ~10s, but time increases rapidly as you bump up that reach. It looks like it needs some caching of some kind, but I've not managed a working solution yet. Any hints welcome - it feels like I want to build a lookup cache as soon as I have a complete funnel, by recursively taking the head of the funnel and mapping it to the remainder.
1
u/containswater646 Sep 12 '18 edited Sep 12 '18
Python 3:
1
u/DEN0MINAT0R Sep 16 '18
C++ (No Bonus)
Output
1
u/Dominic11112 Oct 08 '18
MATLAB
I am already way too slow with Bonus 1, so I am not even going to try to do Bonus 2...
main:
funnel2:
bonus1:
Output:
1
u/Grygon Oct 24 '18
Haven't posted one in a while, here's a recursive Python solution I'm pretty happy with. No bonus:
Python3
1
u/OscarGlo Nov 24 '18 edited Nov 24 '18
JavaScript
Although let's say the bonus doesn't really work as it is very ressource intensive
1
u/Orothrim Nov 25 '18
C++ with bonus 1:
Getting Complecting as the bonus 1 answer
1
u/2kofawsome Feb 12 '19 edited Feb 12 '19
! python3.6
Bonus 1
Output:
Took 618.8277261257172 seconds
1
u/arthurelamothe Aug 23 '18 edited Aug 23 '18
C++
Output
gnash 4 ==> ash ==> gash ==> gnash ==> sh
princesses 8 ==> ice ==> ices ==> prices ==> princes ==> princess ==> princesse ==> princesses ==> rices
turntables 5 ==> tunable ==> turnable ==> turntable ==> turntables ==> unable
implosive 1 ==> implosive
programmer 2 ==> programer ==> programmer
complecting 10 ==> competing ==> comping ==> complecting ==> completing ==> compting ==> coping ==> oping ==> pi ==> pig ==> ping
The word producing a funnel length of 10 is complecting
0
u/SadCombination7 Aug 22 '18 edited Aug 22 '18
#include <vector>
#include <string>
#include <unordered_set>
#include <fstream>
#include <iostream>
using namespace std;
vector<vector<string>> GenerateWordFunnell(string s, const unordered_set<string>& words){
vector<vector<string>> word_funnell;
for(int i=0; i<s.size(); ++i){
string without_char_i = s.substr(0, i) + s.substr(i+1);
if(words.find(without_char_i) != words.end()){
vector<vector<string>> v = GenerateWordFunnell(without_char_i, words);
for(int j=0; j<v.size(); ++j){
v[j].push_back(s);
word_funnell.push_back(v[j]);
}
}
}
if(word_funnell.empty())
return {{s}};
else
return word_funnell;
}
void PrintVec(vector<vector<string>> v){
for(int i=0; i<v.size(); ++i){
for(int j=0; j<v[i].size()-1; ++j){
cout << v[i][j] << " -> ";
}
if(!v[i].empty())
cout << v[i].back();
cout << "\n";
}
}
int main(){
ifstream file("words.txt");
unordered_set<string> words;
string word;
while(file >> word){
words.insert(word);
}
unordered_set<string> longest_funnels;
int longest_funnell = 0;
for(string word: words){
auto v = GenerateWordFunnell(word, words);
for(int i=0; i<v.size(); ++i){
if(v[i].size() > longest_funnell){
longest_funnell = v[i].size();
longest_funnels = {word};
}
else if(v[i].size() == longest_funnell){
longest_funnels.insert(word);
}
}
}
cout << "Longest Funnell size: " << longest_funnell << "\n";
for(string funnell: longest_funnels){
PrintVec(GenerateWordFunnell(funnell, words));
cout << "\n";
}
}
0
u/[deleted] Dec 22 '18
[deleted] — view deleted comment