Advertisement
Adam_mz_

Untitled

Nov 28th, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. import time
  2.  
  3. start_time = time.time()
  4. max_length = [-1, -1, -1]
  5. isPostcode = False
  6.  
  7.  
  8. def chart_to_int(char):
  9. if str(char).isdecimal():
  10. return int(char)
  11. elif abs(ord(char.lower()) - ord('a')) <= 26:
  12. return ord(char.lower()) - ord('a') + 10
  13. else:
  14. return 36
  15.  
  16.  
  17. def radix_sort(strings, sort_by=0):
  18. arr = strings
  19. bins = [[0 for _ in range(0)] for _ in range(37)]
  20.  
  21. max_len = len(arr[0][0])
  22.  
  23. for j in range(max_len - 1, -1, -1):
  24. for i in range(len(arr)):
  25. bins[chart_to_int(str(arr[i][sort_by])[j])].append(arr[i])
  26. arr = []
  27. for num in bins:
  28. if len(num) > 0:
  29. arr.extend(num)
  30. bins = [[0 for _ in range(0)] for _ in range(37)]
  31.  
  32. return arr
  33.  
  34.  
  35. def count_sort(arr, max_el):
  36. count_arr_len = max_el + 1
  37.  
  38. count_arr = [0] * count_arr_len
  39.  
  40. for el in arr:
  41. count_arr[int(el[0])] += 1
  42.  
  43. for j in range(1, count_arr_len):
  44. count_arr[j] += count_arr[j - 1]
  45.  
  46. output_arr = [0] * len(arr)
  47. j = len(arr) - 1
  48. while j >= 0:
  49. current_el = int(arr[j][0])
  50. count_arr[current_el] -= 1
  51. new_pos = count_arr[current_el]
  52. output_arr[new_pos] = [current_el, data[j][1], 0]
  53. j -= 1
  54.  
  55. return output_arr
  56.  
  57.  
  58. def is_phone_number(string):
  59. return string[0] == '+'
  60.  
  61.  
  62. def is_md5(string):
  63. return len(string) == 32
  64.  
  65.  
  66. def is_car_number(string):
  67. if len(string) != 8:
  68. return False
  69. if str(string[0]).isalpha() and str(string[1]).isspace() and str(string[2]).isdigit() and str(
  70. string[3]).isdigit() and str(string[4]).isdigit() and str(string[5]).isspace() and str(
  71. string[6]).isalpha() and str(string[7]).isalpha():
  72. return True
  73. return False
  74.  
  75.  
  76. def is_small_num(string):
  77. if str(string).isdecimal():
  78. if int(string) <= 65535:
  79. return True
  80. return False
  81.  
  82.  
  83. def is_big_number(string):
  84. if str(string).isdecimal():
  85. if int(string) > 65535:
  86. return True
  87. return False
  88.  
  89.  
  90. #
  91. # def is_postcode(string):
  92. # if len(string) >= 8:
  93. # return string[7] == ' '
  94.  
  95. def is_postcode(string):
  96. return len(str(string)) == 6 and str(string).isdecimal()
  97.  
  98.  
  99. def is_date(string):
  100. return '.' in string
  101.  
  102.  
  103. def print_values():
  104. with open('output.txt', 'w') as f:
  105. for i, v, k in data:
  106. if isPostcode:
  107. f.write(f"{str(i).zfill(6)}\t{v}")
  108. else:
  109. f.write(f"{i}\t{v}")
  110.  
  111.  
  112. def process_data(values):
  113. first = values[0][0]
  114. if is_small_num(first):
  115. mx_len = len(str(max_length[0]))
  116. for i in range(len(values)):
  117. # str(values[i][0]).zfill(mx_len)
  118. values[i].append(int(values[i][0]))
  119. elif is_big_number(first):
  120. mx_len = len(str(max_length[0]))
  121.  
  122. for i in range(len(values)):
  123. values[i].append(str(values[i][0]).zfill(mx_len))
  124. elif is_date(first):
  125. for i in range(len(values)):
  126. line = str(values[i][0]).split('.')
  127. values[i].append((line[2].zfill(4) + '.' + line[1].zfill(2) + '.' + line[0].zfill(2)))
  128.  
  129.  
  130. elif is_phone_number(first):
  131. for i in range(len(values)):
  132. vals = values[i][0][1:-1].split('-')
  133. values[i].append(str(vals[0]).zfill(len(str(max_length[0]))) + '-' + str(vals[1]).zfill(
  134. len(str(max_length[1])))
  135.  
  136. + '-' + str(vals[2]).zfill(len(str(max_length[2]))))
  137.  
  138. return values
  139.  
  140.  
  141. data = list()
  142. file_lines = open('input.txt').readlines()
  143. for i in range(len(file_lines)):
  144.  
  145. line = file_lines[i]
  146. if len(line) == 0:
  147. continue
  148. # delim is tab
  149. if '\t' in line:
  150. splt = line.split('\t')
  151. data.append(splt)
  152. isPostcode = is_postcode(splt[0])
  153. if splt[0].isdecimal():
  154. if int(splt[0]) > max_length[0]:
  155. max_length[0] = int(splt[0])
  156.  
  157. elif ' ' in line:
  158. splt = line.split(' ')
  159. data.append(splt)
  160. isPostcode = is_postcode(splt[0])
  161. if splt[0].isdecimal():
  162. if int(splt[0]) > max_length[0]:
  163. max_length[0] = int(splt[0])
  164.  
  165. first = str(data[0][0])
  166. if first.isdecimal():
  167. if not isPostcode:
  168. first = str(max_length[0])
  169. else:
  170. first = str(max_length[0]).zfill(6)
  171. # TODO: COUNTING SORT NG
  172.  
  173. if is_phone_number(first):
  174. for datum in data:
  175. vals = list(map(int,
  176. datum[0][1:len(datum[0])].replace(' ', '-').replace(' ', '-').replace(' ', '-').replace(' ',
  177. '-').split(
  178. '-')[0:-1]))
  179. if vals[0] > max_length[0]:
  180. max_length[0] = vals[0]
  181. if vals[1] > max_length[1]:
  182. max_length[1] = vals[1]
  183. if vals[2] > max_length[2]:
  184. max_length[2] = vals[2]
  185.  
  186. data = process_data(data)
  187. elif is_postcode(first):
  188. isPostcode = True
  189.  
  190. data = process_data(data)
  191. data = count_sort(data, max_length[0])
  192.  
  193. elif is_small_num(first):
  194.  
  195. data = process_data(data)
  196. data = count_sort(data, max_length[0])
  197. elif is_big_number(first):
  198.  
  199. data = process_data(data)
  200. data = radix_sort(data, 2)
  201. elif is_date(first):
  202. data = process_data(data)
  203. data = radix_sort(data, 2)
  204.  
  205. else:
  206. data = process_data(data)
  207. data = radix_sort(data)
  208.  
  209. print_values()
  210.  
  211. # print("--- %s seconds ---" % (time.time() - start_time))
  212. # 0
  213. # 00
  214. # 000
  215. # 0000
  216. # ...
  217. # up to 20
  218. # Numbers, Dates, Phone numbers
  219. #
  220. #
  221. #
  222. #
  223.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement