algos_and_structures/algocode/two_pointers/unurlify.py

86 lines
2.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Дан список s. Нужно заменить все вхождения %20 на пробелы в списке s, а оставшиеся лишние символы заменить на #, сохранив длину списка. В качестве ответа верни изменённый список s.
Пример 1:
Ввод: s = ["h","e","l","l","o","%","2","0","w","o","r","l","d"]
Вывод: ["h","e","l","l","o"," ","w","o","r","l","d","#","#"]
Пример 2:
Ввод: s = ["a","%","2","0","b","%","2","0","%","2","0", "c"]
Вывод: ["a"," ","b"," ", " ", "c","#","#","#","#","#","#"]
Ограничения:
len(s) >= 1
"""
from typing import List
def solve(s: List[str]) -> List[str]:
i, j = 0, 0
s_len = len(s)
for i in range(0, s_len):
if j >= s_len:
while i < s_len:
s[i] = "#"
i += 1
break
s[i] = s[j]
if s[i] == "%":
s[i] = " "
j += 3
i += 1
else:
i += 1
j += 1
return s
print(solve(s=["h", "e", "l", "l", "o", "%", "2", "0", "w", "o", "r", "l", "d"]))
print(solve(s=["a", "%", "2", "0", "b", "%", "2", "0", "%", "2", "0", "c"]))
# i
# "a","%","2","0","b","%","2","0","%","2","0", "c"
# j
#
# --- i+=1 j+=1 ---
#
# i
# "a","%","2","0","b","%","2","0","%","2","0", "c"
# j
#
# Заменяем процент на пробел
#
# --- i+=1 j+=3 ---
#
# i
# "a"," ","2","0","b","%","2","0","%","2","0", "c"
# j
#
# --- i+=1 j+=1 ---
#
# i
# "a"," ","b","0","b","%","2","0","%","2","0", "c"
# j
# Замена s[i] = s[j] приводит к процентной итерации
#
# --- i+=1 j+=3 ---
#
# i
# "a"," ","b"," ","b","%","2","0","%","2","0", "c"
# j
#
# Замена s[i] = s[j] приводит к процентной итерации
#
# --- i+=1 j+=3 ---
#
# i
# "a"," ","b"," "," ","%","2","0","%","2","0", "c"
# j
#
# --- i+=1 j+=1 ---
#
# i
# "a"," ","b"," "," ","c","2","0","%","2","0", "c"
# j