-
-
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"]!) |
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
Hi,
The instruction says "Inside the updateMyTickers function, add an entry to the stockTickers dictionary with the key "WORK" and the value "Slack Technologies Inc"."
So when trying to create a function called updateMyTickers and try to update/add the desired entries the test process I get the following error.
Fatal error: Unexpectedly found nil while unwrapping an Optional value Current stack trace: 0 libswiftCore.so 0x00007fafbd4ef920 _swift_stdlib_reportFatalError + 69 1 libswiftCore.so 0x00007fafbd3fda06 <unavailable> + 3279366 2 libswiftCore.so 0x00007fafbd3fdd85 <unavailable> + 3280261 3 libswiftCore.so 0x00007fafbd225810 _fatalErrorMessage(_:_:file:line:flags:) + 19 4 main 0x000055d0edce42c0 <unavailable> + 4800 5 libc.so.6 0x00007fafbc3ceab0 __libc_start_main + 231 6 main 0x000055d0edce3c9a <unavailable> + 3226 exited, illegal instruction
because you've right word WORK as Work
//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 updateMyTickets() {
stockTickers ["WORK"] = "Slack Technologies Inc" //This line adds additional key WORK with value Slack Tech etc.
stockTickers ["BOOM"] = "DMC Global Inc"// This line replaces value for key BOOM in the dictionary
}
updateMyTickets()
// Don't modify this
print(stockTickers["WORK"]!)
print(stockTickers["BOOM"]!)
//Don't change this
var stockTickers: [String: String] = ["APPL" : "Apple Inc",
"HOG": "Harley-Davidson Inc",
"DMC Global Inc": "DMC Global Inc",
"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["DMC Global Inc"]!)
@TheMuellenator instruction was code must go inside updateMyTickers() function. fix the mistake
Is it just me or the task is confusing?
First of all, no one told me that stockTickers["WORK"] = "Slack Technologies Inc" is a way to add new items to the array.
My first thought was to do smth like this: stockTickers + ["WORK": "Slack Technologies Inc"]. So the challenge isn't fair..
Second, the challenge solution provided above doesn't correspond to the task, it doesn't use functions.
And third, why can't we just change values and add new ones into the existing dictionary? It would be so much easier and shorter in terms of coding.. If coding is all about solving problems efficiently, why giving a task that does the opposite?
IMHO
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"]!)
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"]!)
}
func exerciseDict() {
//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 = [
"APPL" : "Apple Inc",
"HOG": "Harley-Davidson Inc",
"BOOM": "DMC Global Inc",
"HEINY": "Heineken",
"BEN": "Franklin Resources Inc",
"WORK": "Slack Technologies Inc"
]
//Don't modify this
print(stockTickers["WORK"]!)
print(stockTickers["BOOM"]!)
}
// Don't change this
exerciseDict()
Check this
Hi,
The instruction says "Inside the updateMyTickers function, add an entry to the stockTickers dictionary with the key "WORK" and the value "Slack Technologies Inc"."
So when trying to create a function called updateMyTickers and try to update/add the desired entries the test process I get the following error.