namemkazaza

Untitled

Oct 28th, 2020
1,160
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. with open('input.txt', 'r') as line:
  2.     lis = [i.rstrip() for i in line .readlines() if i.rstrip()]
  3. out = open('output.txt', 'w')
  4. for i in sorted(set(lis)):
  5.     out.write(" ".join([i, str(lis.count(i)), "\n"]))
  6.  
  7.  
  8. from collections import Counter
  9. from pathlib import Path
  10.  
  11. d = dict(Counter(Path(r'input.txt').read_text().split()))
  12. Path(r'output.txt').write_text(
  13.     '\n'.join([x + ' ' + str(d[x]) for x in sorted(d, key=lambda x: (-d[x], x))])
  14. )
  15.  
  16.  
  17. from collections import Counter
  18. from pathlib import Path
  19.  
  20. d = dict(Counter(Path(r'input.txt').read_text().split()))
  21. Path(r'output.txt').write_text(
  22.     '\n'.join([x + ' ' + str(d[x]) for x in sorted(d, key=lambda x: (-d[x], x))])
  23. )
  24.  
  25.  
  26. def remove_punctuation(s):
  27.     punctuation = [".", ",", ":", ";", "!", "?"]
  28.     answer = ''
  29.     for i in range(len(s)):
  30.         if s[i] not in punctuation:
  31.             answer += s[i]
  32.     return answer
  33.  
  34.  
  35. input_f = open("input.txt", "r")
  36. output_f = open("output.txt", "w")
  37. frequency = dict()
  38. words = []
  39. for line in input_f.read().split('\n')[:-1]:
  40.     words.extend([remove_punctuation(word).lower() for word in line.strip().split()])
  41.  
  42. for word in words:
  43.     if word in frequency:
  44.         frequency[word] += 1
  45.     else:
  46.         frequency[word] = 1
  47.  
  48. keys_by_frequency = sorted(frequency.keys(), key=lambda x: frequency[x], reverse=True)
  49. answer_list = []
  50. temp = []
  51. frequency_num = frequency[keys_by_frequency[0]]
  52. for key in keys_by_frequency:
  53.     if frequency[key] == frequency_num:
  54.         temp.append(key)
  55.     else:
  56.         frequency_num = frequency[key]
  57.         answer_list += sorted(temp)
  58.         temp = [key]
  59. answer_list += sorted(temp)
  60. answer = ''
  61. for key in answer_list:
  62.     answer += f'{key} {frequency[key]}\n'
  63. output_f.write(answer[:-1])
  64.  
  65. input_f.close()
  66. output_f.close()
  67.  
  68.  
  69. input_f = open("input.txt", "r")
  70. output_f = open("output.txt", "w")
  71.  
  72. l = []
  73. for command in input_f.readlines():
  74.     action = command[0]
  75.     num = int(command[1:-1])
  76.     if action == "+":
  77.         l.append(num)
  78.     elif action == "-":
  79.         try:
  80.             l.remove(num)
  81.         except:
  82.             output_f.write("ERROR")
  83.             quit(0)
  84. if len(l) != 0:
  85.     output_f.write(' '.join([str(num) for num in sorted(l)]) + '\n')
  86. else:
  87.     output_f.write("EMPTY")
  88.  
  89. input_f.close()
  90. output_f.close()
  91.  
  92.  
  93. input_f = open("input.txt", "r")
  94. output_f = open("output.txt", "w")
  95.  
  96. l = []
  97. for command in input_f.readlines():
  98.     action = command[0]
  99.     i = int(command[1:].split()[0]) - 1
  100.     if action != "-":
  101.         text = command[len(str(i)) + 2:]
  102.     if action == "+":
  103.         l.insert(i, text)
  104.     elif action == "-" or action == "*":
  105.         try:
  106.             del l[i]
  107.             if action == "*":
  108.                 l.insert(i, text)
  109.         except:
  110.             output_f.write("ERROR")
  111.             quit(0)
  112.  
  113. if len(l) != 0:
  114.     for line in l:
  115.         output_f.write(line)
  116. else:
  117.     output_f.write("EMPTY")
  118.  
  119. input_f.close()
  120. output_f.close()
RAW Paste Data