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
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:
-endlock-