Question

IN PYTHON Stretch Remember that while it’s a good idea to have one person primarily responsible...

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

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
python problem: ( use a loop to read each character from the string and insert into...
python problem: ( use a loop to read each character from the string and insert into the stack) 1. The function main a5.py continually asks the user for a string, and calls isPalindrome to check whether each string is a palindrome. A palindrome is a word or sequence that reads the same backward as forward, e.g., noon, madam or nurses run. 2. Your function must ignore spaces: when the user enters 'nurses run', the function returns True, to indicate that...
Python Programming Build a python programming that asks the user for a three-letter substring. The program...
Python Programming Build a python programming that asks the user for a three-letter substring. The program should then proceed to read the file strings.txt. Each line in strings.txt will contain a string. Your program should then test to see how many non-overlapping occurrences of the three-letter substring occur in that string and test whether the string is a palindrome. The program should then proceed to create an output file string_stats.txt, which contains the original data on one line and the...
Each person in the group will be responsible for rewriting the code to solve ​one​ of...
Each person in the group will be responsible for rewriting the code to solve ​one​ of the original parts (celebrity names), ​without using arrays or loops.​ The purpose of this assignment is to practice Strings manipulation and String methods so full credit will only be given if you are utilizing the code for Strings (substring, charAt, compareTo, equals, etc). program Java this was my last assigment Take the 4th and the 5th words from part 1 and print out both...
Python (Using for reference please comment which is which!) Exercise 1: Store Unique Words as Dictionary...
Python (Using for reference please comment which is which!) Exercise 1: Store Unique Words as Dictionary Keys Write a program that stores unique words as keys in a dictionary. Don't worry about upper/lowercase for now (i.e., it's ok for you to have separate entries for "The" and "the"), and just assign 1 as the value for each key. Use an input loop (while loop with an input function) to accepts values putting them into the dictionary until the user enters...
Python #Write a function called are_anagrams. The function should #have two parameters, a pair of strings....
Python #Write a function called are_anagrams. The function should #have two parameters, a pair of strings. The function should #return True if the strings are anagrams of one another, #False if they are not. # #Two strings are considered anagrams if they have only the #same letters, as well as the same count of each letter. For #this problem, you should ignore spaces and capitalization. # #So, for us: "Elvis" and "Lives" would be considered #anagrams. So would "Eleven plus...
USING PYTHON do all the he problems using while loop , continue and break 1-This problem...
USING PYTHON do all the he problems using while loop , continue and break 1-This problem provides practice using a while True loop.write a function named twoWords that gets and returns two words from a user. The first word is of a specified length, and the second word begins with a specified letter.The function twoWords takes two parameters: an integer, length, that is the length of the first word and a character, firstLetter, that is the first letter of the...
Using python 3.5 or later, write the following program. A kidnapper kidnaps Baron Barton and writes...
Using python 3.5 or later, write the following program. A kidnapper kidnaps Baron Barton and writes a ransom note. It is not wrriten by hand to avoid having his hand writing being recognized, so the kid napper uses a magazine to create a ransom note. We need to find out, given the ransom note string and magazine string, is it possible to given ransom note. The kidnapper can use individual characters of words. Here is how your program should work...
WRITE IN PYTHON Using any large text file or any literature English book in .txt format....
WRITE IN PYTHON Using any large text file or any literature English book in .txt format. The program will read a .txt file and process the information Write a module called, “book_digest”. The module must have the following functions: ● digest_book(file_path: str) -> None ○ This function collects and stores the required information into global dictionaries, lists, and variables. The file (book) is read and parsed only one time then closed ○ This function should raise a FileNotFoundError exception if...
Use python language please #One of the early common methods for encrypting text was the #Playfair...
Use python language please #One of the early common methods for encrypting text was the #Playfair cipher. You can read more about the Playfair cipher #here: https://en.wikipedia.org/wiki/Playfair_cipher # #The Playfair cipher starts with a 5x5 matrix of letters, #such as this one: # # D A V I O # Y N E R B # C F G H K # L M P Q S # T U W X Z # #To fit the 26-letter alphabet into...
Define a function called divideSentences in the python file. This function will take in one input...
Define a function called divideSentences in the python file. This function will take in one input argument: a string. This string will typically be a whole piece of text, a paragraph or more. This function will separate the text into sentences, assuming that each sentence ends with either a period, a question mark, or an exclamation mark. It should return a list of the sentence strings. Be sure that the sentence-ending mark is still attached to the sentence. Remove any...