Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Last active August 17, 2024 20:58
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)
@NastasiaIOSdev
Copy link

Снимок экрана 2020-12-27 в 23 55 11

@Kashif-khannn
Copy link

I totally misunderstood this challenge

@ReivajGP
Copy link

ReivajGP commented Jan 7, 2021

I did't knew why there was the readLine() function in this challenge. By checking the documentation it is used when user enters a value from the standard input (is this the keyboard?).

So, I solved this challenge by declaring the studentAndscores var as follows:
var studentAndscores : [String : Int]
And so, inside the highestScore func i used the scores object like this:
let amyScore = scores["Amy"]!
let jamesScore = scores["James"]!
let helenScore = scores["Helen"]!
This way it works perfectly!

@irvinds66
Copy link

I did not understand this at all. Anyone I can ask a question to understand it better?

@vishalverma865
Copy link

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

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

print(studentsAndScores.values.max()!)

}

highestScore(scores: studentsAndScores)

@vishalverma865
Copy link

I did't knew why there was the readLine() function in this challenge. By checking the documentation it is used when user enters a value from the standard input (is this the keyboard?).

So, I solved this challenge by declaring the studentAndscores var as follows:
var studentAndscores : [String : Int]
And so, inside the highestScore func i used the scores object like this:
let amyScore = scores["Amy"]!
let jamesScore = scores["James"]!
let helenScore = scores["Helen"]!
This way it works perfectly!

That readline() is used get the input from the person and in this case the values are not given.
after your function you can try to use highestScore(scores: studentsAndScores) to get the input. once your function run compiler will ask u for the input.

@Rgrey27
Copy link

Rgrey27 commented Mar 15, 2021

I'm stumped. I applied the solution at the top, but have not had any luck with printing the sample values. I know the solution works but when I try to apply the following 'var studentsAndScores = ["Amy": 88, "James": 55, "Helen": 99]' I just get a bunch of

frame #8: 0x000000018b9c2740 GraphicsServices`GSEventRunModal + 160
frame #9: 0x00000001843ccbf0 UIKitCore`-[UIApplication _run] + 964
frame #10: 0x00000001843d19d0 UIKitCore`UIApplicationMain + 112
frame #11: 0x0000000100696f7c MyPlayground`main + 216
frame #12: 0x0000000180223cbc libdyld.dylib`start + 4

@Shivacharan1015-code
Copy link

I wrote the below code, it works completely fine on playground when you call the function, but on Udemy it dosent seem to accept it.

var studentsAndScores = ["Amy": 10, "James": 20, "Helen": 99]

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

//Write your code here.
var highScore: Int? = 0

for i in studentsAndScores {
if i.value > highScore! {
highScore = i.value
}

}
print(highScore!)

}
highestScore(scores: studentsAndScores)

@liveinfreefall
Copy link

I keep encountering an error with the Int(readLine()!)! and have been deleting that first line in the recent few Exercises. Can someone help to explain why?

Wrote the following which seems to work, but is not an accepted answer.

var studentsAndScores = ["Amy": 88, "James": 33, "Helen": 9]

func highestScore() {
    let scores = Array(studentsAndScores.values)
    print(scores.max()!)
}
highestScore()

@jeffala
Copy link

jeffala commented May 30, 2021

Ah, I totally misunderstood the challenge then. I looked it up on Google/Stack Overflow and came up with:
print(scores.values.max()!)...

Came to similar conclusion.

@LexShpin
Copy link

LexShpin commented Jun 8, 2021

The solution is simple and elegant, I went with the Google, StackOverflow and a more advanced option though (hehe):

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

//Write your code here.
guard let amyScore: Int = scores["Amy"], let jamesScore: Int = scores["James"], let helenScore: Int = scores["Helen"] else {
print("Some value is nil")
return
}

if amyScore > jamesScore && amyScore > helenScore {
print(amyScore)
} else if jamesScore > amyScore && jamesScore > helenScore {
print(jamesScore)
} else {
print(helenScore)
}

}

@tito3288
Copy link

I am not sure am i technically right, this is my solution:

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

func highestScore(scores: [String: Int]) {
  
  var maxScore =  0
  
  for (_, score) in studentsAndScores {
    if (maxScore < score) {
      maxScore = score
    }
  }
  print(maxScore)
}

//Don't change this
highestScore(scores: studentsAndScores)

@tito3288
Copy link

tito3288 commented Jun 18, 2021

Screen Shot 2021-06-18 at 10 39 33 AM

