[Tutor] Python strings example

Manprit Singh manpritsinghece at gmail.com
Mon Apr 26 22:49:17 EDT 2021


 Dear Sir,

Coming to my question again. what i want to discuss is,

If you look at Peter's solution, you can see it is using two generator
expressions(I mean to say there is a utilization of two for loops that
increases complexity) what practices should be adopted  in such cases ? the
one done by Peter or the one done by me ?

need your guidance.

On Mon, Apr 26, 2021 at 9:18 PM Manprit Singh <manpritsinghece at gmail.com>
wrote:

> Dear sir,
>
> consider a problem where I have to write a function that can return True
> if the string passed to the function contains at least one digit character
> and one alphabet character, else the function must return False. The way i
> am solving it is given below :
> def str_chk(word_tochk):
>     dig = False
>     alpha = False
>     for ele in word_tochk:
>         dig = dig or ele.isdigit()
>         alpha = alpha or ele.isalpha()
>     return dig and alpha
>
>
> st = "Abhay2"
> ans = str_chk(st)
> print(ans) returns True, which is correct as the string contains an
> alphabet as well as a digit character.
>
> st = "Abhay"
> ans = str_chk(st)
> print(ans) returns False . which is also correct as there is no digit
> character in the string now
>
> Any suggestions, if the solution can be improved .
>
> Regards
> Manprit Singh
>
>


More information about the Tutor mailing list