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