这是代码:
The_Start = [1,1]
The_End = [1, 1]
for z in range(20):
for x in range(len(The_Start) - 1):
y = The_Start[x] + The_Start[x + 1]
The_End.insert[x + 1, y]
print The_End
The_Start = The_End
The_End = [1, 1]
Run Code Online (Sandbox Code Playgroud)
这段代码应该是Pascal的三角形.错误发生在第六行.
def bubble_sort_by nums
do_it_again = false
nums[0...-1].each_with_index do |item, index|
if yield(nums[index], nums[index + 1]) > 0
nums[index], nums[index + 1] = nums[index + 1], nums[index]
do_it_again = true
end
end
bubble_sort_by nums if do_it_again
nums
end
bubble_sort_by(["hi","hello","hey"]) do |left,right|
right.length - left.length
end
Run Code Online (Sandbox Code Playgroud)
程序基于块进行冒泡排序。在这种情况下,块按长度排序。所以,我得到一个本地跳转错误。花了我一点,但我想通了。当我递归调用该方法时,我没有给它块。但是我该怎么做呢?
这是代码:
list = [2, 3, 5, 7, 11, 13]
list2 = [range(list[-1], 2000000)]
y =11
x = 1
v = list[-1]>= x
while list[-1] ** 2 < 2000000:
y= y + 2
prime = True
while prime == True:
for x in list:
if x * 2 < y:
if y % x == 0:
prime = False
break
if prime == True:
list.append(y)
prime = False
print sum(list)
for u in list:
for w in list2:
if u * …Run Code Online (Sandbox Code Playgroud)