[英]Ruby: Continue looping through an array of fixed length, adding 1 to each element for each iteration
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
問題:大家好。 我很難找到在 if decors.length == branches 塊中使用的正確語法。 目前,
decorations.map! {|n| n + 1 }
每次迭代時,裝飾數組中的每個元素都加 1。 相反,我想將 1 添加到單個數組元素(從左到右),直到計數器等於裝飾品的數量。
目標: baubles_on_tree function 的最終目標是在聖誕樹的所有分支上均勻分布裝飾品。 如果裝飾品 = 7 和分支 = 5,則要返回的數組將為[2,2,1,1,1]
。
感謝您的指導!
這行得通。
注意:看來你是Ruby的初學者,所以我盡量讓我的代碼盡可能簡單:)
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
您正在考慮如何作為人類來做這件事,一次將裝飾品貼在樹上。 你正在使用循環和其他東西來模擬這種行為。 有時重要的是退后一步,看看你想要的結果,然后想出最有效的方法來獲得這個結果。
如果我們這樣做,我們可以跳過很多循環邏輯:
branches
划分ornaments
。branches
數組的每個元素。這將迭代(循環)的數量減少到不均勻分布在分支上的任何東西。
為了實現這一點,我們需要兩行代碼。
Array.new(branches)
將創建一個元素數量與branches
相同的數組。 該塊告訴Array.new
為每個元素分配什么值。 在這種情況下,我們分配ornaments / branches
。ornaments % branches
是ornaments / branches
的剩余部分。 因此,這一行執行傳遞給#times
方法的代碼塊ornaments % branches
branchs 次,將0
傳遞給 %branchs ornaments % branches - 1
到i
一個接一個。 我們將這些索引處的每個元素加1
。 應用測試baubles_on_tree(50,9)
:
50 / 9
是5
,所以在那一行之后, tree == [5, 5, 5, 5, 5, 5, 5, 5, 5]
。1
添加到前五個元素,因為50 % 9
是5
,我們得到tree == [6, 6, 6, 6, 6, 5, 5, 5, 5]
。 在第 5 行,我們將tree
返回給調用者,並且 Bob 是你的叔叔。 :)
回答你的具體問題:你得到一個很大的1
數組,開頭有幾個2
的原因,而不是你正在尋找的值,是因為你將一個1
推到你的數組上每個裝飾一次,而不是為每個裝飾一次將1
添加到適當的數組元素。 這就是decorations << 1
所做的。 這樣做 50 次,你會得到一個包含 50 個1
的數組。
因此,您會得到一個大小ornaments
數組,每個元素中都有一個1
。 在循環的一次迭代中,您的數組( decorations
)與branches
大小相同,您將1
以當前大小添加到數組中的所有值,然后繼續在其末尾粘貼1
直到你的飾品用完了。
所以你得到一個 size 的數組ornaments
,第一個branches
元素為2
,其他元素為1
。
要做到這一點,你必須循環ornaments
(你做對了),但在裝飾循環內,你必須循環branches
並為每個元素添加1
(並在你在時不斷增加外部ornament
循環計數器它,因為每次循環通過內循環時都會用完一個裝飾品)。 您還必須弄清楚如何開始,以及如何確保在飾品用完時及時停下來。
像這樣的東西(通過你的測試):
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
雖然這可能是一個有趣的練習,只使用while
循環,但只做一些算術並獲得相同的結果要容易得多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.