This is the P2PU Archive. If you want the current site, go to www.p2pu.org!
You haven't posted any discussions yet.
Please read the Data Types course document and complete the excercises. Post your answers to Pastie or Github and then add a link to them below. If you have questions about a specific topic or excercise, post it here as well.
You are designing an inventory and point-of-sale system for a local producers' market. Aspects of the system include:
Q: How can each aspect be represented in Python data types? Give an example for each.
Posted my assignments up on my github at http://www.tylercipriani.com/python101/ At the end of chapter 10 it asks you to compare your results with the letter frequency page on wikipedia - so for a little extra fun I decided to use BeautifulSoup to parse that Wikipedia page and actually compare the results - I describe the full process on that github page posted above.
1. Dict with Tuple key - ex namebooth[(John, Farmer)] => Booth 10
2. String - ex 'Your Mom's Favorite Python Book'
3. Dict - ex namePrice['goldenGooseFruit'] => '$500'
4. List - ex growingSeasons = ['spring','summer','fall',winter']
5. Dict with Tuple Key - ex farmersParticipants[(John, Farmer)] => Wichita
Man, you are working us hard; 4 chapters in a week!
Exercises chapter 6: http://www.pastie.org/1922634
To get zfill to work I had to use repr() instead of str() to use zfill() on a number (exercise 6.6). What is difference between repr and str?
PS: I accidentally studied chapter 7 as well.
Sorry :-) We can stretch this task out over two weeks, although it seems like you are ahead of the game.
Exercises chapter 8: http://www.pastie.org/1922678
In exercise 3 I used an 'or' instead of an 'and' to combine both conditions.
Exercises chapter 9: http://www.pastie.org/1927674
I am pretty amazed that Python can handle a statement like
domain = words[1].split('@')[1]
which I used in exercise 9.5. You take the second element of a list, apply a string function to it which results in a list and returns the second element, all in one go. Whether it is easy to read for humans is something else. What do you think?
Clever indeed :)
It is slightly hard to read, as you point out. How do you think it stands up to the 'Zen of Python'?
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Definitely not zen, but cool.
Homework for for Data Types:
http://pastie.org/1928301
Took a little while to set up a github account:
Set up last week's homework on github.
Chapter 6: https://github.com/jwmnatl/jwmnatl.github.com/blob/master/python101/chap...
I still have Chapters 8, 9 & 10 to go
Exercises chapter 10: http://pastie.org/1930991
Problem with foreign languages were the diactricis (accents, umlaut, ...). How do you solve this?
Chapter 8: https://github.com/jwmnatl/jwmnatl.github.com/blob/master/python101/chap...
chap6 exercises
http://www.pastie.org/1957243
Chapter 8
http://www.pastie.org/2011336
Chapter 9
http://www.pastie.org/2027895
Chapter 10
http://www.pastie.org/2040288
Chapter 9: https://github.com/jwmnatl/jwmnatl.github.com/blob/master/python101/chap...
Brylie, I am a little stuck with Ex. 10.1. I am using the top 10 most common words example from the chapter, and the output is a list of 27 emails with "cwen@ipui.edu 5" at the end rather than just the one email and count returned:
my code:
file = raw_input('Enter a file name: ')
try:
fhand = open(file)
except:
print 'File cannot be opened: ', file
exit()
emails = dict()
for line in fhand:
words = line.split()
# print 'Debug', words
if len(words) < 2 or words[0] != 'From' : continue
emails[words[1]] = emails.get(words[1], 0) + 1
lst = []
for key, val in emails.items():
lst.append((val,key))
lst.sort(reverse=True)
for val, key in lst[:1]:
print key, val
I put this code in my editor and it seemed to work just fine. When called on mbox-short.txt the program runs as follows:
Enter a file name: mbox-short.txt
cwen@iupui.edu 5
My suggestion is to play with your indentation levels as they're not indicated by posting your code here. The furthest indented line I used with your code is the lst.append((val,key)) which is indented twice and it is the only line indented twice - this generates the expected output. Put your code up on gist or pastie if you're still having trouble.
Thanks, Tyler. I know the indentations did not show but since I was posting at 12:30 am, I didn't really care. In any event, you nailed it. My second for loop was in the first for loop. Totally makes sense now...not at 12:30 in the morning, but now it does. I moved the second for loop to the left and the code works. Thanks again, Tyler!
Chapter 10: https://github.com/jwmnatl/jwmnatl.github.com/blob/master/python101/chap...
Exercise 8.1 | http://www.pastie.org/1988496
Exercise 8.4 | http://www.pastie.org/1988583
Not sure my method for 8.4 is quite correct, but get the same outcome.
Have had some unexpected things crop up these last couple of weeks, so haven't been able to devote as much time and reading to this task as I would like to. Hopefully post the rest of the exercises soon.
Your method for 8.4 works fine. The more pythonian method for achieving this outcome, and a method that'll serve you well going forward, is to use a 'continue' to continue with the next iteration of a loop. So if you find that for which you're searching in an array (i.e. 'if x is in array') you can just skip to the next word in the array rather than delete the word and re-append the word to the results array. I've made this adjustment to your code in this gist: https://gist.github.com/bc1f7fe1f16f4522677a for context. Hopefully any of this made sense - I'm not so sure after having re-read it. Let me know if I can clarify.
posted chap 6 exercises
http://pastie.org/1991662
oops - in ex6.5, should have typed the print(found) so it looked like this:
print(float(found)) sorry..
Finished Data Types chapters: https://github.com/brotherhutch/python101
Extra Credit:
1) Dictionary with Tuple key: {('Beanstalk', 'Jack'): 'B1'}
2) String: 'Steve Jobs makes \'em; we fix \'em.'
3) Dictionary: {'Garbanzo Beans': 4.95}
4) List: ('spring', 'summer', 'fall', 'winter')
5) Dictionary with Tuple key: {('Beanstalk', 'Jack'): 'Abbotsford'}