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.
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: - >>> astring = 'argentiferous'
-
>>> s = list(astring)
-
>>> s.sort()
-
>>> print ''.join(s)
-
aeefginorrstu
-
>>>
This will sort the letters of each word and join them back together in a variable named sentence: - print ' '.join([''.join(sorted(list(word))) for word in sentence.split()])
sentence could be equivalent to each line of your text file.
[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.
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. -
import string
-
import os
-
ls = os.linesep
-
word = raw_input('Enter word1: ')
-
word = sorted(word)
-
word = ''.join(word)
-
print word
-
fobj = open('woot.txt', 'r')
-
lista = fobj.readlines()
-
for eachline in lista:
-
x = cmp(word,eachline)
-
if x == 0:
-
print 'Got it'
-
else:
-
print ''
-
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?
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 -
import string
-
import os
-
import linecache
-
ls = os.linesep
-
abc = 0
-
mylist = []
-
line = 0
-
while abc != 10:
-
word = raw_input('Enter word!: ',)
-
word = sorted(word)
-
word = ''.join(word)
-
word = word + '\n'
-
fobj = open('woot.txt', 'r')
-
lista = fobj.readlines()
-
x = 0
-
while x != 1275:
-
z = cmp(word,lista[x])
-
if z == 0:
-
line = x + 1
-
else:
-
z
-
x = x + 1
-
retrieved_line = linecache.getline('wordlist.txt',line)
-
mylist.append(retrieved_line)
-
abc = abc + 1
-
mylist[0] = mylist[0][0:-1]
-
mylist[1] = mylist[1][0:-1]
-
mylist[2] = mylist[2][0:-1]
-
mylist[3] = mylist[3][0:-1]
-
mylist[4] = mylist[4][0:-1]
-
mylist[5] = mylist[5][0:-1]
-
mylist[6] = mylist[6][0:-1]
-
mylist[7] = mylist[7][0:-1]
-
mylist[8] = mylist[8][0:-1]
-
mylist[9] = mylist[9][0:-1]
-
fobj123 = open('finished.txt', 'w')
-
mylist = ','.join(mylist)
-
fobj123.writelines(['%s%s' % (mylist, ls)])
-
fobj123.close()
-
This is what I am trying to use. -
import string
-
import os
-
import linecache
-
ls = os.linesep
-
abc = 0
-
mylist = []
-
line2 = 0
-
line = 0
-
infile = open('input.txt','r')
-
infile.readlines()
-
while line2 != 11:
-
word = linecache.getline('input.txt',line2)
-
while abc != 10:
-
word = sorted(word)
-
word = ''.join(word)
-
word = word + '\n'
-
fobj = open('woot.txt', 'r')
-
lista = fobj.readlines()
-
x = 0
-
while x != 1275:
-
z = cmp(word,lista[x])
-
if z == 0:
-
line = x + 1
-
else:
-
z
-
x = x + 1
-
retrieved_line = linecache.getline('wordlist.txt',line)
-
mylist.append(retrieved_line)
-
abc = abc + 1
-
line2 = line2 + 1
-
mylist[0] = mylist[0][0:-1]
-
mylist[1] = mylist[1][0:-1]
-
mylist[2] = mylist[2][0:-1]
-
mylist[3] = mylist[3][0:-1]
-
mylist[4] = mylist[4][0:-1]
-
mylist[5] = mylist[5][0:-1]
-
mylist[6] = mylist[6][0:-1]
-
mylist[7] = mylist[7][0:-1]
-
mylist[8] = mylist[8][0:-1]
-
mylist[9] = mylist[9][0:-1]
-
fobj123 = open('finished.txt', 'w')
-
mylist = ','.join(mylist)
-
fobj123.writelines(['%s%s' % (mylist, ls)])
-
fobj123.close()
-
infile.close()
-
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?
- lista = [word.strip() for word in fobj.readlines()]
bvdet 2,851
Expert Mod 2GB
I may be missing something. If I understand correctly, this should do most of what you want: - # This will read the input words file, sort each word, and truncate the list to the first 10 words
-
sortedWordList = [sorted(word.strip()) for word in open('file_name').readlines()[:10]]
-
-
# Write the sorted words list to disc
-
f = open('file_name', 'w')
-
f.write('\n'.join(sortedWordList))
-
f.close()
-
-
# Read in your sorted words file
-
lineList = [word.strip() for word in open('file_name').readlines()]
-
-
# Create a list of indices for the sorted input words from the sorted words file
-
# An exception is raised if word is not in lineList
-
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: - if obj in lista:
-
.... do something.....
-
else:
-
....do something else....
I don't understand what you are doing here: - mylist = ','.join(mylist)
-
fobj123.writelines(['%s%s' % (mylist, ls)])
I may be missing something. If I understand correctly, this should do most of what you want: - # This will read the input words file, sort each word, and truncate the list to the first 10 words
-
sortedWordList = [sorted(word.strip()) for word in open('file_name').readlines()[:10]]
-
-
# Write the sorted words list to disc
-
f = open('file_name', 'w')
-
f.write('\n'.join(sortedWordList))
-
f.close()
-
-
# Read in your sorted words file
-
lineList = [word.strip() for word in open('file_name').readlines()]
-
-
# Create a list of indices for the sorted input words from the sorted words file
-
# An exception is raised if word is not in lineList
-
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: - if obj in lista:
-
.... do something.....
-
else:
-
....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. - 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 - fobj123.writelines(['%s%s' % (mylist, ls)])
without this, my program doesn't write any of the unscrambled words to my output file.
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. - 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 - 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: - fobj123 = open('finished.txt', 'w')
-
fobj123.write(','.join(mylist))
-
fobj123.close()
There is no need to write a newline character unless you are planning to append to the file later.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |