Python 程式檢查陣列所有元素組合形成的數字是否為迴文數


在 Python 中,一個用方括號括起來的逗號分隔值(項)陣列(列表)是 Python 中最靈活的資料型別之一。列表的元素不必是相同型別這一事實非常重要。

在本文中,我們將學習一個 Python 程式來檢查由陣列所有元素組合形成的數字是否為迴文數。

使用的方法

以下是完成此任務的各種方法

  • 使用 map() 和 join() 函式

  • 使用型別轉換和字串連線

  • 使用 str()、list()、extend()、join() 和 reverse() 函式

示例

假設我們已經獲取了一個**輸入列表**。我們現在將使用上述方法檢查由列表所有元素組合形成的數字是否為迴文數。

輸入

inputList = [3, 25, 42, 24, 52, 3]

輸出

Yes, it is a Palindrome

在上面的示例中,所有輸入列表元素組合成一個字串形式的數字,即**3254224523**。這個數字是迴文數。因此輸出為迴文數。

使用 map() 和 join() 函式

**str() 函式**(返回物件的字串格式,即轉換為字串)

**join()** - join() 是 Python 中的一個字串函式,用於連線由字串分隔符分隔的序列元素。此函式連線序列元素以轉換為字串。

**list() 函式**(將序列/可迭代物件轉換為列表)。

**map() 函式** - 將任何函式對映到迭代器/列表上。

示例

以下程式使用 map() 和 join() 函式檢查由陣列所有元素組合形成的數字是否為迴文數 -

# creating a function to check whether the string passed to it
# is a palindrome or not
def checkingPalindrome(inputString):
    # reversing the input string
    reverseStr = inputString[::-1]
    # checking whether the input string is equal to its reverse string
    if(inputString == reverseStr):
        # returning True if the condition is true
        return True
    else:
        # else returning False
        return False
      
# creating a function to convert the input list into a string
# by accepting the input list as an argument to it.
def joiningList(inputList):
    # converting all the input list elements into a string
    inputList = list(map(str, inputList))
    # converting the input list to a string using the join() function
    resultNum = ''.join(inputList)
    # checking whether the above-converted string number is a palindrome
    # by calling the above defined checkingPalindrome() function
    if(checkingPalindrome(resultNum)):
        # returning True if the function returns True
        return True
    else:
        # else returning False
        return False
# main code
# input list
inputList = [3, 25, 42, 24, 52, 3]
# Calling the above defined joiningList() function by passing input list to it
# and checking whether the function returns True/False
if(joiningList(inputList)):
  # printing Palindrome if the joiningList() fuction returns True
    print("Yes, it is a Palindrome")
else:
  # else printing NOT a Palindrome
    print("No, it is NOT a Palindrome")

輸出

執行上述程式後,將生成以下輸出 -

Yes, it is a Palindrome

使用型別轉換和字串連線

以下程式使用型別轉換和字串連線檢查由陣列所有元素組合形成的數字是否為迴文數 -

示例

# creating a function to check whether the string passed to is
# palindrome or not
def checkingPalindrome(inputString):
    # reversing the input string
    reverseStr = inputString[::-1]
    # checking whether the input string is equal to its reverse string
    if(inputString == reverseStr):
        # returning True if the condition is true
        return True
    else:
        # else returning False
        return False

# creating a function to convert the input list into a string
# by accepting the input list as an argument to it.
def joiningList(inputList):
    # Creating an empty string for storing list elements as a number
    resultNum = ""
    # travsering through the input list
    for e in inputList:
        # converting each element of list into a string
        e = str(e)
        # adding the string element to the resultNum
        resultNum = resultNum + e
    # checking whether the above-converted string number is a palindrome
    # by calling the above defined checkingPalindrome() function
    if(checkingPalindrome(resultNum)):
        # returning True if the function returns True
        return True
    else:
        # else returning False
        return False
# input list
inputList = [3, 25, 42, 24, 52, 3]
# Calling the above defined joiningList() function by passing input list to it
# and checking whether the function returns True/False
if(joiningList(inputList)):
  # printing Palindrome if the joiningList() fuction returns True
    print("Yes, it is a Palindrome")
else:
  # else printing NOT a Palindrome
    print("No, it is NOT a Palindrome")

輸出

執行上述程式後,將生成以下輸出 -

Yes, it is a Palindrome

使用 str()、list()、extend()、join() 和 reverse() 函式

**extend() 函式**(將可迭代物件(如列表、元組、字串等)的所有元素新增到列表的末尾)

**reverse() 函式** - 一個內建函式,用於反轉列表物件。它只是改變原始列表,而不是佔用額外的空間。

以下程式使用型別轉換和字串連線檢查由陣列所有元素組合形成的數字是否為迴文數 -

示例

# input list
inputList = [3, 25, 42, 24, 52, 3]
# an empty string for storing list elements as a number
resultantStr = ""
# travsering through the input list
for e in inputList:
  # converting each element of the list as a string and adding it to the result string
    resultantStr += str(e)
# converting the result string into a list
newList = list(resultantStr)
# empty list
k = []
# extending the new list to the above empty list
k.extend(newList)
# reversing the new list
newList.reverse()
# checking whether the new list is equal to the extended list
if(newList == k):
  # printing Palindrome if the condition is True
    print("Yes, it is a Palindrome")
else:
  # Else printing NOT a Palindrome
    print("No, it is NOT a Palindrome")

輸出

執行上述程式後,將生成以下輸出 -

Yes, it is a Palindrome

結論

在本文中,我們學習瞭如何使用三種不同的方法檢查由陣列所有元素組合形成的數字是否為迴文數。我們還學習瞭如何使用 map() 方法將函式應用於列表中的每個元素。最後,我們使用 extend() 函式學習瞭如何組合兩個列表。

更新於: 2023年8月18日

107 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.