I'm trying to augment a lengthy string that can contain multiple number of digits (0,1,2,3,4,5,6,?)
Consider a string "000000000004?0??100001??2?0?10000000".
I'm trying to replace all the question marks (?) by the neighbouring largest digit. The comparison should be done from both left character and right character to the question mark (?).
Input String: "000000000004?0??100001??2?0?10000000"
Output String: "000000000004401110000122220110000000"
I wrote a function that ends up replacing them during the first iteration of the loop itself which results in replacing the ? by the highest number i.e, 4 in this case. Check the code snippet below.
Wrong Output: "000000000004404410000144240410000000"
def method_augment(aug_str):
global pos_of_sec_char, sec_char, preced_char
flag_marks_string_end = 0
list_predef = ['0', '1', '2', '3', '4', '5', '6']
len_aug_str = len(aug_str)
for m in range(0, (len_aug_str - 1)):
if aug_str[m] == '?':
pos_of_first_ques = m
if m != 0:
preced_char = aug_str[m - 1]
# print("preced char:", preced_char)
for n in range((pos_of_first_ques + 1), (len_aug_str - 1)):
if aug_str[n] in list_predef:
pos_of_sec_char = n
sec_char = aug_str[n]
print(sec_char)
if preced_char > sec_char:
aug_str = aug_str.replace(aug_str[pos_of_first_ques], preced_char)
del preced_char, sec_char, pos_of_first_ques, m
else:
aug_str = aug_str.replace(aug_str[pos_of_first_ques], sec_char)
del preced_char, sec_char, pos_of_first_ques
break
else:
flag_marks_string_end += 1
else:
for q in range((pos_of_first_ques + 1), (len_aug_str - 1)):
if aug_str[q] in list_predef:
pos_of_sec_char = q
sec_char = aug_str[q]
aug_str = aug_str.replace(aug_str[pos_of_first_ques], sec_char)
break
# if preced_char > sec_char:
# aug_str = aug_str.replace(aug_str[m], preced_char)
# else:
# aug_str = aug_str.replace(aug_str[m], sec_char)
else:
continue
return aug_str
Input String: "000000000004?0??100001??2?0?10000000"
Expected Output String: "000000000004401110000122220110000000"
Actual Output String: "000000000004404410000144240410000000"
There are multiple strings like this with different combinations of digit and ?. I hope I have explained it well. Please help. Thanks.