Created
May 22, 2020 19:05
-
-
Save karims/91c0f7d6229343fd08e3f4ab443ecfa9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| from collections import Counter | |
| import heapq | |
| class Elements(): | |
| def __init__(self, key: int, val: int): | |
| super().__init__() | |
| self.key = key | |
| self.val = val | |
| def __repr__(self): | |
| return f'Key: {self.key}, Val: {self.val}' | |
| def __lt__(self, other): | |
| if self.val < other.val: | |
| return True | |
| elif self.val > other.val: | |
| return False | |
| else: | |
| return self.key < other.key | |
| def insertsort(arr): | |
| count_of_occr = Counter(arr) | |
| lst = [] | |
| for k, v in count_of_occr.items(): | |
| lst.append(Elements(k, v)) | |
| heapq.heapify(lst) | |
| # print(lst) | |
| result = [] | |
| idx = 0 | |
| while lst: | |
| e = heapq.heappop(lst) | |
| for i in range(e.val): | |
| result[idx] = e.key | |
| idx += 1 | |
| return result | |
| a = [4,5,6,5,4,3] | |
| a = [8, 5, 5, 5, 5, 1, 1, 1, 4, 4] | |
| print(insertsort(a)) | |
| ###### | |
| class Solution: | |
| def subarraysDivByK(self, A: List[int], K: int) -> int: | |
| res = 0 | |
| d = [1] + [0] * K | |
| acc = 0 | |
| for a in A: | |
| acc = (acc + a) % K | |
| if d[acc]: | |
| res += d[acc] | |
| d[acc] += 1 | |
| return res | |
| ### | |
| Continuous Subarray Sum | |
| def checkSubarraySum(self, A: List[int], k: int) -> bool: | |
| P = [0] | |
| for x in A: | |
| v = P[-1] + x | |
| if k: v %= abs(k) | |
| P.append(v) | |
| seen = set() | |
| for i in range(len(P) - 3, -1, -1): | |
| seen.add(P[i+2]) | |
| if P[i] in seen: | |
| return True | |
| return False | |
| ## reformat date | |
| def reformateDate(dates): | |
| days2num = {"1st": "01", "2nd": "02", "3rd": "03", "4th": "04", "5th": "05", "6th": "06", | |
| "7th": "07", "8th": "08", "9th": "09", "10th": "10", "11th": "11", "12th": "12", | |
| "13th": "13", "14th": "14", "15th": "15", "16th": "16", "17th": "17", "18th": "18", | |
| "19th": "19", "20th": "20", "21st": "21", "22nd": "22", "23rd": "23", "24th": "24", | |
| "25th": "25", "26th": "26", "27th": "27", "28th": "28", "29th": "29", "30th": "30", "31st": "31"} | |
| month2num = {"Jan": "01", "Feb": "02", "Mar": "03", "Apr": "04", "May": "05", "Jun": "06", | |
| "Jul": "07", "Aug": "08", "Sep": "09", "Oct": "10", "Nov": "11", "Dec": "12"} | |
| results = [] | |
| for date in dates: | |
| day, month, year = date.split() | |
| res = year+"-"+month2num[month]+"-"+days2num[day] | |
| results.append(res) | |
| return results | |
| date_list = ["1st Mar 1984", "2nd Feb 2013", "4th Apr 1990"] | |
| correct_format = reformateDate(date_list) | |
| print(correct_format) | |
| ### Total requests | |
| from collections import defaultdict | |
| def read_file(filename): | |
| host2req = defaultdict(int) | |
| with open(filename) as file: | |
| for line in file: | |
| line_list = line.split() | |
| host2req[line_list[0]] += 1 | |
| file = open("records_"+filename, "w") | |
| for host in host2req: | |
| file.write(host+" "+str(host2req[host])+"\n") | |
| file.close() | |
| file_name = input() | |
| read_file(file_name) | |
| ### Missing words | |
| def missingWords(s, t): | |
| # Write your code here | |
| s_list = s.split() | |
| t_list = t.split() | |
| missing = [] | |
| i = j = 0 | |
| while i < len(s_list) and j < len(t_list): | |
| if s_list[i] == t_list[j]: | |
| i += 1 | |
| j += 1 | |
| else: | |
| missing.append(s_list[i]) | |
| i += 1 | |
| for k in range(i, len(s_list)): | |
| missing.append(s_list[k]) | |
| return missing | |
| #### | |
| def computeParameterValue(sources): | |
| mapping = {} | |
| for idx, source in enumerate(sources): | |
| for kv in source: | |
| k, v = kv.split(":") | |
| mapping[k] = v | |
| results = [] | |
| for key in mapping: | |
| results.append(mapping[key]) | |
| return results | |
| #### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment