Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

2.5 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
698a1a863194f1f4e63f6460 Challenge 209: Element Size 29 challenge-209

--description--

Given a window size, the width of an element in viewport width "vw" units, and the height of an element in viewport height "vh" units, determine the size of the element in pixels.

  • The given window size and returned element size are strings in the format "width x height", "1200 x 800" for example.

  • "vw" units are the percent of window width. "50vw" for example, is 50% of the width of the window.

  • "vh" units are the percent of window height. "50vh" for example, is 50% of the height of the window.

--hints--

get_element_size("1200 x 800", "50vw", "50vh") should return "600 x 400".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_element_size("1200 x 800", "50vw", "50vh"), "600 x 400")`)
}})

get_element_size("320 x 480", "25vw", "50vh") should return "80 x 240".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_element_size("320 x 480", "25vw", "50vh"), "80 x 240")`)
}})

get_element_size("1000 x 500", "7vw", "3vh") should return "70 x 15".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_element_size("1000 x 500", "7vw", "3vh"), "70 x 15")`)
}})

get_element_size("1920 x 1080", "95vw", "100vh") should return "1824 x 1080".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_element_size("1920 x 1080", "95vw", "100vh"), "1824 x 1080")`)
}})

get_element_size("1200 x 800", "0vw", "0vh") should return "0 x 0".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_element_size("1200 x 800", "0vw", "0vh"), "0 x 0")`)
}})

get_element_size("1440 x 900", "100vw", "114vh") should return "1440 x 1026".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_element_size("1440 x 900", "100vw", "114vh"), "1440 x 1026")`)
}})

--seed--

--seed-contents--

def get_element_size(window_size, element_vw, element_vh):

    return window_size

--solutions--

def get_element_size(window_size, element_vw, element_vh):
    window_width, window_height = map(int, window_size.split(" x "))

    vw = int(element_vw.replace("vw", ""))
    vh = int(element_vh.replace("vh", ""))

    width_px = round((vw / 100) * window_width)
    height_px = round((vh / 100) * window_height)

    return f"{width_px} x {height_px}"