Recursively count items in a multidimensional array in generic and idiomatic Swift - arrays

I have a multi-dimensional array and I need a count of all of the items within all of the arrays, excluding container arrays themselves from the count.
What would be the most generic and idiomatic solution in Swift? I'm guessing it's going to be something functional (a reduce() operation?), but not sure on the best overall approach.
The obvious non-functional approach would be to simply iterate over the array and tally up the number of items.

With the latest Swift 2.0 beta 6 you can use flatten()
let array = [[1, 2, 3], [4, 5], [6]]
array.flatten().count
EDIT: Just tested it: Lazy is not needed, the values are never evaluated, it just calculates endIndex - startIndex of every subcollection.

You can do the following :
let array = [[1, 2, 3], [4, 5], [6]]
let countOfAll = array.map { (nested) -> Int in
return nested.count
}.reduce(0, combine: +) // 6

For Swift 2 you can use flatMap.
var anArray = [[1,0,0], ["asdf","df","lef"], [0,0,1]]
var flatArray = anArray.flatMap { $0 }
print(flatArray.count) // 9

Related

Sort an array of arrays by the number of same occurencies in Ruby

This question is different from this one.
I have an array of arrays of AR items looking something like:
[[1,2,3], [4,5,6], [7,8,9], [7,8,9], [1,2,3], [7,8,9]]
I would like to sort it by number of same occurences of the second array:
[[7,8,9], [1,2,3], [4,5,6]]
My real data are more complexes, looking something like:
raw_data = {}
raw_data[:grapers] = []
suggested_data = {}
suggested_data[:grapers] = []
varietals = []
similar_vintage.varietals.each do |varietal|
# sub_array
varietals << Graper.new(:name => varietal.grape.name, :grape_id => varietal.grape_id, :percent => varietal.percent)
end
raw_data[:grapers] << varietals
So, I want to sort raw_data[:grapers] by the max occurrencies of each varietals array comparing this value: grape_id inside them.
When I need to sort a classical array of data by max occurencies I do that:
grapers_with_frequency = raw_data[:grapers].inject(Hash.new(0)) { |h,v| h[v] += 1; h }
suggested_data[:grapers] << raw_data[:grapers].max_by { |v| grapers_with_frequency[v] }
This code doesn't work cos there are sub arrays there, including AR models that I need to analyze.
Possible solution:
array.group_by(&:itself) # grouping
.sort_by {|k, v| -v.size } # sorting
.map(&:first) # optional step, depends on your real data
#=> [[7, 8, 9], [1, 2, 3], [4, 5, 6]]
I recommend you take a look at the Ruby documentation for the sort_by method. It allows you to sort an array using anything associated with the elements, rather than the values of the elements.
my_array.sort_by { |elem| -my_array.count(elem) }.uniq
=> [[7, 8, 9], [1, 2, 3], [4, 5, 6]]
This example sorts by the count of each element in the original array. This is preceded with a minus so that the elements with the highest count are first. The uniq is to only have one instance of each element in the final result.
You can include anything you like in the sort_by block.
As Ilya has pointed out, having my_array.count(elem) in each iteration will be costlier than using group_by beforehand. This may or may not be an issue for you.
arr = [[1,2,3], [4,5,6], [7,8,9], [7,8,9], [1,2,3], [7,8,9]]
arr.each_with_object(Hash.new(0)) { |a,h| h[a] += 1 }.
sort_by(&:last).
reverse.
map(&:first)
#=> [[7.8.9]. [1,2,3], [4,5,6]]
This uses the form of Hash::new that takes an argument (here 0) that is the hash's default value.

Splitting an Array into Sub-Arrays in Swift [duplicate]

This question already has answers here:
In Swift, an efficient function that separates an array into 2 arrays based on a predicate
(7 answers)
Closed 6 months ago.
Problem
Given an array of values how can I split it into sub-arrays made of elements that are equal?
Example
Given this array
let numbers = [1, 1, 1, 3, 3, 4]
I want this output
[[1,1,1], [3, 3], [4]]
What I am NOT looking for
A possible way of solving this would be creating some sort of index to indicate the occurrences of each element like this.
let indexes = [1:3, 3:2, 4:1]
And finally use the index to rebuild the output array.
let subsequences = indexes.sort { $0.0.0 < $0.1.0 }.reduce([Int]()) { (res, elm) -> [Int] in
return res + [Int](count: elm.1, repeatedValue: elm.0)
}
However with this solution I am losing the original values. Of course in this case it's not a big problem (an Int value is still and Inteven if recreated) but I would like to apply this solution to more complex data structures like this
struct Starship: Equatable {
let name: String
let warpSpeed: Int
}
func ==(left:Starship, right:Starship) -> Bool {
return left.warpSpeed == right.warpSpeed
}
Final considerations
The function I am looking for would be some kind of reverse of flatten(), infact
let subsequences: [[Int]] = [[1,1,1], [3, 3], [4]]
print(Array(subsequences.flatten())) // [1, 1, 1, 3, 3, 4]
I hope I made myself clear, let me know should you need further details.
// extract unique numbers using a set, then
// map sub-arrays of the original arrays with a filter on each distinct number
let numbers = [1, 1, 1, 3, 3, 4]
let numberGroups = Set(numbers).map{ value in return numbers.filter{$0==value} }
print(numberGroups)
[EDIT] changed to use Set Initializer as suggested by Hamish
[EDIT2] Swift 4 added an initializer to Dictionary that will do this more efficiently:
let numberGroups = Array(Dictionary(grouping:numbers){$0}.values)
For a list of objects to be grouped by one of their properties:
let objectGroups = Array(Dictionary(grouping:objects){$0.property}.values)
If you could use CocoaPods/Carthage/Swift Package Manager/etc. you could use packages like oisdk/SwiftSequence which provides the group() method:
numbers.lazy.group()
// should return a sequence that generates [1, 1, 1], [3, 3], [4].
or UsrNameu1/TraverSwift which provides groupBy:
groupBy(SequenceOf(numbers), ==)
If you don't want to add external dependencies, you could always write an algorithm like:
func group<S: SequenceType where S.Generator.Element: Equatable>(seq: S) -> [[S.Generator.Element]] {
var result: [[S.Generator.Element]] = []
var current: [S.Generator.Element] = []
for element in seq {
if current.isEmpty || element == current[0] {
current.append(element)
} else {
result.append(current)
current = [element]
}
}
result.append(current)
return result
}
group(numbers)
// returns [[1, 1, 1], [3, 3], [4]].
Let's assume that you have an unsorted array of items. You will need to sort the initial array then you will have something like this:
[1, 1, 1, 3, 3, 4]
After that you will initialize two arrays: one for storing arrays and another one to use it as a current array.
Loop through the initial array and:
if the current value isn't different from the last one, push it to the current array
otherwise push the current array to the first one then empty the current array.
Hope it helps!
Worth mentioning, using Swift Algorithms this is now a one-liner:
import Algorithms
let numbers = [1, 1, 1, 3, 3, 4]
let chunks: [[Int]] = numbers.chunked(by: ==).map { .init($0) }
print(chunks) // [[1, 1, 1], [3, 3], [4]]

Swift create one Int array from two int arrays by taking the max() at each index

I feel like this may call for reduce, map or something like it to solve but I'm not yet familiar enough with these and was hoping someone here might be. Lets say I have
arrayOne = [1, 3, 7]
arrayTwo = [2, 1, 10]
the expected result for what I'm trying to do would be
mergedArray = [2, 3, 10]
I know I can do this with a relatively simple for loop in a method but I am looking for a more "swift" way to do it if it's possible.
And Yes, both arrays will always be the same length.
This will work:
let arrayOne = [1, 3, 7]
let arrayTwo = [2, 1, 10]
let mergedArray = zip(arrayOne, arrayTwo).map{max($0, $1)}
First, pair each element in two arrays with zip, and then use map to each pair.

How to calculate the sum of each position of subarrays?

Now my array is:
[[1,2,3],[4,5,6],[]]
I want to calculate this array and return a result as:
[5,7,9]
If there is a null array, remove it. Then plus every position for each sub array.
If use array's each method, maybe I can get the result. But is there a better way just use ruby's array method?
Another one liner
arr.reject(&:empty?).transpose.map{|x| x.reduce(:+)}
first get rid of the []
reject(&:empty?) # equivalent to reject{|x| x.empty?}
now .transpose to obtain
[[1, 4], [2, 5], [3, 6]]
Add up each sublist with
.map{|x| x.reduce(:+)}
Here's a one-liner that uses a lot of nice Ruby Array methods like reject, reduce, zip, and map.
array.reject(&:empty?).reduce { |result, e| result.zip(e).map { |x,y| x+y } }
See Ruby Array documentation for more details, and to see what other slick things you can do with them.
array = [[1,2,3],[4,5,6],[]]
require 'matrix'
Matrix[*array.reject { |a| a.empty? }].row_vectors.reduce(:+).to_a
#=> [5, 7, 9]

Flatten all values of multiple arrays in Swift

I have a Dictionary of Integer Arrays like below:
let numbers = [1: [2, 3], 4: [5, 6, 7], 8: [9]]
What I really want is a single flattened Array of all of the values (which themselves are arrays), like so:
[2, 3, 5, 6, 7, 9]
Now, I have been able to call numbers.values.array to get:
[[2, 3], [5, 6, 7], [9]]
But what I'm looking for is to merge these one step further, flattening them.
Does Swift (1.1, or 1.2) offer a convenience method for this?
With a combination of numbers.values.array and a reduce function you can simplify this down in one line of code.
numbers.values.array.reduce([], combine: +) // [5,6,7,2,3,9]
However, I would like to note that since you are using a dictionary, you cannot guarantee that the values will be sorted, so you can use the sorted function to accomplish this:
sorted(numbers.values.array.reduce([], combine: +), <) // [2,3,5,6,7,9]
As #Jeffery Thomas stated, you can also use flatmap which was just added in Swift 1.2:
sorted(numbers.values.array.flatMap { $0 }, <)
And to take it a step further, using the global sorted function, the < is extraneous because it is the default and using the global reduce and flatMap functions, you can remove the array property as pointed out by Martin R, so it can be reduced down to:
sorted(reduce(numbers.values, [], +))
sorted(flatMap(numbers.values) { $0 })
Another possible solution is
[].join(numbers.values)
And if you want the values in the order corresponding to the sorted
dictionary keys then it would be
flatMap(sorted(numbers.keys)) { numbers[$0]! }
This is called flattening, and it's a relatively common operation. There are a number of ways to do it, so pick one that suits your needs.
numbers.values.array.reduce([], combine: +) // As stated by #Bluehound
reduce(numbers.values, [], +)
numbers.values.array.flatMap { $0 } // Swift 1.2 (Xcode 6.3)
flatMap(numbers.values) { $0 } // Swift 1.2 (Xcode 6.3)
flatMap may be the most useful, if the next step after flattening is mapping.
NOTE: Thanks #MartinR for the syntax tip.

Resources