r/dailyprogrammer • u/Godspiral 3 3 • Apr 07 '17
[2017-04-07] Challenge #309 [Hard] Patterns overlap
Taken from practice problem for google code jam (which starts tonight)
Input consists of 2 strings, where:
- each string may include
*wildcard(s) *wildcards may be substituted with any string of length 0 to 4
The challenge is to return True if there exists a substitution of *s in both strings that make the 2 strings identical.
Sample:
Shakes*e
S*speareoutput:
True - 1st string can replace * with pear and 2nd string can replace * with hake
sample 2:
a*baa**ba**aa
*ca*b**a*baaccan be quickly determined false in that the first string cannot be made to end in c.
a*baa**ba**aa
*ca*b**a*baaaTrue: both strings can be made into acabaabaaa
Challenges:
bb*aaaaa*ba**
*baabb*b*aaaa
dnKeeuCCyHOnobnDYMGoXDdNWhTsaoedbPifJ*ki*wWfXjIUwqItTmGqtAItoNWpDeUnNCWgZsKWbuQxKaqemXuFXDylQubuZWhMyDsXvDSwYjui*LviGAEkyQbtR*cELfxiAbbYyJRGtcsoJZppINgJGYeZKGeWLbenBEKaoCgheYwOxLeFZJPGhTFRAjNn*
d*eeuCCyHOnobnDYMGoXDdNWhTsaoedbP*ijrwWfXjIUwqItTmGqtAItoNWpDeUnNCWgZs*WbuQxKaqemXuFXDylQubuZWhMyDsXvDSwYjuijkLviGAEkyQbtRUsncELfxiAbbYyJRG*soJZppINgJGYeZKGeWLbenBEKaoCghe*YwOxLeFZJPGhTFRAjNn
THAkZYrkUWgcTpZ*SsNQKsEnvdUveZxssEtCEQuoMqToJjMdCatMs*v*GyMlROpiIDUZyJjhwmjxFWpEwDgRLlLsJYebMSkwxEUvoDcLPLIwHY*GvoRhgcfkdsenObSjWGNYRDJAzRzavAGRoZZ*fDXIRlJkufqHDjLMJKEjLAkRRyQqTrUaWRIndSX
*THAkZYrkUWgcTpZSsNQKsEnvdUveZxssEtCEQuoMqToJjMdCatMsYa*nBvIFuGyMlROpiIDUZyJjh*FWpEwDgRLlLsJYebMSkw*oDcLPLIwHYbeBGvoRhgcfkdsenObSjWGNYRDJAzRzavAGRoZZvbEfDXIRlJkufqHDjLMJKEjLAkRRyQqTrU*aWRIndSX
jEAmXdDUtthXNLbIZFeWdiQPGEvyCEeLI**EyficABUH*YiSZRREvniDexKJSjLXMYfsw*YlbTSZBlYSecorJsWidfALQYzOdrKNrJZRdrQEDoyhPMYAfTiHZIuqGtEkKqYBzxtCOJhRYfZNSYNxRWFrfahlSLvdBTebrXDgGlZEqxRIvGhN*mfhLLSExNHaHLAZI
jEAmXdDUtthXNLbIZFeWdiQPGEvyCEeL**BUHYiSZRREvniDexKJSjLXMYfswlaYlbTSZBlYSecorJsWidfALQYzOdrKNrJZ*EDoyhPMYAfTiHZIuqGtEkKqYBzxtC*YfZNSYNxRWFrfahlSLvdBT*ebrXDgGlZEqxRIvGhNcmfhLLSExNHaHLAZI
74
Upvotes
5
u/Godspiral 3 3 Apr 08 '17 edited Apr 25 '17
in J,
1
9
u/rakkar16 Apr 07 '17
SWI-Prolog
I've put the code on Swish, and made a few examples to play around with.
I'm not very experienced with Prolog, but this sort of stuff is basically what it was built do do, so I managed to solve the problem.
5
u/[deleted] Apr 07 '17 edited Apr 07 '17
Python 3
Not the best approach for sure. Creates 5n possibilities and then performs O(n2) search. Very interested in seeing what you guys come up with. But it's working, I think.
Output:
5
u/moeghoeg Apr 10 '17 edited Apr 10 '17
Python 3 dynamic programming recursive solution. Algorithm is efficient; O(nm) where n and m are the lengths of the two words. I find this problem to be sort of similar to the edit distance problem but with a yes/no answer and slightly different operations.
The idea is to replace all '*' with '####'. The algorithm then goes through the strings beginning to end and simulates either removing or replacing '#'s with the letter on the corresponding position in the other string, backtracking when impossible strings are reached (i.e both strings have different alphabetic characters on the same position or we reach the end of one string before the other). No two letters are compared more than once since every comparison is noted in a matrix.
Tried the tests on the Google challenge page. The small set succeeded, but the large one caused maximum recursion depth, so I'll have to do one without recursion! To be continued!
EDIT: Fixed an error caused by algorithm failing because of trailing '*'s. EDIT2: When increasing the recursion depth (kind of an ugly hack, but still), I was able to solve Google's large set in 1 min, 13 seconds on my computer. Those problems definitely require a non-exponential solution. EDIT3: I made a callstack friendly version as well!
3
u/Allanon001 Apr 07 '17 edited Apr 07 '17
Python 3
Edit: Cleaned up code to make it easier to read
Code:
Output:
3
u/ericula Apr 08 '17
Python 3 using recursion.
Result for challenges:
2
u/Trebonic Apr 08 '17 edited Apr 08 '17
Racket
Gist
Passes all the tests.
Self-criticism: The expand-wildcard function is a bit of a mess. Also, it would be more efficient to track indexes than to pass around lists.
Anyone know how to avoid writing conditions in a cond twice, but with the argument order reversed? Happened two times in my code:
2
u/moeghoeg Apr 10 '17 edited Apr 11 '17
I made a callstack friendly version of my Python 3 solution, simulating recursion with a stack instead. Solves the set of large problems (pattern length <= 2000) on the Google challenge page (with different I/O) in 1 minute, 8 seconds on my computer.
2
u/eruonna Apr 12 '17
Haskell
1
u/MightExpunge Apr 08 '17
JavaScript
Hope I'm not too late to post my attempt! I'm concerned there may be edge cases my solution will fail, but it appears to work on the test inputs and other combinations I've tried. Solution is quite bloated, sorry.
Strategy:
Code: https://gist.github.com/anonymous/99c10163020f563419bc74b7e9cda609
Output:
1
u/ReverendRocky Apr 12 '17
SWI-Prolog
https://gist.github.com/RevRocky/3961ccefc76699387e2fbcb9021635e8
Perhaps a bit late on this one but seeing someone else attempt a prolog solution made me wonder if I could do my own.
Do give pointers. I've only been learning the language for ~4 weeks so I'm far from good
1
u/jacwah Apr 29 '17
Functional Rust solution. I had to add a memoizing hash set to solve sample 2 (I had to stop it after minutes). It now runs everything I throw at it in milliseconds.