473,716 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New to programming.

11 New Member
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 1169
bvdet
2,851 Recognized Expert Moderator Specialist
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
Mage1989
11 New Member
[code]
import string
word = raw_input('Ente r word: ')
word = sorted(word)
x = len(word)

print word


fobj = open('wordlist. txt', 'r')
lista = fobj.readlines( )
final = []
for each in lista:
final.append(so rted(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
Mage1989
11 New Member
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
Mage1989
11 New Member
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 Recognized Expert Moderator Specialist
............... .....
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 Recognized Expert Moderator Specialist
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
Mage1989
11 New Member
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 Recognized Expert Moderator Specialist
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
2927
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 programming?
12
8475
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 I really want to get back into it as I see a point with GNU/Linux. I want to get my old skills back and write something or help on some projects etc. I need some good books. I used to have one called "A Book On C", but sold it,
3
2484
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 a QA in an MNC in India, since I graduated in September 2003. I am working in a QA role which requires me to do some Winrunner automation, and modifying/creating a framework in Perl for Automated Regression Testing of some command Line utilities...
134
8021
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 increasingly been using scripting languages (especially Python and Bourne shell) which offer the same speed and yet are far more simple and safe to use. I can no longer understand why anyone would willingly use C to program anything but the lowest...
7
4957
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 developed significant content for the C programming language that is available at: https://www.securecoding.cert.org/ by clicking on the "CERT C Programming Language Secure Coding Standard"
30
32251
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 has a C/C++ syntax structure, but want to move on to compliled languages. I was all ready to go with C++ and wxWindows, but I'm applying for an entry level programming possition. They use their own propriatary language, with
47
5948
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 site at http://ldeniau.web.cern.ch/ldeniau/html/oopc/oopc.html. The approach is very instructive. I know that I could do much of this stuff with e.g. C++, but the intellectual challenge of implementing these concepts with pure ANSI C is relevant to...
111
5571
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 in advance! -entengk
14
3293
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 open source world, what is the programmer's language of choice? Judging by the number of members in each of these http://www.google.com/Top/Computers/Programming/Languages/Open_Source/ user groups, it looks like the top 3 open source languages...
17
4714
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 smallest tightest algorithms to preform specific functions. I've also grown and matured a lot, and am wiser and older. I'm reading through the C+ + Programming Language, Third Edition now, and I can actually understand it. I can understand it...
0
8823
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8718
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7980
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6647
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5969
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2119
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.