DEV Community

Daily Challenge #109 - Decorate with Wallpaper

dev.to staff on November 06, 2019

John wants to decorate his room with wallpaper. He's heard that having the right amount of wallpaper can be trickier than it sounds. He'd like a fo...
Collapse
 
kesprit profile image
kesprit • Edited

My solution in Swift, I check if one parameter equal to 0.0 so it's not a room and then calculate each wall :

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)
Collapse
 
scrabill profile image
Shannon Crabill

Why the double at the beginning?

Collapse
 
kesprit profile image
kesprit

To avoid cast during the calculation but it's true I could use Float too.