+ 2

Ordered-containment of a string(D) - MAKE MY PYTHON CODE BETTER

PLEASE SUGGEST THE BEST SOLUTION FOR THIS QUESTION This is the question: A string S2 is said is said to be order-contained in another string S1 if all the letters of S2 is present in S1 and order of occurrence of letters of S2 in S1 is same as in S2. For example, elephant contains ant, hat but not tap. Given two strings S1 and S2, write a code to Print 'Yes' if S2 is order contained in S1 and 'No' otherwise. All letters in the input will be lowercase in the given string with no spaces Input Format First line contains the string, S1 Second line contains the string, S2 Output format Print Yes if S2 is order contained in S1 and Print No otherwise This is my code in python def leftout(s): s1 = '' for i in range(1,len(s)): s1 = s1 + s[i] return s1 s2 = str(input()) s3 = str(input()) while((s2 != "")&(s3 != "")): if(s2[0] == s3[0]): s3 = leftout(s3) s2 = leftout(s2) if(s3 == ""): print("Yes") else: print("No")

16th Nov 2022, 3:52 PM
Krishna Sarawagi
Krishna Sarawagi - avatar
6 Answers
+ 5
Mozzy , we do not need to convert input() to string, since input() returns everything as string by default. so s1 = input() is ok.
16th Nov 2022, 8:25 PM
Lothar
Lothar - avatar
16th Nov 2022, 4:19 PM
Oma Falk
Oma Falk - avatar
+ 4
As pointed out by Oma Falk, the best way of solving this problem is using regex. Your original solution seems to work, but it can be refined. 1. The leftout function can be replaced with a simpler string slice. It has the same outcome and is easier to understand, not to mention more efficient. s = s[1:] 2. In Python, non-empty strings are evaluated as True and empty strings as False, so there's no need to compare them directly to empty string literals (""). 3. Parentheses around conditions are legal, but unnecessary. They tend to clutter the code, making it hard to read. 4. As metioned by Lothar , the input() function already returns a string, so the additional string conversion is unnecessary. Keeping these in mind, your code can be refined into this: s1 = input() s2 = input() while s1 and s2: if s1[0] == s2[0]: s2 = s2[1:] s1 = s1[1:] if not s2: print("Yes") else: print("No")
16th Nov 2022, 5:19 PM
Mozzy
Mozzy - avatar
+ 1
#Krishna Sarawagi How about: def order_contained(str1, str2): res = [str1.index(i) for i in str2] print(res==sorted(res)) #tests s1="elephant" s2="lepe" s3="epht" order_contained(s1,s2) order_contained(s1,s3) #if you want user inputs order_contained(input(), input())
16th Nov 2022, 10:25 PM
Bob_Li
Bob_Li - avatar
+ 1
Lothar Good catch! Thanks for pointing it out. Karolina Augustyniak Yes that's right, it's a double negation when s2 is empty and therefore evaluated to a True. "If not a non-empty string.." is logically the same as "If string is empty.."
16th Nov 2022, 11:54 PM
Mozzy
Mozzy - avatar
0
Mozzy thank you for this neat code! Do I understand the bit right? if not s2 means if not False (empty s2) -> if True ?
16th Nov 2022, 10:13 PM
Karolina Augustyniak
Karolina Augustyniak - avatar