My solution in Swift, first time I use nested functions :
letdistanceReferences=["X1":100.0,"X2":200.0,"X3":250.0,"X4":300.0]letfriendsReferences=["A1":"X1","A2":"X2","A3":"X3","A4":"X4"]letfriendsOrder=["A1","A2","A3","A4","A5"]funcgrandmaTravel(friendsToVisit:[String])->Int{funcdistanceBetween(firstPoint:String,tosecondPoint:String)->Int{letfirst=distanceReferences.first{$0.key==firstPoint}letsecond=distanceReferences.first{$0.key==secondPoint}guardletdistanceToFirst=first?.value,letdistanceToSecond=second?.valueelse{return0}letresult=(pow(distanceToSecond,2)-pow(distanceToFirst,2)).squareRoot()returnInt(result.rounded())}vardistance=0varpoints=friendsToVisit.compactMap{friendsReferences[$0]}varcurrentPoint=points.removeFirst()// X0 to fist towndistance+=Int(distanceReferences[currentPoint]??0)points.forEach{distance+=distanceBetween(firstPoint:currentPoint,to:$0)currentPoint=$0}// last town to X0distance+=Int(distanceReferences[currentPoint]??0)returndistance}grandmaTravel(friendsToVisit:friendsOrder)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
My solution in Swift, first time I use nested functions :