![]() |
|
Re: ** Python Support Thread **
lol shabby isn't this exactly the same as the list filtering problem i posed last week?
|
Re: ** Python Support Thread **
Quote:
|
Re: ** Python Support Thread **
I'm not sure if people get that the code I wrote was purely an example for this thread...
@tyler: Almost, since I'm combining it with a for loop. I would normally have just used filter, but I wanted to know what the "pythonic" way of doing it was. :) |
Re: ** Python Support Thread **
shabby,
just pointing out that this thread already contains some examples of pythonic ways to solve this problem. although in other news i have to use python at work so i'm thinking about picking up scheme and SICP in my copious free time :). |
Re: ** Python Support Thread **
Quote:
I was reading SO and came across this: This throws and error instead of a code, which is more Pythonic IMO. I laughed. Quote:
But I'm on class number 9/24 and problemSet 6/13, so that's probably another 3 to 4 weeks. |
Re: ** Python Support Thread **
Quote:
Just curious if people had favorite ways of addressing this. |
Re: ** Python Support Thread **
Here's the python docs on list comprehensions, followed by nested list comprehensions.
http://docs.python.org/tutorial/data...comprehensions and then there is a quick explanation that is a bit closer to English, and it uses FOR LOOPs: http://www.greenteapress.com/thinkpy...ml/chap08.html I think that option one looks cleaner. |
Re: ** Python Support Thread **
Had to do a problem where the teachers conveniently added their own test codes. I was really proud of myself for creating a nice eloquent solution that only took one parameter. The test code is this cheesy thing:
Code:
def test_get_word_score():Code:
def get_word_score(word, n):but end up having to change Code:
if n >= 7:Code:
if len(word) >= 7 and n >= 7:and seriusly, Python: Code:
if len(word) and n >= 7:I feel oppressed. |
Re: ** Python Support Thread **
I have a dictionary problem (written in pseudocode):
Code:
|
Re: ** Python Support Thread **
Code:
if any([ v < 0 for v in d.values() ]): |
Re: ** Python Support Thread **
The n parameter doesn't look like the word length. As you say, it would be ******ed to force the caller to supply that to the function. Look at their test cases, n is always 7 or 8 even if the word has only 3 letters. Maybe it's the number of letters they started with on their rack, for some generalised Scrabble game where you can have more than 7 letters?
You probably shouldn't be so quick to assume the lecturers wrote something really stupid: a more appropriate reaction would be to think "what did I misread"? |
Re: ** Python Support Thread **
Quote:
You shouldn't assume the programming language is broken. You should assume your code is broken. |
Re: ** Python Support Thread **
Quote:
Quote:
Quote:
The code should probably be malleable enough so that if there are variations on the rules/ deal/ scoring/ etc, then what I have should be able to deal with that without turning into a total rewrite. I had some epiphany about why they did this particular test case, which basically deals with adding variations on scoring, what amount of letters are in a hand, etc. ocw-specific observation: Spoiler:
It is stupid and stubborn of me to imply I know all the goals when I am still a crappy programmer learning the ropes. If I can barely handle the coding, then the architecture shouldn't be something I feel I can judge. I always promise myself not to do this when I am tired and/or grumpy. I should keep my thoughts to myself for a day or two. I take this all as a valuable lesson. Anyways, I didn't do what I should have: draw a bunch of diagrams to show what the functions do. I have that done now, and hopefully all of this will be put into a semi-readable flow-chart when it is time to figure out the interactions. |
Re: ** Python Support Thread **
dave,
it's good to think about variations and stuff, particularly on larger projects. but more importantly, there's a rule that you may as well start learning now[1]: YAGNI -- you ain't gonna need it. [1] and i say "start learning" because this is a temptation that all coders must fight until their dying keystroke. (of course programming is like poker in that there are no absolutes, so by the time you've got YAGNI figured out you'll encounter YMGNI -- you might gonna need it.) |
Re: ** Python Support Thread **
Google YAGNI and I am now sad.
I hated the .copy anyways. |
Re: ** Python Support Thread **
YAGNI should not make you sad; it should appeal to your well-developed senses of Laziness and Impatience!
|
Re: ** Python Support Thread **
I'm in the process of moving, so things are slowed down with the ocw.
I am upset. I know that if I created this code one month ago, I would be very pleased with myself. Now I am displeased, but I don't know why I am displeased. I only feel like this wasn't the best solution and that upsets me. One month from now, I can look back and think I am an idiot, but now I know I am an idiot, so I am stuck with this: Spoiler:
I don't know if/when I should use 'if' or 'if not.' Both solutions work though, at least in this particular assignment. |
Re: ** Python Support Thread **
First, your code really isn't too bad. I use to work with first and second year CS students and code like this is very common (and a lot of it is a lot worse).
A couple things in terms of improving your code: 1. You've got a pretty big bug in there with how you treat the return value. One hint, once you know that a word isn't in the word list or that a letter isn't in the hand, do you need to keep doing more work? 2. Don't forget that you can return variables and that conditional checks with one of the operators being a constant boolean are usually unnecessary. So we can simplify as follows: Code:
if test == True:Code:
if test:Code:
return testAbout the mutation thing: I wouldn't worry about it until you get this function in a good place, but I think it's worth thinking about/understanding what exactly is happening and if/why you need the copy (or how you could modify your solution to avoid needing it). |
Re: ** Python Support Thread **
That's true. Functions default to false. I forgot about that one.
|
Re: ** Python Support Thread **
ocw specific:
Spoiler:
The bug you mentioned was the first to go, but now there's about 5 more to go. If I read an output plus None again, I am going to scream. At least that corrects my misstatement about functions defaulting to False. |
Re: ** Python Support Thread **
Potentially interesting link on Slashdot yesterday:
http://learnpythonthehardway.org/book/ seems to be a lot of "if this doesn't work you did something wrong, figure it out" type statements in the exercises. |
Re: ** Python Support Thread **
Quote:
for instance: Code:
g = (v < 0 for v in d.values()) #g is now iteratorCode:
if any( v < 0 for v in d.values() ):Difference is that you do not create list, use extra storage and iteration is stooped as soon as first true element is found. |
Re: ** Python Support Thread **
Very good point. I'm not sure why, but I'd been assuming that list comprehensions returned an iterator. Thanks for pointing that out.
|
Re: ** Python Support Thread **
Python reduce, generators and lazy evaluation is awesome. I have assembled haskell like code and cursed functions.
Code:
from functools import reduce #remove this line for python 2.x |
Re: ** Python Support Thread **
This'll rankle some posters here:
For at least four decades, people have been building tools called debuggers. Things to help you find bugs. And there are some built into Idol. My personal view is most of them are not worth the trouble. The two best debugging tools are the same now that they have almost always been. And they are the print statement, and reading. -- Prof. John Guttag, MIT |
| All times are GMT -4. The time now is 07:40 AM. |
|
Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Copyright © 2008-2020, Two Plus Two Interactive