![]() |
| ||
| Reading from a file question Hi, I've been trying to make a really simple word unscrambler, that takes a scrambled word, and then compares it to a txt file filled with words. The problem I'm getting, is that I'm trying to only read the first word of each line, and if it finds the word, then it prints out the entire line. So, my txt file kinda looks like this: cart - art car tar If the user inputs "ikeb", then the program would only look at "cart", "bagel", and then "bike" before printing out "bike - ". Can anybody help me? |
| ||
| Re: Reading from a file question Suppose you have the scrambled string, "urcoppnei". You want to be able to quickly go from scrambled string to unscrambled. How? Sort the letters of the string in increasing order. That gives us "ceinoppru". Now take every word in the dictionary, and sort their letters in increasing order. Put this in a hash, where the sorted form is the key and a list of words is the value. (Use a list, not a single word, because anagrams have the same sorted form. For example, the key "dgino" would have as its value a list containing both "doing" and "dingo".) Once you do that, look up "ceinoppru" in the hash. You'll probably find that the key "ceinoppru" maps to a list containing one element, "porcupine". Of course, it would be better to have your dictionary file in the format ... It wouldn't hurt to sort it by the letter-sorted key. So, you would unscramble by sorting the letters of the word in ascending order, by looking for this result in the text file (or hash, or map, or whatever). I don't really understand your file format -- it looks like it's listing words with subwords. Why? And here's a second edit. What you are asking and your sample input and output seem to be on completely different topics! You leave me baffled... |
| ||
| Re: Reading from a file question Sorry, I stated my problem poorly. I can search for the words fine. (I don't sort them into least to greatest, I just check the first character, and then see if it's in the word I'm checking, and if it is, I delete both. Then I go onto the next character, and repeat. If all the characters are deleted, then it's a matching word.) That's probably not a very good way of doing it, but I was just wondering if there was anyway of reading only the first word of each line in a file. so if I had a .txt that had the word, followed by it's definition (see fig 1) Fig 1. the only thing the program would check, would be the first words on each line (fig 2) Fig 2. and then if it found the word, i'd print out both it's definition, as well as the word. So if the scrambled word was erebed (beered) the program would actually print out beered - A drunk person Sorry for the bad explanation earlier, I used a pretty bad example. But, yeah, that's the problem I'm kinda facing, and I was wondering if anyone could help me with that? |
| ||
| Re: Reading from a file question So all you need is a list where each element is the first word on each line in the text file? This is a fairly simple regular expression problem: import sys, reRead the regex part of the Python tutorial to figure out what (([\w]+)[^\\n]+)\\n means. |
| All times are GMT -4. The time now is 11:45 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC