Question

1.) Write a recursive function named isPalindrome that will take a single string as an argument...

1.) Write a recursive function named isPalindrome that will take a single string as an argument and determine if the argument is a palindrome. The function must return True if the argument is a palindrome, False otherwise. Recall that any string is a palindrome if its first and last characters are the same and the rest of it is also a palindrome (therefore, a single character is a palindrome):

2.) Consider a text file named samplestrings.txt that contains a collection of strings, one per line. Write a short Python program to read the entire file and display the percentage of strings in the file that are palindromes. Assume that the input file resides in the current working directory. Your code must include the following:

A call to the function you created in problem (1) to determine if a particular string is a palindrome.

A try-catch block to detect any file IOError exceptions. If an error is detected, your program should inform the user that an error occurred and exit.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def isPalindrome(s):

if len(s) <= 1:

return True

if s[0] != s[-1]:

return False

return isPalindrome(s[1:-1])

try:

f = open("samplestrings.txt")

words = f.read().split()

f.close()

palCount = 0

for word in words:

if isPalindrome(word):

palCount += 1

print("Percentage =", round(palCount*100/len(words), 2))

except:

print("Invalid file")

''' SAMPLE FILE

racecar

madam

HomeworkLib

india

dad

mom

SAMPLE OUTPUT

Percentage = 66.67

'''

# -- Please up vote or comment if you have any doubts. Happy Learning!

Add a comment
Know the answer?
Add Answer to:
1.) Write a recursive function named isPalindrome that will take a single string as an argument...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • python Problem 3 (Palindrome) Write a function called ispalindrome(string) that takes a string (with spaces) as...

    python Problem 3 (Palindrome) Write a function called ispalindrome(string) that takes a string (with spaces) as argument and returns True if that string (ignoring spaces) is a palindrome. A palindrome is a word or phrase that spells the same thing forwards as backwards (ignoring spaces). Your program should use a recursive process to determine the result, by comparing the first and last letters of the string, and if necessary, calling ispalindrome() on the rest of the string. Sample run: print(ispalindrome('never...

  • Write a program that uses a recursive function to determine whether a string is a character-unit...

    Write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, the options for doing case sensitive comparisons and not ignoring spaces are indicated with flags. For example "A nut for a jar of tuna" is a palindrome if spaces are ignored and not otherwise. "Step on no pets" is a palindrome whether spaces are ignored or not, but is not a palindrome if it is case sensitive. Background Palindromes are character sequences...

  • Help please Write a program named one.c that takes a single command-line argument, the name of...

    Help please Write a program named one.c that takes a single command-line argument, the name of a file. Your program should read all the strings (tokens) from this file and write all the strings that are potentially legal words (the string contains only upper-case and lower-case characters in any combination) to the file words. Your program should ignore everything else (do not write those strings anywhere 1. As an example, running /a.out dsia would result in the generation of the...

  • Write a recursive function, take a String as input, return true, if the String is a...

    Write a recursive function, take a String as input, return true, if the String is a palindrome; false otherwise. For instance, if the input is: “A nut for a jar of tuna” Then the return value is true. Notice that the non English letters are ignored; the spaces are ignored; and it is NOT case sensitive. You must write recursive function. You shall turn in a complete program, including main function to use your function to test if a String...

  • For this assignment, you will create a program that tests a string to see if it...

    For this assignment, you will create a program that tests a string to see if it is a palindrome. A palindrome is a string such as “madam”, “radar”, “dad”, and “I”, that reads the same forwards and backwards. The empty string is regarded as a palindrome. Write a recursive function: bool isPalindrome(string str, int lower, int upper) that returns true if and only if the part of the string str in positions lower through upper (inclusive at both ends) is...

  • You need to write a program (one java class containing Main calling function isPalindrome (String str)....

    You need to write a program (one java class containing Main calling function isPalindrome (String str). The function isPalindrome (returns Boolean T/F) needs to determine whether or not a string is a palindrome, using recursion. The algorithm to check whether a string is a palindrome is shown below: /* check for first and last char of String: * if they are same then do the same thing for a substring * with first and last char removed. and carry on...

  • ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName)...

    ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...

  • 1. DOES A DIGIT APPEAR IN AN INTEGER, Write a recursive function appears(n,i) that returns true...

    1. DOES A DIGIT APPEAR IN AN INTEGER, Write a recursive function appears(n,i) that returns true if the digit i appears in the positive (base 10) integer n, false if it does not in c++. ex. Enter a positive integer: 54321 Enter a digit: 7 The digit 7 does NOT appear in the integer 54321. 2. IS AN INPUT STRING A PALINDROME? A palindrome is a string in which the letters are the same in both directions, disregarding capitalization and...

  • 10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string...

    10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string object arguments. Let's call them string1, string2, and string3. It should search string for all occurrences of string2. When it finds an occurrence of Programming Challenges string2, it should replace it with string. For example, suppose the three arguments have the following values: stringt: "the dog jumped over the fence" string 2 "the" string3: "that" With these three arguments, the function would return a...

  • Write a function that takes two arguments, both strings. Print "YES" if the second string argument...

    Write a function that takes two arguments, both strings. Print "YES" if the second string argument contains only characters found in the first string argument. Print "NO" if the second string argument contains characters not in the first. Here are some sample calls. In your program, call the function 3 times with the same arguments shown below. makeStr("hello", "") makeStr("hello", "olleloheloeloheloel") makeStr("hello", "olleloheloteloheloel") # YES # YES # NO

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT