在我认为是 Python 2 的 JES 中,我如何做一个嵌套的 for 循环来计算重复次数,直到列表被排序
In JES which I believe is Python 2, how do I do a nested for loop to count reps until a list is sorted
我正在参加 class 使用 JES 的初学者编程,我认为这是 Python 2。
我的老师要求我们创建一个空列表,然后向其中添加随机数,然后将其从低到高排序。那部分我做对了。额外的功劳是添加一个嵌套的 for 循环,该循环将计算对列表进行排序所需的重复次数。
这是我的:
from random import *
def main():
# create list of 25 (this can be changed to number of your choice) random numbers from -100 to 100
# print list with message "list before shuffling"
numList = []
count = 0
while count < 15:
num = randint( -100 , 100 )
numList.append(num)
count = count + 1
printNow("the List before shuffling: " +str(numList))
n = len(numList)
add = 0
# Randomly shuffle list with items only moving if a lower number is being switched with a larger number
# do this 1000 times ( this can be changed to number of your choice)
# print list with message "list after shuffling"
for reps in range (0 , 500):
i = randrange(0, n)
j = randrange(0, n)
# extra credit, add nested for loop to count number of reps it takes to sort the list
for sort in range(0, len(numList)):
for item in range(sort+1, len(numList)):
while numList[sort] < numList[item] or add < reps:
add = add + 1
if i < j:
if numList[i] > numList[j]:
temp = numList[i]
numList[i] = numList[j]
numList[j] = temp
elif j < i:
if numList[i] < numList[j]:
temp = numList[j]
numList[j] = numList[i]
numList[i] = temp
print(" List was sorted in " + str(add) + " reps.")
printNow("The List after shuffling: " +str(numList))
我的老师说我的加分部分有太多循环。我被困住了,正在找人解释我做错了什么。
我不希望有人为我解决它,但请告诉我我做错了什么。
我认为这不是您在 two-level for
循环中放置 while
循环的任何理由。使用两个 for
循环,您应该能够遍历列表并使用其他一些 conditional-checker (提示:您已经在显示的脚本中使用过)来确定当前列表元素是否在正确的位置(目前 - 可能会在以后改组)。您可以查看:
冒泡排序
我正在参加 class 使用 JES 的初学者编程,我认为这是 Python 2。 我的老师要求我们创建一个空列表,然后向其中添加随机数,然后将其从低到高排序。那部分我做对了。额外的功劳是添加一个嵌套的 for 循环,该循环将计算对列表进行排序所需的重复次数。 这是我的:
from random import *
def main():
# create list of 25 (this can be changed to number of your choice) random numbers from -100 to 100
# print list with message "list before shuffling"
numList = []
count = 0
while count < 15:
num = randint( -100 , 100 )
numList.append(num)
count = count + 1
printNow("the List before shuffling: " +str(numList))
n = len(numList)
add = 0
# Randomly shuffle list with items only moving if a lower number is being switched with a larger number
# do this 1000 times ( this can be changed to number of your choice)
# print list with message "list after shuffling"
for reps in range (0 , 500):
i = randrange(0, n)
j = randrange(0, n)
# extra credit, add nested for loop to count number of reps it takes to sort the list
for sort in range(0, len(numList)):
for item in range(sort+1, len(numList)):
while numList[sort] < numList[item] or add < reps:
add = add + 1
if i < j:
if numList[i] > numList[j]:
temp = numList[i]
numList[i] = numList[j]
numList[j] = temp
elif j < i:
if numList[i] < numList[j]:
temp = numList[j]
numList[j] = numList[i]
numList[i] = temp
print(" List was sorted in " + str(add) + " reps.")
printNow("The List after shuffling: " +str(numList))
我的老师说我的加分部分有太多循环。我被困住了,正在找人解释我做错了什么。 我不希望有人为我解决它,但请告诉我我做错了什么。
我认为这不是您在 two-level for
循环中放置 while
循环的任何理由。使用两个 for
循环,您应该能够遍历列表并使用其他一些 conditional-checker (提示:您已经在显示的脚本中使用过)来确定当前列表元素是否在正确的位置(目前 - 可能会在以后改组)。您可以查看:
冒泡排序