I have been trying to run the tasks on playground for a while but i keep getting this error. Can anyone explain what optional value is talking about and how to fix this please.

@bhayangkara
Copy link

This is my solution in Angela challenge test... Worked..

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

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

//Write your code here.
var tertinggi: Int = studentsAndScores["Amy"]!
if studentsAndScores["James"]! > tertinggi {
tertinggi = studentsAndScores["James"]!

}else if studentsAndScores["Helen"]! > tertinggi {
    tertinggi = studentsAndScores["Helen"]!
}else {
    tertinggi = studentsAndScores["Amy"]!
}
print(tertinggi)

//Write your code here.

}

@tito3288
Copy link

tito3288 commented Jul 2, 2021

What does the var temp stand for? what do you mean by temp.

@bahurl
Copy link

bahurl commented Sep 19, 2021

This was my solution:

    let amysScore = scores["Amy"]!
    let jamesScore = scores["James"]!
    let helenScore = scores["Helen"]!
 
    if helenScore > jamesScore && helenScore > amysScore {
        print(helenScore)
    } else if amysScore > jamesScore && amysScore > helenScore {
        print(amysScore)
    } else {
        print(jamesScore)
    }

@abdulvokhid
Copy link

hi guys this what i did it looks pretty much the same like other

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 if c > a && c > {
print(c)
}else{
print("Error")
}

@abdulrsf
Copy link

abdulrsf commented Jan 7, 2022

My Solution (Similar to others, took a while until I understood the task)

`//Don't change this
var studentsAndScores = ["Amy": 25, "James": 27, "Helen": 30]

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

//Write your code here.
let amy = studentsAndScores["Amy"]!
let james = studentsAndScores["James"]!
let helen = studentsAndScores["Helen"]!

if amy > james && amy > helen {
print(amy)
}
else if james > amy && james > helen {
print(james)
}
else if helen > amy && helen > james {
print(helen)
}
else {
print("Error: Value not found.")
}

}

highestScore(scores: studentsAndScores)`

@Magdalenaspace
Copy link

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

func highestScore(scores: [String: Int]) {
print(scores.values.max()!)

}

//Try some different scores.
//Dont add the lines below to udemy!

highestScore(
scores: [
"Amy": 78,
"James": 65,
"Helen": 99
]
)

@Magdalenaspace
Copy link

This worked perfectly for me, I don't understand, if you guys can explain, why I have to create a variable if I already can store the same inside of the function and get the result more clean and in a short way??

@baurrm
Copy link

baurrm commented Feb 1, 2022

Well this is my solution, it passed successfully in Udemy when I checked solution. But in Replit I couldn't check because it is not printing the values. I hope that this is also correct, I understand the task, bud can someone confirm if it is correct way as well?

//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 Amy = studentsAndScores["Amy"]!
let James = studentsAndScores["James"]!
let Helen = studentsAndScores["Helen"]!

if Amy > James && Amy > Helen {
print(Amy)
} else if James > Amy && James > Helen {
print(James)
} else if Helen > James && Helen > Amy {
print(Helen)
}

}

@Bsheskee
Copy link

I can't see a point of watching tutorials like this to find the solution by yourself on the Internet. I hoped that the challenges I encounter in this tutorial were going to match what has been presented before. This time I'm actually disappointed. I don't even understand the code starting with the var temp = 0. I think it would be quicker to find the solution as other people did on stack overflow.

@let-butterChicken
Copy link

let-butterChicken commented May 7, 2022

I have managed to write the following code.

Shall also try using switch case.

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

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

  if 
  
  scores["Amy"]! > scores["James"]! && scores["Amy"]! > scores["Helen"]! {
    print(scores["Amy"]!)
  } 

    else if
      
    scores["James"]! > scores["Helen"]! && scores["James"]! > scores["Amy"]! {
       print(scores["James"]!)
      
    }

    else {
      
       print(scores["Helen"]!)
    }

  }

highestScore(scores: studentsAndScores)

(Ignore indentation)

I run this code on Replit and it works fine. But when I run the same code on playgrounds, the program crashes with the error.

**Fatal error: Unexpectedly found nil while unwrapping an Optional value
Playground execution failed:

error: Execution was interrupted, reason: EXC_BREAKPOINT (code=1, subcode=0x18f6dd21c).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.**

Why I don't get this error on Replit but only on the playground? I am not sure what do I write in the if statement to prevent force unwrap the optional string. Can anyone help pleaae?

@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