-
Notifications
You must be signed in to change notification settings - Fork 0
Sourcery refactored master branch #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ def lcs(s1, s2): | |
| cols = len(s1) + 1 | ||
| rows = len(s2) + 1 | ||
|
|
||
| t = [[0 for i in range(cols)] for i in range(rows)] | ||
| t = [[0 for _ in range(cols)] for _ in range(rows)] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| max_length = 0 | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ def lcs(s1, s2): | |
| cols = len(s1) + 1 | ||
| rows = len(s2) + 1 | ||
|
|
||
| t = [[0 for i in range(cols)] for i in range(rows)] | ||
| t = [[0 for _ in range(cols)] for _ in range(rows)] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| max_length = 0 | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,20 +13,16 @@ | |
| """ | ||
|
|
||
| def find_seq(arr, n): | ||
| s = set() | ||
|
|
||
| for num in arr: | ||
| s.add(num) | ||
|
|
||
| s = set(arr) | ||
| ans = 0 | ||
| elements = [] | ||
|
|
||
| for i in range(n): | ||
| temp = [] | ||
|
|
||
| if arr[i] - 1 not in s: | ||
| j = arr[i] | ||
|
|
||
| temp = [] | ||
|
Comment on lines
-16
to
+24
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| while j in s: | ||
| temp.append(j) | ||
| j += 1 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| def longest_palindromic_substring_DP(s): | ||
|
|
||
| S = [[False for i in range(len(s))] for j in range(len(s))] | ||
| S = [[False for _ in range(len(s))] for _ in range(len(s))] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| max_palindrome = "" | ||
|
|
||
|
|
@@ -23,10 +23,10 @@ def longest_palindromic_substring_expansion(s): | |
| max_palindrome = "" | ||
|
|
||
| for i in range(len(s) * 2 - 1): | ||
| # This is when you are "on" an actual character | ||
| # o = offset, ind = current character | ||
| o = 0 | ||
| if i % 2 == 0: | ||
| # This is when you are "on" an actual character | ||
| # o = offset, ind = current character | ||
| o = 0 | ||
|
Comment on lines
+26
to
-29
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
| ind = i // 2 | ||
| while ind + o < len(s) and ind - o >= 0: | ||
| if(s[ind + o] != s[ind - o]): | ||
|
|
@@ -35,9 +35,6 @@ def longest_palindromic_substring_expansion(s): | |
| max_palindrome = s[ind-o:ind+o + 1] | ||
| o += 1 | ||
| else: | ||
| # This is when you are "in the middle of" two characters | ||
| # o = offset, sind = start char, eind = end char | ||
| o = 0 | ||
| sind = i // 2 | ||
| eind = i // 2 + 1 | ||
| while sind - o >= 0 and eind + o < len(s): | ||
|
|
@@ -54,4 +51,4 @@ def longest_palindromic_substring_expansion(s): | |
|
|
||
| ans_DP = longest_palindromic_substring_DP(input_string) | ||
| ans_expansion = longest_palindromic_substring_expansion(input_string) | ||
| print("DP Solution: {}, Expansion Solution: {}".format(ans_DP, ans_expansion)) | ||
| print(f"DP Solution: {ans_DP}, Expansion Solution: {ans_expansion}") | ||
|
Comment on lines
-57
to
+54
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,15 +39,14 @@ def find_length(arr, k): | |
| for i in range(0, len(mod_arr)): | ||
| if mod_arr[i] == 0: | ||
| length += 1 | ||
| elif mod_arr[i] in hash_table: | ||
| if length < (i - mod_arr[i]): | ||
| length = i - mod_arr[i] | ||
| start = mod_arr[i] | ||
| end = i - 1 # i-1 because the current number is not to considered as it makes the sum not divisible by k | ||
|
|
||
| else: | ||
| if mod_arr[i] not in hash_table: | ||
| hash_table[mod_arr[i]] = i | ||
| else: | ||
| if length < (i - mod_arr[i]): | ||
| length = i - mod_arr[i] | ||
| start = mod_arr[i] | ||
| end = i - 1 # i-1 because the current number is not to considered as it makes the sum not divisible by k | ||
|
|
||
| hash_table[mod_arr[i]] = i | ||
|
Comment on lines
+42
to
+49
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return length, arr[start:end+1] | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,16 +7,11 @@ | |
|
|
||
| def find_activities(arr): | ||
| n = len(arr) | ||
| selected = [] | ||
|
|
||
| arr.sort(key = lambda x: x[1]) | ||
|
|
||
| i = 0 | ||
|
|
||
| # since it is a greedy algorithm, the first acitivity is always | ||
| # selected because it is the most optimal choice at that point | ||
| selected.append(arr[i]) | ||
|
|
||
| selected = [arr[i]] | ||
|
Comment on lines
-10
to
+14
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
| for j in range(1, n): | ||
| start_time_next_activity = arr[j][0] | ||
| end_time_prev_activity = arr[i][1] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,20 +18,19 @@ def cost(arr, A, B): | |
|
|
||
| for i in range(n): | ||
| j = 0 | ||
|
|
||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| while j < m: | ||
| if arr[i][j] == '*': # tile is already there | ||
| j += 1 | ||
| continue | ||
|
|
||
| if j == m - 1: # if j is pointing to last tile, you can use only 1*1 tile | ||
| ans += A | ||
| elif arr[i][j+1] == '.': | ||
| ans += min(2 * A, B) | ||
| j += 1 | ||
| else: | ||
| if arr[i][j+1] == '.': | ||
| ans += min(2 * A, B) | ||
| j += 1 | ||
| else: | ||
| ans += A | ||
| ans += A | ||
|
|
||
| j += 1 | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,19 @@ | ||
| def dijkstra(graph, start, end): | ||
| shortest_distance = {} | ||
| non_visited_nodes = {} | ||
| for i in graph: | ||
| non_visited_nodes[i] = graph[i] | ||
|
|
||
| non_visited_nodes = {i: graph[i] for i in graph} | ||
| infinit = float('inf') | ||
|
|
||
| for no in non_visited_nodes: | ||
| shortest_distance[no] = infinit | ||
| shortest_distance = {no: infinit for no in non_visited_nodes} | ||
| shortest_distance[start] = 0 | ||
|
|
||
| while non_visited_nodes != {}: | ||
| shortest_extracted_node = None | ||
| for i in non_visited_nodes: | ||
| if shortest_extracted_node is None: | ||
| shortest_extracted_node = i | ||
| elif shortest_distance[i] < shortest_distance[shortest_extracted_node]: | ||
| if ( | ||
| shortest_extracted_node is None | ||
| or shortest_distance[i] | ||
| < shortest_distance[shortest_extracted_node] | ||
| ): | ||
| shortest_extracted_node = i | ||
|
|
||
|
Comment on lines
-2
to
-20
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| for no_v, Weight in graph[shortest_extracted_node]: | ||
| if Weight + shortest_distance[shortest_extracted_node] < shortest_distance[no_v]: | ||
| shortest_distance[no_v] = Weight + shortest_distance[shortest_extracted_node] | ||
|
|
@@ -31,7 +27,7 @@ def dijkstra(graph, start, end): | |
|
|
||
| cities, origin, destiny = map(int, input().split()) | ||
| graph = {i:[] for i in range(1, cities+1)} | ||
| for i in range(cities-1): | ||
| for _ in range(cities-1): | ||
|
Comment on lines
-34
to
+30
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| u, v, w = map(int, input().split()) | ||
| graph[v].append((u, w)) | ||
| graph[u].append((v, w)) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ def find_platforms(arrival, departure): | |
| plat += 1 | ||
| i += 1 | ||
|
|
||
| elif arrival[i] > departure[j]: | ||
| else: | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| plat -= 1 | ||
| j += 1 | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,46 +3,28 @@ | |
| class divisors: | ||
|
|
||
| def findAllDivisors(self, n): | ||
| result = list() | ||
|
|
||
| for i in range(1, n+1): | ||
| if (n%i == 0): | ||
| result.append(i) | ||
|
|
||
| return result | ||
| return [i for i in range(1, n+1) if (n%i == 0)] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def divisorsCount(self, n): | ||
| divs = self.findAllDivisors(n) | ||
| return len(divs) | ||
|
|
||
| def oddFactors(self, n): | ||
| result = list() | ||
|
|
||
| for i in range(1, n+1): | ||
| if (n%i == 0 and i%2==1): | ||
| result.append(i) | ||
|
|
||
| return result | ||
| return [i for i in range(1, n+1) if (n%i == 0 and i%2==1)] | ||
|
Comment on lines
-19
to
+13
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def oddFactorsSum(self, n): | ||
| divs = self.evenFactors(n) | ||
| return sum(divs) | ||
|
|
||
| def evenFactors(self, n): | ||
| result = list() | ||
|
|
||
| for i in range(1, n+1): | ||
| if (n%i == 0 and i%2==0): | ||
| result.append(i) | ||
|
|
||
| return result | ||
| return [i for i in range(1, n+1) if (n%i == 0 and i%2==0)] | ||
|
Comment on lines
-32
to
+20
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def evenFactorsSum(self, n): | ||
| divs = self.evenFactors(n) | ||
| return sum(divs) | ||
|
|
||
| def primeFactors(self, n): | ||
| result = list() | ||
| result = [] | ||
|
Comment on lines
-45
to
+27
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| while n % 2 == 0: | ||
| result.append(2) | ||
|
|
@@ -52,10 +34,10 @@ def primeFactors(self, n): | |
| while n % i== 0: | ||
| result.append(i) | ||
| n = n / i | ||
|
|
||
| if n > 2: | ||
| result.append(n) | ||
|
|
||
| return result | ||
|
|
||
| def primeFactorsSum(self, n): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,8 @@ def factorial(number): | |
|
|
||
| if number == 0: | ||
| return 1 | ||
| else: | ||
| for num in range(1, number+1): | ||
| answer = answer * num | ||
| for num in range(1, number+1): | ||
| answer = answer * num | ||
|
Comment on lines
-9
to
+10
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return answer | ||
|
|
||
| if __name__ == '__main__': | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,7 @@ | |
|
|
||
| def factorial(number): | ||
|
|
||
| if number == 0 or number == 1: | ||
| return 1 | ||
|
|
||
| answer = number * factorial(number - 1) | ||
|
|
||
| return answer | ||
| return 1 if number in [0, 1] else number * factorial(number - 1) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| if __name__ == '__main__': | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,10 +23,7 @@ def gcd(x, y): | |
| def gcd(x, y): | ||
| if x == 0: | ||
| return y | ||
| if y == 0: | ||
| return x | ||
|
|
||
| return gcd(y, x % y) | ||
| return x if y == 0 else gcd(y, x % y) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| # Iterative optimized | ||
| def gcd(x, y): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,10 +18,7 @@ def verify_base(x): | |
| try: | ||
| return int(x) | ||
| except: | ||
| if x in bases: | ||
| return bases[x] | ||
| else: | ||
| return default | ||
| return bases[x] if x in bases else default | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| def decimal_value(x): | ||
|
|
@@ -91,10 +88,10 @@ def convert(n, from_base, to_base): | |
| decimal_number += (multi * decimal_value(n[i])) | ||
| multi *= from_base | ||
|
|
||
| if(to_base == 10): | ||
| if (to_base == 10): | ||
| decimal_number = str(decimal_number) | ||
| if(negative): | ||
| decimal_number = '-' + decimal_number | ||
| if negative: | ||
| decimal_number = f'-{decimal_number}' | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| return decimal_number | ||
|
|
||
|
|
@@ -105,8 +102,8 @@ def convert(n, from_base, to_base): | |
| result = to_special_caracter(value) + result | ||
| decimal_number = int((decimal_number - value)/to_base) | ||
|
|
||
| if(negative): | ||
| result = '-' + result | ||
| if negative: | ||
| result = f'-{result}' | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,12 +8,9 @@ def is_perfect_square(num): | |
| :rtype: bool | ||
| """ | ||
| i = 0 | ||
| while i * i < num: | ||
| while i**2 < num: | ||
| i += 1 | ||
| if i * i == num: | ||
| return True | ||
| else: | ||
| return False | ||
| return i**2 == num | ||
|
Comment on lines
-11
to
+13
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| # Test | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ def rec_fib(n): | |
| return rec_fib(n-1)+rec_fib(n-2) | ||
|
|
||
| def binary_rec_fib(n): | ||
| if n == 2 or n == 1: | ||
| if n in [2, 1]: | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return 1 | ||
| elif n == 0: | ||
| return 0 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
knapsackrefactored with the following changes:for-index-underscore)