编辑:我不要求一个函数来计算的occurence。我问一个功能通过3 10 10 2算一笔occurence 2,3,等等...这是我的问题
我有成绩的数组,可以说:
[2,2,3,4,4,4,4,5,6,6,8,8,8,9,10,10]
我想有一个改变这个Array
成Dictionary [Int: Int]()
有类似的东西的功能:
func groupArrayBy(array: Array<Int>, coef: Int) -> Array<Int, Int>{
// if coef = 2 -> 2 by 2, count occurence
// Transform the array to:
// [2: 3, 4: 5, 6: 2, 8: 4, 10: 2]
// return Dictionary
}
(配COEF = 3,这将是:[2: 7, 5: 3, 8: 6]
- > 3×3)
我没有发现任何有关。它甚至有可能?
这是我的版本,其中我在一系列筛选基于阵列和coef
变量的第一值,基于该结果I切片远已经计数的那些元件和在循环中的更小的阵列上再次进行过滤。此解决方案要求输入数组以升序进行排序
func group(_ array: [Int], coef: Int) -> [Int: Int] {
var result:[Int:Int] = [:]
var start = array[0]
var end = start + coef - 1
var arr = array
while start <= array[array.count - 1] {
let count = arr.filter({ $0 >= start && $0 <= end}).count
result[start] = count
start = end + 1
end = start + coef - 1
arr = Array(arr[count...])
}
return result
}
这里是上述函数的递归版本
func group(_ array: [Int], coef: Int) -> [Int: Int] {
var result:[Int:Int] = [:]
if array.isEmpty { return result }
let end = array[0] + coef - 1
let count = array.filter({ $0 >= array[0] && $0 <= end}).count
result[array[0]] = count
result = result.merging(group(Array(array[count...]), coef: coef)) { $1 }
return result
}
我还是有点困惑系数。然而,让我们假设你想要的是组的值N
下一个N+coefficient
值。
然后我会通过重新映射原始数组入组值开始:
let items = [2,2,3,4,4,4,4,5,6,6,8,8,8,9,10,10]
let uniqueItems = Set(items)
var itemCoefficientMapping: [Int: Int] = [:]
let coefficient = 3
for item in uniqueItems.sorted() {
// look whether exists some lower value that is not itself mapped to another value
let normalizedItem = (item - (coefficient - 1)...item).first {
uniqueItems.contains($0) && itemCoefficientMapping[$0] == $0
} ?? item
itemCoefficientMapping[item] = normalizedItem
}
// count by mapped value
let counts: [Int: Int] = items.reduce(into: [:]) { result, value in
result[itemCoefficientMapping[value] ?? value, default: 0] += 1
}
print(counts)
你的问题是如此混乱什么coef
意味着我们也可以期待输入数组进行排序,你想要的输出类型。有斯威夫特没有Array<Int, Int>
。
假设输入数组进行排序,你想Array<(Int, Int)>
,你可以写这样的事情:
func getRanges(_ arr: [Int], _ step: Int) -> [Range<Int>] {
return stride(from: arr.first!, through: arr.last!, by: step)
.map {$0..<$0+step}
}
func groupArrayBy(array: Array<Int>, coef: Int) -> [(Int, Int)] {
let grpArr = getRanges(arr, coef).map {rng in
(rng.lowerBound, arr.lazy.filter{rng.contains($0)}.count)
}
return grpArr
}
print(groupArrayBy(array: arr, coef: 2)) //->[(2, 3), (4, 5), (6, 2), (8, 4), (10, 2)]
print(groupArrayBy(array: arr, coef: 3)) //->[(2, 7), (5, 3), (8, 6)]
我的代码是不那么有效,有可能是有人谁可以告诉你更高效的代码。
它可以有另一种更简单的代码如下:
let aa : [Int] = [2,2,3,4,4,4,4,5,6,6,8,8,8,9,10,10]
let mini = aa.min()!
let coeff : Int = 3
let dict = Dictionary(grouping: aa) {
mini + (($0 - mini) / coeff) * coeff
}.mapValues{$0.count}
print (dict)