471,334 Members | 2,221 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,334 software developers and data experts.

New to programming.

Ok, here is what I am trying to do. I have a text file with 1274 words in it, one on each line.

I am trying to make a program that will take ten words and unscramble them.

I want to put each character in each word, in the text file, in alphabetic order.

I am able to put strings into alphabetic order, but not the characters in the strings.

Because I am unscrambling the words, I do not want to alter their original conditions. (that would change the word and I would not know what the unscrambled word was)


What I thought about doing was creating a copy of the text file. Opening the copy. Putting the characters into alphabetic order. Putting the characters of the scrambled words into alphabetic order. Then comparing the two. Then saving the line numbers of the words that matched into variables.

After finding the variables the program can open the original file (that is not in alphabetic order) and print the words that are on the lines found.
Jan 6 '08 #1
8 1088
bvdet
2,851 Expert Mod 2GB
Ok, here is what I am trying to do. I have a text file with 1274 words in it, one on each line.

I am trying to make a program that will take ten words and unscramble them.

I want to put each character in each word, in the text file, in alphabetic order.

I am able to put strings into alphabetic order, but not the characters in the strings.

Because I am unscrambling the words, I do not want to alter their original conditions. (that would change the word and I would not know what the unscrambled word was)


What I thought about doing was creating a copy of the text file. Opening the copy. Putting the characters into alphabetic order. Putting the characters of the scrambled words into alphabetic order. Then comparing the two. Then saving the line numbers of the words that matched into variables.

After finding the variables the program can open the original file (that is not in alphabetic order) and print the words that are on the lines found.
I am a little confused about what you are trying to do. You can scramble words in alphabetical order:
Expand|Select|Wrap|Line Numbers
  1. >>> astring = 'argentiferous'
  2. >>> s = list(astring)
  3. >>> s.sort()
  4. >>> print ''.join(s)
  5. aeefginorrstu
  6. >>> 
This will sort the letters of each word and join them back together in a variable named sentence:
Expand|Select|Wrap|Line Numbers
  1. print ' '.join([''.join(sorted(list(word))) for word in sentence.split()])
sentence could be equivalent to each line of your text file.
Jan 6 '08 #2
[code]
import string
word = raw_input('Enter word: ')
word = sorted(word)
x = len(word)

print word


fobj = open('wordlist.txt', 'r')
lista = fobj.readlines()
final = []
for each in lista:
final.append(sorted(each))


print final
[code]


