We're a place where coders share, stay up-to-date and grow their careers.
My solution in Swift, I check if one parameter equal to 0.0 so it's not a room and then calculate each wall :
0.0
func wallpaper(length: Double, width: Double, height: Double) -> String { let numberOfRolls = [0: "zero", 1: "one", 2: "two", 3: "three", 4: "four", 5: "five", 6: "six", 7: "seven", 8: "eight", 9: "nine", 10: "ten", 11: "eleven", 12: "twelve", 13: "thirteen", 14: "fourteen", 15: "fiveteen", 16: "sixteen", 17: "seventeen", 18: "eighteen", 19: "nineteen", 20: "twenty" ] guard length != 0.0 && width != 0.0 && height != 0.0 else { return numberOfRolls[0]! } var amount = 0.0 [length, width].forEach { amount += (($0 / 0.52) / ( 10.0 / height)) * 2 } let total = Int((amount * 1.15).rounded(.up)) return numberOfRolls[total] ?? numberOfRolls[0]! } wallpaper(length: 0.0, width: 3.5, height: 3.0) wallpaper(length: 4.0, width: 3.5, height: 3.0) wallpaper(length: 6.3, width: 4.5, height: 3.29) wallpaper(length: 7.8, width: 2.9, height: 3.29) wallpaper(length: 6.3, width: 5.8, height: 3.13) wallpaper(length: 6.1, width: 6.7, height: 2.81)
Why the double at the beginning?
To avoid cast during the calculation but it's true I could use Float too.
Float
My solution in Swift, I check if one parameter equal to
0.0
so it's not a room and then calculate each wall :Why the double at the beginning?
To avoid cast during the calculation but it's true I could use
Float
too.