Skip to main content
Add note about lack of repeated values; added 51 characters in body
Source Link
mjolka
  • 16.2k
  • 2
  • 29
  • 73

Since a, b can be equal, I wrap this list into a set to produce my code below.

Why not just handle this case separately? It's then simple to prove that if a != b, we don't have repeated values, and we can just generate them in increasing order.

def last_stone(a, b, n):
    if a == b:
        return [a * (n - 1)]
    if a < b:
        return last_stone(b, a, n)
    return [i * a + (n - i - 1) * b for i in range(n)]

Since a, b can be equal, I wrap this list into a set to produce my code below.

Why not just handle this case separately?

def last_stone(a, b, n):
    if a == b:
        return [a * (n - 1)]
    if a < b:
        return last_stone(b, a, n)
    return [i * a + (n - i - 1) * b for i in range(n)]

Since a, b can be equal, I wrap this list into a set to produce my code below.

Why not just handle this case separately? It's then simple to prove that if a != b, we don't have repeated values, and we can just generate them in increasing order.

def last_stone(a, b, n):
    if a == b:
        return [a * (n - 1)]
    if a < b:
        return last_stone(b, a, n)
    return [i * a + (n - i - 1) * b for i in range(n)]
Source Link
mjolka
  • 16.2k
  • 2
  • 29
  • 73

Since a, b can be equal, I wrap this list into a set to produce my code below.

Why not just handle this case separately?

def last_stone(a, b, n):
    if a == b:
        return [a * (n - 1)]
    if a < b:
        return last_stone(b, a, n)
    return [i * a + (n - i - 1) * b for i in range(n)]