Find and arrange words with same letters from list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and arrange words with same letters from list
# 1  
Old 08-14-2012
Find and arrange words with same letters from list

I am making a word game and I am wondering how to find and arrange words in a list that have the same letters. In my game, you are presented with 5 letters, and you then have to rearrange the letters tp make a word. So the word could be "acorn", but those 5 letters could also make up "narco" or "racon" which are words in my .txt dictionary (shown on the left).

Example, third line on the right: i.stack.imgur.com/2EVpB.png

I got some assistance at Stackoverflow, but I am wondering how I can actually do this using unix tools like sed/gsed:

Here's the solution I got there:
Quote:
Easy to build this, sudo algorithm is

Hashmap<String, HashSet<String>> h;
foreach word in list
String sorted = sort(word); \\so if word is sent, sorted is enst
HashSet<String> currentSet = h.get(sorted);
currentSet.add(word);

Any tips or help would be greatly appreciated!
# 2  
Old 08-14-2012
Main tip is to index your master word list with a key of the word's letters sorted to alphabetic order.

Code:
wordlist.txt

acnor:acorn
acnor:narco
acnor:racon

The sort the search term to alphabetic order and use grep to search the master word list:

Code:
input="acorn"
search=`echo "${input}"|fold -w 1|sort|tr -d '\n'`
grep \^"${search}:" wordlist.txt | awk -F: '{print $2}'

./scriptname
acorn
narco
racon

# 3  
Old 08-15-2012
Thank you for your reply methyl.

Basically I need to find out how to index the master list automatically, because my list is 3000 words long so it would take too long to go through every word manually. Can sed or grep do this?

And I tried running the script, but it doesn't print out anything. I made it search for a word which I knew had anagrams, but it just doesn't do anything. Is it easy to make it write to a new .txt file instead to more easily verify that it works?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find words containing small letters

Hello, I have a file containing different words. How can i print the words which contain at least one small letter, for example if i have: today TOMORROw 12345 123a next preViou5 no it should print the following: today TOMORROw 123a next preViou5 no Please use code tags as required... (5 Replies)
Discussion started by: JhonTheNewbie
5 Replies

2. Shell Programming and Scripting

sed command to arrange words

Hi I have a text file as given below: Input format I have a text file as given below . This is a (9 Replies)
Discussion started by: my_Perl
9 Replies

3. Shell Programming and Scripting

Generate list of letters

Heyas I want to list passed arguments and make an incrementing 'marker'. That 'marker' should be a letter between a-z, in proper order. I'm not aware of a seq pendant, so i tried it with this: C=141 list="" while ];do printf \\$C list+=" \\$C" C=$((C+1)) done echo... (3 Replies)
Discussion started by: sea
3 Replies

4. Shell Programming and Scripting

How to words in arrange as Table

Hi Team, I have one file in that almost 100+ words in lines Eg:- Unix windows solaris Linux ... ... But I want arrange all lines in table format and can able read on screen Eg: - Unix windows solaris Lunix...... Hp unix Mac-os ...... Like as Table...... (11 Replies)
Discussion started by: Bhaskar Alagala
11 Replies

5. Shell Programming and Scripting

AWK count letters words

Hi All! can anyone help me with this code? I want to count words or letters in every line with if(count>20){else echo $myline} awk '/<script /{p=1} /<\/script>/{p=0; next}!p' index.html | while read myline; do echo $myline done Thank you !!! (3 Replies)
Discussion started by: sanantonio7777
3 Replies

6. UNIX for Dummies Questions & Answers

Grep.Need help with finding the words which start at [A-K] letters in thesecond column of the table

Hi buddies ! I need some help with one grep command :) I have this table: 1 Petras Pavardenis 1980 5 08 Linas Bajoriunas 1970 10 3 Saulius Matikaitis 1982 2 5 Mindaugas Stulgis 1990... (1 Reply)
Discussion started by: vaidastf
1 Replies

7. Shell Programming and Scripting

ENQUIRY WHETHER SCRIPT FOR DETECTING WORDS FROM A SET OF LETTERS EXISTS

Hello, I am interested in finding out whether someone has a perl or awk script which takes a set of letters such as wak and referring to a dictionary spews out all possible forms such as awk, kaw etc. If someone has such a script, could it be put up please. The script should handle Unicode. Many... (0 Replies)
Discussion started by: gimley
0 Replies

8. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

9. Shell Programming and Scripting

extract words with even numbr of letters

Hello All I need to extract words which are of even number of letters and not greater than 10. Any help?? Thanks, Manish (3 Replies)
Discussion started by: manish205
3 Replies

10. Shell Programming and Scripting

How can I find the 3 first letters from the name file

Hello, I have a name file in Unix for example : ABC_TODAYFirst.001 and I want just capture or display the 3 first letters so : ABC. I tried with cut -c,1-3 and the name but it displays the 3 first letters of all lines. Can you help , Thanks a lot (8 Replies)
Discussion started by: steiner
8 Replies
Login or Register to Ask a Question