I need to write a program to find all maximum substrings of digits. Return only these substrings separated by spaces in their order. Do not use regular expressions, just the simplest capabilities of programming language
34234 34 dfd gfd 5
34535
fsdflskfjsdflk
fsdkfj sdf34fdfd
1,3
1/5
should give this:
34234 34 5
34535
34
1 3
1 5
but in last two outputs i get 13 and 15 instead of 1 3 and 1 5. So if there is digit like x.x or x,x or x/x it should give digits and space between them. Any idea how to reach that? My code is below
for linia in plik:
linia = linia.strip("\n")
i=0
current_string = ''
for i in range(len(linia)):
if linia[i].isdigit() or (linia[i-1].isdigit() and linia[i].isspace()) :
current_string+=linia[i]
i+=1
if(current_string!=''):
print(current_string)
You can use a flag to check for non-digit gaps. Write a space for each gap.
Try this code:
plik = [
"34234 34 dfd gfd 5",
"34535",
"fsdflskfjsdflk",
"fsdkfj sdf34fdfd",
"1,3",
"1/5"]
for linia in plik:
linia = linia.strip("\n")
i=0
current_string = ''
gap = False
for i in range(len(linia)):
if linia[i].isdigit() :
if gap : current_string+=' '
current_string+=linia[i]
i+=1
gap = False
elif len(current_string):
gap = True
if(current_string!=''):
print(current_string)
Output
34234 34 5
34535
34
1 3
1 5