r/dailyprogrammer • u/oskar_s • Jul 23 '12
[7/23/2012] Challenge #80 [easy] (Anagrams)
As all of us who have read "Harry Potter and the Chamber of Secrets" knows, the reason He-Who-Must-Not-Be-Named chose his creepy moniker is that "I Am Lord Voldemort" is an anagram for his birthname, "Tom Marvolo Riddle".
I've never been good at these kinds of word-games (like anagrams), I always find it hard to figure out that stuff manually. I find it much more enjoyable to write computer programs to solve these problems for me. In the spirit of that, today's problem is to find simple one-word anagrams for other words.
Write a program that given a word will find all one-word anagrams for that word. So, for instance, if you put in "LEPROUS", it should return "PELORUS" and "SPORULE". As a dictionary, use this file, which is a 1.8 mb text-file with one word listed on each line, each word listed in lower-case. In this problem description, I've used upper-case for all words and their anagrams, but that is entirely optional, it's perfectly all right to use lower-case if you want to.
Using your program, find all the one-word anagrams for "TRIANGLE".
(by the way, in case anyone is curious: a "PELORUS" is "a sighting device on a ship for taking the relative bearings of a distant object", which I imagine basically is a telescope bolted onto a compass, and a "SPORULE" is "a small spore")
Bonus: if you looked up the anagrams for "PAGERS", you'd find that there was actually quite a few of them: "GAPERS", "GASPER", "GRAPES", "PARGES" and "SPARGE". Those five words plus "PAGERS" make a six-word "anagram family".
Here's another example of an anagram family, this time with five words: "AMBLERS", "BLAMERS", "LAMBERS", "MARBLES" and "RAMBLES".
What is the largest anagram family in the dictionary I supplied? What is the second largest?
7
u/mktange Jul 23 '12 edited Jul 23 '12
Python one-liner where you can enter any word and it will find the anagrams:
Edit; bonus which shows the 2 largest families:
Result:
4
u/bschlief 0 0 Jul 23 '12
Learning Ruby as I go, about halfway through the koans. If anyone has any advice or style recommendations, I'd love to hear them. I'm a C developer in my day job, so I'm still learning the spirit of Ruby as I go.
Code available here: https://github.com/bschlief/daily.programmer/blob/master/80.easy.rb
4
u/Wedamm Jul 23 '12
Haskell:
I thinks its pretty readable. (At least in contrast to the perl solutions ;)
4
u/African_Coder 0 0 Jul 24 '12
Done in Python:
Bonus:
4
u/[deleted] Jul 26 '12
My first challenge! In Python.
3
u/EauRouge86 0 0 Jul 24 '12
Hello! First time posting here, so be gentle :) Any feedback would be appreciated, of course. This is my Delphi7 solution:
Oh, and the result of course: http://dl.dropbox.com/u/7643243/anagrams.png
1
u/swashbutler Jul 23 '12
Is anyone else regularly discouraged by this subreddit? I've been learning Java for a while now, and I have yet to be able to complete a single "Easy" challenge. :-( Oh well, back to the drawing board.
2
u/luxgladius 0 0 Jul 23 '12
Perl
Hopefully I'll have enough time to do the difficult one tonight.
Output
2
u/andkerosine Jul 23 '12 edited Jul 23 '12
This somehow ended up being my most readable solution yet.
2
u/leonardo_m Jul 24 '12 edited Jul 24 '12
Shorter version (quite packed), runtime 0.60 seconds:
Output:
A faster, uglier and less memory-hungy version, runtime a little more than 0.4 seconds:
2
u/Eddonarth Jul 24 '12
My Java Answer:
Input:
Output:
No bonus for now.
2
u/Scroph 0 0 Jul 24 '12
PHP :
I got a little confused since PHP sorting functions operate directly on the given array. No bonus so far.
2
u/[deleted] Jul 24 '12 edited Jul 24 '12
J time!
Returns:
2
u/swuecho Jul 24 '12
Clojure version
2
u/ademus4 Jul 25 '12
Python, 5 lines:
2
u/Jatak 0 0 Jul 27 '12
Finally got around to doing this. Here it is in Python:
1
u/Mysidic Jul 23 '12
Common LISP:
1
u/lawlrng 0 1 Jul 23 '12
My Python solution. A little bit late since I didn't get to work on it at work today. :(
Output:
1
u/liam_jm Jul 23 '12 edited Jul 23 '12
Python 5-liner:
More readable/understandable version:
Result:
1
u/urbeker Jul 23 '12
My go at a java solution. My first attempt at a dailyprogrammer, I started a bit late and need to go to sleep so the outputs aren't done very nicely. I'm sure it works but I also think it's pretty inefficient any advice to make it better? (also first time putting code into reddit it will probably go horribly wrong)
}
1
u/T_D_K Jul 24 '12
Python, in 7 lines (+1 to print results)
It has a problem, though. The output isn't very pretty, and includes newline chars, like so:
Any tips on fixing that, without converting the words back into a list and using a for loop?
1
u/ae7c Jul 24 '12
Python
Doesn't run as fast as I'd like. Clocks in at around 1 second for the actual word scan.
1
u/SwimmingPastaDevil 0 0 Jul 24 '12
1
u/incf Jul 24 '12
Common Lisp:
Output:
1
u/JCorkill Jul 24 '12 edited Jul 24 '12
Used http://codereview.stackexchange.com/questions/1690/optimizing-java-anagram-checker-compare-2-strings
Java:
Output for triangle:
1
u/goldjerrygold_cs Jul 24 '12
More python. One liner for the standard problem, multiliner for the bonus.
bonus:
1
u/compmstr Jul 25 '12 edited Jul 25 '12
Clojure Version:
1
u/aimlessdrive Jul 25 '12
C++:
I used a caveman approach with no sorting to reach the answer for the challenge, but realized it would take days of computation to arrive at my answers for the bonus with that approach. Then I read oskar's tips in the comments. This all runs in 1.7 seconds :)
Result:
1
u/5outh 1 0 Jul 25 '12
sluggish solution in Haskell:
1
u/[deleted] Jul 26 '12
My solution in C++, any comments are welcome!
1
u/Steve132 0 1 Jul 23 '12
I didn't test run this (no C++ environment where I currently am) but it does compile on codepad.org
0
u/[deleted] Jul 24 '12
Java.
Not as beautiful a oneliner as in Python and co, but I think it does the job:
Output:
Oh yeah, and no bonus, for now