This is what I am using so far. What I am trying to do is place each word in alphabetic order. As well as place the words I input in alphabetic order. Then compare the two. ( I haven't gotten that far yet)

Right now, the print final is there because I am just testing it out. However, it prints it every time it adds an element to final.
Jan 6 '08 #3
Ok, so I did some more research and was able to put all the words in alphabetic order. Now I have two text files. One with the original format of the words. And one with the characters of each word in alphabetic order.

Expand|Select|Wrap|Line Numbers
  1. import string
  2. import os
  3. ls = os.linesep
  4. word = raw_input('Enter word1: ')
  5. word = sorted(word)
  6. word = ''.join(word)
  7. print word
  8. fobj = open('woot.txt', 'r')
  9. lista = fobj.readlines()
  10. for eachline in lista:
  11.     x = cmp(word,eachline)
  12.     if x == 0:
  13.         print 'Got it'
  14.     else:
  15.         print ''
  16.  
Now, this will put any word I enter into alphabetic order. It will then check to see if the word I entered is in the list (woot.txt is the list with the characters in alphabetic order)

However, when I enter a word (That I know is in the list) it still tells me that it is not in the list.

When the program reads the list (lista) each element in the list (the words I am comparing my input to) contains '\n'. How do I remove that from each element in the list?
Jan 6 '08 #4
Ok. I got it to work 100% of the time. It is just slow for what I am trying to do. This is for an online game. I have 30 seconds to complete it. It takes me about 50 seconds.

At the moment I have to enter each word individually. I know I can put the words into a file and then upload the file. I just don't know how to make it read each line of that file and update the list accordingly.

This is what I am using now. I know it works

Expand|Select|Wrap|Line Numbers
  1. import string
  2. import os
  3. import linecache
  4. ls = os.linesep
  5. abc = 0
  6. mylist = []
  7. line = 0
  8. while abc != 10:
  9.     word = raw_input('Enter word!: ',)
  10.     word = sorted(word)
  11.     word = ''.join(word)
  12.     word = word + '\n'
  13.     fobj = open('woot.txt', 'r')
  14.     lista = fobj.readlines()
  15.     x = 0
  16.     while x != 1275:
  17.         z = cmp(word,lista[x])
  18.         if z == 0:
  19.             line = x + 1
  20.         else:
  21.             z
  22.         x = x + 1
  23.     retrieved_line = linecache.getline('wordlist.txt',line)
  24.     mylist.append(retrieved_line)
  25.     abc = abc + 1
  26. mylist[0] = mylist[0][0:-1]
  27. mylist[1] = mylist[1][0:-1]
  28. mylist[2] = mylist[2][0:-1]
  29. mylist[3] = mylist[3][0:-1]
  30. mylist[4] = mylist[4][0:-1]
  31. mylist[5] = mylist[5][0:-1]
  32. mylist[6] = mylist[6][0:-1]
  33. mylist[7] = mylist[7][0:-1]
  34. mylist[8] = mylist[8][0:-1]
  35. mylist[9] = mylist[9][0:-1]
  36. fobj123 = open('finished.txt', 'w')
  37. mylist = ','.join(mylist)
  38. fobj123.writelines(['%s%s' % (mylist, ls)])
  39. fobj123.close()
  40.  


This is what I am trying to use.

Expand|Select|Wrap|Line Numbers
  1. import string
  2. import os
  3. import linecache
  4. ls = os.linesep
  5. abc = 0
  6. mylist = []
  7. line2 = 0
  8. line = 0
  9. infile = open('input.txt','r')
  10. infile.readlines()
  11. while line2 != 11:
  12.     word = linecache.getline('input.txt',line2)
  13.     while abc != 10:
  14.         word = sorted(word)
  15.         word = ''.join(word)
  16.         word = word + '\n'
  17.         fobj = open('woot.txt', 'r')
  18.         lista = fobj.readlines()
  19.         x = 0
  20.         while x != 1275:
  21.             z = cmp(word,lista[x])
  22.             if z == 0:
  23.                 line = x + 1
  24.             else:
  25.                 z
  26.             x = x + 1
  27.         retrieved_line = linecache.getline('wordlist.txt',line)
  28.         mylist.append(retrieved_line)
  29.         abc = abc + 1
  30.     line2 = line2 + 1
  31. mylist[0] = mylist[0][0:-1]
  32. mylist[1] = mylist[1][0:-1]
  33. mylist[2] = mylist[2][0:-1]
  34. mylist[3] = mylist[3][0:-1]
  35. mylist[4] = mylist[4][0:-1]
  36. mylist[5] = mylist[5][0:-1]
  37. mylist[6] = mylist[6][0:-1]
  38. mylist[7] = mylist[7][0:-1]
  39. mylist[8] = mylist[8][0:-1]
  40. mylist[9] = mylist[9][0:-1]
  41. fobj123 = open('finished.txt', 'w')
  42. mylist = ','.join(mylist)
  43. fobj123.writelines(['%s%s' % (mylist, ls)])
  44. fobj123.close()
  45. infile.close()
  46.  
Jan 7 '08 #5
bvdet
2,851 Expert Mod 2GB
....................
When the program reads the list (lista) each element in the list (the words I am comparing my input to) contains '\n'. How do I remove that from each element in the list?
Expand|Select|Wrap|Line Numbers
  1. lista = [word.strip() for word in fobj.readlines()]
Jan 7 '08 #6
bvdet
2,851 Expert Mod 2GB
I may be missing something. If I understand correctly, this should do most of what you want:
Expand|Select|Wrap|Line Numbers
  1. # This will read the input words file, sort each word, and truncate the list to the first 10 words
  2. sortedWordList = [sorted(word.strip()) for word in open('file_name').readlines()[:10]]
  3.  
  4. # Write the sorted words list to disc
  5. f = open('file_name', 'w')
  6. f.write('\n'.join(sortedWordList))
  7. f.close()
  8.  
  9. # Read in your sorted words file
  10. lineList = [word.strip() for word in open('file_name').readlines()]
  11.  
  12. # Create a list of indices for the sorted input words from the sorted words file
  13. # An exception is raised if word is not in lineList
  14. idxs = [lineList.index(word) for word in lineList]
You should read 'woot.txt' only once. You can also test if an object is in lista this way:
Expand|Select|Wrap|Line Numbers
  1. if obj in lista:
  2.     .... do something.....
  3. else:
  4.     ....do something else....
I don't understand what you are doing here:
Expand|Select|Wrap|Line Numbers
  1. mylist = ','.join(mylist)
  2. fobj123.writelines(['%s%s' % (mylist, ls)])
Jan 7 '08 #7
I may be missing something. If I understand correctly, this should do most of what you want:
Expand|Select|Wrap|Line Numbers
  1. # This will read the input words file, sort each word, and truncate the list to the first 10 words
  2. sortedWordList = [sorted(word.strip()) for word in open('file_name').readlines()[:10]]
  3.  
  4. # Write the sorted words list to disc
  5. f = open('file_name', 'w')
  6. f.write('\n'.join(sortedWordList))
  7. f.close()
  8.  
  9. # Read in your sorted words file
  10. lineList = [word.strip() for word in open('file_name').readlines()]
  11.  
  12. # Create a list of indices for the sorted input words from the sorted words file
  13. # An exception is raised if word is not in lineList
  14. idxs = [lineList.index(word) for word in lineList]
You should read 'woot.txt' only once. You can also test if an object is in lista this way:
Expand|Select|Wrap|Line Numbers
  1. if obj in lista:
  2.     .... do something.....
  3. else:
  4.     ....do something else....
I don't understand what you are doing here:
If the object is not in the list my program sends me an error and stops running. that is fine for what I need it to do. every word I input will be in the list.

Thank you very much for your help.

Expand|Select|Wrap|Line Numbers
  1. mylist = ','.join(mylist)
I am using this to change how the output looks in the output file (finished.txt)
without it the output looks like this:
['word', 'word', 'word', 'word', 'word', 'word', 'word', 'word', 'word', 'word']


with it the output looks like this:
word,word,word,word,word,word,word,word,word,word


Expand|Select|Wrap|Line Numbers
  1. fobj123.writelines(['%s%s' % (mylist, ls)])
without this, my program doesn't write any of the unscrambled words to my output file.
Jan 7 '08 #8
bvdet
2,851 Expert Mod 2GB
If the object is not in the list my program sends me an error and stops running. that is fine for what I need it to do. every word I input will be in the list.

Thank you very much for your help.

Expand|Select|Wrap|Line Numbers
  1. mylist = ','.join(mylist)
I am using this to change how the output looks in the output file (finished.txt)
without it the output looks like this:
['word', 'word', 'word', 'word', 'word', 'word', 'word', 'word', 'word', 'word']


with it the output looks like this:
word,word,word,word,word,word,word,word,word,word


Expand|Select|Wrap|Line Numbers
  1. fobj123.writelines(['%s%s' % (mylist, ls)])
without this, my program doesn't write any of the unscrambled words to my output file.
In that case you can write the words to disc like this:
Expand|Select|Wrap|Line Numbers
  1. fobj123 = open('finished.txt', 'w')
  2. fobj123.write(','.join(mylist))
  3. fobj123.close()
There is no need to write a newline character unless you are planning to append to the file later.
Jan 7 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Martin | last post: by
12 posts views Thread by G. | last post: by
134 posts views Thread by evolnet.regular | last post: by
7 posts views Thread by Robert Seacord | last post: by
30 posts views Thread by Jakle | last post: by
47 posts views Thread by Thierry Chappuis | last post: by
14 posts views Thread by deko | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.