I am currently completing a challenge on hacker rank called compare the triplets (https://www.hackerrank.com/challenges/compare-the-triplets/problem), and I was just wondering if using too many IF statements is considered bad programming practice? What are the alternatives other than using switch statements. Please see my code solution below :
import Foundation
func compareTriplets(a: [Int], b: [Int]) -> [Int] {
var compareArray = [0,0]
if a[0] == b[0]{
compareArray[0] = compareArray[0]
}
if a[0] < b[0]{
compareArray[0] = compareArray[0] + 1
}
if a[1] < b[1] {
compareArray[0] = compareArray[0] + 1
}
if a[2] < b[2] {
compareArray[0] = compareArray[0] + 1
}
if a[0] > b[0]{
compareArray[1] = compareArray[1] + 1
}
if a[1] > b[1] {
compareArray[1] = compareArray[1] + 1
}
if a[2] > b[2] {
compareArray[1] = compareArray[1] + 1
}
return compareArray
}
print(compareTriplets(a: [17,28,30], b: [99,28,8]))
Aucun commentaire:
Enregistrer un commentaire