Skip to content

2118. Build the Equation 👎

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
WITH
  PowerToTerm AS (
    SELECT
      CONCAT(
        IF(factor > 0, '+', ''),
        factor,
        IF(power = 0, '', 'X'),
        IF(power IN (0, 1), '', CONCAT('^', power))
      ) AS term,
      power
    FROM Terms
    ORDER BY power DESC
  )
SELECT
  CONCAT(
    GROUP_CONCAT(
      term
      ORDER BY power DESC SEPARATOR ''
    ),
    '=0'
  ) AS equation
FROM PowerToTerm;