473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1157
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

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

Similar topics

5
by: Martin | last post by:
When was inheritance intruduced into object oriented programming? More generally, does anyone know or have any sources on when the different features were introduced into object oriented...
12
by: G. | last post by:
Hi all, During my degree, BEng (Hons) Electronics and Communications Engineering, we did C programming every year, but I never kept it up, as I had no interest and didn't see the point. But now...
3
by: user | last post by:
Hi all, At the outset, I regret having to post this slightly OT post here. However, I strongly feel that people in this group would be the best to advise me on my predicament. I am working as...
134
by: evolnet.regular | last post by:
I've been utilising C for lots of small and a few medium-sized personal projects over the course of the past decade, and I've realised lately just how little progress it's made since then. I've...
7
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already...
30
by: Jakle | last post by:
I have been googling, but can seem to find out about C GUI libraries. My main platform is Windows, but it would be nice to find a cross platform library. I've been programming with php, which...
47
by: Thierry Chappuis | last post by:
Hi, I'm interested in techniques used to program in an object-oriented way using the C ANSI language. I'm studying the GObject library and Laurent Deniau's OOPC framework published on his web...
111
by: Enteng | last post by:
Hi I'm thinking about learning C as my first programming language. Would you recommend it? Also how do you suggest that I learn it?What books/tutorials should I read for someone like me? Thanks...
14
by: deko | last post by:
For building Windows desktop apps, the clear favorite is C#. But my clients can't afford to buy Microsoft products. So I need to develop software for Linux users and web applications. In the...
17
by: CoreyWhite | last post by:
I bought this book years ago, when I was just learning C++. Since then I've gone through every math course offered at my college, taken courses on coding C & thinking in terms how how to make the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.