Skip to content

Commit 327632a

Browse files
committed
Update README.md
1 parent 428fe2a commit 327632a

File tree

1 file changed

+2
-115
lines changed

1 file changed

+2
-115
lines changed

README.md

Lines changed: 2 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,3 @@
1-
Leetcode
2-
===
3-
4-
This is my solution to Leetcode Online Judge's problems. Currently I am revamping the problems all over again towards more idiomatic Python. Stay tuned for updates.
1+
This project is no longer maintained here.
52

6-
Feel free to submit pull requests for submitting more elegant solution.
7-
8-
###List of idiomatic Python I prefer (as of now)
9-
---
10-
- Prefer list comprehension over map / filter.
11-
```
12-
# from Gray Code
13-
14-
# Bad
15-
def grayCode(self, n):
16-
return map(lambda x: (x / 2) ^ x, range(1 << n))
17-
18-
# Idiomatic
19-
def grayCode(self, n):
20-
return [(x / 2) ^ x for x in range(1 << n)]
21-
```
22-
- Prefer using `in` keyword over repetitive variable in conditional statement.
23-
```
24-
# from Post Order Traversal
25-
26-
# Bad
27-
if parent.right == None or parent.right == prev
28-
29-
# Idiomatic
30-
if parent.right in (None, prev):
31-
```
32-
- Prefer using docstring over single line comment when describing functionality of a method.
33-
```
34-
# from Search Insert Position
35-
36-
# Bad
37-
# Iterative solution is also fine.
38-
def searchInsert(self, A, target):
39-
40-
# Idiomatic
41-
def searchInsert(self, A, target):
42-
"""Iterative solution is also fine.
43-
"""
44-
```
45-
- Prefer implicit evaluation of condition (e.g. `if`, `while`, etc.) over explicit comparison in condition.
46-
Notice empty list and dictionary will be evaluated to False, so that is very handy.
47-
```
48-
# from Binary Tree Preorder Traversal
49-
50-
# Bad
51-
while len(stack) > 0:
52-
current = stack.pop()
53-
54-
# Idiomatic
55-
while stack:
56-
current = stack.pop()
57-
```
58-
- Prefer `is None` over `== None`. Notice `is` looks for referential equality, and `None` is a singleton.
59-
The fundamental reason for this preference is much improved speed, and `==` can be overriden by `__eq__`.
60-
```
61-
# from Binary Tree Preorder Traversal
62-
63-
# Bad
64-
if root == None:
65-
66-
# Idiomatic
67-
if root is None:
68-
```
69-
One interesting side note in Python regarding this is on [Stackoverflow](http://stackoverflow.com/questions/306313/python-is-operator-behaves-unexpectedly-with-integers/306347). Trust me, that is worth your 60 seconds of time. But that only works in REPL though, not on a executable python file.
70-
71-
READ THIS or above two rules will only do you harm:
72-
Sometimes you have to use `if foo is not None` over `if foo`. For example, if foo is 0, then `if foo` will become False. But 0 is not None. Just watch out. A rule of the thumb is if you want to check if the default argument of a function is None, then use `if foo is not None`, otherwise you can most likely use `if foo` if you know what you are doing.
73-
- Consider using enumerate when index accessing looks verbose
74-
```
75-
# from Two Sum
76-
77-
# Bad
78-
for i in range(len(nums)):
79-
if target - nums[i] in lookup:
80-
return (lookup[target - nums[i]] + 1, i + 1)
81-
lookup[nums[i]] = i
82-
83-
# Idiomatic
84-
for i, num in enumerate(nums):
85-
if target - num in lookup:
86-
return (lookup[target - num] + 1, i + 1)
87-
lookup[num] = i
88-
```
89-
- Readability counts.
90-
91-
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." Martin Fowler is right.
92-
```
93-
# from Edit Distance
94-
95-
# Bad
96-
def minDistance(self, word1, word2):
97-
distance = [[i] for i in range(len(word1) + 1)]
98-
distance[0] = [i for i in range(len(word2) + 1)]
99-
for i in range(1, len(word1) + 1):
100-
for j in range(1, len(word2) + 1):
101-
distance[i].append(min(distance[i - 1][j] + 1, distance[i][j - 1] + 1, distance[i - 1][j - 1] + (word1[i - 1] != word2[j - 1])))
102-
return distance[-1][-1]
103-
104-
# Idiomatic
105-
def minDistance(self, word1, word2):
106-
distance = [[i] for i in range(len(word1) + 1)]
107-
distance[0] = [i for i in range(len(word2) + 1)]
108-
for i in range(1, len(word1) + 1):
109-
for j in range(1, len(word2) + 1):
110-
deletion = distance[i - 1][j] + 1
111-
addition = distance[i][j - 1] + 1
112-
substitution = distance[i - 1][j - 1]
113-
if word1[i - 1] != word2[j - 1]:
114-
substitution += 1
115-
distance[i].append(min(deletion, addition, substitution))
116-
return distance[-1][-1]
3+
Please go to https://github.com/jw2013/Leetcode-Py for latest update and for making pull request. Thanks!

0 commit comments

Comments
 (0)