GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - pharrell90/Leetcode-Py: And they say Python is slow...

BranchesTags
Open more actions menu
 
 

Latest commit

 

History

11 Commits
11 Commits

Folders and files

NameName
Last commit message
Last commit date
3Sum Closest.py
3Sum Closest.py
 
 
3Sum.py
3Sum.py
 
 
Add Binary.py
Add Binary.py
 
 
Add Two Numbers.py
Add Two Numbers.py
 
 
Anagrams.py
Anagrams.py
 
 
Balanced Binary Tree.py
Balanced Binary Tree.py
 
 
Best Time to Buy and Sell Stock II.py
Best Time to Buy and Sell Stock II.py
 
 
Best Time to Buy and Sell Stock III.py
Best Time to Buy and Sell Stock III.py
 
 
Best Time to Buy and Sell Stock.py
Best Time to Buy and Sell Stock.py
 
 
Binary Tree Inorder Traversal.py
Binary Tree Inorder Traversal.py
 
 
Binary Tree Level Order Traversal II.py
Binary Tree Level Order Traversal II.py
 
 
Binary Tree Level Order Traversal.py
Binary Tree Level Order Traversal.py
 
 
Binary Tree Maximum Path Sum.py
Binary Tree Maximum Path Sum.py
 
 
Binary Tree Postorder Traversal.py
Binary Tree Postorder Traversal.py
 
 
Binary Tree Preorder Traversal.py
Binary Tree Preorder Traversal.py
 
 
Binary Tree Zigzag Level Order Traversal.py
Binary Tree Zigzag Level Order Traversal.py
 
 
Candy.py
Candy.py
 
 
Climbing Stairs.py
Climbing Stairs.py
 
 
Clone Graph.py
Clone Graph.py
 
 
Combination Sum II.py
Combination Sum II.py
 
 
Combination Sum.py
Combination Sum.py
 
 
Combinations.py
Combinations.py
 
 
Construct Binary Tree from Inorder and Postorder Traversal.py
Construct Binary Tree from Inorder and Postorder Traversal.py
 
 
Construct Binary Tree from Preorder and Inorder Traversal.py
Construct Binary Tree from Preorder and Inorder Traversal.py
 
 
Container With Most Water.py
Container With Most Water.py
 
 
Convert Sorted Array to Binary Search Tree.py
Convert Sorted Array to Binary Search Tree.py
 
 
Convert Sorted List to Binary Search Tree.py
Convert Sorted List to Binary Search Tree.py
 
 
Copy List with Random Pointer.py
Copy List with Random Pointer.py
 
 
Count and Say.py
Count and Say.py
 
 
Decode Ways.py
Decode Ways.py
 
 
Distinct Subsequences.py
Distinct Subsequences.py
 
 
Edit Distance.py
Edit Distance.py
 
 
Evaluate Reverse Polish Notation.py
Evaluate Reverse Polish Notation.py
 
 
First Missing Positive.py
First Missing Positive.py
 
 
Flatten Binary Tree to Linked List.py
Flatten Binary Tree to Linked List.py
 
 
Gas Station.py
Gas Station.py
 
 
Generate Parentheses.py
Generate Parentheses.py
 
 
Gray Code.py
Gray Code.py
 
 
Implement strStr().py
Implement strStr().py
 
 
Insert Interval.py
Insert Interval.py
 
 
Insertion Sort List.py
Insertion Sort List.py
 
 
Integer to Roman.py
Integer to Roman.py
 
 
Interleaving String.py
Interleaving String.py
 
 
Jump Game II.py
Jump Game II.py
 
 
Jump Game.py
Jump Game.py
 
 
LRU Cache.py
LRU Cache.py
 
 
Largest Rectangle in Histogram.py
Largest Rectangle in Histogram.py
 
 
Length of Last Word.py
Length of Last Word.py
 
 
Letter Combinations of a Phone Number.py
Letter Combinations of a Phone Number.py
 
 
Linked List Cycle II.py
Linked List Cycle II.py
 
 
Linked List Cycle.py
Linked List Cycle.py
 
 
Longest Common Prefix.py
Longest Common Prefix.py
 
 
Longest Consecutive Sequence.py
Longest Consecutive Sequence.py
 
 
Longest Palindromic Substring.py
Longest Palindromic Substring.py
 
 
Longest Substring Without Repeating Characters.py
Longest Substring Without Repeating Characters.py
 
 
Longest Valid Parentheses.py
Longest Valid Parentheses.py
 
 
Max Points on a Line.py
Max Points on a Line.py
 
 
Maximal Rectangle.py
Maximal Rectangle.py
 
 
Maximum Depth of Binary Tree.py
Maximum Depth of Binary Tree.py
 
 
Maximum Subarray.py
Maximum Subarray.py
 
 
Merge Intervals.py
Merge Intervals.py
 
 
Merge Sorted Array.py
Merge Sorted Array.py
 
 
Merge Two Sorted Lists.py
Merge Two Sorted Lists.py
 
 
Merge k Sorted Lists.py
Merge k Sorted Lists.py
 
 
Minimum Depth of Binary Tree.py
Minimum Depth of Binary Tree.py
 
 
Minimum Path Sum.py
Minimum Path Sum.py
 
 
Multiply Strings.py
Multiply Strings.py
 
 
N-Queens II.py
N-Queens II.py
 
 
N-Queens.py
N-Queens.py
 
 
Next Permutation.py
Next Permutation.py
 
 
Palindrome Number.py
Palindrome Number.py
 
 
Palindrome Partitioning II.py
Palindrome Partitioning II.py
 
 
Palindrome Partitioning.py
Palindrome Partitioning.py
 
 
Partition List.py
Partition List.py
 
 
Pascal's Triangle II.py
Pascal's Triangle II.py
 
 
Pascal's Triangle.py
Pascal's Triangle.py
 
 
Path Sum II.py
Path Sum II.py
 
 
Path Sum.py
Path Sum.py
 
 
Permutation Sequence.py
Permutation Sequence.py
 
 
Permutations II.py
Permutations II.py
 
 
Permutations.py
Permutations.py
 
 
Plus One.py
Plus One.py
 
 
Populating Next Right Pointers in Each Node II.py
Populating Next Right Pointers in Each Node II.py
 
 
Populating Next Right Pointers in Each Node.py
Populating Next Right Pointers in Each Node.py
 
 
Pow(x, n).py
Pow(x, n).py
 
 
README.md
README.md
 
 
Remove Duplicates from Sorted Array II.py
Remove Duplicates from Sorted Array II.py
 
 
Remove Duplicates from Sorted Array.py
Remove Duplicates from Sorted Array.py
 
 
Remove Duplicates from Sorted List II.py
Remove Duplicates from Sorted List II.py
 
 
Remove Duplicates from Sorted List.py
Remove Duplicates from Sorted List.py
 
 
Remove Element.py
Remove Element.py
 
 
Remove Nth Node From End of List.py
Remove Nth Node From End of List.py
 
 
Reorder List.py
Reorder List.py
 
 
Restore IP Addresses.py
Restore IP Addresses.py
 
 
Reverse Integer.py
Reverse Integer.py
 
 
Reverse Linked List II.py
Reverse Linked List II.py
 
 
Reverse Words in a String.py
Reverse Words in a String.py
 
 
Roman to Integer.py
Roman to Integer.py
 
 
Rotate Image.py
Rotate Image.py
 
 
Rotate List.py
Rotate List.py
 
 

