Map Dictionary Keys to add values - Swift [closed] - swift

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have two dictionaries: a data dict and a results dict
var data = ["flushes": 0.0, "times": 0.0, "glasses": 0.0, "showers": 0.0, "brushings": 0.0, "loads": 0.0, "washings": 0.0, "baths": 252.0, "dishes": 0.0]
let results = ["flushes": 21.0, "times": 0.0, "glasses": 0.0, "showers": 150.0, "brushings": 4.0, "loads": 0.0, "washings": 5.0, "baths": 0.0, "dishes": 9.0]
I am wondering how to add like values based on key and have only one dictionary.

Assuming data is mutable, this should do it:
data.merge(results, uniquingKeysWith: { $0 + $1 })

In addition to Ole`s answer, at this point there are two other -syntactic sugar- options:
You could type it as:
data.merge(results, uniquingKeysWith: +)
Or as a trailing closure syntax:
data.merge(results) { $0 + $1 }
Hence:
print(data)
/*
["flushes": 21.0,
"times": 0.0,
"glasses": 0.0,
"showers": 150.0,
"brushings": 4.0,
"loads": 0.0,
"washings": 5.0,
"baths": 252.0,
"dishes": 9.0]
*/

Related

How do I spot the smallest negative number using swift

OK,
Having a mental block here... I got a range of vectors
node 0 SCNVector3(x: 8.208711e-08, y: 0.0, z: -1.9389672)
node 1 SCNVector3(x: -0.93896717, y: 0.0, z: -1.0000001)
node 2 SCNVector3(x: -8.208711e-08, y: 0.0, z: -0.06103283)
node 3 SCNVector3(x: 0.93896717, y: 0.0, z: -0.99999994)
node 4 SCNVector3(x: 0.0, y: 0.93896717, z: -1.0)
node 5 SCNVector3(x: 0.0, y: -0.93896717, z: -1.0)
And I want to select node2 since it has the smallest negative number. It's a stupid question, I know; I am sorry.
struct Sats {
var theNode = SCNNode()
var theIndex:Int = 7
}
if cubeNode != nil {
cubeNode.enumerateChildNodes { (node, stop) in
let search = /face(\d+)/
if let result = try? search.wholeMatch(in: node.name!) {
print("node \(result.1) \(node.worldPosition) ")
if node.worldPosition.z < -0.05 {
print("eureka \(theOne.theIndex)")
theOne.theNode = node
theOne.theIndex = Int(result.1)!
}
}
}
}
If all your objects are in an array, you can use min(by:). Like a lot of Swift's sorting methods it takes a block with two values, and you return TRUE if the second one is larger than the first:
myVectors.min { $0.x < $1.x }
The returned value is an optional – it's either the smallest value in the array, or nil if the array was empty.

Anylogic constructor PalletRack not working

In the Anylogic 8.3 version PalletRack constructor works fine as defined:
PalletRack palletRack1 = new PalletRack(root, SHAPE_DRAW_2D3D, true, 760.0, 90.0, 0.0, 160.0, 14.0, 14.0, 10.0, 0.0, PALLET_RACK_TWO_PALLET_RACKS, PALLET_RACK_NO_DIRECTION,
11.0, 40.0, 16.0, 10, 2, 1000, cyan, maroon, 100 );
But when install the newest version of Anylogic (8.7.2) the PalletRack constructor not work the error message is:
Description: The constructor PalletRack(Main, ShapeDrawMode, boolean, double, double, double, double, double, double, double, double, PalletRackType, PalletRackDirection, double, double, double, int, int, int, Color, Color, int) is undefined.
seems that there is one new argument is expected in this constructor.
It is "boolean isObstacle". And its expected at 4th position.
So this code is compiled sucessfully:
PalletRack palletRack1 = new PalletRack(root, SHAPE_DRAW_2D3D, true, false, 760.0, 90.0, 0.0, 160.0, 14.0, 14.0, 10.0, 0.0, PALLET_RACK_TWO_PALLET_RACKS, PALLET_RACK_NO_DIRECTION,
11.0, 40.0, 16.0, 10, 2, 1000, cyan, maroon, 100 );

In Swift: how could I sort the smallest number to the highest?

Here is my data:
["aRetinol": (800.0, 24.0, 3.0, 24.0, 3.0), "aBetacarotene": (2100.0, 231.0, 11.0, 100.0, 4.7619047), "vitamineC": (80.0, 197.38002, 246.72504, 100.0, 125.0), "vitamineB1": (1.1, 0.6, 54.545456, 0.6, 54.545456), "omega3": (2.0, 2.06, 103.0, 2.06, 103.0), "calcium": (800.0, 297.4, 37.175, 100.0, 12.5)]
It's a dictionary type : [String: (Float, Float, Float, Float, Float)]
This is for a table view where I have to display the second smallest value of each nutrient.
I need to obtain a dictionary like this (the value concerned is between **) :
["calcium": (800.0, **297.4**, 37.175, 100.0, 12.5), "aBetacarotene": (2100.0, **231.0**, 11.0, 100.0, 4.7619047),"vitamineC": (80.0, **197.38002**, 246.72504, 100.0, 125.0), "aRetinol": (800.0, **24.0**, 3.0, 24.0, 3.0), "omega3": (2.0, **2.06**, 103.0, 2.06, 103.0), "vitamineB1": (1.1, **0.6**, 54.545456, 0.6, 54.545456)]
As suggested in the comments, a struct might work better than a tuple for this kind of data.
Also, you can't sort a dictionary and get a dictionary as a result because the dictionary is not an ordered collection, i.e. you can't have the keys be ordered arbitrarily. A result of sorting a dictionary will be an array of tuples where each tuple consists of a key and a value.
That said, if you had no choice but using tuples, here's how this problem could be solved:
extension Dictionary where Value == (Float, Float, Float, Float, Float) {
func sortedBySecondSubvalue(ascending: Bool) -> [(key: Key, value: Value)] {
self.sorted { keyValuePair1, keyValuePair2 -> Bool in
let secondSubvalue1 = keyValuePair1.value.1
let secondSubvalue2 = keyValuePair2.value.1
return ascending ? secondSubvalue1 < secondSubvalue2 : secondSubvalue1 > secondSubvalue2
}
}
}
And you'd use it like this:
let sampleDict: [String:(Float, Float, Float, Float, Float)] = ["aRetinol": (800.0, 24.0, 3.0, 24.0, 3.0), "aBetacarotene": (2100.0, 231.0, 11.0, 100.0, 4.7619047), "vitamineC": (80.0, 197.38002, 246.72504, 100.0, 125.0), "vitamineB1": (1.1, 0.6, 54.545456, 0.6, 54.545456), "omega3": (2.0, 2.06, 103.0, 2.06, 103.0), "calcium": (800.0, 297.4, 37.175, 100.0, 12.5)]
print(sampleDict.sortedBySecondSubvalue(ascending: false))
// Prints: [(key: "calcium", value: (800.0, 297.4, 37.175, 100.0, 12.5)), (key: "aBetacarotene", value: (2100.0, 231.0, 11.0, 100.0, 4.7619047)), (key: "vitamineC", value: (80.0, 197.38002, 246.72504, 100.0, 125.0)), (key: "aRetinol", value: (800.0, 24.0, 3.0, 24.0, 3.0)), (key: "omega3", value: (2.0, 2.06, 103.0, 2.06, 103.0)), (key: "vitamineB1", value: (1.1, 0.6, 54.545456, 0.6, 54.545456))]
The extension like that can be surely modified for dictionaries containing structs as values rather than tuples.

how to find the key of max value of a map in flutter?

This is the required map. I want the output to be neutral.
{anger: 0.0, contempt: 0.02, disgust: 0.0, fear: 0.0, happiness: 0.0, neutral: 0.978, sadness: 0.002, surprise: 0.0}
Map themap={"anger": 0.0, "contempt": 0.02, "disgust": 0.0, "fear": 2.0, "happiness": 0.0, "neutral": 0.978, "sadness": 0.002, "surprise": 0.0};
var thevalue=0.0;
var thekey;
themap.forEach((k,v){
if(v>thevalue) {
thevalue = v;
thekey = k;
}
});
print (thekey);

Use an array stored colour (string) to set UIColor of a label element

I am working on an iOS app (just a personal project at the moment) and I am trying to change a label bg and text colours based on variables stored in an array, and I'm really struggling!
I have tried using a few methods, but I am very new to Swift and don't understand all of the logic behind what is and isn't best practice here.
This is my array:
let testItem = [["Name Here"], ["red: 1.0, green: 0, blue: 0, alpha: 1"], ["red: 1.0, green: 0, blue: 0, alpha: 1"]]
And basically I thought I would be able to do something like this:
labelOne.backgroundColor = UIColor(testItem[1])
labelOne.textColor = UIColor(testItem[2])
But of course, that won't work... so any ideas?
There are a number of ways you can do this, and here are a few to help you understand data collections (there are other ways).
You can simply create an array of colors [UIColor]:
let colors = [UIColor.red, UIColor.green, UIColor.blue]
labelOne.backgroundColor = colors[0]
labelOne.textColor = colors[2]
You can store all of that one label's styling in a dictionary [String : UIColor]:
let labelOneStyling = ["background": UIColor.red, "text": UIColor.blue]
labelOne.backgroundColor = labelOneStyling["background"]
labelOne.textColor = labelOneStyling["text"]
You can store all label styling in a dictionary of dictionaries [String : [String : UIColor]]:
let allLabelStyling = ["labelOne": ["background": UIColor.red, "text": UIColor.blue], "labelTwo": ["background": UIColor.green, "text": UIColor.yellow]]
labelOne.backgroundColor = allLabelStyling["labelOne"]?["background"]
labelOne.textColor = allLabelStyling["labelOne"]?["text"]
A really useful tip is to option-click on any property, like the colors array in the first example, and select "Show Quick Help". Xcode will tell you what type that property is. You will see let colors: [UIColor] which tells you that colors is a constant of type array of UIColors. Swift is a strongly-typed language and types are the building blocks of OOP so if there is anything to learn right off the bat, types are it.