Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MARK: - Разбить массив по границам
- func calculate(borders: [Int], values: [Int]) -> [(quantity: Int, sum: Int)] {
- var curRangeIndex = 0
- // кол-во = кол-во интервалов + 1
- var res = Array(repeating: (quantity: 0, sum: 0), count: borders.count + 1)
- for value in values {
- // while нужен на случай если границы без пропусков 4 5 6
- while curRangeIndex < borders.count && value > borders[curRangeIndex] {
- curRangeIndex += 1
- }
- res[curRangeIndex].quantity += 1
- res[curRangeIndex].sum += value
- }
- return res
- }
- // Пример использования:
- let borders = [4, 8]
- let values = [1, 3, 4, 5, 8, 9]
- let result = calculate(borders: borders, values: values)
- print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement