Skip to content

Commit

Permalink
print() is a function in Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Jul 15, 2018
1 parent 9109012 commit ff14185
Show file tree
Hide file tree
Showing 169 changed files with 449 additions and 279 deletions.
4 changes: 3 additions & 1 deletion Python/candy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(n)
# Space: O(n)
#
Expand All @@ -11,6 +12,7 @@
#

import operator
from functools import reduce

class Solution:
# @param ratings, a list of integer
Expand All @@ -29,5 +31,5 @@ def candy(self, ratings):

if __name__ == "__main__":
result = Solution().candy([1, 2, 3, 2, 3, 5, 2, 5])
print result
print(result)

3 changes: 2 additions & 1 deletion Python/climbing-stairs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(n)
# Space: O(1)
#
Expand Down Expand Up @@ -29,4 +30,4 @@ def climbStairs1(self, n):

if __name__ == "__main__":
result = Solution().climbStairs(2)
print result
print(result)
3 changes: 2 additions & 1 deletion Python/combination-sum-ii.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(k * C(n, k))
# Space: O(k)
#
Expand Down Expand Up @@ -42,4 +43,4 @@ def combinationSumRecu(self, candidates, result, start, intermediate, target):
if __name__ == "__main__":
candidates, target = [10, 1, 2, 7, 6, 1, 5], 8
result = Solution().combinationSum2(candidates, target)
print result
print(result)
3 changes: 2 additions & 1 deletion Python/combination-sum.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(k * n^k)
# Space: O(k)
#
Expand Down Expand Up @@ -37,4 +38,4 @@ def combinationSumRecu(self, candidates, result, start, intermediate, target):
if __name__ == "__main__":
candidates, target = [2, 3, 6, 7], 7
result = Solution().combinationSum(candidates, target)
print result
print(result)
3 changes: 2 additions & 1 deletion Python/combinations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(k * C(n, k))
# Space: O(k)

Expand Down Expand Up @@ -61,4 +62,4 @@ def combineDFS(n, start, intermediate, k, result):

if __name__ == "__main__":
result = Solution().combine(4, 2)
print result
print(result)
7 changes: 4 additions & 3 deletions Python/compare-version-numbers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(n)
# Space: O(1)

Expand Down Expand Up @@ -99,6 +100,6 @@ def compareVersion4(self, version1, version2):


if __name__ == "__main__":
print Solution().compareVersion("21.0", "121.1.0")
print Solution().compareVersion("01", "1")
print Solution().compareVersion("1", "1.0")
print(Solution().compareVersion("21.0", "121.1.0"))
print(Solution().compareVersion("01", "1"))
print(Solution().compareVersion("1", "1.0"))
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(n)
# Space: O(n)
#
Expand Down Expand Up @@ -37,6 +38,6 @@ def buildTreeRecu(self, lookup, postorder, inorder, post_end, in_start, in_end):
inorder = [2, 1, 3]
postorder = [2, 3, 1]
result = Solution().buildTree(inorder, postorder)
print result.val
print result.left.val
print result.right.val
print(result.val)
print(result.left.val)
print(result.right.val)
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(n)
# Space: O(n)
#
Expand Down Expand Up @@ -37,6 +38,6 @@ def buildTreeRecu(self, lookup, preorder, inorder, pre_start, in_start, in_end):
preorder = [1, 2, 3]
inorder = [2, 1, 3]
result = Solution().buildTree(preorder, inorder)
print result.val
print result.left.val
print result.right.val
print(result.val)
print(result.left.val)
print(result.right.val)
3 changes: 2 additions & 1 deletion Python/container-with-most-water.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(n)
# Space: O(1)
#
Expand Down Expand Up @@ -26,4 +27,4 @@ def maxArea(self, height):
if __name__ == "__main__":
height = [1, 2, 3, 4, 3, 2, 1, 5]
result = Solution().maxArea(height)
print result
print(result)
7 changes: 4 additions & 3 deletions Python/convert-sorted-array-to-binary-search-tree.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(n)
# Space: O(logn)
#
Expand Down Expand Up @@ -49,6 +50,6 @@ def perfect_tree_pivot(self, n):
if __name__ == "__main__":
num = [1, 2, 3]
result = Solution().sortedArrayToBST(num)
print result.val
print result.left.val
print result.right.val
print(result.val)
print(result.left.val)
print(result.right.val)
7 changes: 4 additions & 3 deletions Python/convert-sorted-list-to-binary-search-tree.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(n)
# Space: O(logn)
#
Expand Down Expand Up @@ -44,6 +45,6 @@ def sortedListToBSTRecu(self, start, end):
head.next = ListNode(2)
head.next.next = ListNode(3)
result = Solution().sortedListToBST(head)
print result.val
print result.left.val
print result.right.val
print(result.val)
print(result.left.val)
print(result.right.val)
7 changes: 4 additions & 3 deletions Python/copy-list-with-random-pointer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(n)
# Space: O(1)
#
Expand Down Expand Up @@ -71,6 +72,6 @@ def copyRandomList(self, head):
head.next = RandomListNode(2)
head.random = head.next
result = Solution().copyRandomList(head)
print result.label
print result.next.label
print result.random.label
print(result.label)
print(result.next.label)
print(result.random.label)
3 changes: 2 additions & 1 deletion Python/count-and-say.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(n * 2^n)
# Space: O(2^n)
#
Expand Down Expand Up @@ -33,6 +34,6 @@ def getNext(self, seq):

