Pagini recente » Cod sursa (job #587335) | Cod sursa (job #1669517) | Cod sursa (job #1398798) | Cod sursa (job #2299563) | Cod sursa (job #2758225)
file = 'secv8.in'
with open(file, 'rt') as f:
content = f.readlines()
content = [line.strip().split() for line in content]
# print(content)
n = int(content[0][0])
reverse_op = int(content[0][1])
ops = content[1:]
def insert(string, position, element):
if string == '':
string = element
else:
string = string[:position - 1] + element + string[position - 1:]
return string
def access(string, position):
return string[position - 1]
def delete(string, start, end):
string = string[:start - 1] + string[end:]
return string
def reverse(string, start, end):
def reverse_slicing(substring):
return substring[::-1]
string = string[:start - 1] + reverse_slicing(string[start - 1:end]) + string[end:]
return string
def spaces(string):
return " ".join(string)
string = ''
for op in ops:
if op[0] == 'I':
string = insert(string, int(op[1]), op[2])
# print(spaces(string))
elif op[0] == 'R':
string = reverse(string, int(op[1]), int(op[2]))
# print(spaces(string))
elif op[0] == 'A':
# print(access(string, int(op[1])))
with open('secv8.out', 'a') as f:
f.write(access(string, int(op[1])))
f.write('\n')
elif op[0] == 'D':
string = delete(string, int(op[1]), int(op[2]))
# print(spaces(string))
with open('secv8.out', 'a') as f:
f.write(spaces(string))