Featured Posts

CS50 Python , Nutrition Facts Table

Image
Nutrition Facts: Python Practice for Beginners Nutrition Facts Table for Python Practice Welcome to this comprehensive guide for Python beginners! If you are learning how to work with lists, dictionaries, and loops, this post will help you build practical skills using a real-world example: nutrition facts for fruits. Understanding how to organize and manipulate data is a key part of programming, and this exercise will give you hands-on experience. Below is a sample table of fruits and their calorie values, formatted as a Python list of dictionaries. This structure is ideal for coding exercises, projects, or even building your own nutrition calculator. You can expand this list, add new fruits, or use it as a foundation for more advanced Python tasks. Python List of Dictionaries Example: fruits = [ {'name': 'Apple', 'calories': 130}, {'name': 'Avocado', 'calories': 50}, {'name': 'Banana', 'ca...

Python 2.5.1.7 LAB: Palindromes

Python: Check If a String is a Palindrome

Palindrome
Palindrome

Objectives

  • improving the student's skills in operating with strings;
  • encouraging the student to look for non-obvious solutions.

Scenario

Do you know what a palindrome is?
It's a word which look the same when read forward and backward. For example, "kayak" is a palindrome, while "loyal" is not.
Your task is to write a program which:
  • asks the user for some text;
  • checks whether the entered text is a palindrome, and prints result.
Note:
  • assume that an empty string isn't a palindrome;
  • treat upper- and lower-case letters as equal;
  • spaces are not taken into account during the check - treat them as non-existent;
  • there are more than a few correct solutions - try to find more than one.
Test your code using the data we've provided.

Test data

Sample input:
Ten animals I slam in a net

Sample output:
It's a palindrome

Sample input:
Eleven animals I slam in a net

Sample output:
It's not a palindrome

Solution Code:

""" method 1 - detailed variables"""
text = input("Enter a text\n")
a_string = text.lower().replace(' ', '')
b_string = a_string[::-1]                    

while not text:
    print("It's not a palindrome")
    break
else:
    if a_string == b_string:
        print("It's a palindrome")

    else:
        print("It's not a palindrome")

"""  method 2 - reversed() function"""

text = input("Enter a text\n")
text = text.lower().replace(" ", "")
rev_text = "".join(reversed(text))

while not text:
    print("It's not a palindrome")
    break
else:
    if text.casefold() == rev_text.casefold():
        print("It's a palindrome")
    else:
        print("It's not a palindrome")

""" methos 3 - for loop """

text = input("Enter a text\n")

def palindrome(string):
    if text == "":
        print("It's Not a palindrome")
   
    string = string.lower().replace(' ', '')
    reversed = ''
    for i in range(len(string[::-1])):
        reversed += string[::-1]
        if string == reversed:
            return "It's a palindrome"
        else:
            return "It's Not a palindrome"
print(palindrome(text))

methos 4 - while loop"""

text = input("Enter a text\n")

def palindrome(string):
    while len(string.strip()) ==0:
        return "It's Not a palindrome"
    string = string.lower().replace(' ', '')
    first, last = 0, len(string) - 1
    while(first < last):
        if(string[first] == string[last]):
            first += 1
            last -= 1
        else:
            return "It's Not a palindrome"
       
        return "It's a palindrome"
print(palindrome(text))


Still, there are other solutions, and other ways to make the mentioned solutions better. Can you think about them?
See you in the comments 👀
Remember "Learn Less, Practice More"

Comments

Popular posts from this blog

فرصتك للدراسة في ألمانيا: منحة ممولة بالكامل لطلاب الدراسات العليا

How to Deactivate Screen Reader in Kali Linux

Murphy's Law: Expect the Unexpected

Python 3.2.1.14 LAB: Essentials of the while loop

Data Analysis Roadmap 2026: From Excel Lover to Python-Powered Analyst