我需要更改最大值以訂購。首先是最大值和最大值 - 1。( 100, 99, 100 ) 必須等于 1。它將是 [5, 55, 1 , 2, 1 , 1 , 98]。然后我需要將 (98) 更改為 2,因為現在 98 是陣列中的最大值。我的目標是擁有 [4, 3, 1, 5, 1, 1, 2]。我嘗試了我的第一步并收到了這個
var arr = [5, 55, 100, 2, 99, 100, 98]
func toOrder(_ arr: [Int]) -> [Int] {
var arr = arr
var max = arr.max()!
var order = 1
for i in arr.indices {
if arr[i] == max || arr[i] == max - 1 {
arr[i] = order
}
}
return arr
}
toOrder(arr)
我被卡住了。我收到了 [5, 55, 1, 2, 1, 1, 98],但是如何繼續使用保存的值遍歷陣列?
uj5u.com熱心網友回復:
如果我正確理解了您的邏輯(因為您提供了示例,答案似乎不對,我稍后再談):
我會使用遞回方法。
- 找到最大的“未處理”值(否則,一旦你有了最終輸出,它將一遍又一遍地迭代),如果沒有找到就停止。
- 更新陣列的值等于最大值或等于最大值 - 1。
- 重做
func iterates(items: inout [Item], iteration: Int = 0) {
let maxValue = items.filter { !$0.hasBeenTreated }.max(by: { $0.value < $1.value })
print("maxValue found: \(maxValue)")
guard let maxValue = maxValue else { return } //Check if there are still value max non treated, else, it ends here
let newItems = items.map { anItem -> Item in
if anItem.hasBeenTreated == false && (anItem.value == maxValue.value || anItem.value == maxValue.value - 1) {
return Item(value: iteration 1, hasBeenTreated: true)
} else {
return anItem
}
}
print("NewItems: \(newItems)")
items = newItems
iterates(items: &items, iteration: iteration 1)
}
在...的幫助下:
struct Item: CustomStringConvertible {
var value: Int
var hasBeenTreated: Bool
var description: String {
return "\(value) (\(hasBeenTreated))"
}
}
要測驗它:
var itemsToTest1 = [5, 55, 100, 2, 99, 100, 98].map { Item(value: $0, hasBeenTreated: false) }
iterates(items: &itemsToTest1)
print("Output Items1: \(itemsToTest1)")
print("Output Array1: \(itemsToTest1.map({ $0.value }))")
var itemsToTest2 = [1, 2, 3, 4, 5, 6, 7, 8, 9].map { Item(value: $0, hasBeenTreated: false) }
iterates(items: &itemsToTest2)
print("Output Items2: \(itemsToTest2)")
print("Output Array2: \(itemsToTest2.map({ $0.value }))")
輸出僅用于除錯,但您可以看到計算的進展:
maxValue found: Optional(100 (false))
NewItems: [5 (false), 55 (false), 1 (true), 2 (false), 1 (true), 1 (true), 98 (false)]
maxValue found: Optional(98 (false))
NewItems: [5 (false), 55 (false), 1 (true), 2 (false), 1 (true), 1 (true), 2 (true)]
maxValue found: Optional(55 (false))
NewItems: [5 (false), 3 (true), 1 (true), 2 (false), 1 (true), 1 (true), 2 (true)]
maxValue found: Optional(5 (false))
NewItems: [4 (true), 3 (true), 1 (true), 2 (false), 1 (true), 1 (true), 2 (true)]
maxValue found: Optional(2 (false))
NewItems: [4 (true), 3 (true), 1 (true), 5 (true), 1 (true), 1 (true), 2 (true)]
maxValue found: nil
Output Items1: [4 (true), 3 (true), 1 (true), 5 (true), 1 (true), 1 (true), 2 (true)]
Output Array1: [4, 3, 1, 5, 1, 1, 2]
maxValue found: Optional(9 (false))
NewItems: [1 (false), 2 (false), 3 (false), 4 (false), 5 (false), 6 (false), 7 (false), 1 (true), 1 (true)]
maxValue found: Optional(7 (false))
NewItems: [1 (false), 2 (false), 3 (false), 4 (false), 5 (false), 2 (true), 2 (true), 1 (true), 1 (true)]
maxValue found: Optional(5 (false))
NewItems: [1 (false), 2 (false), 3 (false), 3 (true), 3 (true), 2 (true), 2 (true), 1 (true), 1 (true)]
maxValue found: Optional(3 (false))
NewItems: [1 (false), 4 (true), 4 (true), 3 (true), 3 (true), 2 (true), 2 (true), 1 (true), 1 (true)]
maxValue found: Optional(1 (false))
NewItems: [5 (true), 4 (true), 4 (true), 3 (true), 3 (true), 2 (true), 2 (true), 1 (true), 1 (true)]
maxValue found: nil
Output Items2: [5 (true), 4 (true), 4 (true), 3 (true), 3 (true), 2 (true), 2 (true), 1 (true), 1 (true)]
Output Array2: [5, 4, 4, 3, 3, 2, 2, 1, 1]
現在,使用評論中給出的最后一個示例(已洗掉答案):
[1, 2, 3, 4, 5, 6, 7, 8, 9]
應該產生 `[ 5, 5, 4, 4, 3, 3, 2, 2, 1, 1],但我不同意。
迭代應該是:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 1, 1]
[1, 2, 3, 4, 5, 2, 2, 1, 1]
[1, 2, 3, 3, 3, 2, 2, 1, 1]
[1, 4, 4, 3, 3, 2, 2, 1, 1]
[5, 4, 4, 3, 3, 2, 2, 1, 1] (with only one 5 value at start)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/470080.html
上一篇:通過R在集群之間繪制直方圖