Back to Freecodecamp

Challenge 168: Scaled Image

curriculum/challenges/english/blocks/daily-coding-challenges-python/696655d24b614176d4c9b78b.md

latest1.4 KB
Original Source

--description--

Given a string representing the width and height of an image, and a number to scale the image, return the scaled width and height.

  • The input string is in the format "WxH". For example, "800x600".
  • The scale is a number to multiply the width and height by.

Return the scaled dimensions in the same "WxH" format.

--hints--

scale_image("800x600", 2) should return "1600x1200".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(scale_image("800x600", 2), "1600x1200")`)
}})

scale_image("100x100", 10) should return "1000x1000".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(scale_image("100x100", 10), "1000x1000")`)
}})

scale_image("1024x768", 0.5) should return "512x384".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(scale_image("1024x768", 0.5), "512x384")`)
}})

scale_image("300x200", 1.5) should return "450x300".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(scale_image("300x200", 1.5), "450x300")`)
}})

--seed--

--seed-contents--

py
def scale_image(size, scale):

    return size

--solutions--

py
def scale_image(size, scale):
    width, height = map(int, size.split("x"))

    new_width = round(width * scale)
    new_height = round(height * scale)

    return f"{new_width}x{new_height}"