FREE DOWNLOAD

Python three consecutive letters alphabets script

Problem:

Alphabetical Order: The following words have three consecutive letters that are also consecutive letters in the alphabet: THIRSTY, NOPE, AFGHANISTAN, STUDENT. Write a program that accepts a word as input and determines whether or not it has three consecutive letters that are also consecutive letters in the alphabet.

Solution:

def consec(s):
l = len(s)
n = 3
if l < n:
raise ValueError(“String too short to contain repetition of length {}”.format(n))
for i in range(l-2):
# Check if you get the same letter by adding 1 or 2 to the next ones
# or by substracting 1 or 2…
if ord(s[i]) == ord(s[i+1])+1 == ord(s[i+2])+2 or \
ord(s[i]) == ord(s[i+1])-1 == ord(s[i+2])-2:
return s[i:i+3] return “”

user_word = input(“Enter a word here:”)
result=consec(user_word)
if len(result) == 3:
print(user_word + ” has three consecutive letters ‘” + result + “‘.”)
else:
print(user_word + ” do not have three consecutive letters.”)

Leave a Reply

Your email address will not be published. Required fields are marked *



Solutions Authored by 15+ Years Experienced Industry Expert