Arrays Ruby:继续循环一个固定长度的数组,每次迭代为每个元素添加1
为每个迭代向装饰数组中的每个元素添加1。相反,我想在单个数组元素中添加1(从左到右),直到计数器等于装饰的数量 目标:圣诞树上装饰物功能的最终目标是将装饰物均匀分布在圣诞树的所有树枝上。如果装饰=7且分支=5,则返回的数组将是Arrays Ruby:继续循环一个固定长度的数组,每次迭代为每个元素添加1,arrays,ruby,conditional-statements,Arrays,Ruby,Conditional Statements,为每个迭代向装饰数组中的每个元素添加1。相反,我想在单个数组元素中添加1(从左到右),直到计数器等于装饰的数量 目标:圣诞树上装饰物功能的最终目标是将装饰物均匀分布在圣诞树的所有树枝上。如果装饰=7且分支=5,则返回的数组将是[2,2,1,1,1] 感谢您的指导 这是有效的 注意:您似乎是Ruby的初学者,所以我试着尽可能简单地编写代码:) def创建数组(nb_元素,nb_数组) 数组=[] i=0 环道 数组[i]=数组.new(nb_元素) i+=1 如果i==nb_数组 中断#这将导致执
[2,2,1,1,1]
感谢您的指导 这是有效的
注意:您似乎是Ruby的初学者,所以我试着尽可能简单地编写代码:)
def创建数组(nb_元素,nb_数组)
数组=[]
i=0
环道
数组[i]=数组.new(nb_元素)
i+=1
如果i==nb_数组
中断#这将导致执行退出循环
结束
结束
返回数组
结束
def get_nb_阵列(装饰品、树枝)
如果装饰==0
返回分支
其他的
返回装饰/分支+装饰%分支
结束
结束
树上的装饰物(装饰品、树枝)
如果分支==0
return“奶奶,我们得先买棵圣诞树!”
结束
数组=创建数组(分支,获取数组(装饰,分支))
n=装饰品
i=0
而我的长度
ar=阵列[i]
j=0
而j0
ar[j]=1
其他的
ar[j]=0
结束
n-=1
j+=1
结束
i+=1
结束
返回arrays.transpose.map{| x | x.reduce(:+)}
结束
祝你好运 这是有效的
注意:您似乎是Ruby的初学者,所以我试着尽可能简单地编写代码:)
def创建数组(nb_元素,nb_数组)
数组=[]
i=0
环道
数组[i]=数组.new(nb_元素)
i+=1
如果i==nb_数组
中断#这将导致执行退出循环
结束
结束
返回数组
结束
def get_nb_阵列(装饰品、树枝)
如果装饰==0
返回分支
其他的
返回装饰/分支+装饰%分支
结束
结束
树上的装饰物(装饰品、树枝)
如果分支==0
return“奶奶,我们得先买棵圣诞树!”
结束
数组=创建数组(分支,获取数组(装饰,分支))
n=装饰品
i=0
而我的长度
ar=阵列[i]
j=0
而j0
ar[j]=1
其他的
ar[j]=0
结束
n-=1
j+=1
结束
i+=1
结束
返回arrays.transpose.map{| x | x.reduce(:+)}
结束
祝你好运 您的方法可以大大简化 捷径 你在想,作为一个人类,你会如何做,一次一个地把装饰品贴在树上。您正在使用循环和其他东西来模拟这种行为。有时候退一步,看看你想要的结果,并想出最有效的方法来获得结果是很重要的 如果我们这样做,我们可以跳过很多循环逻辑:
装饰品
除以分支
分支的数组的每个元素
分支
元素数相同的数组。块告诉数组。新的分配给每个元素的值。在这种情况下,我们分配装饰/分支
装饰物%分支
是装饰物/分支
的剩余部分。因此,这行执行传递给#times
方法装饰%branchs
次的块,依次传递0
到装饰%branchs-1
到i
。我们将1
添加到这些索引处的每个元素中饰品:
第3行创建了一个包含九个元素的数组50/9
是5
,因此在该行之后,树==[5,5,5,5,5,5,5]
第4行将1
添加到前五个元素,因为50%9
是5
,我们得到树==[6,6,6,6,5,5,5]
在第5行,我们将tree
返回给调用者,以及鲍勃的叔叔。:)
艰难的道路,解释你的错误
回答您的具体问题:您在开始时会得到大量的1
,其中有一些2
,而不是您正在寻找的值,这是因为您为每个装饰将1
推到阵列上一次,而不是为每个装饰向相应的数组元素添加一次1
。这就是你的方法可以大大简化的地方
捷径
你在想,作为一个人类,你会如何做,一次一个地把装饰品贴在树上。您正在使用循环和其他东西来模拟这种行为。有时候退一步,看看你想要的结果,并想出最有效的方法来获得结果是很重要的
如果我们这样做,我们可以跳过很多循环逻辑:
将装饰品
除以分支
将该值分配给大小分支的数组的每个元素
迭代您的其余装饰,在每个分支上添加一个,直到用完为止
这会将迭代(循环)的数量减少到不均匀分布在分支上的数量
要实现这一点,我们需要两行代码
在第3行,w
def baubles_on_tree(ornaments, branches)
counter = 0
decorations = []
puts ornaments, branches
# Evenly distribute ornaments across branches
# if there are no branches, return string
if branches == 0
return "Grandma, we will have to buy a Christmas tree first!"
end
puts "The number of ornaments (#{ornaments}) divided by branches (#{branches}) equal " + ((ornaments / branches).to_f).to_s
# add 1 to the decorations array while counter <= ornaments.
# ensure decoration.length maxes out at branches
while counter <= ornaments
# Add 1 to counter until it reaches the number of ornaments
counter += 1
#puts decorations.length
# Push 1 to the decorations array for each iteration
decorations << 1
# if the decorations array length equals the number of branches,
# stop creating new indices and instead add 1 to each array element
if decorations.length == branches
print decorations.length
decorations.map! {|n| n + 1 }
end
end
print decorations
end
Test.describe("Here are some test cases") do
Test.assert_equals(baubles_on_tree(5,5),[1,1,1,1,1])
Test.assert_equals(baubles_on_tree(5,0),"Grandma, we will have to buy a Christmas tree first!")
Test.assert_equals(baubles_on_tree(6,5),[2,1,1,1,1])
Test.assert_equals(baubles_on_tree(50,9),[6,6,6,6,6,5,5,5,5])
Test.assert_equals(baubles_on_tree(0,10),[0,0,0,0,0,0,0,0,0,0])
end
decorations.map! {|n| n + 1 }
def create_arrays(nb_element, nb_arrays)
arrays = []
i = 0
loop do
arrays[i] = Array.new(nb_element)
i += 1
if i == nb_arrays
break # this will cause execution to exit the loop
end
end
return arrays
end
def get_nb_arrays(ornaments, branches)
if ornaments == 0
return branches
else
return ornaments/branches + ornaments % branches
end
end
def baubles_on_tree(ornaments, branches)
if branches == 0
return "Grandma, we will have to buy a Christmas tree first!"
end
arrays = create_arrays(branches, get_nb_arrays(ornaments, branches))
n = ornaments
i = 0
while i < arrays.length
ar = arrays[i]
j = 0
while j < ar.length
if n > 0
ar[j] = 1
else
ar[j] = 0
end
n -= 1
j += 1
end
i += 1
end
return arrays.transpose.map {|x| x.reduce(:+)}
end
1 def baubles_on_tree(ornaments, branches)
2 return "Grandma, we will have to buy a Christmas tree first!" if branches.zero?
3 tree = Array.new(branches) { |e| ornaments / branches }
4 (ornaments % branches).times { |i| tree[i] += 1 }
5 tree
6 end
def baubles_on_tree(ornaments, branches)
return "Grandma, we will have to buy a Christmas tree first!" if branches.zero?
decorations = []
# First, put the right amount of zeros in the array (ensure that
# decorations.size == branches, and each element has a 0 in it)
counter = 0
while counter < branches
decorations << 0
counter += 1
end
# Then, loop through ornaments
counter = 0
while counter < ornaments
# Inside the ornaments loop, loop through branches. We have to keep track
# of the outer loop here, too, because if we run out of ornaments before
# we're done putting an ornament on each branch, we want to quit right away.
counter2 = 0
while counter2 < branches && counter < ornaments
# ADD 1, don't push 1 (don't use <<)
decorations[counter2] += 1
# increment both counters; since we're putting an ornament on each branch,
# we have to keep the outer loop going while we loop through the inner one
counter2 += 1
counter += 1
end
end
decorations # Return the array
end