Python Slice with For

nw3965

Nw3965

Posted on January 9, 2024

Python Slice with For

TASK 1 - To check if given word is like a Mirror

# e.g. #  [noon],[level],[deed],[civic] [dad] are mirror word, it's the same when read from right to the left.
Enter fullscreen mode Exit fullscreen mode

1st tried by myself.

def check(str):
    # Get input words using "for" method/sentence one by one.
    x = []
    for str in range(x):
        #Check words the 1st character is the same as 4th character.
        if x[:0] == x[:3]:
            return True
        else:
            False
Enter fullscreen mode Exit fullscreen mode

2nd.
Chat GPT

def check_mirror(words):
    # [noon], [level], [deed], [civic], [dad] are mirror words; they are the same when read from right to left.

    # Initialize a list to store the results for each word
    results = []

    # Loop through each word in the list
    for word in words:
        # Check if the word is a mirror word
        if word == word[::-1]:  # Use slicing to reverse the word
            results.append(True)
        else:
            results.append(False)

    return results

# Example usage:
word_list = ["noon", "level", "deed", "civic", "dad", "python"]
result_list = check_mirror(word_list)
print(result_list)

    #  while:
    #  [hello], [good], [bye] are not mirror.
    #   Now, try to check if the word is mirror image.   

Enter fullscreen mode Exit fullscreen mode

3rd.
Replit.com AI

def check(word):
    for x in word:
        # Check the word if the 1st character is the same as the 4th character.
        return x[0] == x[3]

#Test case.
word = ["noon", "mom", "dad", "racecar"]
Enter fullscreen mode Exit fullscreen mode

4th

def check(words):
  # Initialize a list to store the results for each word
  x = []

  for word in words:
      # Check if the word is a mirror word (1st character is the same as the 4th character)
      x.append(word[0] == word[-1])

  return x

# Example usage:
word_list = ["noon", "mom", "dad", "free"]
result_list = check(word_list)
print(result_list)
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
nw3965
Nw3965

Posted on January 9, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Learning Python
javascript Learning Python

November 28, 2024

Calculate savings with Python!
beginners Calculate savings with Python!

November 26, 2024

UV the game-changer package manager
programming UV the game-changer package manager

November 24, 2024

Beginners Guide for Classes
python Beginners Guide for Classes

November 20, 2024