Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Chương trình Python kiểm tra hai list identical theo vòng tròn


Cho hai list. Nhiệm vụ kiểm tra xem hai list cho trước có identical theo vòng tròn (circularly identical) hay không.

Ví dụ

Input: A = [100, 100, 10, 10, 100]
B = [100, 100, 100, 10, 10]
Output: True

Giải thích

True vì khi các phần tử trong list xoay vòng tròn thì chúng sẽ tương tự list kia.

Thuật toán

Bước 1: Tạo List thứ nhất và thứ hai.
Bước 2: Chuyển List sang map.
Bước 3: Dùng join() chuyển đối tượng list thành string.
Bước 4: Nhân đôi list A và chuyển sang map.
Bước 5: So sánh hai list. Nếu true thì identical vòng tròn, false thì không.

Ví dụ mã

# Chương trình Python kiểm tra hai list identical vòng tròn
A = list()
n = int(input("Enter the size of the First List ::"))
print("Enter the Element of First List ::")
for i in range(int(n)):
    k = int(input(""))
    A.append(k)

B = list()
n1 = int(input("Enter the size of the Second List ::"))
print("Enter the Element of the Second List ::")
for i in range(int(n1)):
    k = int(input(""))
    B.append(k)

C = list()
n3 = int(input("Enter the size of the Third List ::"))
print("Enter the Element of the Third List ::")
for i in range(int(n3)):
    k = int(input(""))
    C.append(k)

print("Compare First List and Second List ::>")
print(' '.join(map(str, B)) in ' '.join(map(str, A * 2)))

print("Compare Second List and Third List ::>")
print(' '.join(map(str, C)) in ' '.join(map(str, A * 2)))

Kết quả

Enter the size of the First List :: 5
Enter the Element of First List ::
10
10
0
0
10
Enter the size of the Second List :: 5
Enter the Element of the Second List ::
10
10
10
0
0
Enter the size of the Third List :: 5
Enter the Element of the Third List ::
1
10
10
0
0
Compare First List and Second List ::> True
Compare Second List and Third List ::> False