本章主要講述:用python判斷[奇數or偶數][列表是否為軸對稱]
"""
匯總曾經被問到過的算法題
2、判斷某個數為奇數還是偶數
3、判斷列表是否是軸對稱列表:采用切片,list[::-1]
"""
def judge_odd_or_even_number(n):
"""
二、判斷是奇數還是偶數
:param n:
:return:
"""
if n % 2 == 0:
print(n, "為偶數")
else:
print(n, "為奇數")
def judge_list_axial_symmetry1():
"""
三、判斷列表是否是軸對稱
方法1、
:return:
"""
list1 = [2, 3, 4, 5, 6, 6, 4, 3, 2]
# list1 = [2, 3, 4, 5, 6, 5, 4, 3, 2]
i = 0
list_length = len(list1)
print("列表長度:", list_length)
# 1.第一種實現方法
while i < 0.5 * list_length - 1:
if list1[i] != list1[list_length - 1 - i]:
print("list1[i]:", list1[i])
print("list1[list_length - 1 - i]:", list1[list_length - 1 - i])
print("它不是對稱的")
sys.exit(0)
else:
i = i + 1
print("它是對稱的")
def judge_list_axial_symmetry2():
"""
三、判斷列表是否是軸對稱
方法2、【切片判斷】
:return:
"""
# before_list = [2, 3, 4, 5, 6, 6, 4, 3, 2]
before_list = [2, 3, 4, 5, 6, 5, 4, 3, 2]
# list[::-1]: 表示從右往左以步長為1進行切片。步長小於0時,返回序列為倒序
after_list = before_list[::-1]
print("after_list:", after_list)
if (after_list == before_list):
print("它是對稱的")
else:
print("它不是對稱的")
if __name__ == '__main__':
judge_odd_or_even_number(3)
# judge_list_axial_symmetry1()
# judge_list_axial_symmetry2()