Skip to main content
2 of 6
added 255 characters in body
John K.
  • 265
  • 1
  • 6

First of all, whenever you see this ... this is a big no-no:

'9'*work_dict[9] + '8'*work_dict[8] + '7'*work_dict[7] +
'6'*work_dict[6] + '5'*work_dict[5] + '4'*work_dict[4] +
'3'*work_dict[3] + '2'*work_dict[2] + '1'* work_dict[1] +
'0'*work_dict[0]

it could be replaced by a simple

''.join(str(i)'*work_dict[i] for i in reversed(range(10)))

# or if you want to use Python >=3.6 f-string notation:
''.join(f'{i}'*work_dict[i] for i in reversed(range(10)))

Of course, the initialization of the work_dict is similar. And in fact, you don't need to initialize it if you take care to use dict.get instead of dict[]:

work_dict[number] = work_dict[number] + 1
# is equivalent to
work_dict[number] = work_dict.get(number, 0) + 1  # default to 0 if not in dict

If you want to have it as compact and "functional" as possible, it would be much easier to just sort the entire input and output it:

def maximum_number(lst):
    return int(''.join(reversed(sorted(''.join(str(x) for x in lst)))))

However, note that this doesn't work on the empty list (which might be okay, depending on the specification of the function).

It should also be mentioned that

  1. it is harder to write than the "manual loop" variant, which can be important in an interview
  2. it might be harder to read and thus to debug, but I believe that this is up to the eye of the beholder to determine
John K.
  • 265
  • 1
  • 6