-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed a lot of problems from all the progs we need to parse
- Loading branch information
Showing
23 changed files
with
336 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
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 | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,28 @@ | ||
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 | ||
# 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 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
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 | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
from collections import defaultdict | ||
def grouping_dictionary(l): | ||
d = defaultdict(list) | ||
for k, v in l: | ||
d[k].append(v) | ||
return d | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
def count_first_elements(test_tup): | ||
for count, ele in enumerate(test_tup): | ||
if isinstance(ele, tuple): | ||
break | ||
return (count) | ||
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
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 | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.