Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Last active March 20, 2024 00:00
Show Gist options
  • Save TheMuellenator/f290a658bfd19773dadacffc8000498e to your computer and use it in GitHub Desktop.
Save TheMuellenator/f290a658bfd19773dadacffc8000498e to your computer and use it in GitHub Desktop.
iOS repl.it - Optionals Challenge Solution
//Don't change this
var studentsAndScores = ["Amy": Int(readLine()!)!, "James": Int(readLine()!)!, "Helen": Int(readLine()!)!]
func highestScore(scores: [String: Int]) {
//Write your code here.
let a = studentsAndScores["Amy"]!
let b = studentsAndScores["James"]!
let c = studentsAndScores["Helen"]!
var temp = 0
//If a is greater than b
if a > temp {
//And a is also greater than c
temp = a
}
if b > temp {
temp = b
}
if c > temp {
temp = c
}
print(temp)
}
//Don't change this
highestScore(scores: studentsAndScores)
@NavakanthMesala
Copy link

Tried this and it works -

var studentsAndScores = ["Amy": Int(readLine()!)!, "James": Int(readLine()!)!, "Helen": Int(readLine()!)!]

func highestScore(scores: [String: Int]) {

//Write your code here.

let maxScore = (scores.values.max())!
if maxScore > 0 {
print(maxScore)
}

}

@peilic
Copy link

peilic commented Aug 25, 2022

Like the solution but tried avoiding forced unwrapping and it worked:

var studentsAndScores = ["Amy": Int(readLine()!)!, "James": Int(readLine()!)!, "Helen": Int(readLine()!)!]

func highestScore(scores: [String: Int]) {

//Write your code here.

let a = studentsAndScores["Amy"]
let b = studentsAndScores["James"]
let c = studentsAndScores["Helen"]

var temp = 0

if let a = a {
if a > temp { temp = a }
}
if let b = b {
if b > temp { temp = b }
}
if let c = c {
if c > temp { temp = c }
}
print(temp)
}

highestScore(scores: studentsAndScores)

@Josemanu77
Copy link

//Don't change this
var studentsAndScores = ["Amy": 78, "James": 55, "Helen": 86]

func highestScore(scores: [String: Int]) {

//Write your code here.

let amyScore = scores ["Amy"]!
let jamesScore = scores ["James"]!
let helenScore = scores ["Helen"]!

var Ttemp = 0

if amyScore > Ttemp {
    Ttemp = amyScore
}  
if jamesScore > Ttemp{
    Ttemp = jamesScore
} 
if helenScore > Ttemp{
    Ttemp = helenScore
}
print (Ttemp)

}

//the result is 86 not 99, the hellen score must be changed to 86, in order to solve the challenge

@Mark8673
Copy link

it didn't pass but my answer is the simplest?

var studentsAndScores = ["Amy": 88, "James": 55, "Helen": 99]

func highestScore() {

print("\(studentsAndScores.values.max()!)")
}
highestScore()

Copy link

ghost commented Jul 14, 2023

There is no need to create a variable called temp

var studentsAndScores = ["Amy": Int(readLine()!)!, "James": Int(readLine()!)!, "Helen": Int(readLine()!)!]

func highestScore(scores: [String: Int]) {

// Store the scores and force unwrap it

 let a = studentsAndScores["Amy"]!
 let b = studentsAndScores["James"]!
 let c = studentsAndScores["Helen"]!
    
// Create a nested if statement

    if a > b{
         if a > c{
             print(a)
         }
     }
     if b > c{
         if b > a{
             print(b)
         }
     }
     if c > a{
         if c > b{
             print(c)
         }
     }
}

@efadugba
Copy link

Here's how I solved this exercise:

//Don't change this
var studentsAndScores = ["Amy": Int(readLine()!)!, "James": Int(readLine()!)!, "Helen": Int(readLine()!)!]

func highestScore(scores: [String: Int]) {

//Write your code here.
let a = studentsAndScores["Amy"]!
let b = studentsAndScores["James"]!
let c = studentsAndScores["Helen"]!

if a > b && a > c {
print(a)
} else if b > a && b > c {
print(b)
} else {
print(c)
}

}

@alexnavter
Copy link

alexnavter commented Oct 17, 2023

// List of students with their scores
var studentsAndScores = ["Amy": 10, "James": 5, "Helen": 7]

// Function that takes a dictionary as a parameter to find and print the highest score among students
func highestScore(scores: [String: Int]) {
    print(studentsAndScores.values.max()!)
}

// We call the function and pass studentsAndScores dictionary as an argument
highestScore(scores: studentsAndScores) // Output: 10

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