Question: 1 . ( 2 5 points ) A palindrome is a string that reads the same backward as forward. For example, madam and test tset
points A palindrome is a string that reads the same backward as forward. For example, "madam" and "test tset" are palindromes but "banana" is not. Given a string s write a function canFormPalindromes that tests whether the letters in s can be permuted to form a palindrome.
Note:
Your algorithm must use hashing or dictionary in Python
The time complexity is On where n is the length of the input string.
Assume an empty string is a palindrome.
Examples:
canFormPalindrome aamdm should return True Explanation: "aamdm" can be permuated to "madam", which is a palindrome.
canFormPalindrome abbddaaa should return True
Explanation: "abbddaaa" can be permuated to "aabddbaa", which is a palindrome.
canFormPalindrome abcc should return False
points Suppose you need to write an anonymous letter and disguise your handwriting. You decide to cut the characters you need from a book and paste the characters on a paper to form the letter. Note that we mean a physical book and a physical letter, not digital. Given the content of a book book and the content of the letter letter each is reprented as a string write a function anonymousLetterbook letter that determines if it is possible to write the anonymous letter using the book.
Note:
Your algorithm must use hashing or dictionary in Python
The time complexity is Omax m n where m is the length of the book string and n is the length of the letter.
Examples:
Let the book be "I am a book. Not a letter." and the letter be "I am a letter." anonymousLetterbook letter should return True.
Let the book be "I am a book. Not a letter." and the letter be "I am a letter!" anonymousLetterbook letter should return False.
Let the book be "abcde" and the letter be "abcdef" anonymousLetterbook letter should return False.
from collections import
Tests whether the letters in a string can be permuted to form a palindrome.
def canFormPalindromes:
Determines if it is possible to write an anonymous letter using a book.
def anonymousLetterbook letter:
from hw import
def checkval expected:
if val expected:
printPassed
else:
printFailed
printYour output val
printExpected output expected
class TestCanFormPalindrome:
def testself:
printcanFormPalindrome case : s x point:
l x
val canFormPalindromel
checkval True
def testself:
printcanFormPalindrome case : s xzz point:
l xzz
val canFormPalindromel
checkval True
def testself:
printcanFormPalindrome case : s 'abxzzbbx' point:
l "abxzzbbx"
val canFormPalindromel
checkval False
def testself:
printcanFormPalindrome case : s 'axzzbbxa' point:
l "axzzbbxa"
val canFormPalindromel
checkval True
def testself:
printcanFormPalindrome case : s
'aaabacaaadacabaaaaaaaaaadadaaaadaaaeaaaaaaaaaaa' point:
l "aaabacaaadacabaaaaaaaaaadadaaaadaaaeaaaaaaaaaaa"
val canFormPalindromel
checkval True
test TestCanFormPalindrome
test.test
test.test
test.test
test.test
test.test
from hw import
def checkval expected:
if val expected:
printPassed
else:
printFailed
printYour output val
printExpected output expected
class TestAnonymousLetter:
def testself:
printanonymousLetter case : book 'alsfjei', letter point:
b "alsfjei"
l
val anonymousLetterb l
checkval True
def testself:
printanonymousLetter case : book xzz letter xzz point:
b xzz
l xzz
val anonymousLetterb l
checkval True
def testself:
printanonymousLetter case : book xzz letter zxzx point:
b xzz
l zxzx
val anonymousLetterb l
checkval False
def testself:
printanonymousLetter case : book 'febxxzz', letter zxzx
point:
b "febxxzz"
l zxzx
val anonymousLetterb l
checkval True
def testself:
printanonymousLetter case : book 'abxaxabbz', letter 'abxzzbbxz'
point:
b "abxaxabbz"
l "abxzzbbxa"
val anonymousLetterb l
checkval False
test TestAnonymousLetter
test.test
test.test
test.test
test.test
test.test
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
