Skip to main content
fixed for case [50,501]
Source Link

EDIT: After talking to Baldrickk in the comments, I've realized that my sort by key method doesn't work for this problem. For instance, the below approach would sort [50,505040] as 50504050 instead of 50505040. I'm leaving the answer up for those who are interested.


This is my attempt at solving the problem assuming that the OP did indeed misinterpret the question and that the numbers can't be split up into digits. Going off of AlexV's answer I've come up with a simple sort by key solution (rather than sort by cmp). I would have posted this as a comment but I lack the reputation.

The first step is to realize that since the first digit is the most significant, the second digit the second most significant and so on, we can simply do an alphabetical sort.

def maximum_number(lst):
  return int("".join((str(n) for n in sorted(lst, key=str, reverse=True))))

So this will work for most cases.

But as AlexV pointed out in a comment, this neglects that, for instance, 5 should be sorted ahead of 51 (since 551>515), 1 ahead of 10, etc.

The key element to take note of here is that a number n consisting only ofthat begins with a digit d should be sorted ahead of a number nk if k < d, but behind nk if k > d. If k = d, the order is arbitrary. This can be adjusted for by appending the first digit of every number onto itself, yielding the following solution.

def sorting_key(num):
  num_str = str(num)
  return num_str + num_str[0]

def maximum_number(lst):
  return int("".join((str(n) for n in sorted(lst, key=sorting_key, reverse=True))))

This passes all examples I've seen posted in other answers.


Thanks to Baldrickk for pointing out that the first revision of this answer would fail at [50,501]

This is my attempt at solving the problem assuming that the OP did indeed misinterpret the question and that the numbers can't be split up into digits. Going off of AlexV's answer I've come up with a simple sort by key solution (rather than sort by cmp). I would have posted this as a comment but I lack the reputation.

The first step is to realize that since the first digit is the most significant, the second digit the second most significant and so on, we can simply do an alphabetical sort.

def maximum_number(lst):
  return int("".join((str(n) for n in sorted(lst, key=str, reverse=True))))

So this will work for most cases.

But as AlexV pointed out in a comment, this neglects that, for instance, 5 should be sorted ahead of 51 (since 551>515), 1 ahead of 10, etc.

The key element to take note of here is that a number n consisting only of a digit d should be sorted ahead of a number nk if k < d, but behind nk if k > d. If k = d, the order is arbitrary. This can be adjusted for by appending the first digit of every number onto itself, yielding the following solution.

def sorting_key(num):
  num_str = str(num)
  return num_str + num_str[0]

def maximum_number(lst):
  return int("".join((str(n) for n in sorted(lst, key=sorting_key, reverse=True))))

This passes all examples I've seen posted in other answers.


Thanks to Baldrickk for pointing out that the first revision of this answer would fail at [50,501]

EDIT: After talking to Baldrickk in the comments, I've realized that my sort by key method doesn't work for this problem. For instance, the below approach would sort [50,505040] as 50504050 instead of 50505040. I'm leaving the answer up for those who are interested.


This is my attempt at solving the problem assuming that the OP did indeed misinterpret the question and that the numbers can't be split up into digits. Going off of AlexV's answer I've come up with a simple sort by key solution (rather than sort by cmp). I would have posted this as a comment but I lack the reputation.

The first step is to realize that since the first digit is the most significant, the second digit the second most significant and so on, we can simply do an alphabetical sort.

def maximum_number(lst):
  return int("".join((str(n) for n in sorted(lst, key=str, reverse=True))))

So this will work for most cases.

But as AlexV pointed out in a comment, this neglects that, for instance, 5 should be sorted ahead of 51 (since 551>515), 1 ahead of 10, etc.

The key element to take note of here is that a number n that begins with a digit d should be sorted ahead of a number nk if k < d, but behind nk if k > d. If k = d, the order is arbitrary. This can be adjusted for by appending the first digit of every number onto itself, yielding the following solution.

def sorting_key(num):
  num_str = str(num)
  return num_str + num_str[0]

def maximum_number(lst):
  return int("".join((str(n) for n in sorted(lst, key=sorting_key, reverse=True))))

This passes all examples I've seen posted in other answers.


Thanks to Baldrickk for pointing out that the first revision of this answer would fail at [50,501]

fixed for case [50,501]
Source Link

This is my attempt at solving the problem assuming that the OP did indeed misinterpret the question and that the numbers can't be split up into digits. Going off of AlexV's answer I've come up with a simple sort by key solution (rather than sort by cmp). I would have posted this as a comment but I lack the reputation.

