먼저 목록을 만들고 시작 주소의 인덱스는 0이고 첫 번째 세 번째 요소의 위치는 2이며 목록이 비어 있을 때까지 탐색해야 하며 다음 인덱스를 찾아야 할 때마다 해야 할 또 다른 중요한 작업이 필요합니다. 세 번째 요소를 만들고 값을 인쇄한 다음 목록의 길이를 줄입니다.
예
A:[10,20,30,40] OUTPUT:30 20 40 10
설명
첫 번째 세 번째 요소는 30이고 다음 세 번째 요소는 20에 대해 40부터 계산하고 다음 세 번째 요소는 40 자체에 대해 다시 40부터 시작하여 마지막으로 10이 인쇄됩니다.
알고리즘
1단계:목록의 인덱스는 0부터 시작하고 첫 번째 세 번째 요소는 위치 2에 있습니다.
variable p=2,starting index id=0.
2단계:목록의 길이를 찾습니다.
listlen=len (LST) // length of the list(LST)
3단계:목록이 비어 있을 때까지 탐색하고 매번 다음 세 번째 요소의 인덱스를 찾습니다.
While(listlen>0)
Id=(p+id)%listlen
A=LST.pop(id)// removes and prints the required element
Listlen-=1
End while
예시 코드
# To remove to every third element until list becomes empty
def removenumber(no):
# list starts with
# 0 index
p = 3 - 1
id = 0
lenoflist = (len(no))
# breaks out once the
# list becomes empty
while lenoflist > 0:
id = (p + id) % lenoflist
# removes and prints the required
# element
print(no.pop(id))
lenoflist -= 1
# Driver code
A=list()
n=int(input("Enter the size of the array ::"))
print("Enter the INTEGER number")
for i in range(int(n)):
p=int(input("n="))
A.append(int(p))
print("After remove third element, The List is")
removenumber(A) # call function
출력
Enter the size of the array ::9 Enter the number n=10 n=20 n=30 n=40 n=50 n=60 n=70 n=80 n=90 After remove third element, The List is 30 60 90 40 80 50 20 70 10