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.3 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69f8c998d78ad3171a0713bf Challenge 292: Wider Aspect Ratio 29 challenge-292

--description--

Given two strings for different image dimensions, return the aspect ratio of the image with a greater width-to-height ratio.

  • The given strings will be in the format "WxH", for example, "1920x1080".
  • The aspect ratio is the ratio of width to height, reduced to the lowest whole numbers. For example, "1920x1080" reduces to "16:9".
  • Return a string in format "W:H", for example, "16:9".

--hints--

get_wider_aspect_ratio("1920x1080", "800x600") should return "16:9".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_wider_aspect_ratio("1920x1080", "800x600"), "16:9")`)
}})

get_wider_aspect_ratio("1080x1350", "2048x1536") should return "4:3".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_wider_aspect_ratio("1080x1350", "2048x1536"), "4:3")`)
}})

get_wider_aspect_ratio("640x480", "2440x1220") should return "2:1".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_wider_aspect_ratio("640x480", "2440x1220"), "2:1")`)
}})

get_wider_aspect_ratio("360x640", "1080x1920") should return "9:16".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_wider_aspect_ratio("360x640", "1080x1920"), "9:16")`)
}})

get_wider_aspect_ratio("3440x1440", "2048x858") should return "43:18".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_wider_aspect_ratio("3440x1440", "2048x858"), "43:18")`)
}})

get_wider_aspect_ratio("12345x61234", "12534x51234") should return "2089:8539".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_wider_aspect_ratio("12345x61234", "12534x51234"), "2089:8539")`)
}})

--seed--

--seed-contents--

def get_wider_aspect_ratio(a, b):

    return a

--solutions--

from math import gcd

def get_wider_aspect_ratio(a, b):
    def parse(s):
        w, h = map(int, s.split("x"))
        g = gcd(w, h)
        return w / h, f"{w // g}:{h // g}"

    ratio_a, simplified_a = parse(a)
    ratio_b, simplified_b = parse(b)

    return simplified_a if ratio_a >= ratio_b else simplified_b