IN PYTHON
Stretch
Remember that while it’s a good idea to have one person primarily responsible for typing (the driver) and the other reviewing each line of code and making suggestions (the navigator) you must switch roles after every problem.
1). List Construction
Write a Python program that prompts the user to enter a sequence of lowercase words and stores in a list only those words whose first letter occurs again elsewhere in the word (e.g., "baboon"). Continue entering words until the user enters an empty string ("", not " "), at which point your program should print the elements stored in the list, one word per line.
You may want to create a helper function that takes in a single string, and returns True if the first letter is repeated, or False otherwise. You could use a loop to do this, but you may also want to consider just using slicing and the in keyword, or the .count method.
Be sure to use an appropriate while condition here: while(True) might work in this case but it’s bad style.
Example:
Enter a word: im
Enter a word: a
Enter a word: member
Enter a word: of
Enter a word: the
Enter a word: imperial
Enter a word: senate
Enter a word: on
Enter a word: a
Enter a word: diplomatic
Enter a word: mission
Enter a word: to
Enter a word: alderaan
Enter a word:
member
imperial
alderaan
2) Palindrome Check
Write a function named is_palindrome that will take a single string argument and return True if the argument is a palindrome (the letters appear in the same order the same forwards as they do backwards), False otherwise.
You might want to consider writing a helper function for this problem to break up the steps, but it’s not required.
Hint: To remove punctuation and spaces, you could make a new empty string, loop through the characters in the original, and use the .isalpha() string method on each character to determine whether it’s a letter: only add characters that are actually letters to your new string.
Alternatively, you could use .replace and just replace any punctuation/spaces you find with the empty string.
Then convert the new string to lowercase and see if the modified string is equal to its reverse. Remember, you can easily reverse a string using slicing, or just write a helper function.
Example:
>>> is_palindrome('Abba')
True
>>> is_palindrome('Telat')
False
>>> is_palindrome("MadaM I'm Adam")
True
Here is the code:
def help_repeat(word):
if(len(word) != 0):
if(word[0] in word[1:]):
return(True)
else:
return(False)
def list_construction():
word_list = list()
word = input('Enter a word: ') # taking first input
if(help_repeat(word) == True): # checking the condition if the first letter reappears
word_list.append(word)
while(len(word) != 0):
word = input('Enter a word: ')
if(help_repeat(word) == True):
word_list.append(word)
for item in word_list:
print(item)
# calling the function
list_construction()
----------------
def is_palindrome(words):
words = words.replace("'","") # removing ampersand
words = words.replace(" ","") # removing space
words = words.lower() # converting into lower case
rev_words = words[::-1] # reversing the word
if(words == rev_words):
return(True)
else:
return(False)
# calling the function
is_palindrome(input('Enter a word: '))
Here is the output:
---------
For any doubt, please comment below.
Get Answers For Free
Most questions answered within 1 hours.