Repository files navigation

Leetcode

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.

Feel free to submit pull requests for submitting more elegant solution.

###List of idiomatic Python I prefer (as of now)

  • Prefer list comprehension over map / filter.
	# from Gray Code

	# Bad
	def grayCode(self, n):
        return map(lambda x: (x / 2) ^ x, range(1 << n))

	# Idiomatic
	def grayCode(self, n):
        return [(x / 2) ^ x for x in range(1 << n)]
  • Prefer using in keyword over repetitive variable in conditional statement.
	# from Post Order Traversal

	# Bad
	if parent.right == None or parent.right == prev

	# Idiomatic
	if parent.right in (None, prev):
  • Prefer using docstring over single line comment when describing functionality of a method.
	# from Search Insert Position

	# Bad
	# Iterative solution is also fine.
	def searchInsert(self, A, target):

	# Idiomatic
	def searchInsert(self, A, target):
    	"""Iterative solution is also fine.
    	"""
  • Prefer implicit evaluation of condition (e.g. if, while, etc.) over explicit comparison in condition.
    Notice empty list and dictionary will be evaluated to False, so that is very handy.
	# from Binary Tree Preorder Traversal

	# Bad
	while len(stack) > 0:
		current = stack.pop()

	# Idiomatic
	while stack:
		current = stack.pop()
  • Prefer is None over == None. Notice is looks for referential equality, and None is a singleton.
    The fundamental reason for this preference is much improved speed, and == can be overriden by __eq__.
	# from Binary Tree Preorder Traversal

	# Bad
	if root == None:

	# Idiomatic
	if root is None:

One interesting side note in Python regarding this is on Stackoverflow. Trust me, that is worth your 60 seconds of time. But that only works in REPL though, not on a executable python file.

READ THIS or above two rules will only do you harm:
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.

  • Consider using enumerate when index accessing looks verbose
	# from Two Sum

	# Bad
	for i in range(len(nums)):
        if target - nums[i] in lookup:
            return (lookup[target - nums[i]] + 1, i + 1)
        lookup[nums[i]] = i

    # Idiomatic
    for i, num in enumerate(nums):
        if target - num in lookup:
            return (lookup[target - num] + 1, i + 1)
        lookup[num] = i
  • Readability counts.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." Martin Fowler is right.

	# from Edit Distance

	# Bad
	def minDistance(self, word1, word2):
        distance = [[i] for i in range(len(word1) + 1)]
        distance[0] = [i for i in range(len(word2) + 1)]
        for i in range(1, len(word1) + 1):
            for j in range(1, len(word2) + 1):
                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])))
        return distance[-1][-1]

    # Idiomatic
    def minDistance(self, word1, word2):
        distance = [[i] for i in range(len(word1) + 1)]
        distance[0] = [i for i in range(len(word2) + 1)]
        for i in range(1, len(word1) + 1):
            for j in range(1, len(word2) + 1):
                deletion = distance[i - 1][j] + 1
                addition = distance[i][j - 1] + 1
                substitution = distance[i - 1][j - 1]
                if word1[i - 1] != word2[j - 1]:
                    substitution += 1
                distance[i].append(min(deletion, addition, substitution))
        return distance[-1][-1]

About

And they say Python is slow...

Resources

Readme
Activity

Stars

0 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.