From 77ca93cfc54edc34e75830552f53be82e68c446d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=8Dtalo=20Oliveira?= Date: Thu, 3 Oct 2024 22:22:30 +0200 Subject: [PATCH] update --- docs/index.md | 18 +++++++++++++++--- problem_set/ex23.py | 12 ++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/docs/index.md b/docs/index.md index 47c5f7c..ebf282e 100644 --- a/docs/index.md +++ b/docs/index.md @@ -35,7 +35,7 @@ _This is a work in progress_. The last commit shows the latest updates. - [Exercise 20: Flattening a nested list](#exercise-20-flattening-a-nested-list) - [Exercise 21: Merging two dictionaries](#exercise-21-merging-two-dictionaries) - [Exercise 22: Removing all whitespace from a string](#exercise-22-removing-all-whitespace-from-a-string) - - [Exercise 23](#exercise-23) + - [Exercise 23: Checking if a string is a palindrome](#exercise-23-checking-if-a-string-is-a-palindrome) - [Exercise 24](#exercise-24) - [Exercise 25](#exercise-25) - [Exercise 26](#exercise-26) @@ -482,9 +482,21 @@ print(new_string) ``` -### Exercise 23 +### Exercise 23: Checking if a string is a palindrome -Checking if a string is a palindrome +Palindrome is a word, phrase, or sequence that reads the same backwards as forwards, e.g. madam or nurses run. Write a program that checks if an arbitrary string is a palindrome. + +
+Click here to see a possible solution + +```python +my_string =input('Enter a string to check if it is a palindrome: ') +if my_string[::-1] == my_string: + print(my_string, "is a palindrome.") +else: + print(my_string, "is not a palindroe.") +``` +
### Exercise 24 diff --git a/problem_set/ex23.py b/problem_set/ex23.py index ffc13af..ecf6e29 100644 --- a/problem_set/ex23.py +++ b/problem_set/ex23.py @@ -1,5 +1,9 @@ -# Flattening a nested list +# Palindrome is a word, phrase, or sequence that reads the same backwards as forwards, e.g. madam or nurses run. +# Checking if a string is a palindrome -my_list = [[1,2], [3,4], [5,6]] -flattened_list = [x for sublist in my_list for x in sublist] -print(flattened_list) \ No newline at end of file +my_string =input('Enter a string to check if it is a palindrome: ') + +if my_string[::-1] == my_string: + print(my_string, "is a palindrome.") +else: + print(my_string, "is not a palindroe.") \ No newline at end of file