The first step is to realize that since the first digit is the most significant, the second digit the second most significant and so on, we can simply do an alphabetical sort.

def maximum_number(lst):
  return int("".join((str(n) for n in sorted(lst, key=str, reverse=True))))

So this will work for most cases.

But as AlexV pointed out in a comment, this neglects that, for instance, 5 should be sorted ahead of 51 (since 551>515), 1 ahead of 10, etc.

The key element to take note of here is that a number n consisting only of a digit d should be sorted ahead of a number nk if k < d, but behind nk if k > d. If k = d, the order is arbitrary. This can be adjusted for by appending the lastfirst digit of every number onto itself, yielding the following solution.

def sorting_key(num):
  returnnum_str = str(num*10num)
  return num_str + num%10)num_str[0]

def maximum_number(lst):
  return int("".join((str(n) for n in sorted(lst, key=sorting_key, reverse=True))))

This passes all examples I've seen posted in other answers.


Thanks to Baldrickk for pointing out that the first revision of this answer would fail at [50,501]

This is my attempt at solving the problem assuming that the OP did indeed misinterpret the question and that the numbers can't be split up into digits. Going off of AlexV's answer I've come up with a simple sort by key solution (rather than sort by cmp). I would have posted this as a comment but I lack the reputation.

The first step is to realize that since the first digit is the most significant, the second digit the second most significant and so on, we can simply do an alphabetical sort.

def maximum_number(lst):
  return int("".join((str(n) for n in sorted(lst, key=str, reverse=True))))

So this will work for most cases.

But as AlexV pointed out in a comment, this neglects that, for instance, 5 should be sorted ahead of 51 (since 551>515), 1 ahead of 10, etc.

The key element to take note of here is that a number n consisting only of a digit d should be sorted ahead of a number nk if k < d, but behind nk if k > d. If k = d, the order is arbitrary. This can be adjusted for by appending the last digit of every number onto itself, yielding the following solution.

def sorting_key(num):
  return str(num*10 + num%10)

def maximum_number(lst):
  return int("".join((str(n) for n in sorted(lst, key=sorting_key, reverse=True))))

This passes all examples I've seen posted in other answers.

This is my attempt at solving the problem assuming that the OP did indeed misinterpret the question and that the numbers can't be split up into digits. Going off of AlexV's answer I've come up with a simple sort by key solution (rather than sort by cmp). I would have posted this as a comment but I lack the reputation.

The first step is to realize that since the first digit is the most significant, the second digit the second most significant and so on, we can simply do an alphabetical sort.

def maximum_number(lst):
  return int("".join((str(n) for n in sorted(lst, key=str, reverse=True))))

So this will work for most cases.

But as AlexV pointed out in a comment, this neglects that, for instance, 5 should be sorted ahead of 51 (since 551>515), 1 ahead of 10, etc.

The key element to take note of here is that a number n consisting only of a digit d should be sorted ahead of a number nk if k < d, but behind nk if k > d. If k = d, the order is arbitrary. This can be adjusted for by appending the first digit of every number onto itself, yielding the following solution.

def sorting_key(num):
  num_str = str(num)
  return num_str + num_str[0]

def maximum_number(lst):
  return int("".join((str(n) for n in sorted(lst, key=sorting_key, reverse=True))))

This passes all examples I've seen posted in other answers.


Thanks to Baldrickk for pointing out that the first revision of this answer would fail at [50,501]

Source Link

This is my attempt at solving the problem assuming that the OP did indeed misinterpret the question and that the numbers can't be split up into digits. Going off of AlexV's answer I've come up with a simple sort by key solution (rather than sort by cmp). I would have posted this as a comment but I lack the reputation.

The first step is to realize that since the first digit is the most significant, the second digit the second most significant and so on, we can simply do an alphabetical sort.

def maximum_number(lst):
  return int("".join((str(n) for n in sorted(lst, key=str, reverse=True))))

So this will work for most cases.

But as AlexV pointed out in a comment, this neglects that, for instance, 5 should be sorted ahead of 51 (since 551>515), 1 ahead of 10, etc.

The key element to take note of here is that a number n consisting only of a digit d should be sorted ahead of a number nk if k < d, but behind nk if k > d. If k = d, the order is arbitrary. This can be adjusted for by appending the last digit of every number onto itself, yielding the following solution.

def sorting_key(num):
  return str(num*10 + num%10)

def maximum_number(lst):
  return int("".join((str(n) for n in sorted(lst, key=sorting_key, reverse=True))))

This passes all examples I've seen posted in other answers.