Finding the smallest number lexicographically
I'm trying to print smallest number. My prof calls it lexicographical order I think.
For example, I have this list :
[27312,31539]
[49494,26089]
[2946,28149]
The result should be:
27312
26089
28149
Why 28149 is smaller than 2946 so, 2 = 2, but the next int 8<9 ,so it prints 28149.
Is it possible to implement this using lambda ?
python python-3.x
add a comment |
I'm trying to print smallest number. My prof calls it lexicographical order I think.
For example, I have this list :
[27312,31539]
[49494,26089]
[2946,28149]
The result should be:
27312
26089
28149
Why 28149 is smaller than 2946 so, 2 = 2, but the next int 8<9 ,so it prints 28149.
Is it possible to implement this using lambda ?
python python-3.x
Islambda
compulsory? Because, it does not look like a good use case forlambda
s.
– Austin
Nov 14 '18 at 17:40
As I see it can be solved also without ,thank you :)
– LordNord
Nov 14 '18 at 17:54
add a comment |
I'm trying to print smallest number. My prof calls it lexicographical order I think.
For example, I have this list :
[27312,31539]
[49494,26089]
[2946,28149]
The result should be:
27312
26089
28149
Why 28149 is smaller than 2946 so, 2 = 2, but the next int 8<9 ,so it prints 28149.
Is it possible to implement this using lambda ?
python python-3.x
I'm trying to print smallest number. My prof calls it lexicographical order I think.
For example, I have this list :
[27312,31539]
[49494,26089]
[2946,28149]
The result should be:
27312
26089
28149
Why 28149 is smaller than 2946 so, 2 = 2, but the next int 8<9 ,so it prints 28149.
Is it possible to implement this using lambda ?
python python-3.x
python python-3.x
edited Nov 14 '18 at 17:51
isherwood
37.2k1082111
37.2k1082111
asked Nov 14 '18 at 17:33
LordNordLordNord
707
707
Islambda
compulsory? Because, it does not look like a good use case forlambda
s.
– Austin
Nov 14 '18 at 17:40
As I see it can be solved also without ,thank you :)
– LordNord
Nov 14 '18 at 17:54
add a comment |
Islambda
compulsory? Because, it does not look like a good use case forlambda
s.
– Austin
Nov 14 '18 at 17:40
As I see it can be solved also without ,thank you :)
– LordNord
Nov 14 '18 at 17:54
Is
lambda
compulsory? Because, it does not look like a good use case for lambda
s.– Austin
Nov 14 '18 at 17:40
Is
lambda
compulsory? Because, it does not look like a good use case for lambda
s.– Austin
Nov 14 '18 at 17:40
As I see it can be solved also without ,thank you :)
– LordNord
Nov 14 '18 at 17:54
As I see it can be solved also without ,thank you :)
– LordNord
Nov 14 '18 at 17:54
add a comment |
1 Answer
1
active
oldest
votes
You can use the min
function with str
as a key function:
print(min([27312,31539], key=str))
print(min([49494,26089], key=str))
print(min([2946,28149], key=str))
This outputs:
27312
26089
28149
1
Thank you, I didn't even think that it can be solved so easily. It's strange how it works, i mean by key of str
– LordNord
Nov 14 '18 at 17:53
With lambda -func = lambda array: min(array, key=str)
– Freund Allein
Nov 14 '18 at 17:55
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%2f53305834%2ffinding-the-smallest-number-lexicographically%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the min
function with str
as a key function:
print(min([27312,31539], key=str))
print(min([49494,26089], key=str))
print(min([2946,28149], key=str))
This outputs:
27312
26089
28149
1
Thank you, I didn't even think that it can be solved so easily. It's strange how it works, i mean by key of str
– LordNord
Nov 14 '18 at 17:53
With lambda -func = lambda array: min(array, key=str)
– Freund Allein
Nov 14 '18 at 17:55
add a comment |
You can use the min
function with str
as a key function:
print(min([27312,31539], key=str))
print(min([49494,26089], key=str))
print(min([2946,28149], key=str))
This outputs:
27312
26089
28149
1
Thank you, I didn't even think that it can be solved so easily. It's strange how it works, i mean by key of str
– LordNord
Nov 14 '18 at 17:53
With lambda -func = lambda array: min(array, key=str)
– Freund Allein
Nov 14 '18 at 17:55
add a comment |
You can use the min
function with str
as a key function:
print(min([27312,31539], key=str))
print(min([49494,26089], key=str))
print(min([2946,28149], key=str))
This outputs:
27312
26089
28149
You can use the min
function with str
as a key function:
print(min([27312,31539], key=str))
print(min([49494,26089], key=str))
print(min([2946,28149], key=str))
This outputs:
27312
26089
28149
answered Nov 14 '18 at 17:47
blhsingblhsing
33.4k41437
33.4k41437
1
Thank you, I didn't even think that it can be solved so easily. It's strange how it works, i mean by key of str
– LordNord
Nov 14 '18 at 17:53
With lambda -func = lambda array: min(array, key=str)
– Freund Allein
Nov 14 '18 at 17:55
add a comment |
1
Thank you, I didn't even think that it can be solved so easily. It's strange how it works, i mean by key of str
– LordNord
Nov 14 '18 at 17:53
With lambda -func = lambda array: min(array, key=str)
– Freund Allein
Nov 14 '18 at 17:55
1
1
Thank you, I didn't even think that it can be solved so easily. It's strange how it works, i mean by key of str
– LordNord
Nov 14 '18 at 17:53
Thank you, I didn't even think that it can be solved so easily. It's strange how it works, i mean by key of str
– LordNord
Nov 14 '18 at 17:53
With lambda -
func = lambda array: min(array, key=str)
– Freund Allein
Nov 14 '18 at 17:55
With lambda -
func = lambda array: min(array, key=str)
– Freund Allein
Nov 14 '18 at 17:55
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.
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%2f53305834%2ffinding-the-smallest-number-lexicographically%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
Is
lambda
compulsory? Because, it does not look like a good use case forlambda
s.– Austin
Nov 14 '18 at 17:40
As I see it can be solved also without ,thank you :)
– LordNord
Nov 14 '18 at 17:54