Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.64 KB | None | 0 0
  1. import os
  2.  
  3.  
  4. def count(file):
  5.     n = 0
  6.     for char in file.read():
  7.         if char == " ":
  8.             n += 1
  9.     n += 1
  10.     return n
  11.  
  12.  
  13. def separate(file, n):
  14.     separator = True
  15.     b = open('C:/Users/chuxo/Desktop/b.txt', 'w')
  16.     c = open('C:/Users/chuxo/Desktop/c.txt', 'w')
  17.     space_num = 0
  18.     for char in file.read():
  19.         if char != " ":
  20.             if separator:
  21.                 b.write(char)
  22.             else:
  23.                 c.write(char)
  24.         else:
  25.             space_num += 1
  26.             if separator:
  27.                 b.write(' ')
  28.                 if space_num == n:
  29.                     separator = False
  30.                     space_num = 0
  31.             elif not separator:
  32.                 c.write(' ')
  33.                 if space_num == n:
  34.                     separator = True
  35.                     space_num = 0
  36.  
  37.     b.close()
  38.     c.close()
  39.  
  40.  
  41. def sym_count(file):
  42.     n = 0
  43.     for char in file.read():
  44.         n += 1
  45.     return n
  46.  
  47.  
  48. def read(file, s_count, num_count, counter):
  49.     string = ""
  50.     for i in range(s_count):
  51.         sym = file.read(1)
  52.         if sym == '':
  53.             break
  54.         if sym != ' ':
  55.             string = string + sym
  56.         else:
  57.             num_b = int(string)
  58.             return num_b
  59.  
  60.     if counter == num_count:
  61.         num_b = int(string)
  62.         return num_b
  63.  
  64.  
  65. def compare(a, b, c, n):
  66.     num_in_group_c = 0
  67.     num_in_group_b = 0
  68.     c_marker = True
  69.     b_marker = True
  70.  
  71.     count_b = count(b)
  72.     b.seek(0)
  73.     sym_count_b = sym_count(b)
  74.     b.seek(0)
  75.  
  76.     count_c = count(c)
  77.     c.seek(0)
  78.     sym_count_c = sym_count(c)
  79.     c.seek(0)
  80.  
  81.     counter_b = 1
  82.     counter_c = 1
  83.  
  84.     looper_b = count_b
  85.     looper_c = count_c
  86.  
  87.     for i in range(looper_b):
  88.         if b_marker:
  89.             num_b = read(b, sym_count_b, count_b, counter_b)
  90.             counter_b += 1
  91.  
  92.         for j in range(looper_c):
  93.             if num_in_group_c == n:
  94.                 a.write(str(num_b) + " ")
  95.                 num_in_group_b += 1
  96.                 for k in range(num_in_group_b, n):
  97.                     a.write(str(read(b, sym_count_b, count_b, counter_b)) + " ")
  98.                     counter_b += 1
  99.                     looper_b -= 1
  100.                 num_in_group_b = 0
  101.                 num_in_group_c = 0
  102.                 b_marker = True
  103.                 c_marker = True
  104.                 break
  105.  
  106.             if num_in_group_b == n:
  107.                 a.write(str(num_c) + " ")
  108.                 num_in_group_c += 1
  109.                 for k in range(num_in_group_c, n):
  110.                     a.write(str(read(c, sym_count_c, count_c, counter_c)) + " ")
  111.                     counter_c += 1
  112.                 num_in_group_b = 0
  113.                 num_in_group_c = 0
  114.                 b_marker = True
  115.                 c_marker = True
  116.                 break
  117.  
  118.             if c_marker:
  119.                 num_c = read(c, sym_count_c, count_c, counter_c)
  120.                 counter_c += 1
  121.  
  122.             if num_c <= num_b:
  123.                 a.write(str(num_c) + " ")
  124.                 b_marker = False
  125.                 c_marker = True
  126.                 num_in_group_c += 1
  127.                 if num_in_group_c == n:
  128.                     looper_c += 1
  129.             else:
  130.                 a.write(str(num_b) + " ")
  131.                 c_marker = False
  132.                 b_marker = True
  133.                 num_in_group_b += 1
  134.  
  135.                 if num_in_group_b == n:
  136.                     looper_c += 1
  137.                 else:
  138.                     break
  139.  
  140.  
  141. def sort(n):
  142.     a = open('C:/Users/chuxo/Desktop/a.txt', 'w')
  143.     b = open('C:/Users/chuxo/Desktop/b.txt', 'r')
  144.     c = open('C:/Users/chuxo/Desktop/c.txt', 'r')
  145.     b_count = count(b)
  146.     b.seek(0)
  147.     c_count = count(c)
  148.     c.seek(0)
  149.  
  150.     if b_count >= c_count:
  151.         compare(a, b, c, n)
  152.     else:
  153.         compare(a, c, b, n)
  154.  
  155.     a.close()
  156.     b.close()
  157.     c.close()
  158.  
  159.  
  160. def main():
  161.     a = open('C:/Users/chuxo/Desktop/a.txt', 'r')
  162.     n_count = count(a)
  163.     print(n_count)
  164.     a.close()
  165.     nums_in_group = 1
  166.  
  167.     while nums_in_group < n_count:
  168.         a = open('C:/Users/chuxo/Desktop/a.txt', 'r')
  169.         separate(a, nums_in_group)
  170.         a.close()
  171.  
  172.         # Убираем последний пробел
  173.         c = open('C:/Users/chuxo/Desktop/c.txt', 'rb+')
  174.         b = open('C:/Users/chuxo/Desktop/b.txt', 'rb+')
  175.         c.seek(-1, os.SEEK_END)
  176.         if c.read(1) == b' ':
  177.             c.seek(-1, os.SEEK_END)
  178.             c.truncate()
  179.         else:
  180.             b.seek(-1, os.SEEK_END)
  181.             b.truncate()
  182.         c.close()
  183.         b.close()
  184.  
  185.         sort(nums_in_group)
  186.  
  187.         nums_in_group = nums_in_group * 2
  188.  
  189.  
  190. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement