Considering we have the following:
typealias Fruit = String
typealias Count = Int
struct Smoothie {
let uuid: String
let fruits: [Fruit: Count]
}
let smoothies: [Smoothie] = ...
Given a conditions array set by the user:
let conditions: [Fruit] = ...
The lowest the index in the conditions array, the most important we consider it is for sorting.
I would like to sort the smoothies array so that the smoothies that contain the highest number of fruits (Count) from the conditions array, appear first.
If a fruit doesn't appear in the fruits dictionary, we consider its count as 0.
For example:
let fruitsA = ["Apple": 3, "Mango": 4, "Pear": 8, "Cherry": 1, "Banana": 2]
let smoothieA = Smoothie(uuid: "smoothie_a", fruits: fruitsA)
let fruitsB = ["Apple": 10, "Mango": 9, "Grapes": 8, "Cherry": 9]
let smoothieB = Smoothie(uuid: "smoothie_b", fruits: fruitsB)
let fruitsC = ["Apple": 23, "Kiwi": 4, "Pear": 1, "Cherry": 17, "Banana": 8]
let smoothieC = Smoothie(uuid: "smoothie_c", fruits: fruitsC)
let fruitsD = ["Apple": 1, "Orange": 6, "Pear": 8]
let smoothieD = Smoothie(uuid: "smoothie_d", fruits: fruitsD)
let conditions: [Fruit] = ["Apple", "Banana", "Cherry"]
Should return the following:
let sortedSmoothies = [smoothieC, smoothieB, smoothieA, smoothieD]
Because we're first sorting by Apple, then by Banana, then by Cherry count.
If I knew it would always be about Apple, Banana & Cherry, I could do the following and it would just work:
let sortedSmoothies = smoothies.sorted {
if $0.fruits["Apple"] != $1.fruits["Apple"] {
return $0.fruits["Apple"] > $1.fruits["Apple"]
} else if $0.fruits["Banana"] != $1.fruits["Banana"] {
return $0.fruits["Banana"] > $1.fruits["Banana"]
} else {
return $0.fruits["Cherry"] > $1.fruits["Cherry"]
}
}
But in my case I do not know what are the fruits (and how many of them) that the user is going to select for filtering in the conditions array.
Does anyone has an idea how to sort this out please?
Thank you!
let sorted = [smoothieA, smoothieB, smoothieC, smoothieD].sorted { (a, b) in
for condition in conditions {
let aAmount = a.fruits[condition] ?? 0
let bAmount = b.fruits[condition] ?? 0
if aAmount == bAmount {
continue
}
return aAmount > bAmount
}
return true
}
Keep in mind that with your sample data, since "Apple" doesn't have any smoothies with equal values, any test will return that same order. But, you could test with some more interesting numbers that have smoothies that have equal numbers of fruits:
let fruitsA = ["Apple": 10, "Mango": 4, "Pear": 8, "Cherry": 20, "Banana": 2]
let smoothieA = Smoothie(uuid: "smoothie_a", fruits: fruitsA)
let fruitsB = ["Apple": 10, "Mango": 9, "Grapes": 8, "Cherry": 9, "Banana": 2]
let smoothieB = Smoothie(uuid: "smoothie_b", fruits: fruitsB)
let fruitsC = ["Apple": 23, "Kiwi": 4, "Pear": 1, "Cherry": 17, "Banana": 8]
let smoothieC = Smoothie(uuid: "smoothie_c", fruits: fruitsC)
let fruitsD = ["Apple": 1, "Orange": 6, "Pear": 8]
let smoothieD = Smoothie(uuid: "smoothie_d", fruits: fruitsD)
You could create an array of the fruit arrays - String[][] which you might call FruitsArr. You would also create an array or list of fruits that can hold as many fruits as the user puts in.
This is all in loose terms, because the swift syntax is unfamiliar. It might just confuse you to see too much Java syntax, sorry. The logic is universal, though.
for (int i = 0; i < fruits.length; i++){
/*fruits is our imaginary array of user-input fruits. This is sorting the arrays one by one by each fruit in fruits*/
String[] mostFruit; //fill it with zeroes or something.
for (int j = 0; j < FruitsArr.length; j++){
String[] thisFruit = FruitsArr[j]l
if (thisFruit[i] > mostFruit[i]){mostFruit = thisFruit;}
}
}
That checks the question: is the value at index i in thisFruit greater than the value in index i in mostFruit? If so, mostFruit becomes thisFruit. The first thisFruit that has at least 1 of the fruit becomes mostFruit, because 1 > 0. Then the comparison becomes more simple.
Your loop will continue through fruit 1, fruit 2, fruit 3, and the rest that the user puts in. It will check each fruit against every array:
Fruit 1:
Does thisFruit have more than 0 of fruit1?
(next)
Does thisFruit have more of this fruit than the current mostFruit?
(next)
Does thisFruit have more of this fruit than the current mostFruit?
Thus it continues through the rest of the fruit arrays and the rest of the fruits.
Related
If I have an array [-23,0,43,7,5,2,4], how to do I split the array [odd][even].
I want 4 elements in the first array and 3 elements in the second array.
I tried to do something like this:
let leftArray = Array(mainArray[0..<mainArray.count/2])
let rightArray = Array(mainArray[mainArray.count/2..<mainArray.count])
I keep on getting [-23,0,43] and [7,5,2,4].
I'd create an extension implementing properties that yield the left and right halves of the array. In this implementation left includes the larger half of the array if the array has an odd number of elements.
extension Array {
var left: ArraySlice<Element> {
prefix(count / 2 + count % 2)
}
var right: ArraySlice<Element> {
suffix(count / 2)
}
}
And its usage would be:
let main = [-23, 0, 43, 7, 5, 2, 4]
let left = main.left
let right = main.right
The result of the above is an ArraySlice for efficiency, but if you want an Array you can just use map.
let main = [-23, 0, 43, 7, 5, 2, 4]
let left = main.left.map { $0 }
let right = main.right.map { $0 }
I think you want to split your array in the middle, and for odd counts, have the first part be the larger one. Use a function like this (intentionally spelled our very explicitly):
func splitArray(_ arr: [Int]) -> ([Int], [Int]) {
let count = arr.count
let half = count.isMultiple(of: 2) ? count / 2 : count / 2 + 1
let left = arr[0..<half]
let right = arr[half..<count]
return (Array(left), Array(right))
}
splitArray([-23,0,43,7,5,2,4]) // ([-23, 0, 43, 7], [5, 2, 4])
splitArray([-23,0,43,7,5,2,4,1]) // ([-23, 0, 43, 7], [5, 2, 4, 1])
This can be simplified and be made generic (i.e., work on all kinds of Collections) like so:
extension Collection {
func splitHalf() -> (SubSequence, SubSequence) {
let count = self.count
let left = self.prefix(count / 2 + count % 2)
let right = self.suffix(count / 2)
return (left, right)
}
}
[-23,0,43,7,5,2,4].splitHalf() // ([-23, 0, 43, 7], [5, 2, 4])
[-23,0,43,7,5,2,4,1].splitHalf() // ([-23, 0, 43, 7], [5, 2, 4, 1])
"12345".splitHalf() // (.0 "123", .1 "45")
"123456".splitHalf() // (.0 "123", .1 "456")
Function for split the array
func getSplitArr(arr:[Int])->Void{
let count = arr.count
let secArrCount = abs(count/2)
var firstArray = [Int]()
for i in 0..<secArrCount{
let value = arr[i]
firstArray.append(value)
}
var secondArray = [Int]()
for i in secArrCount..<count{
let value = arr[i]
secondArray.append(value)
}
print("\(firstArray)")
print("\(secondArray)")
}
Use of Functions
self.getSplitArr(arr: [-23,0,43,7,5,2,4])
Your first question wasn't clear. This should return two arrays one where the first array is even and the second is odd every time.
var array = [-23,0,43,7,5,2,4]
func splitArray(in array : [Int]) -> (firstArray :[Int], secondArray: [Int]) {
let firstArray = array.dropLast(array.count / 2).compactMap { item -> Int in
item
}
let secondArray = array.dropFirst((array.count / 2) + 1).compactMap { item -> Int in
item
}
return (firstArray,secondArray)
}
var newArray = splitArray(in: array)
print(newArray)
This is the solution I had tried but It was in the order of O(n^2) so didn't passed the test result
func sortArrayByValueAndByFrequency(nums : [Int]) {
var countDict = [Int : Int]()
var count = Int()
var values = Int()
var output = [Int]()
for index in 0 ..< nums.count {
for index2 in 0 ..< nums.count{
if nums[index2] == nums[index] {
values = nums[index2]
count += 1
}
}
countDict[values] = count
count = 0
}
let sortedByKey = countDict.sorted { ($0.key < $1.key)}
let sortedByValue = sortedByKey.sorted { ($0.value < $1.value)}
for (k,v) in sortedByValue {
for _ in 1 ... v {
output.append(k)
}
}
output.forEach { (orderedNumber) in
print(orderedNumber)
}
}
Example input/output:
Example array = [1,1,2,3,4,5,5,6,7,7,7,8,9,9,9,20,25,21,20]
Expected output = [2,3,4,6,8,21,25,1,1,5,5,20,20,7,7,7,9,9,9]
example 2 = [1,2,3,4,4,3,3]
output = [1,2,4,4,3,3,3]
This question was asked to me on HackerRank
First determine the number of occurrences of each value (O(n)),
then sort the values, with the number of occurrences as the
first sort criterion, and the value itself as the second
sort criterion (O(n log(n))). The sorting is conveniently done
with a tuple-comparison (compare Swift - Sort array of objects with multiple criteria):
let array = [1,1,2,3,4,5,5,6,7,7,7,8,9,9,9,20,25,21,20]
let countDict = array.reduce(into: [Int:Int]()) {
$0[$1, default: 0] += 1
}
let sorted = array.sorted(by: {
(countDict[$0]!, $0) < (countDict[$1]!, $1)
})
print(sorted)
// [2, 3, 4, 6, 8, 21, 25, 1, 1, 5, 5, 20, 20, 7, 7, 7, 9, 9, 9]
I have this code and I want to pick a random element from Array1 and a random element from Array2 but Xcode is only giving me the number of the element.
let firstNrVar = [0, 2, 4, 6, 8, 10]
let secondNrVar = [0, 2, 4, 6, 8, 10]
func numberRandomizer() {
let shuffledFirstNr = Int(arc4random_uniform(UInt32(firstNrVar.count)))
firstNrLbl.text = "\(shuffledFirstNr)"
print(shuffledFirstNr)
let shuffledSecondNr = Int(arc4random_uniform(UInt32(secondNrVar.count)))
secondNrLbl.text = "\(shuffledSecondNr)"
print(shuffledSecondNr)
}
you have to get the random index between 0 and array count and then get the value at that random index and then set that value to the text
func numberRandomizer() {
let shuffledFirstIndex = Int(arc4random_uniform(UInt32(firstNrVar.count)))
firstNrLbl.text = "\(firstNrVar[shuffledFirstIndex])"
print("\(firstNrVar[shuffledFirstIndex])")
let shuffledSecondIndex = Int(arc4random_uniform(UInt32(secondNrVar.count)))
secondNrLbl.text = "\(secondNrVar[shuffledSecondIndex])"
print("\(secondNrVar[shuffledSecondIndex])")
}
Just get the values using the number of the element.
let value = firstNrVar[shuffledFirstNr]
firstNrLbl.text = "\(value)"
let secondValue = secondNrVar[shuffledSecondNr]
secondNrLbl.text = "\(secondValue)"
This question already has answers here:
Find min / max value in Swift Array
(16 answers)
Closed 1 year ago.
I am writing an algorithm to find the lowest number in an array however my print statement keeps saying that the lowest number is 0. I have the following:
var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
func findMin(numbers: NSArray) {
var minValue = numbers[0]
var isSmallest: Bool
for i in 0...numbers.count {
isSmallest = true
for j in 0...numbers.count {
if i > j {
isSmallest = false
}
}
if isSmallest {
minValue = i
}
}
print("Smallest value in the list is \(minValue)")
}
findMin(numbers: list as NSArray)
My print statement returns as:
"Smallest value in the list is 0\n"
I feel like the algorithm is correct. Any ideas?
EDIT: Answered my own question
I was iterating over indices and not actual values. Thanks to one of the users in the comments. The correct code should be:
var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
func findMin(numbers: NSArray) {
var minValue = numbers[0]
var isSmallest: Bool
for i in list {
isSmallest = true
for j in list {
if i > j {
isSmallest = false
}
}
if isSmallest {
minValue = i
}
}
print("Smallest value in the list is \(minValue)")
}
findMin(numbers: list as NSArray)
Simply
let list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
let minValue = list.min()
For logic use try this
var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
var minValue = list[0]
for num in list {
minValue = (num < minValue) ? num : minValue
}
print("Smallest value in the list is \(minValue)")
For direct get min value by property
let list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
let minValue = list.min()
Or you could just use
var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
list.min() // returns 2
If you'd like to find the min value without an extra loop, try this:
var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
func findMin(numbers: NSArray) {
var minValIdx = 0
var minValue = numbers[0] as! Int
for i in 1..<numbers.count {
if (numbers[i] as! Int) < minValue {
minValue = numbers[i] as! Int
minValIdx = i
}
}
print("Smallest value in the list is \(minValue)")
}
findMin(numbers: list as NSArray)
You can use this code in Swift for manual algorithm:
let list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
var smallest = list[0]
for item in list {
if (item < smallest) {
smallest = item
}
}
print("smallest number is: \(smallest)")
And if you want Swift to do the hard work then use this:
let smallest = list.min()
print("smallest number is: \(smallest)")
here it is your solution
let numbers = [1, 6, 3, 9, 4, 6]
let min = minElement(numbers) // 1
let position = find(array, min)// it will return index
Just to throw a few more options out there, assuming you have to actually show some logic:
func min<T:Comparable>(_ elements:[T]) -> T? {
guard let first = elements[0] else {
return nil
}
return elements.reduce(first, min)
}
print(min(list))
or put it in an extension, this is essentially the definition of Array.min
extension Array where Element : Comparable {
func smallest() -> Element? {
guard let first = self.first else {
return nil
}
// Use 'Swift.min' to get to the global function since Array
// already has a min function
return reduce(first, Swift.min)
}
}
print(list.smallest())
You can use this code:
it is in C#
var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
int minVal = list[0];
for (int i = 1; i < list.Length; i++)
{
if (list[i] < minVal)
{
minVal = intArray[i];
}
}
To find the minimum element in the sequence, Swift 3 have an istance method called min():
var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
let minElem = list.min()
If the sequence has no elements, returns nil.
This method can be used also for a list of floats:
let heights = [67.5, 65.7, 64.3, 61.1, 58.5, 60.3, 64.9]
let minHeight = heights.min()
You can find the official document referece here
How to remove elements from array that match elements in another array?
Assume we have an array and we loop through it and find out which elements to remove:
var sourceItems = [ ... ]
var removedItems = [SKShapeNode]()
for item : SKShapeNode in sourceItems {
if item.position.y > self.size.height {
removedItems.append(item)
item.removeFromParent()
}
}
sourceItems -= removedItems // well that won't work.
You can use the filter function.
let a = [1, 2, 3]
let b = [2, 3, 4]
let result = a.filter { element in
return !b.contains(element)
}
result will be [1]
Or more succinctly...
let result = a.filter { !b.contains($0) }
Check out the Swift Standard Library Reference
Or you can use the Set type.
let c = Set<Int>([1, 2, 3])
let d = Set<Int>([2, 3, 4])
c.subtract(d)
Be mindful if using the Set option, that your results only be unique values and will not maintain the initial ordering, if that matters to you, whereas the Array filter option will maintain the initial array's order, at least what elements remain.
Swift 3
let c = Set<Int>([65, 1, 2, 3, 1, 3, 4, 3, 2, 55, 43])
let d = Set<Int>([2, 3, 4])
c.subtracting(d)
c = {65, 2, 55, 4, 43, 3, 1}
d = {2, 3, 4}
result = {65, 55, 43, 1}