if __name__ == "__main__":
for i in xrange(1, 4):
print Solution().countAndSay(i)
print(Solution().countAndSay(i))


3 changes: 2 additions & 1 deletion Python/counting-bits.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(n)
# Space: O(n)

Expand Down Expand Up @@ -52,4 +53,4 @@ def countBits2(self, num):
if __name__ == '__main__':
s = Solution()
r = s.countBits2(5)
print r
print(r)
2 changes: 1 addition & 1 deletion Python/course-schedule-iii.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def scheduleCourse(self, courses):
:type courses: List[List[int]]
:rtype: int
"""
courses.sort(key=lambda(t, end): end)
courses.sort(key=lambda t_end: t_end[1])
max_heap = []
now = 0
for t, end in courses:
Expand Down
3 changes: 2 additions & 1 deletion Python/course-schedule.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(|V| + |E|)
# Space: O(|E|)
#
Expand Down Expand Up @@ -72,4 +73,4 @@ def canFinish(self, numCourses, prerequisites):


if __name__ == "__main__":
print Solution().canFinish(1, [])
print(Solution().canFinish(1, []))
3 changes: 2 additions & 1 deletion Python/decode-ways.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(n)
# Space: O(1)
#
Expand Down Expand Up @@ -36,4 +37,4 @@ def numDecodings(self, s):

if __name__ == "__main__":
for i in ["0", "10", "10", "103", "1032", "10323"]:
print Solution().numDecodings(i)
print(Solution().numDecodings(i))
3 changes: 2 additions & 1 deletion Python/distinct-subsequences.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(n^2)
# Space: O(n)
#
Expand Down Expand Up @@ -28,5 +29,5 @@ def numDistinct(self, S, T):
S = "rabbbit"
T = "rabbit"
result = Solution().numDistinct(S, T)
print result
print(result)

9 changes: 5 additions & 4 deletions Python/divide-two-integers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(logn) = O(1)
# Space: O(1)
#
Expand Down Expand Up @@ -46,7 +47,7 @@ def divide2(self, dividend, divisor):
return min(max(-2147483648, res), 2147483647)

if __name__ == "__main__":
print Solution().divide(123, 12)
print Solution().divide(123, -12)
print Solution().divide(-123, 12)
print Solution().divide(-123, -12)
print(Solution().divide(123, 12))
print(Solution().divide(123, -12))
print(Solution().divide(-123, 12))
print(Solution().divide(-123, -12))
7 changes: 4 additions & 3 deletions Python/dungeon-game.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(m * n)
# Space: O(m + n)
#
Expand Down Expand Up @@ -98,10 +99,10 @@ def DP(self, dungeon, HP):
dungeon = [[ -2, -3, 3], \
[ -5, -10, 1], \
[ 10, 30, -5]]
print Solution().calculateMinimumHP(dungeon)
print(Solution().calculateMinimumHP(dungeon))

dungeon = [[ -200]]
print Solution().calculateMinimumHP(dungeon)
print(Solution().calculateMinimumHP(dungeon))

dungeon = [[0, -3]]
print Solution().calculateMinimumHP(dungeon)
print(Solution().calculateMinimumHP(dungeon))
7 changes: 4 additions & 3 deletions Python/edit-distance.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(n * m)
# Space: O(n + m)
#
Expand Down Expand Up @@ -53,6 +54,6 @@ def minDistance(self, word1, word2):
return distance[-1][-1]

if __name__ == "__main__":
print Solution().minDistance("Rabbit", "Racket")
print Solution2().minDistance("Rabbit", "Rabket")
print Solution().minDistance("Rabbit", "Rabbitt")
print(Solution().minDistance("Rabbit", "Racket"))
print(Solution2().minDistance("Rabbit", "Rabket"))
print(Solution().minDistance("Rabbit", "Rabbitt"))
7 changes: 4 additions & 3 deletions Python/evaluate-reverse-polish-notation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(n)
# Space: O(n)
#
Expand Down Expand Up @@ -25,6 +26,6 @@ def evalRPN(self, tokens):
return numerals.pop()

if __name__ == "__main__":
print Solution().evalRPN(["2", "1", "+", "3", "*"])
print Solution().evalRPN(["4", "13", "5", "/", "+"])
print Solution().evalRPN(["10","6","9","3","+","-11","*","/","*","17","+","5","+"])
print(Solution().evalRPN(["2", "1", "+", "3", "*"]))
print(Solution().evalRPN(["4", "13", "5", "/", "+"]))
print(Solution().evalRPN(["10","6","9","3","+","-11","*","/","*","17","+","5","+"]))
3 changes: 2 additions & 1 deletion Python/excel-sheet-column-number.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(n)
# Space: O(1)

Expand Down Expand Up @@ -29,4 +30,4 @@ def titleToNumber(self, s):


if __name__ == "__main__":
print Solution().titleToNumber("AAAB")
print(Solution().titleToNumber("AAAB"))
3 changes: 2 additions & 1 deletion Python/excel-sheet-column-title.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(logn)
# Space: O(1)

Expand Down Expand Up @@ -30,4 +31,4 @@ def convertToTitle(self, n):

if __name__ == "__main__":
for i in xrange(1, 29):
print Solution().convertToTitle(i)
print(Solution().convertToTitle(i))
3 changes: 2 additions & 1 deletion Python/factorial-trailing-zeroes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(logn) = O(1)
# Space: O(1)
#
Expand All @@ -16,4 +17,4 @@ def trailingZeroes(self, n):
return result

if __name__ == "__main__":
print Solution().trailingZeroes(100)
print(Solution().trailingZeroes(100))
3 changes: 2 additions & 1 deletion Python/find-all-numbers-disappeared-in-an-array.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(n)
# Space: O(1)

Expand Down Expand Up @@ -54,4 +55,4 @@ def findDisappearedNumbers3(self, nums):
if __name__ == '__main__':
s = Solution()
r = s.findDisappearedNumbers([4, 3, 2, 7, 8, 2, 3, 1])
print r
print(r)
5 changes: 3 additions & 2 deletions Python/find-minimum-in-rotated-sorted-array-ii.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(logn) ~ O(n)
# Space: O(1)
#
Expand Down Expand Up @@ -55,5 +56,5 @@ def findMin(self, nums):


if __name__ == "__main__":
print Solution().findMin([3, 1, 1, 2, 2, 3])
print Solution2().findMin([2, 2, 2, 3, 3, 1])
print(Solution().findMin([3, 1, 1, 2, 2, 3]))
print(Solution2().findMin([2, 2, 2, 3, 3, 1]))
11 changes: 6 additions & 5 deletions Python/find-minimum-in-rotated-sorted-array.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(logn)
# Space: O(1)
#
Expand Down Expand Up @@ -49,8 +50,8 @@ def findMin(self, nums):


if __name__ == "__main__":
print Solution().findMin([1])
print Solution().findMin([1, 2])
print Solution().findMin([2, 1])
print Solution().findMin([3, 1, 2])
print Solution().findMin([2, 3, 1])
print(Solution().findMin([1]))
print(Solution().findMin([1, 2]))
print(Solution().findMin([2, 1]))
print(Solution().findMin([3, 1, 2]))
print(Solution().findMin([2, 3, 1]))
3 changes: 2 additions & 1 deletion Python/find-peak-element.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# Time: O(logn)
# Space: O(1)

Expand Down Expand Up @@ -38,4 +39,4 @@ def findPeakElement(self, nums):

if __name__ == "__main__":
# print Solution().findPeakElement([1,2,1])
print Solution().findPeakElement([1,2,3,1])
print(Solution().findPeakElement([1,2,3,1]))
Loading

0 comments on commit ff14185

Please sign in to comment.