r/dailyprogrammer • u/jnazario 2 0 • Aug 24 '16
[2016-08-24] Challenge #280 [Intermediate] Anagram Maker
Description
Anagrams, where you take the letters from one or more words and rearrange them to spell something else, are a fun word game.
In this challenge you'll be asked to create anagrams from specific inputs. You should ignore capitalization as needed, and use only English language words. Note that because there are so many possibilities, there are no "right" answers so long as they're valid English language words and proper anagrams.
Example Input
First you'll be given an integer on a single line, this tells you how many lines to read. Then you'll be given a word (or words) on N lines to make anagrams for. Example:
1
Field of dreams
Example Output
Your program should emit the original word and one or more anagrams it developed. Example:
Field of dreams -> Dads Offer Lime
Field of dreams -> Deaf Fold Miser
Challenge Input
6
Desperate
Redditor
Dailyprogrammer
Sam likes to swim
The Morse Code
Help, someone stole my purse
English Wordlist
Feel free to use the venerable http://norvig.com/ngrams/enable1.txt
8
u/dekx Aug 24 '16
In the list of characters, are spaces significant.... such that input is 3 words, output must also be 3 words?
3
u/DemiPixel Aug 24 '16
I presume we need a list of English words to complete this. Maybe provide a link to an alphabetical one?
2
u/wral Aug 24 '16 edited Aug 24 '16
I need a bit of explaining. Is printing just one anagram considered solution? (even if more are possible)
And are words considered separate? For example if I have word Code, is
a valid solution? Or I cannot print 2nd word because I already used up the letters?
4
u/ZebbRa Aug 24 '16 edited Aug 25 '16
Python 3 - I couldn't figure out how to enumerate all the possible partitions of the string, so I generated random partitions until I found one that works. Suggestions Welcome!
Output:
3
u/DemiPixel Aug 24 '16 edited Aug 25 '16
Javascript
Warning:I did not exactly follow the input instructions (for the lines). I hope you'll forgive me.Now I have.There are three commented lines in parse. If you remove this, it will instead return an array of ALL anagrams instead of the first
The output:
Only the very end of the ouput for "all anagrams" (it is very long...):
So what did I do to speed up this brute force? Spoilers...?
{h:{e:{l:{l:{o:{is:true}}}}}}(obviously for all letters for all words).SHIT GOT TO GET TO SCHOOL I'M LATE I'LL EDIT LATER["a", "p", "p", "r"]and"p"takes a long time, I'm only doing that once3
u/skeeto -9 8 Aug 24 '16
C99. It converts the input into a table of character counts. The word shuffle in the middle is less efficient than strictly necessary and I could instead shuffle a smaller array of indices. But it's so fast it doesn't matter.
Here's example output. Unfortunately enable1.txt contains uninteresting stuff like "mm".
3
u/wral Aug 24 '16 edited Aug 24 '16
C++
output:
I tried to avoid printing trash like "mm" and "aa", so it is searching starting from the longest words.
I hope third line qualifies LOL
EDIT: and for this code to work you need to convert enable1.txt to unix-like text file (no crlfs...)
3
u/adrian17 1 4 Aug 24 '16 edited Aug 24 '16
That took a while... I wanted to generate all anagrams as fast as possible, so I messed around with it a bit. It started with very nice but slow Python (link), then I moved to C++, for a while it stayed pretty neat(link), and slowly turned into the abomination below.
At least it's pretty fast... I think. I have nothing to compare to :)
I took the approach of storing a character count table and using them for comparisons. Turns out, vectorisation with SSE helps a lot here.
Also, I don't show "duplicates", so from the sentences "field of dreams", "dreams field of" etc only one will appear.
2
u/iWriteC0de Aug 25 '16
C#. Tries random size keys until it gets a hit. Easier to just randomise the partition sizes rather than brute force using recursion/nested loops to calculate them.
3
u/fvandepitte 0 0 Aug 24 '16
D*mn bruteforcing this could take a while...
I'll give it a shot later on. Nice challenge
1
u/gabyjunior 1 2 Aug 25 '16
C, for each word/sentence in the input it will display the first solution found and total number of solutions, as well as the search space size.
It takes as arguments the path to the dictionary file and the minimal length of words generated in the anagrams (to avoid the use of meaningless words, filtering short words will also speed up the search).
A trie is built from the dictionary file provided. Each input is reformatted (all non letters are trimmed, remaining characters are converted to lowercase and sorted). For example "The Morse Code" becomes "cdeeehmoorst". As the letters are also sorted in each node of the trie we will be able to choose letters in one single loop at each step.
Source code
Output for challenge (5 first sentences with minimal length = 4)
Last sentence of challenge (minimal length = 7)
1
u/thorwing Aug 25 '16 edited Aug 25 '16
Java 8
Version 1: brute force, might update later. So theoretically, this should be working pretty fast because of the parallism; problem is, parallism is everything but random, which is what I wanted to achieve. For the word; "Daillyprogrammer" It takes a very long time because apparantly it's first doing all the words that start with any letters, than does "fie" and then any other random combination. This way, because it's trying a certain type of combination a lot, which is inherently wrong, it's taking some time with some words.
Anyways, this is the code.
Version 2: This one works almost instantly, but tries randomly, which I dislike: solution is given below;
with outputs:
1
u/Scroph 0 0 Aug 26 '16 edited Aug 26 '16
D (dlang) bruteforce solution. It basically tries all possible combos of the words until it finds one that is an anagram (permutation) of the given string. In order to speed it up, I only picked words whose characters are all present in the string, so for example we'll pick "rate" for "desperate", but not "aspirate". My reasoning for this is that "rate" can later be combined with "despe" (just an example) to give "desperate", but "aspirate" can never be combined with another word to give "desperate", so I discard it completely.
It is possible to optimize it further by checking not only for the presence of the potential dictionary entry's letters in the input string, but also by seeing if there are no duplicates. For example, right now "aspartate" is considered to be a valid candidate because all its letters can be found in "desperate". However, the algorithm doesn't take into consideration the fact that "aspartate" has 2 a's in it while "desperate" only has one.
It ended up being pretty fast :
The solve function can definitely be written in a more elegant way (with the help of std.algorithm.setopts.cartesianProduct maybe ?) but for now, I'll just leave it as it is.
Output :
1
u/thtoeo Aug 28 '16 edited Aug 28 '16
C#
1
u/evmorov Aug 28 '16 edited Aug 28 '16
Ruby
It's the first my submission. Suggestions are welcome!
Sadly it's rather slow. I think I choose the wrong direction in the solving this problem. The 'Challenge Input' takes 6.7 seconds.
Gist:
Solution:
Output of the runner:
1
u/stewartj95 Aug 28 '16
1
u/zandekar Aug 29 '16 edited Aug 29 '16
Haskell
1
u/mikeymike619 Sep 02 '16
Here is my Java solution, still a newbie.
public class Intermediate_280 {
}
1
u/den510 Sep 09 '16 edited Sep 09 '16
Python 3.5 I enjoyed this one, a good exercise in recursion. Program runs swiftly.
from my personal libs
All in all, not too hard. It was just tedious to code. [edit] placed more relevant code at top.
1
u/[deleted] Aug 25 '16
[removed] — view removed comment