Write a function isPalindrome(WORDS) in Python, that takes the dictionary, WORDS as an argument and returns an array of words (in lowercase) that are palindromes in the dictionary. 

Answer:

Answer by student

[Sample Paper] Write a function isPalindrome(WORDS) in Python, that - Sample Paper 1 by Teachoo

Detailed answer by teachoo

-lock-

To solve this question, we need to do the following steps:

  • Define a function named isPalindrome that takes one parameter, WORDS, which is a dictionary of words.
  • Inside the function, create an empty list named palindromes to store the words that are palindromes.
  • Loop through the values of the dictionary using a for loop . We can use the .values() method to get all the values of a dictionary as a list.
  • For each value, which is a word, convert it to lowercase using the .lower() method. This is because we want to ignore the case of the letters when checking for palindromes.
  • To check if a word is a palindrome, we can use slicing to reverse it and compare it with itself. For example, word[::-1] will give us the reverse of the word. If word == word[::-1], then the word is a palindrome.
  • If the word is a palindrome, we can append it to the list of palindromes using the .append() method.
  • After the loop ends, we can return the list of palindromes as the output of the function.

So, the correct answer is:

part 2 - Question 21 (Choice 2) - Sample Paper 1 by Teachoo - Teachoo Sample Paper - Computer Science - Class 11

 

-endlock-

Remove Ads
Teachoo · Class 11 Explore Class 11
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.