Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Last active July 15, 2024 18:17
Show Gist options
  • Save TheMuellenator/febd64195828e681985a0746f0908f92 to your computer and use it in GitHub Desktop.
Save TheMuellenator/febd64195828e681985a0746f0908f92 to your computer and use it in GitHub Desktop.
iOS repl.it - Dictionaries Challenge Solution
//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"]!)
@vnaveen-coder
Copy link

vnaveen-coder commented Apr 1, 2020

//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"]!)

@jihredoy
Copy link

Screen Shot 2020-04-16 at 2 19 38 PM

@jihredoy
Copy link

@TheMuellenator instruction was code must go inside updateMyTickers() function. fix the mistake
Screen Shot 2020-04-16 at 2 20 49 PM

@verebro
Copy link

verebro commented May 11, 2020

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

@Burcugul123
Copy link

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"]!)

@freeflightproductions
Copy link

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"]!)

@SergeyTLV
Copy link

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"]!)

@gyos23
Copy link

gyos23 commented May 28, 2020

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"]!)

@steimaar
Copy link

steimaar commented Jun 9, 2020

Func part still doesn't work. Quite confusing, trying to check for what was wrong, and nothing was wrong with my code.

@verorega
Copy link

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

@Arjunkalidas
Copy link

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)

@LalBul
Copy link

LalBul commented Jul 16, 2020

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"

@darsh2727
Copy link

image

@karolinaa589
Copy link

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?

@Dimanikin
Copy link

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"]!)

}

@NastasiaIOSdev
Copy link

Снимок экрана 2020-12-27 в 23 51 58

@TusharWoody
Copy link

//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"]!)
}

@Shah0651
Copy link

Shah0651 commented Jun 12, 2021

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 :)

@GK-90
Copy link

GK-90 commented Nov 25, 2021

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

@73307
Copy link

73307 commented Jan 22, 2022

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"]!)

}

@Magdalenaspace
Copy link

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"]!)

@Magdalenaspace
Copy link

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"]!)

@danieljaw
Copy link

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)

@alexnavter
Copy link

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()

@BunyaminMete
Copy link

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"]!)

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment