Finished PS 5 Ghost. Anyone want to critique my code? In particular I'm worried about 3 things: 1) I've done a few different functions for all the subtasks I could think of, but I still feel like it's not encapsulated enough and there's too much procedure in ghost(). Is there a way to improve that? 2) The step-through-dictionary check for a word fragment in is_valid_frag() might not be efficient. Is this OK? If not is there a better way to do it? 3) Passing arguments and returning: Am I doing this correctly? Thanks! http://ideone.com/XV8ol
Also, from the first time my word fragment is printed there is some extra space between the quotes, one space for each quote, which I can't figure out how to get rid of, so if you enter a letter it will say: Current word fragment: ' a '. I can't figure out how to get it to just say 'a'. Anyone know what I'm doing wrong? Thanks so much!
For the spaces, maybe try converting your 'word' variable into a second one with quotes around it. quote_word = "'" + word + "'" print 'Current word fragment:', quote_word
how do you get it to find your file? I keep getting 'No such file or directory: ' and I have typed out the complete pathname in my code... no typos, I searched the pathname through Finder (on a mac) and it brought me right to the location of words.txt... I know there is something wrong with my python or something else in the code... running python 3.2 with mac os x 10.7
@btbessaay as long as I have it in the same directory as the source files, I haven't had any problems. I think by default, the filepath is relative to the directory the source file is in, so the full filepath wouldn't be looking in the right place. So if you have the list in the same folder as the source, I wouldn't be able to tell you what's wrong (i'm using python 2.6 and os x 10.5, so my setup is very different from yours)
@polymath thanks that's a lovely little workaround. Do you have the same problem when concatenating string literals with variables though? Doesn't seem like that should be the standard behavior.. It's been driving me crazy!
I think the issue is only in this specific problem. You want a string with single quotes around it, but you have a bunch of operations operating on the string assuming it will only be letters, so you can't have those quotes! The way Python seems to work is that any time you print out different object types, it puts a space in between objects, which is a fairly logical approach. What you're really asking it to do is make a new string that jams a single quote up to each end of your word string. The way I suggested is the only way I can think of doing it, but I'm only a few lectures ahead of you myself.
the spaces are printed for every comma in a print statement - that is the standard behavior http://docs.python.org/reference/simple_stmts.html#the-print-statement you can try string formatting instead format method print "Current word fragment: '{}'".format(word) http://docs.python.org/library/stdtypes.html#str.format % operator: print "Current word fragment: '%s'" % word http://docs.python.org/library/stdtypes.html#string-formatting-operations
is_valid_frag could be performed with a binary search which is shown in lectures 8 and 9. the word list has to be sorted for it to work - it looks like it is. python can compare strings using the > and < operators. you can use these to decide which direction to go in the search http://dpaste.com/573854/ from the docs - "Strings are compared lexicographically using the numeric equivalents (the result of the built-in function ord()) of their characters. ( http://docs.python.org/reference/expressions.html#notin)
@bwCA thanks a ton for that idea! They haven't explicitly stated that you can do binary searches with alpha strings like that in lecture, so I didn't think of that. Great idea to improve efficiency... going to go change my own to a binary! The other idea I had was to divide and conquer the wordlist. Split it into 26 wordlists based on starting letter. The binary search is probably a more efficient way to do it though. Thanks!
I cannot get the code to find the document for the life of me... I type it in to Finder under 'Go to folder' and it takes me right to the document (words.txt). But then I try to find it in a terminal, again 'No such file or directory'
@bwCA yes, that is very useful, I just didn't think to check if strings could be compared to that way. So if you can search this dictionary with a binary search, I almost feel like the 4th assignment in PS6 (faster computer player) would not even be necessary. Seems like it would take the same amount of time either way. Also, good to know about the formatting options. I saw info about the % operator in PS7, but I couldn't find info on how to do multiple variable references in a single string, like "You found %s in %.02f seconds." % word % total_time (Of course that syntax doesn't work...) Is there a way to do this in python?
@btbessaay so are you saying that you are able to view the contents of the directory in terminal using "ls <directory>", and the file is simply not there?
Well, when I type "ls '<pathname>' it returns a blank line rather than no such file or directory as it did before. I'm assuming a blank line isn't right either?
oh what am i talking about, you don't need to type <directory>. what i meant was, if you go to the folder 1 at a time using cd and then once you're in the folder where the file is, just type ls. i wanted to know if the file was listed there
what exactly to you mean? Because I have the exact right pathname, and I have the exact same code as the template, yet it doesn't work.
well, i don't have a ton of experience with this issue, but as far as I know there are two basic reasons the file might not be found: 1) the program is looking for a relative path and you're giving it an absolute path or vice versa, or 2) the file is not in the folder you think it is. I've only used python for a little while, but I think it's looking for the relative filepath, meaning if it's in the same folder as your code, you don't write a filepath at all, just the name. However, I don't know how you're having that problem because that's how the code is prepared: all you have to do is download the code and txt file into the same folder and it should just find the word list and run. If you put the filepath in there, it's not going to find the file anymore. You also mentioned that you can't find the file in terminal although you can find it in the osx finder. That's what I was talking addressing before. I wonder if you start in the root directory, and just use cd to browse into the folder where you think the file is, and then do the ls command, if you can see that file in listed in that directory.
of course there could just be some kind of issue with osx 7 and the python 3.2 environment as well
That's what I'm going to have to tell myself because I just cannot find any way to make it work and multiple users of multiple forums have not been able to help me... thanks though
argh, sorry to hear it :/
the format method and the % string operator take a tuple - it should be in the docs, see the link i posted. you can also use keywords
ok thanks, I'll go through the docs more carefully. I google-searched "python string more than one variable" or something like that and didn't find anything useful
Join our real-time social learning platform and learn together with your friends!