0
숫자 1 ... 9의 홀수 순열을 인쇄하는 python 프로그램을 작성했습니다.결과가 다른 이유는 무엇입니까?
list1=[1,2,3,4,5,6,7,8,9] #this is used to generate permutations in lexicographic order
l3=[] #list to store odd permutations
k=7 # initial value of k
l3.append(list1)
while k!=-1:
l=0
#find value of l
for x in range(len(list1)-1,k,-1):
if list1[x]>list1[k]:
l=x
break
#swap values
p=list1[k]
list1[k]=list1[l]
list1[l]=p
#reverse the list
c1=list1[0:k+1]
c2=list1[k+1:]
c2.reverse()
list1=c1+c2
#finiding odd ones and printing them and storing them in l3 list
if list1[8]%2!=0:
l3.append(list1)
print list1 #in next program I replace this line with a for loop to print items in l3 list
k=-1
#find next value of k
for x in range(len(list1)-2,-1,-1):
if list1[x]<list1[x+1]:
k=x
break
이 프로그램은 예상되는 결과를 인쇄합니다. 그러나이 코드를 인쇄 줄 대신 끝에 추가하면 결과가 완전히 변경됩니다.
for x in l3:
print x
어리석은 실수를 범했지만 어떤 오류도 찾을 수 없습니다. plz 내가 그것을 디버깅하는 데 도움이됩니다. (나는 그것을 사용하여 k, l 변수를 사용하여 Wikipedia에서 순열 생성 알고리즘을 사용).
('l1')마다'l3'에 그냥 이 발생에게의를 교체합니다. –