diff options
author | Santo Cariotti <santo@dcariotti.me> | 2024-06-27 22:39:22 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2024-06-27 22:39:22 +0200 |
commit | d653d3598d71fea30d45d118e3d046a3aed53ac1 (patch) | |
tree | 9d6720f83e4752aa1dd54daf549734b67747b60a | |
parent | 259580d38338ef53e6f98b2b279d1d4aa8ecff04 (diff) |
Uncommented test files
-rw-r--r-- | progs/a284.py | 24 | ||||
-rw-r--r-- | progs/a339.py | 52 | ||||
-rw-r--r-- | progs/a394.py | 46 | ||||
-rw-r--r-- | progs/a52.py | 16 | ||||
-rw-r--r-- | progs/a641.py | 12 | ||||
-rw-r--r-- | progs/a679.py | 2 | ||||
-rw-r--r-- | progs/a745.py | 32 | ||||
-rw-r--r-- | src/ast/nodes/AtomNode.java | 4 |
8 files changed, 91 insertions, 97 deletions
diff --git a/progs/a284.py b/progs/a284.py index cfd429d..41d2914 100644 --- a/progs/a284.py +++ b/progs/a284.py @@ -1,13 +1,11 @@ -# FIXME: multiple variable assignment in for loop
-#
-# def is_Isomorphic(str1,str2):
-# dict_str1 = {}
-# dict_str2 = {}
-# for i, value in enumerate(str1):
-# dict_str1[value] = dict_str1.get(value,[]) + [i]
-# for j, value in enumerate(str2):
-# dict_str2[value] = dict_str2.get(value,[]) + [j]
-# if sorted(dict_str1.values()) == sorted(dict_str2.values()):
-# return True
-# else:
-# return False
\ No newline at end of file +def is_Isomorphic(str1, str2):
+ dict_str1 = {}
+ dict_str2 = {}
+ for i, value in enumerate(str1):
+ dict_str1[value] = dict_str1.get(value, []) + [i]
+ for j, value in enumerate(str2):
+ dict_str2[value] = dict_str2.get(value, []) + [j]
+ if sorted(dict_str1.values()) == sorted(dict_str2.values()):
+ return True
+ else:
+ return False
diff --git a/progs/a339.py b/progs/a339.py index 95724d2..53fcb7e 100644 --- a/progs/a339.py +++ b/progs/a339.py @@ -1,28 +1,28 @@ -# FIXME: chiamata a funzione definita dopo
-#
-# def heap_sort(arr):
-# heapify(arr)
-# end = len(arr) - 1
-# while end > 0:
-# arr[end], arr[0] = arr[0], arr[end]
-# shift_down(arr, 0, end - 1)
-# end -= 1
-# return arr
+def heap_sort(arr):
+ heapify(arr)
+ end = len(arr) - 1
+ while end > 0:
+ arr[end], arr[0] = arr[0], arr[end]
+ shift_down(arr, 0, end - 1)
+ end -= 1
+ return arr
-# def heapify(arr):
-# start = len(arr) // 2
-# while start >= 0:
-# shift_down(arr, start, len(arr) - 1)
-# start -= 1
-# def shift_down(arr, start, end):
-# root = start
-# while root * 2 + 1 <= end:
-# child = root * 2 + 1
-# if child + 1 <= end and arr[child] < arr[child + 1]:
-# child += 1
-# if child <= end and arr[root] < arr[child]:
-# arr[root], arr[child] = arr[child], arr[root]
-# root = child
-# else:
-# return
+def heapify(arr):
+ start = len(arr) // 2
+ while start >= 0:
+ shift_down(arr, start, len(arr) - 1)
+ start -= 1
+
+
+def shift_down(arr, start, end):
+ root = start
+ while root * 2 + 1 <= end:
+ child = root * 2 + 1
+ if child + 1 <= end and arr[child] < arr[child + 1]:
+ child += 1
+ if child <= end and arr[root] < arr[child]:
+ arr[root], arr[child] = arr[child], arr[root]
+ root = child
+ else:
+ return
diff --git a/progs/a394.py b/progs/a394.py index 25f8f3e..d977184 100644 --- a/progs/a394.py +++ b/progs/a394.py @@ -1,24 +1,22 @@ -# FIXME: multiple variable assignment in for loop
-#
-# def func(nums, k):
-# import collections
-# d = collections.defaultdict(int)
-# for row in nums:
-# for i in row:
-# d[i] += 1
-# temp = []
-# import heapq
-# for key, v in d.items():
-# if len(temp) < k:
-# temp.append((v, key))
-# if len(temp) == k:
-# heapq.heapify(temp)
-# else:
-# if v > temp[0][0]:
-# heapq.heappop(temp)
-# heapq.heappush(temp, (v, key))
-# result = []
-# while temp:
-# v, key = heapq.heappop(temp)
-# result.append(key)
-# return result
\ No newline at end of file +def func(nums, k):
+ import collections
+ d = collections.defaultdict(int)
+ for row in nums:
+ for i in row:
+ d[i] += 1
+ temp = []
+ import heapq
+ for key, v in d.items():
+ if len(temp) < k:
+ temp.append((v, key))
+ if len(temp) == k:
+ heapq.heapify(temp)
+ else:
+ if v > temp[0][0]:
+ heapq.heappop(temp)
+ heapq.heappush(temp, (v, key))
+ result = []
+ while temp:
+ v, key = heapq.heappop(temp)
+ result.append(key)
+ return result
diff --git a/progs/a52.py b/progs/a52.py index 1d116bc..131af01 100644 --- a/progs/a52.py +++ b/progs/a52.py @@ -1,8 +1,8 @@ -# FIXME: multiple variable assignment in for loop
-#
-# from collections import defaultdict
-# def grouping_dictionary(l):
-# d = defaultdict(list)
-# for k, v in l:
-# d[k].append(v)
-# return d
\ No newline at end of file +from collections import defaultdict
+
+
+def grouping_dictionary(l):
+ d = defaultdict(list)
+ for k, v in l:
+ d[k].append(v)
+ return d
diff --git a/progs/a641.py b/progs/a641.py index 52b0f6a..1b7e4bb 100644 --- a/progs/a641.py +++ b/progs/a641.py @@ -1,7 +1,5 @@ -# FIXME: multiple variable assignment in for loop
-#
-# def count_first_elements(test_tup):
-# for count, ele in enumerate(test_tup):
-# if isinstance(ele, tuple):
-# break
-# return (count)
\ No newline at end of file +def count_first_elements(test_tup):
+ for count, ele in enumerate(test_tup):
+ if isinstance(ele, tuple):
+ break
+ return count
diff --git a/progs/a679.py b/progs/a679.py index 97472a6..6f1d11c 100644 --- a/progs/a679.py +++ b/progs/a679.py @@ -10,4 +10,4 @@ def find_last_occurrence(A, x): right = mid - 1
else:
left = mid + 1
- return result
\ No newline at end of file + return result
diff --git a/progs/a745.py b/progs/a745.py index e8ad671..1d9fe80 100644 --- a/progs/a745.py +++ b/progs/a745.py @@ -1,17 +1,15 @@ -# FIXME: unpacking assignment
-#
-# def find_rotation_count(A):
-# (left, right) = (0, len(A) - 1)
-# while left <= right:
-# if A[left] <= A[right]:
-# return left
-# mid = (left + right) // 2
-# next = (mid + 1) % len(A)
-# prev = (mid - 1 + len(A)) % len(A)
-# if A[mid] <= A[next] and A[mid] <= A[prev]:
-# return mid
-# elif A[mid] <= A[right]:
-# right = mid - 1
-# elif A[mid] >= A[left]:
-# left = mid + 1
-# return -1
\ No newline at end of file +def find_rotation_count(A):
+ (left, right) = (0, len(A) - 1)
+ while left <= right:
+ if A[left] <= A[right]:
+ return left
+ mid = (left + right) // 2
+ next = (mid + 1) % len(A)
+ prev = (mid - 1 + len(A)) % len(A)
+ if A[mid] <= A[next] and A[mid] <= A[prev]:
+ return mid
+ elif A[mid] <= A[right]:
+ right = mid - 1
+ elif A[mid] >= A[left]:
+ left = mid + 1
+ return -1
diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index 16281d6..cea617f 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -53,7 +53,8 @@ public class AtomNode implements Node { Pattern noneVariable = Pattern.compile("^(None)$"); Pattern booleanVariable = Pattern.compile("^(True|False)$"); Pattern reservedWords = Pattern.compile("^(continue|break|int|float)$"); - // this regex should match every possible atom name written in this format: CHAR (CHAR | DIGIT)* + // this regex should match every possible atom name written in this format: CHAR + // (CHAR | DIGIT)* Pattern simpleVariable = Pattern.compile("^[a-zA-Z][a-zA-Z0-9]*$", Pattern.CASE_INSENSITIVE); Matcher noneVariableMatcher = noneVariable.matcher(this.val); @@ -87,6 +88,7 @@ public class AtomNode implements Node { @Override public String toPrint(String prefix) { + // FIXME: can be a testlist_comp with two expr and two atoms if (val != null) { return prefix + "Atom(" + val + ")\n"; } |