LordMirai

practice

Jul 11th, 2022
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.80 KB | None | 0 0
  1. from time import sleep
  2.  
  3. if __name__ != "__main__":
  4.     print("external run, terminate.")
  5.     exit(0)
  6.  
  7.  
  8. class TextUtils:
  9.     @staticmethod
  10.     def slowPrint(message, delay=0.2):
  11.         for let in message:
  12.             sleep(delay)
  13.             print(let, end="")
  14.  
  15.  
  16. def test_slowprint():
  17.     TextUtils.slowPrint("Hello, this should print nicely :)")
  18.  
  19.  
  20. # Thread(target=test_slowprint).start()
  21. def clamp(input, min, max):
  22.     if input < min:
  23.         return min
  24.     if input > max:
  25.         return max
  26.     return input
  27.  
  28.  
  29. class Solution:
  30.     @staticmethod
  31.     def findTime(self, S1, S2):
  32.         time = 0
  33.         index = 0
  34.  
  35.         for let in S2:
  36.             cur_index = 0
  37.             for key in S1:
  38.                 if key == let:
  39.                     time += abs(index - cur_index)
  40.                     index = cur_index
  41.                     break
  42.                 cur_index += 1
  43.         return time
  44.  
  45.     @staticmethod
  46.     def substrCount(self, s, k):
  47.         sub_count = 0
  48.         max_index = len(s) - k + 1
  49.         substring_array = []
  50.         array_index = 0
  51.  
  52.         for index in range(max_index):
  53.             subs = s[index:index + k]
  54.             try:
  55.                 if substring_array[array_index] == subs:
  56.                     next_letter = s[index + k + 1]
  57.                     print("next: " + next_letter)
  58.                     if subs.__contains__(next_letter):
  59.                         subs += next_letter
  60.             except IndexError:
  61.                 pass  # silent pass
  62.             substring_array.append(subs)
  63.             array_index += 1
  64.             print(f"[{array_index}]: {subs}")
  65.             print(substring_array)
  66.  
  67.         return sub_count
  68.  
  69.     @staticmethod
  70.     def reformat_string(S, K):
  71.         S = S.upper().replace("-", "")
  72.         temp = ""
  73.         group_array = []
  74.         for lt in S[::-1]:
  75.             if len(temp) == K:
  76.                 group_array.append(temp[::-1])
  77.                 temp = lt
  78.             else:
  79.                 temp += lt
  80.  
  81.         if temp != "":
  82.             group_array.append(temp[::-1])
  83.         return '-'.join(group_array[::-1])
  84.  
  85.     @staticmethod
  86.     def findAndReplace(S, Q, index, sources, targets):
  87.         word_array = []
  88.         active_index = 0
  89.         skip = 0
  90.         for key, letter in enumerate(S):
  91.             if skip > 0:
  92.                 skip -= 1
  93.                 continue
  94.             try:
  95.                 dna = False
  96.                 if key == index[active_index]:
  97.                     if S[key: key + len(sources[active_index])] == sources[active_index]:
  98.                         word_array.append(targets[active_index])
  99.                         skip += (len(sources[active_index]) - 1)
  100.                         active_index += 1
  101.                         dna = True
  102.                 if not dna:
  103.                     word_array.append(letter)
  104.             except IndexError:
  105.                 pass
  106.         return ''.join(word_array)
  107.  
  108.     @staticmethod
  109.     def look_and_say(n):
  110.         str = "1"
  111.         row = 1
  112.         while row < n:
  113.             # print(f"[{row}] ",end='')
  114.             str = Solution.parse_look(str)
  115.             row += 1
  116.  
  117.         return str
  118.  
  119.     @staticmethod
  120.     def parse_look(strn):
  121.         # print(f"Parsing '{str}'")
  122.         new_str = ""
  123.         segment_array = []
  124.         active_number = 0
  125.         buffer = 0
  126.  
  127.         for num in strn:
  128.             # print(f"In string we have: number {num} active {active_number} buffer {buffer}")
  129.             if active_number != num:
  130.                 app = active_number != 0
  131.                 if app:
  132.                     segment_array.append(str(buffer) + str(active_number))
  133.                 active_number = num
  134.                 buffer = 1
  135.             else:
  136.                 buffer += 1
  137.         segment_array.append(str(buffer) + str(active_number))
  138.  
  139.         new_str = ''.join(segment_array)
  140.         return new_str
  141.  
  142.     @staticmethod
  143.     def duplicates(arr, n):
  144.         dict = {}
  145.         out = []
  146.         for i in range(len(arr)):
  147.             if arr[i] in dict:
  148.                 dict[arr[i]] += 1
  149.                 if dict[arr[i]] == 1:
  150.                     out.append(arr[i])
  151.             else:
  152.                 dict[arr[i]] = 0
  153.         if out:
  154.             out.sort()
  155.             return out
  156.         return [-1]
  157.  
  158.     @staticmethod
  159.     def rearrange(arr, n):
  160.         positive = []
  161.         negative = []
  162.         output = []
  163.  
  164.         for num in arr:
  165.             if num >= 0:
  166.                 positive.append(num)
  167.             else:
  168.                 negative.append(num)
  169.         max_size = max(len(positive), len(negative))
  170.         for ind in range(max_size):
  171.             try:
  172.                 output.append(positive[ind])
  173.             except IndexError:
  174.                 pass
  175.             try:
  176.                 output.append(negative[ind])
  177.             except IndexError:
  178.                 pass
  179.  
  180.         return output
  181.  
  182.     @staticmethod
  183.     def maxSumPath(arr1, arr2, m, n):
  184.         start = arr1 if min(arr1[0], arr2[0]) in arr1 else arr2
  185.         stop = arr2 if start == arr1 else arr1
  186.  
  187.         print(start, stop, sep=" :: ")
  188.  
  189.         ind = 0
  190.         path = 0
  191.         for el in start:
  192.             print(el, end=", ")
  193.             path += el
  194.             try:
  195.                 ind = stop.index(el)
  196.                 break
  197.             except ValueError:
  198.                 pass
  199.         for el in stop[ind + 1:]:
  200.             print(el, end=", ")
  201.             path += el
  202.  
  203.         print("\n")
  204.         return path
  205.  
  206.     @staticmethod
  207.     def row_with_max_1s(arr, n, m):
  208.         row_ones = []
  209.         for row in arr:
  210.             ones = 0
  211.             for elem in row:
  212.                 if elem == 1:
  213.                     ones += 1
  214.             row_ones.append(ones)
  215.         max_ones = -1
  216.         winning_index = -1
  217.         for key, r in enumerate(row_ones):
  218.             if r > max_ones:
  219.                 max_ones = r
  220.                 winning_index = key
  221.  
  222.         if max_ones == 0:
  223.             return -1
  224.         return winning_index
  225.  
  226.     @staticmethod
  227.     def areIsomorphic(str1, str2):
  228.         first_letters = []
  229.         second_letters = []
  230.         for let in str1:
  231.             if let not in first_letters:
  232.                 first_letters.append(let)
  233.  
  234.         for let in str2:
  235.             if let not in second_letters:
  236.                 second_letters.append(let)
  237.  
  238.         if len(first_letters) != len(second_letters):
  239.             return 0
  240.         return 1
  241.  
  242.     @staticmethod
  243.     def trappingWater(arr, n=None):
  244.         heights = []
  245.         collected_amount = 0
  246.         for tower in arr:
  247.             heights.append(tower)
  248.         heights.sort()
  249.         # print(heights)
  250.         water_level = heights[-2]
  251.         # print("water level ", water_level)
  252.  
  253.         for tower in heights:
  254.             am = water_level - clamp(tower, 0, water_level)
  255.             # print(tower, am)
  256.             collected_amount += am
  257.         return collected_amount
  258.  
  259.     @staticmethod
  260.     def ExcelColumn(N):
  261.         temp = N
  262.         st = ''
  263.         while temp:
  264.             lt = temp % 26
  265.             if lt == 0:
  266.                 st += 'Z'
  267.                 temp = temp // 26 - 1
  268.             else:
  269.                 st += chr(64 + lt)
  270.                 temp = temp // 26
  271.         return st[::-1]
  272.  
  273.  
  274. sol = Solution()
  275.  
  276.  
  277. def pangram(s):
  278.     s = s.lower()
  279.     let = [str(chr(x)) for x in range(97, 123)]
  280.     used = {}
  281.     # print(let, len(let))
  282.     for letter in s:
  283.         if letter in let:
  284.             used[letter] = True
  285.     return int(len(used) == 26)
  286.  
  287.  
  288. def add_binary(A, B):
  289.     rev_A = A[::-1]
  290.     rev_B = B[::-1]
  291.     sum = ""
  292.     carry = 0
  293.  
  294.     for bit in range(max(len(rev_A), len(rev_B))):
  295.         result = '0'
  296.         try:
  297.             op_1 = rev_A[bit]
  298.         except IndexError:
  299.             op_1 = '0'
  300.         try:
  301.             op_2 = rev_B[bit]
  302.         except IndexError:
  303.             op_2 = '0'
  304.  
  305.         # print(op_1, op_2)
  306.         if op_1 == '0':
  307.             if op_2 == '0':
  308.                 if carry == 1:
  309.                     result = '1'
  310.                     carry = 0
  311.                 else:
  312.                     result = '0'
  313.  
  314.             elif op_2 == '1':
  315.                 if carry == 1:
  316.                     result = '0'
  317.                     carry = 1
  318.                 else:
  319.                     result = '1'
  320.  
  321.         elif op_1 == '1':
  322.             if op_2 == '0':
  323.                 if carry == 1:
  324.                     result = '0'
  325.                     carry = 1
  326.                 else:
  327.                     result = '1'
  328.             elif op_2 == '1':
  329.                 if carry == 1:
  330.                     result = '1'
  331.                     carry = 1
  332.                 else:
  333.                     result = '0'
  334.                     carry = 1
  335.         sum += result
  336.  
  337.     if carry == 1:
  338.         sum += "1"
  339.  
  340.     while sum[-1] == '0':
  341.         sum = sum[:-1]
  342.  
  343.     return sum[::-1]
  344.  
  345.  
  346.  
Advertisement
Add Comment
Please, Sign In to add comment