Python: Check the nth-1 string is a proper substring of nth string of a given list of strings
Python Programming Puzzles: Exercise-5 with Solution
Write a Python program to check the nth-1 string is a proper substring of the nth string in a given list of strings.
Input: ['a', 'abb', 'sfs', 'oo', 'de', 'sfde'] Output: True Input: ['a', 'abb', 'sfs', 'oo', 'ee', 'sfde'] Output: False Input: ['a', 'abb', 'sad', 'ooaaesdfe', 'sfsdfde', 'sfsd', 'sfsdf', 'qwrew'] Output: False Input: ['a', 'abb', 'sad', 'ooaaesdfe', 'sfsdfde', 'sfsd', 'sfsdf', 'qwsfsdfrew'] Output: True
Pictorial Presentation:


Sample Solution:
Python Code:
#License: https://bit.ly/3oLErEI
def test(str1):
return str1[len(str1)-2] in str1[len(str1)-1] and str1[len(str1)-2] != str1[len(str1)-1]
str11 = ["a","abb","sfs", "oo", "de", "sfde"]
print("Original list:")
print(str11)
print("Check the nth-1 string is a proper substring of nth string of the said list of strings:")
print(test(str11))
str11 = ["a","abb","sfs", "oo", "ee", "sfde"]
print("\nOriginal list:")
print(str11)
print("Check the nth-1 string is a proper substring of nth string of the said list of strings:")
print(test(str11))
str11 = ["a","abb","sad", "ooaa" "esdfe", "sfsdfde", "sfsd", "sfsdf", "qwrew"]
print("\nOriginal list:")
print(str11)
print("Check the nth-1 string is a proper substring of nth string of the said list of strings:")
print(test(str11))
str11 = ["a","abb","sad", "ooaa" "esdfe", "sfsdfde", "sfsd", "sfsdf", "qwsfsdfrew"]
print("\nOriginal list:")
print(str11)
print("Check the nth-1 string is a proper substring of nth string of the said list of strings:")
print(test(str11))
Sample Output:
Original list: ['a', 'abb', 'sfs', 'oo', 'de', 'sfde'] Check the nth-1 string is a proper substring of nth string of the said list of strings: True Original list: ['a', 'abb', 'sfs', 'oo', 'ee', 'sfde'] Check the nth-1 string is a proper substring of nth string of the said list of strings: False Original list: ['a', 'abb', 'sad', 'ooaaesdfe', 'sfsdfde', 'sfsd', 'sfsdf', 'qwrew'] Check the nth-1 string is a proper substring of nth string of the said list of strings: False Original list: ['a', 'abb', 'sad', 'ooaaesdfe', 'sfsdfde', 'sfsd', 'sfsdf', 'qwsfsdfrew'] Check the nth-1 string is a proper substring of nth string of the said list of strings: True
Flowchart:

Python Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Find the number of stones in each pile.
Next: Find a list of one hundred integers between 0 and 999 which all differ by ten from one another.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join