-
-
Save TheMuellenator/febd64195828e681985a0746f0908f92 to your computer and use it in GitHub Desktop.
//Don't change this | |
var stockTickers: [String: String] = ["APPL" : "Apple Inc", "HOG": "Harley-Davidson Inc", "BOOM": "Dynamic Materials", "HEINY": "Heineken", "BEN": "Franklin Resources Inc"] | |
//Write your code here. | |
stockTickers["WORK"] = "Slack Technologies Inc" | |
stockTickers["BOOM"] = "DMC Global Inc" | |
//Don't modify this | |
print(stockTickers["WORK"]!) | |
print(stockTickers["BOOM"]!) |
I went through the process to figure it out.
I browsed Google for merge in swift Stackoverflow and found this:
var d1 = ["a": "b"]
var d2 = ["c": "e"]
extension Dictionary {
mutating func merge(dict: [Key: Value]){
for (k, v) in dict {
updateValue(v, forKey: k)
}
}
}
d1.merge(d2)
Realized this was the piece I needed:
updateValue(v, forKey: k)
Then I concatenated the stockTickers Variable with what I found modifying the v and k
The next line:
stockTickers["WORK"] = "Slack Technologies Inc"
I received errors as I tried to concatenate stockTickers.["WORK" : "Slack Technologies Inc", "BOOM" : "DMC Global Inc"]
So next I looked up, "Updating indexes in swift" and found:
var newCharacter = "a"
arr[2] = newCharacter
The bottom was the formatting I needed, So I used stockTickers in place of arr. Bracketed the WORK string and followed by the (=) is value of DMC Global Inc
Hopefully, that helps someone to discover future answers.
Final Result:
//Don't change this
var stockTickers: [String: String] = ["APPL" : "Apple Inc",
"HOG": "Harley-Davidson Inc",
"BOOM": "Dynamic Materials",
"HEINY": "Heineken",
"BEN": "Franklin Resources Inc"]
//Write your code here.
func updateMyTickers(){
stockTickers.updateValue("BOOM", forKey: "DMC Global Inc")
stockTickers["WORK"] = "Slack Technologies Inc"
}
updateMyTickers()
// Don't modify this
print(stockTickers["WORK"]!)
print(stockTickers["BOOM"]!)
I created function with inputs. It works great at my desktop swift playground.
But when I paste it to repl.it it gives an error
Swift version 5.0.1 (swift-5.0.1-RELEASE)
main.swift:11:50: warning: expression of type 'String' is unused
stockTickers.updateValue(value1, forKey:key1)!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
Slack Technologies Inc
DMC Global Inc
//MY SOLUTION IS
func updateMyTickers (key:String, value:String) {
stockTickers.updateValue(value, forKey:key)!
stockTickers ["WORK"] = "Slack Technologies Inc"
}
updateMyTickers(key: "BOOM", value: "DMC Global Inc")
// Don't modify this
print(stockTickers["WORK"]!)
print(stockTickers["BOOM"]!)
I see this exercise's purpose was to have use research the answer but the instructions weren't clear. After reviewing the solution above, it makes sense that the point of the challenge was to highlight how to use a function, using a dictionary, and redefining a variable. I see the value in pointing out the subtle syntax changes between the dictionary and redefining the variable however it could have been communicated in a more effective manner.
//MY SOLUTION -- however I did have to look up the answer and reverse engineer it so I can better understand it.
//Don't change this
var stockTickers: [String: String] = ["APPL" : "Apple Inc",
"HOG": "Harley-Davidson Inc",
"BOOM": "Dynamic Materials",
"HEINY": "Heineken",
"BEN": "Franklin Resources Inc"]
//Write your code here.
func updateMyTickers(){
stockTickers = ["WORK" : "Slack Technologies Inc"]
stockTickers["BOOM"] = "DMC Global Inc"
}
updateMyTickers()
// Don't modify this
print(stockTickers["WORK"]!)
print(stockTickers["BOOM"]!)
Func part still doesn't work. Quite confusing, trying to check for what was wrong, and nothing was wrong with my code.
var stockTickers: [String: String] = ["APPL" : "Apple Inc",
"HOG": "Harley-Davidson Inc",
"BOOM": "Dynamic Materials",
"HEINY": "Heineken",
"BEN": "Franklin Resources Inc"]func updateMyTickers() {
stockTickers["WORK"] = "Slack Technologies Inc"
stockTickers["BOOM"] = "DMC Global Inc"
}
updateMyTickers()
print(stockTickers["WORK"]!)
print(stockTickers["BOOM"]!)// i've maded like this and it's worked
I liked that solution. It worked beautifully. Thank you
var stockTickers = [String: String]()
func updateMyTickers(ticker: String, companyName: String) {
stockTickers = ["APPL" : "Apple Inc",
"HOG": "Harley-Davidson Inc",
"BOOM": "Dynamic Materials",
"HEINY": "Heineken",
"BEN": "Franklin Resources Inc"]
// iterating over just the keys before update/add
for key in stockTickers.keys {
// update an entry
if(stockTickers[key] != nil) {
stockTickers[ticker] = companyName
} else {
// add a new entry
stockTickers[ticker] = companyName
}
}
}
updateMyTickers(ticker: "BOOM", companyName: "DMC Global Inc")
updateMyTickers(ticker: "WORK", companyName: "Slack Corporation")
print(stockTickers)
var stockTickers: [String: String] = ["APPL" : "Apple Inc", "HOG": "Harley-Davidson Inc", "BOOM": "Dynamic Materials", "HEINY": "Heineken", "BEN": "Franklin Resources Inc", ]
stockTickers["WORK"] = "Slack Technologies Inc"
stockTickers["BOOM"] = "DMC Global Inc"
I wrote :
stockTickers["WORK"] = "Slack Technologies Inc"
stockTickers.updateValue("DMC GLOBAL Inc", forKey:"BOOM")
however, I get an error "command not found" when I try to run the code, can anyone explain what I'm doing wrong? Do we need a function to update the tickers?
func exercise() {
//Don't change this
var stockTickers: [String: String] = [
"APPL" : "Apple Inc",
"HOG": "Harley-Davidson Inc",
"BOOM": "Dynamic Materials",
"HEINY": "Heineken",
"BEN": "Franklin Resources Inc"
]
stockTickers["WORK"] = "Slack Technologies lnk"
stockTickers["BOOM"] = "DMC Global lnk"
//Don't modify this
print(stockTickers["WORK"]!)
print(stockTickers["BOOM"]!)
}
//This Worked Properly
func exercise() {
//Don't change this
var stockTickers: [String: String] = [
"APPL" : "Apple Inc",
"HOG": "Harley-Davidson Inc",
"BOOM": "Dynamic Materials",
"HEINY": "Heineken",
"BEN": "Franklin Resources Inc"
]
//Write your code here.
func updateMyTickers() {
stockTickers["WORK"] = "Slack Technologies Inc"
stockTickers["BOOM"] = "DMC Global Inc"
}
updateMyTickers()
//Don't modify this
print(stockTickers["WORK"]!)
print(stockTickers["BOOM"]!)
}
func exercise() {
//Don't change this
var stockTickers: [String: String] = [
"APPL" : "Apple Inc",
"HOG": "Harley-Davidson Inc",
"BOOM": "Dynamic Materials",
"HEINY": "Heineken",
"BEN": "Franklin Resources Inc"
]
//Write your code here. 👇
stockTickers = [
"BOOM": "DMC Global Inc",
"WORK": "Slack Technologies Inc",
]
//Don't modify this
print(stockTickers["WORK"]!)
print(stockTickers["BOOM"]!)
}
// Don't change this
exercise()
This Worked for me , please don't take this serious :)
var stockTickers: [String: String] = ["APPL" : "Apple Inc", "HOG": "Harley-Davidson Inc", "BOOM": "Dynamic Materials", "HEINY": "Heineken", "BEN": "Franklin Resources Inc"]
func updateMyTickers() {
stockTickers["WORK"] = "Slack Technologies Inc"
stockTickers["BOOM"] = "DMC Global Inc"
}
updateMyTickers()
print(stockTickers["WORK"]!) print(stockTickers["BOOM"]!)
// i've maded like this and it's worked
//why did you use two dictionaries , when you can use only one
func exercise() {
//Don't change this
var stockTickers: [String: String] = [
"APPL" : "Apple Inc",
"HOG": "Harley-Davidson Inc",
"BOOM": "Dynamic Materials",
"HEINY": "Heineken",
"BEN": "Franklin Resources Inc"
]
//Write your code here. 👇
stockTickers = [
"WORK": "Slack Technologies Inc",
"BOOM": "DMC Global Inc",
]
//Don't modify this
print(stockTickers["WORK"]!)
print(stockTickers["BOOM"]!)
}
func exercise() {
//Don't change this
var stockTickers: [String: String] = [
"APPL" : "Apple Inc",
"HOG": "Harley-Davidson Inc",
"BOOM": "Dynamic Materials",
"HEINY": "Heineken",
"BEN": "Franklin Resources Inc"]
func updateMyTickers() {
stockTickers["WORK"] = "Slack Technologies Inc"
stockTickers.updateValue("DMC Global Inc", forKey: "BOOM")
//Don't modify this
}
updateMyTickers()
print(stockTickers["WORK"]!)
print(stockTickers["BOOM"]!)
var stockTickers: [String: String] = [
"APPL" : "Apple Inc",
"HOG": "Harley-Davidson Inc",
"BOOM": "Dynamic Materials",
"HEINY": "Heineken",
"BEN": "Franklin Resources Inc"
]
func ubdateMyTickers(){
stockTickers["WORK"] = "Slack Technologies Inc"
stockTickers["BOOM"] = "DMC Global Inc"
}
ubdateMyTickers()
print(stockTickers["WORK"]!)
print(stockTickers["BOOM"]!)
var stockTickers: [String: String] = [
"APPL" : "Apple Inc",
"HOG": "Harley-Davidson Inc",
"BOOM": "Dynamic Materials",
"HEINY": "Heineken",
"BEN": "Franklin Resources Inc"
]
stockTickers["WORK"] = "Slack Technologies Inc"
stockTickers["BOOM"] = "DMC Global Inc"
print(stockTickers["WORK"]!)
print(stockTickers["BOOM"]!)
// I wanted to see how the full dictionary looks like
print(stockTickers)
func exercise() {
var stockTickers: [String: String] = [
"APPL" : "Apple Inc",
"HOG": "Harley-Davidson Inc",
"BOOM": "Dynamic Materials",
"HEINY": "Heineken",
"BEN": "Franklin Resources Inc",
]
//Write your code here.
stockTickers["WORK"] = "Slack Technologies Inc"
stockTickers["BOOM"] = "DMC Global Inc"
//Don't modify this
print(stockTickers["WORK"]!)
print(stockTickers["BOOM"]!)
}
exercise()
func exercise() {
//Don't change this
var stockTickers: [String: String] = [
"APPL" : "Apple Inc",
"HOG": "Harley-Davidson Inc",
"BOOM": "Dynamic Materials",
"HEINY": "Heineken",
"BEN": "Franklin Resources Inc"
]
//Write your code here.
stockTickers["WORK"] = "Slack Technologies Inc"
stockTickers["BOOM"] = "DMC Global Inc"
//Don't modify this
print(stockTickers["WORK"]!)
print(stockTickers["BOOM"]!)
}
I agree with most of the comments. Because it says create a function called "updateMyTickers" in the instructions, the solution offered for this challenge doesn't go along with the instructions.
I tried to create a function in which I can add new dictionaries but it didn't work... I don't know why. Can anyone help?
Here is the code I added:
//Write your code here.
func updateMyTickers() {
stockTickers["WORK" : "Slack Technologies Inc", "BOOM" : "DMC Global Inc"]
}
updateMyTickers()
print(stockTickers["WORK"]!)
print(stockTickers["BOOM"]!)