Skip to content

Commit 74f7af3

Browse files
committed
Finished Week 3
1 parent 411d011 commit 74f7af3

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
This idiom works because tuples are compared lexicographically; the first items are compared; if they are the same then the second items are compared, and so on.
2+
3+
It is not strictly necessary in all cases to include the index i in the decorated list. Including it gives two benefits:
4+
5+
The sort is stable - if two items have the same key, their order will be preserved in the sorted list.
6+
The original items do not have to be comparable because the ordering of the decorated tuples will be determined by at most the first two items. So for example the original list could contain complex numbers which cannot be sorted directly.
7+
Another name for this idiom is Schwartzian transform, after Randal L. Schwartz, who popularized it among Perl programmers.
8+
9+
For large lists and lists where the comparison information is expensive to calculate, and Python versions before 2.4, DSU is likely to be the fastest way to sort the list. For 2.4 and later, key functions provide the same functionality.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import socket
2+
3+
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
4+
mysocket.connect(('www.pythonlearn.com', 80))
5+
mysocket.send('GET http://www.pythonlearn.com/code/intro-short.txt HTTP/1.0\n\n'.encode())
6+
# Difference between a unicode string and a byte string
7+
# http://stackoverflow.com/questions/6224052/what-is-the-difference-between-a-string-and-a-byte-string#answer-6224384
8+
# The encoding and decoding of the two types of string
9+
# http://stackoverflow.com/questions/11781639/typeerror-str-does-not-support-buffer-interface#answer-11781741
10+
11+
data = mysocket.recv(512)
12+
13+
while len(data) > 1:
14+
print(data)
15+
data = mysocket.recv(512)
16+
17+
mysocket.close()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import urllib.request as ur
2+
3+
f = ur.urlopen('http://www.baidu.com')
4+
5+
for line in f:
6+
print(line.strip())
7+
8+
# urllib only gets the content of the file; headers are omitted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
counts = {}
2+
3+
file = open('sample text.txt')
4+
5+
for line in file:
6+
words = line.split()
7+
for word in words:
8+
counts[word] = counts.get(word, 0) + 1
9+
10+
print(counts)
11+
12+
file.close()

0 commit comments

Comments
 (0)