Custom comparison for integer strings
For the input 3 30 34 5 9
, the expected largest number output is: 9534330
Any inputs why the sorted is returning the same result as input: 3303459
?
import functools
def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)
inputList = input().split()
sorted(inputList, key=functools.cmp_to_key(compare))
max = "".join([ x for x in inputList])
print(max)
python python-3.x
add a comment |
For the input 3 30 34 5 9
, the expected largest number output is: 9534330
Any inputs why the sorted is returning the same result as input: 3303459
?
import functools
def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)
inputList = input().split()
sorted(inputList, key=functools.cmp_to_key(compare))
max = "".join([ x for x in inputList])
print(max)
python python-3.x
4
Becausesorted
returns a new list.
– jonrsharpe
Nov 20 at 7:56
add a comment |
For the input 3 30 34 5 9
, the expected largest number output is: 9534330
Any inputs why the sorted is returning the same result as input: 3303459
?
import functools
def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)
inputList = input().split()
sorted(inputList, key=functools.cmp_to_key(compare))
max = "".join([ x for x in inputList])
print(max)
python python-3.x
For the input 3 30 34 5 9
, the expected largest number output is: 9534330
Any inputs why the sorted is returning the same result as input: 3303459
?
import functools
def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)
inputList = input().split()
sorted(inputList, key=functools.cmp_to_key(compare))
max = "".join([ x for x in inputList])
print(max)
python python-3.x
python python-3.x
edited Nov 20 at 8:00
jonrsharpe
76.7k11101208
76.7k11101208
asked Nov 20 at 7:54
Rock
8117
8117
4
Becausesorted
returns a new list.
– jonrsharpe
Nov 20 at 7:56
add a comment |
4
Becausesorted
returns a new list.
– jonrsharpe
Nov 20 at 7:56
4
4
Because
sorted
returns a new list.– jonrsharpe
Nov 20 at 7:56
Because
sorted
returns a new list.– jonrsharpe
Nov 20 at 7:56
add a comment |
3 Answers
3
active
oldest
votes
The reason is either because you are sorting the list in the wrong order and you did not use the sorted list. Currently, you are sorting the list in an ascending order by the first digit. In addition to that, sorted()
function returns a new list, so you need to save that in a variable.
You can fix that by either using the reverse()
on the sorted list
import functools
def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)
inputList = input().split()
inputList = sorted(inputList, key=functools.cmp_to_key(compare))
inputList.reverse()
max = "".join([ x for x in inputList])
print(max)
Or switch operation in the compare()
function
import functools
def compare(item1, item2):
return int(item2 + item1) - int(item1 + item2)
inputList = input().split()
inputList = sorted(inputList, key=functools.cmp_to_key(compare))
max = "".join([ x for x in inputList])
print(max)
add a comment |
For exhaustiveness, you can also set the order argument in the sorted() function:
import functools
def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)
input_list = input().split(",")
custom_sorted_list = sorted(input_list, key=functools.cmp_to_key(compare), reverse=True)
max = "".join([ x for x in custom_sorted_list])
print(max)
add a comment |
The sorted function returns the list in ascending order.
But, as you only want the largest number, it is even easier:
import itertools
input_list = [3, 30, 34, 5, 9]
answer = max(map("".join, itertools.permutations(map(str, input_list))))
print(answer)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388492%2fcustom-comparison-for-integer-strings%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The reason is either because you are sorting the list in the wrong order and you did not use the sorted list. Currently, you are sorting the list in an ascending order by the first digit. In addition to that, sorted()
function returns a new list, so you need to save that in a variable.
You can fix that by either using the reverse()
on the sorted list
import functools
def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)
inputList = input().split()
inputList = sorted(inputList, key=functools.cmp_to_key(compare))
inputList.reverse()
max = "".join([ x for x in inputList])
print(max)
Or switch operation in the compare()
function
import functools
def compare(item1, item2):
return int(item2 + item1) - int(item1 + item2)
inputList = input().split()
inputList = sorted(inputList, key=functools.cmp_to_key(compare))
max = "".join([ x for x in inputList])
print(max)
add a comment |
The reason is either because you are sorting the list in the wrong order and you did not use the sorted list. Currently, you are sorting the list in an ascending order by the first digit. In addition to that, sorted()
function returns a new list, so you need to save that in a variable.
You can fix that by either using the reverse()
on the sorted list
import functools
def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)
inputList = input().split()
inputList = sorted(inputList, key=functools.cmp_to_key(compare))
inputList.reverse()
max = "".join([ x for x in inputList])
print(max)
Or switch operation in the compare()
function
import functools
def compare(item1, item2):
return int(item2 + item1) - int(item1 + item2)
inputList = input().split()
inputList = sorted(inputList, key=functools.cmp_to_key(compare))
max = "".join([ x for x in inputList])
print(max)
add a comment |
The reason is either because you are sorting the list in the wrong order and you did not use the sorted list. Currently, you are sorting the list in an ascending order by the first digit. In addition to that, sorted()
function returns a new list, so you need to save that in a variable.
You can fix that by either using the reverse()
on the sorted list
import functools
def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)
inputList = input().split()
inputList = sorted(inputList, key=functools.cmp_to_key(compare))
inputList.reverse()
max = "".join([ x for x in inputList])
print(max)
Or switch operation in the compare()
function
import functools
def compare(item1, item2):
return int(item2 + item1) - int(item1 + item2)
inputList = input().split()
inputList = sorted(inputList, key=functools.cmp_to_key(compare))
max = "".join([ x for x in inputList])
print(max)
The reason is either because you are sorting the list in the wrong order and you did not use the sorted list. Currently, you are sorting the list in an ascending order by the first digit. In addition to that, sorted()
function returns a new list, so you need to save that in a variable.
You can fix that by either using the reverse()
on the sorted list
import functools
def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)
inputList = input().split()
inputList = sorted(inputList, key=functools.cmp_to_key(compare))
inputList.reverse()
max = "".join([ x for x in inputList])
print(max)
Or switch operation in the compare()
function
import functools
def compare(item1, item2):
return int(item2 + item1) - int(item1 + item2)
inputList = input().split()
inputList = sorted(inputList, key=functools.cmp_to_key(compare))
max = "".join([ x for x in inputList])
print(max)
answered Nov 20 at 8:07
Andreas
1,7961618
1,7961618
add a comment |
add a comment |
For exhaustiveness, you can also set the order argument in the sorted() function:
import functools
def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)
input_list = input().split(",")
custom_sorted_list = sorted(input_list, key=functools.cmp_to_key(compare), reverse=True)
max = "".join([ x for x in custom_sorted_list])
print(max)
add a comment |
For exhaustiveness, you can also set the order argument in the sorted() function:
import functools
def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)
input_list = input().split(",")
custom_sorted_list = sorted(input_list, key=functools.cmp_to_key(compare), reverse=True)
max = "".join([ x for x in custom_sorted_list])
print(max)
add a comment |
For exhaustiveness, you can also set the order argument in the sorted() function:
import functools
def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)
input_list = input().split(",")
custom_sorted_list = sorted(input_list, key=functools.cmp_to_key(compare), reverse=True)
max = "".join([ x for x in custom_sorted_list])
print(max)
For exhaustiveness, you can also set the order argument in the sorted() function:
import functools
def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)
input_list = input().split(",")
custom_sorted_list = sorted(input_list, key=functools.cmp_to_key(compare), reverse=True)
max = "".join([ x for x in custom_sorted_list])
print(max)
answered Nov 20 at 8:11
leoburgy
1086
1086
add a comment |
add a comment |
The sorted function returns the list in ascending order.
But, as you only want the largest number, it is even easier:
import itertools
input_list = [3, 30, 34, 5, 9]
answer = max(map("".join, itertools.permutations(map(str, input_list))))
print(answer)
add a comment |
The sorted function returns the list in ascending order.
But, as you only want the largest number, it is even easier:
import itertools
input_list = [3, 30, 34, 5, 9]
answer = max(map("".join, itertools.permutations(map(str, input_list))))
print(answer)
add a comment |
The sorted function returns the list in ascending order.
But, as you only want the largest number, it is even easier:
import itertools
input_list = [3, 30, 34, 5, 9]
answer = max(map("".join, itertools.permutations(map(str, input_list))))
print(answer)
The sorted function returns the list in ascending order.
But, as you only want the largest number, it is even easier:
import itertools
input_list = [3, 30, 34, 5, 9]
answer = max(map("".join, itertools.permutations(map(str, input_list))))
print(answer)
answered Nov 20 at 8:12
Vitor SRG
1763
1763
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388492%2fcustom-comparison-for-integer-strings%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
Because
sorted
returns a new list.– jonrsharpe
Nov 20 at 7:56