Reputation: 467
I have some pretty confusing code - at least I think so! And I wanted to ask you guys, if there was a simpler way to do this, so that this part of the code wouldn't take up that much space.
Here's what my code is able to:
Make a string into an array with components(separatedBy: "")
. It need to do that because the allCars
is actually a value that it is getting online.
Check the last value to get the category of the vehicle. Check how many car
categories, motorcycle
categories etc. there are.
Check the weight of the vehicle (by looking at the array of the specific vehicle in countingVehicleOne
, countingVehicleTwo
... etc.
Calculate the average of each of the vehicles.
I should say that sometimes there is not going to be a car.. Sometimes there are not going to be a motorcycle - that is very important.
Here is the code:
let allCars = "Peugeot#950#yellow#car--Yamaha#180#black#motorcycle--Kawazasaki#210#green#motorcycle"
let countingCategories = allCars.components(separatedBy: "--")
var carWeight: Int = 0
var motorcycleWeight: Int = 0
var carCount: Int = 0
var motorcycleCount: Int = 0
if countingCategories.count == 1 {
let countingVehicleOne = countingCategories[0].components(separatedBy: "#")
if countingVehicleOne[3] == "car" {
carWeight = Int(countingVehicleOne[1])!
var carCount: Int = 1
} else if countingVehicleOne[3] == "motorcycle" {
motorcycleWeight = Int(countingVehicleOne[1])!
var motorcycleCount: Int = 1
}
} else if countingCategories.count == 2 {
let countingVehicleOne = countingCategories[0].components(separatedBy: "#")
let countingVehicleTwo = countingCategories[1].components(separatedBy: "#")
let numbers:[[String]] = [countingVehicleOne, countingVehicleTwo]
for number in numbers {
if number.last == "car"{
carCount = carCount + 1
}
}
for number in numbers {
if number.last == "motorcycle"{
motorcycleCount = motorcycleCount + 1
}
}
if countingVehicleOne[3] == "car" {
carWeight = Int(countingVehicleOne[1])!
} else if countingVehicleOne[3] == "motorcycle" {
motorcycleWeight = Int(countingVehicleOne[1])!
}
if countingVehicleTwo[3] == "car" {
carWeight = Int(countingVehicleTwo[1])! + carWeight
} else if countingVehicleTwo[3] == "motorcycle" {
motorcycleWeight = Int(countingVehicleTwo[1])! + motorcycleWeight
}
} else if countingCategories.count == 3 {
let countingVehicleOne = countingCategories[0].components(separatedBy: "#")
let countingVehicleTwo = countingCategories[1].components(separatedBy: "#")
let countingVehicleThree = countingCategories[2].components(separatedBy: "#")
let numbers:[[String]] = [countingVehicleOne, countingVehicleTwo, countingVehicleThree]
for number in numbers {
if number.last == "car"{
carCount = carCount + 1
}
}
for number in numbers {
if number.last == "motorcycle"{
motorcycleCount = motorcycleCount + 1
}
}
if countingVehicleOne[3] == "car" {
carWeight = Int(countingVehicleOne[1])!
} else if countingVehicleOne[3] == "motorcycle" {
motorcycleWeight = Int(countingVehicleOne[1])!
}
if countingVehicleTwo[3] == "car" {
carWeight = Int(countingVehicleTwo[1])! + carWeight
} else if countingVehicleTwo[3] == "motorcycle" {
motorcycleWeight = Int(countingVehicleTwo[1])! + motorcycleWeight
}
if countingVehicleTwo[3] == "car" {
carWeight = Int(countingVehicleThree[1])! + carWeight
} else if countingVehicleTwo[3] == "motorcycle" {
motorcycleWeight = Int(countingVehicleThree[1])! + motorcycleWeight
}
}
let averageCarWeight = carWeight / carCount
let averageMotorcycleWeight = motorcycleWeight / motorcycleCount
print("Average weight for cars: \(averageCarWeight), average weight for motorcycle: \(averageMotorcycleWeight)")
I'm not sure it can be done in a simpler way, but I wanted to check with you guys to see if there is :-) Thank you for your time!
Upvotes: 0
Views: 91
Reputation: 1413
Hi you can actually use higher order functions to achieve what you need.
let allVehicles = "Peugeot#950#yellow#car--Yamaha#180#black#motorcycle--Kawazasaki#210#green#motorcycle"
let vehichlesArray = allVehicles.componentsSeparatedByString("--")
let vehicles = vehichlesArray.map { $0.componentsSeparatedByString("#") }
let cars = vehicles.filter { $0[3] == "car" }
let motorcycles = vehicles.filter { $0[3] == "motorcycle" }
let carsCount = cars.count
let motorcyclesCount = motorcycles.count
let carWgt = cars.reduce(0, combine: { $0 + (Int($1[1]) ?? 0) })
let motorcycleWgt = motorcycles.reduce(0, combine: { $0 + (Int($1[1]) ?? 0) })
However, I believe that object-oriented architecture will better suit this kind of scenario.
Upvotes: 1