[Tutor] Python program to extract numeric suffix from a string
Manprit Singh
manpritsinghece at gmail.com
Sun Jul 4 02:28:09 EDT 2021
Dear sir,
Again working and found something little better which will give speed
improvements :
The function(To extract a numeric suffix) written below:
def ret_suffix(st):
if st.isdigit():
return st
else:
for x, y in enumerate(reversed(st)):
if not y.isdigit():
return st[-x:] if x else "No digit suffix found"
Need your comments on this function . Kindly give comments .
Regards
Manprit Singh
On Sun, Jul 4, 2021 at 11:47 AM Manprit Singh <manpritsinghece at gmail.com>
wrote:
> Dear sir,
>
> Correcting the function written for the problem :
> Problem to extract a numeric suffix from a string
>
> def ret(st):
> for x, y in enumerate(reversed(st)):
> if not y.isdigit():
> return st[-x:] if x else "No digit suffix found"
> return st
>
> Anything more readable in comparison to this function be done ? Kindly
> guide
>
> Regards
> Manprit Singh
>
>
>
>
> On Sun, Jul 4, 2021 at 9:46 AM Manprit Singh <manpritsinghece at gmail.com>
> wrote:
>
>> Dear sir,
>>
>> Assume a problem to extract a numeric suffix from a string .
>> String is st = "abcd12ghi456"
>> The numeric suffix of this string is "456" in case if there is no suffix,
>> the program should print " no suffix found"
>> The code i have written is given below :
>>
>> def return_suffix(st):
>> for x, y in enumerate(reversed(st)):
>> if not y.isdigit():
>> return st[-x:] if x else "No digit suffix found"
>>
>> st1 = "abcd1gh123"
>> ans = return_suffix(st1)
>> print(ans) # gives the right answer as '123'
>>
>> st1 = "abcd1gh"
>> ans = return_suffix(st1)
>> print(ans) # gives the right answer as 'No digit suffix found'
>>
>> I feel I have used enumerate and reversed both which makes the program a
>> little difficult to read . Can this be written in a more efficient way ?
>>
>> Need your guidance.
>>
>> Regards
>> Manprit Singh
>>
>
More information about the Tutor
mailing list