There is a simple decryption algorithm, there was no problem to compose it.
What are we doing? We read the lines and for each letter, after which there is a number we do the conversion word[0] * int(word[1:]) . That is, duplicate the letter a specified number of times.
The code is:
import re def readfile(filename): result = [] with open (filename, "r") as file: lines = file.readlines() for line in lines: line = re.findall(r'\w\d*', line) result.append(line) return result def decrypt(lines): result = [] for line in lines: for word in line: source_line = word[0] * int(word[1:]) result.append(source_line) return result def encrypt(lines): pass def writefile(filename, lines): with open (filename, "w") as file: for line in lines: file.write(line) lines = readfile("input.txt") decrypted_lines = decrypt(lines) writefile("output.txt", decrypted_lines) The code works on the input data
a2b4c3f5 c2b1m5v6 It turns out:
aabbbbcccfffffccbmmmmmvvvvvv The question is: how to implement the encryption algorithm, that is, the search for repeated letters in a row. And with this implement it as efficiently as possible?
That is, how to most effectively from, for example, such a line:
aabbbbccceeeeeff Get this:
a2b4c3e5f2
kwill pick up very quickly, in most cases, generally from the first time - andreymal