loading...

Daily Challenge #109 - Decorate with Wallpaper

thepracticaldev profile image dev.to staff ・1 min read

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 fool-proof way to get it just right.

John is measuring his room by length l, width w, and height h in meters. The rolls of wallpaper John purchased has a width of 52 centimeters. The length of a roll is 10 meters. Bear in mind however, that it is best to have an extra length of wallpaper handy in case of mistakes. John wants to buy 15% more wallpaper in length than he needs to account for miscalculations.

Write a function wallpaper(l, w, h) and return a plain English word in lower case the number of rolls he should buy. All rolls will be put edge to edge, with no space leftover. The number of rolls will always be less than or equal to 20.

Examples:
wallpaper(4.0, 3.5, 3.0) should return "ten"
wallpaper(0.0, 3.5, 3.0) should return "zero"

Tests:
wallpaper(6.3, 4.5, 3.29)
wallpaper(7.8, 2.9, 3.29)
wallpaper(6.3, 5.8, 3.13)
wallpaper(6.1, 6.7, 2.81)

Good luck!


Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Discussion

pic
Editor guide
Collapse
kespri profile image
kespri

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
kespri profile image
kespri

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