From b87cab2f933d75606e6735a8289844e087a01740 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Mon, 24 Jul 2023 22:38:30 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- Python/101.SymmetricTree.py | 3 +-- Python/150.EvaluateReversePolishNotation.py | 12 ++++++++---- Python/496_nextgreaterelement.py | 7 +++---- Python/50.Powxn.py | 2 +- Python/5_LongestPalindromicSubstring.py | 5 +---- Python/994_Rotting_Oranges.py | 17 +++++++---------- Python/SmallestDifference.py | 17 ++++++++++------- Python/ThreeNumbersSum.py | 6 +++--- Python/baseK.py | 8 ++++---- Python/contains-duplicate.py | 7 ++++--- Python/detect-capital.py | 4 +--- Python/jumpGame.py | 10 ++-------- Python/longest-valid-parentheses.py | 4 ++-- Python/rotate-array.py | 2 +- Python/search-in-rotated-sorted-array.py | 14 ++++++-------- Python/single-number.py | 10 +++++----- Python/wildcard-matching.py | 6 +++--- 17 files changed, 62 insertions(+), 72 deletions(-) diff --git a/Python/101.SymmetricTree.py b/Python/101.SymmetricTree.py index 0057d870..ea86603c 100644 --- a/Python/101.SymmetricTree.py +++ b/Python/101.SymmetricTree.py @@ -14,8 +14,7 @@ def isSymmetric(self, root: TreeNode) -> bool: level.append(node.val if node else None) length -= 1 if node: - queue.append(node.left) - queue.append(node.right) + queue.extend((node.left, node.right)) i = 0 j = len(level) - 1 while i <= j: diff --git a/Python/150.EvaluateReversePolishNotation.py b/Python/150.EvaluateReversePolishNotation.py index bb8ebe05..e3c2ba49 100644 --- a/Python/150.EvaluateReversePolishNotation.py +++ b/Python/150.EvaluateReversePolishNotation.py @@ -12,8 +12,12 @@ def evalRPN(self, tokens: List[str]) -> int: else: y = stack.pop() x = stack.pop() - if token == '+': stack.append(x + y) - if token == '-': stack.append(x - y) - if token == '*': stack.append(x * y) - if token == '/': stack.append(int(x / y)) + if token == '*': + stack.append(x * y) + elif token == '+': + stack.append(x + y) + elif token == '-': + stack.append(x - y) + elif token == '/': + stack.append(int(x / y)) return stack[0] \ No newline at end of file diff --git a/Python/496_nextgreaterelement.py b/Python/496_nextgreaterelement.py index 73faac76..062056e0 100644 --- a/Python/496_nextgreaterelement.py +++ b/Python/496_nextgreaterelement.py @@ -1,10 +1,9 @@ class Solution: def check(self, number, nums1, nums2): index = nums2.index(number) - for _ in range(index, len(nums2)): - if(nums2[_]>number): - return nums2[_] - return -1 + return next( + (nums2[_] for _ in range(index, len(nums2)) if (nums2[_] > number)), -1 + ) def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: return [self.check(x, nums1, nums2) for x in nums1] diff --git a/Python/50.Powxn.py b/Python/50.Powxn.py index fea275cd..9c275a15 100644 --- a/Python/50.Powxn.py +++ b/Python/50.Powxn.py @@ -6,7 +6,7 @@ class Solution: def myPow(self, x: float, n: int) -> float: if not n: return 1 - power = self.myPow(x, int(n/2)) + power = self.myPow(x, n // 2) if n % 2 == 0: return power * power else: diff --git a/Python/5_LongestPalindromicSubstring.py b/Python/5_LongestPalindromicSubstring.py index 1f9fa91a..9d13af85 100644 --- a/Python/5_LongestPalindromicSubstring.py +++ b/Python/5_LongestPalindromicSubstring.py @@ -28,10 +28,7 @@ def longestPalindrome(self, s: str) -> str: left,right = i,i ls = "" while 0<=left int: n = len(grid) m = len(grid[0]) - + count = 0 rotten = [] for i in range(n): @@ -15,13 +15,13 @@ def orangesRotting(self, grid: List[List[int]]) -> int: count+=1 elif grid[i][j] == 1: count+=1 - - + + if count == 0: return 0 - + cycles = 0 - + while rotten: count-=len(rotten) newRotten = [] @@ -32,11 +32,8 @@ def orangesRotting(self, grid: List[List[int]]) -> int: self.destroy(grid,i,j-1,n,m,newRotten) rotten = newRotten cycles+=1 - - if count>0: - return -1 - else: - return cycles-1 + + return -1 if count>0 else cycles-1 def destroy(self, grid, i, j, n, m, rotten): if 0<=i= k : + while n >= k: summation = summation + n%k - n=n//k - print(n) + n //= k + print(n) return (summation + n) \ No newline at end of file diff --git a/Python/contains-duplicate.py b/Python/contains-duplicate.py index 53591d67..b48f64d2 100644 --- a/Python/contains-duplicate.py +++ b/Python/contains-duplicate.py @@ -6,8 +6,9 @@ class Solution: def containsDuplicate(self, nums: List[int]) -> bool: count = {} - for n in nums : - if count.get(n) != None : + for n in nums: + if count.get(n) is None: + count[n] = 1 + else: return True - count[n] = 1 return False diff --git a/Python/detect-capital.py b/Python/detect-capital.py index 234e8873..034c3931 100644 --- a/Python/detect-capital.py +++ b/Python/detect-capital.py @@ -32,6 +32,4 @@ def detectCapitalUse(self, word: str) -> bool: num = int(str(num),2) #Checking if it matches any of the valid number - if num==(1<<(size-1)) or num==(0<<(size-1)) or num==((1<= 0: if nums[ptr2] >= ptr1 - ptr2: ptr1 = ptr2 - ptr2 -= 1 - else : - ptr2 -= 1 - - if ptr1 == 0: - return True - else: - return False + ptr2 -= 1 + return ptr1 == 0 print(canJump[3,2,1,0,4]) \ No newline at end of file diff --git a/Python/longest-valid-parentheses.py b/Python/longest-valid-parentheses.py index 78f06ac9..d82a900c 100644 --- a/Python/longest-valid-parentheses.py +++ b/Python/longest-valid-parentheses.py @@ -9,11 +9,11 @@ def longestValidParentheses(self, s): ans=0 stack=[-1] for i in range(len(s)): - if(s[i]=='('): + if (s[i]=='('): stack.append(i) else: stack.pop() - if(len(stack)==0): + if not stack: stack.append(i) else: ans=max(ans,i-stack[-1]) diff --git a/Python/rotate-array.py b/Python/rotate-array.py index 1ad93b26..460d22bf 100644 --- a/Python/rotate-array.py +++ b/Python/rotate-array.py @@ -4,7 +4,7 @@ def rotate(self, nums: List[int], k: int) -> None: Do not return anything, modify nums in-place instead. """ lenz = len(nums) - k = k % lenz + k %= lenz if k == 0: return nums nums += nums[:-k] diff --git a/Python/search-in-rotated-sorted-array.py b/Python/search-in-rotated-sorted-array.py index fadf888c..8219f906 100644 --- a/Python/search-in-rotated-sorted-array.py +++ b/Python/search-in-rotated-sorted-array.py @@ -15,20 +15,18 @@ def search(self, nums: List[int], target: int) -> int: mid = (low+high)//2 if nums[mid]==target: return mid - - # if left half is sorted + + # if left half is sorted if nums[low]<=nums[mid]: if nums[low]<=target int: - d = dict() - for i in range(len(nums)) : - if nums[i] not in d : - d[nums[i]] = 1 + d = {} + for num in nums: + if num not in d: + d[num] = 1 else: - d[nums[i]] += 1 + d[num] += 1 for key, value in d.items() : if value == 1 : return key \ No newline at end of file diff --git a/Python/wildcard-matching.py b/Python/wildcard-matching.py index 127afc20..5ef168dc 100644 --- a/Python/wildcard-matching.py +++ b/Python/wildcard-matching.py @@ -48,8 +48,8 @@ def isMatch(self, s: str, p: str) -> bool: m,n = len(p),len(s) if len(p) - p.count("*") > len(s): return False - - dp = [[False]*(n+1) for i in range(m+1)] + + dp = [[False]*(n+1) for _ in range(m+1)] for i in range(m+1): for j in range(n+1): if j==0 and i==0: @@ -58,7 +58,7 @@ def isMatch(self, s: str, p: str) -> bool: dp[i][j] = False elif j==0: dp[i][j] = (p[i-1]=="*" and dp[i-1][j]) - elif p[i-1] == s[j-1] or p[i-1]=='?': + elif p[i - 1] in [s[j - 1], '?']: dp[i][j] = dp[i-1][j-1] elif p[i-1]=='*': dp[i][j] = dp[i-1][j] or dp[i][j-1]