76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
# "Ручной" аналог функции replace()
|
|
def replace_string(string, to_find, to_replace):
|
|
result = ""
|
|
ln_find = len(to_find)
|
|
ln_str = len(string)
|
|
|
|
i = 0
|
|
while i < ln_str:
|
|
if i + ln_find <= ln_str:
|
|
found = True
|
|
for j in range(ln_find):
|
|
if to_find[j] != string[i + j]:
|
|
found = False
|
|
break
|
|
|
|
if found:
|
|
result += to_replace
|
|
i += ln_find
|
|
else:
|
|
result += string[i]
|
|
i += 1
|
|
else:
|
|
result += string[i]
|
|
i += 1
|
|
|
|
return result
|
|
|
|
|
|
#################################################
|
|
def verifier(test_name, actual, expected):
|
|
print(test_name)
|
|
if actual != expected:
|
|
print("=======> FAILED! <=======")
|
|
print(f"Got value <{actual}>")
|
|
print(f"Expected value is <{expected}>")
|
|
else:
|
|
print("PASSED")
|
|
print()
|
|
|
|
|
|
test_replace_string1 = "The World is not enough"
|
|
test_replace_string2 = "Testing is key"
|
|
test_replace_string3 = "Sir, I demand, I am a maid named Iris"
|
|
test_replace_string4 = "Satire: Veritas"
|
|
test_replace_string5 = "Brum Brum Brum Brum "
|
|
|
|
replace_string_tests = [test_replace_string1, test_replace_string2, test_replace_string3, test_replace_string4, test_replace_string5, '']
|
|
|
|
|
|
for i in range(len(replace_string_tests)):
|
|
to_find = ' '
|
|
to_replace = '#'
|
|
verifier(f"SPLIT_STRING (FIND='{to_find}', REPLACE='{to_replace}') TEST {i+1}",
|
|
replace_string(replace_string_tests[i], to_find, to_replace),
|
|
replace_string_tests[i].replace(to_find, to_replace))
|
|
|
|
for i in range(len(replace_string_tests)):
|
|
to_find = 'no'
|
|
to_replace = 'NO'
|
|
verifier(f"SPLIT_STRING (FIND='{to_find}', REPLACE='{to_replace}') TEST {i+1}",
|
|
replace_string(replace_string_tests[i], to_find, to_replace),
|
|
replace_string_tests[i].replace(to_find, to_replace))
|
|
|
|
for i in range(len(replace_string_tests)):
|
|
to_find = ' is '
|
|
to_replace = 'BLUB'
|
|
verifier(f"SPLIT_STRING (FIND='{to_find}', REPLACE='{to_replace}') TEST {i+1}",
|
|
replace_string(replace_string_tests[i], to_find, to_replace),
|
|
replace_string_tests[i].replace(to_find, to_replace))
|
|
|
|
for i in range(len(replace_string_tests)):
|
|
to_find = 'Brum '
|
|
to_replace = ''
|
|
verifier(f"SPLIT_STRING (FIND='{to_find}', REPLACE='{to_replace}') TEST {i+1}",
|
|
replace_string(replace_string_tests[i], to_find, to_replace),
|
|
replace_string_tests[i].replace(to_find, to_replace)) |