Increase performance of simple push pop “stack”
up vote
0
down vote
favorite
I was asked to do an interview question on Hackerrank to implement a stack. One of the methods of the stack was 'incr' which increments by the bottom of the stacks elements by a value. Although I got the basic test cases right, a few of them "timed out".
class Stack:
def __init__(self):
self.stack =
def push(self, element):
self.stack.append(element)
def pop(self):
if self.stack:
self.stack.pop()
def incr(self, num_elements, value):
'''Increments bottom num_elements by value'''
for idx in range(num_elements):
self.stack[idx] += value
def print_top(self):
print(self.stack[-1])
I'm guessing my implementation was too slow. I'm not sure how to make it faster. I tried to replace the 'incr' with the following list comprension but it didn't make a significant difference when I timed it with a million elements.
[x + value for x in self.stack[:num_elements]].extend(self.stack[num_elements:])
So I'm not exactly sure why I got it wrong.. maybe it was due to the push and the pop?
python performance
|
show 5 more comments
up vote
0
down vote
favorite
I was asked to do an interview question on Hackerrank to implement a stack. One of the methods of the stack was 'incr' which increments by the bottom of the stacks elements by a value. Although I got the basic test cases right, a few of them "timed out".
class Stack:
def __init__(self):
self.stack =
def push(self, element):
self.stack.append(element)
def pop(self):
if self.stack:
self.stack.pop()
def incr(self, num_elements, value):
'''Increments bottom num_elements by value'''
for idx in range(num_elements):
self.stack[idx] += value
def print_top(self):
print(self.stack[-1])
I'm guessing my implementation was too slow. I'm not sure how to make it faster. I tried to replace the 'incr' with the following list comprension but it didn't make a significant difference when I timed it with a million elements.
[x + value for x in self.stack[:num_elements]].extend(self.stack[num_elements:])
So I'm not exactly sure why I got it wrong.. maybe it was due to the push and the pop?
python performance
Can you link to the original question?
– SilverSlash
Nov 12 at 2:38
It was a timed question for a company, I don't think I can view it anymore
– Kevin Nasto
Nov 12 at 2:40
1
Instead of incrementing every entry, perhaps keep a list of increments, and when youpop()
apply pending increments. And this would probably be better on Code Review, I think.
– Ken Y-N
Nov 12 at 2:40
I didn't even think of that.. maybe that's what it was
– Kevin Nasto
Nov 12 at 2:42
1
There was another question on SO about this same problem and they too could not pass all tests. stackoverflow.com/questions/51001791/…
– SilverSlash
Nov 12 at 2:52
|
show 5 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I was asked to do an interview question on Hackerrank to implement a stack. One of the methods of the stack was 'incr' which increments by the bottom of the stacks elements by a value. Although I got the basic test cases right, a few of them "timed out".
class Stack:
def __init__(self):
self.stack =
def push(self, element):
self.stack.append(element)
def pop(self):
if self.stack:
self.stack.pop()
def incr(self, num_elements, value):
'''Increments bottom num_elements by value'''
for idx in range(num_elements):
self.stack[idx] += value
def print_top(self):
print(self.stack[-1])
I'm guessing my implementation was too slow. I'm not sure how to make it faster. I tried to replace the 'incr' with the following list comprension but it didn't make a significant difference when I timed it with a million elements.
[x + value for x in self.stack[:num_elements]].extend(self.stack[num_elements:])
So I'm not exactly sure why I got it wrong.. maybe it was due to the push and the pop?
python performance
I was asked to do an interview question on Hackerrank to implement a stack. One of the methods of the stack was 'incr' which increments by the bottom of the stacks elements by a value. Although I got the basic test cases right, a few of them "timed out".
class Stack:
def __init__(self):
self.stack =
def push(self, element):
self.stack.append(element)
def pop(self):
if self.stack:
self.stack.pop()
def incr(self, num_elements, value):
'''Increments bottom num_elements by value'''
for idx in range(num_elements):
self.stack[idx] += value
def print_top(self):
print(self.stack[-1])
I'm guessing my implementation was too slow. I'm not sure how to make it faster. I tried to replace the 'incr' with the following list comprension but it didn't make a significant difference when I timed it with a million elements.
[x + value for x in self.stack[:num_elements]].extend(self.stack[num_elements:])
So I'm not exactly sure why I got it wrong.. maybe it was due to the push and the pop?
python performance
python performance
edited Nov 12 at 2:32
coldspeed
114k18104182
114k18104182
asked Nov 12 at 2:29
Kevin Nasto
6026
6026
Can you link to the original question?
– SilverSlash
Nov 12 at 2:38
It was a timed question for a company, I don't think I can view it anymore
– Kevin Nasto
Nov 12 at 2:40
1
Instead of incrementing every entry, perhaps keep a list of increments, and when youpop()
apply pending increments. And this would probably be better on Code Review, I think.
– Ken Y-N
Nov 12 at 2:40
I didn't even think of that.. maybe that's what it was
– Kevin Nasto
Nov 12 at 2:42
1
There was another question on SO about this same problem and they too could not pass all tests. stackoverflow.com/questions/51001791/…
– SilverSlash
Nov 12 at 2:52
|
show 5 more comments
Can you link to the original question?
– SilverSlash
Nov 12 at 2:38
It was a timed question for a company, I don't think I can view it anymore
– Kevin Nasto
Nov 12 at 2:40
1
Instead of incrementing every entry, perhaps keep a list of increments, and when youpop()
apply pending increments. And this would probably be better on Code Review, I think.
– Ken Y-N
Nov 12 at 2:40
I didn't even think of that.. maybe that's what it was
– Kevin Nasto
Nov 12 at 2:42
1
There was another question on SO about this same problem and they too could not pass all tests. stackoverflow.com/questions/51001791/…
– SilverSlash
Nov 12 at 2:52
Can you link to the original question?
– SilverSlash
Nov 12 at 2:38
Can you link to the original question?
– SilverSlash
Nov 12 at 2:38
It was a timed question for a company, I don't think I can view it anymore
– Kevin Nasto
Nov 12 at 2:40
It was a timed question for a company, I don't think I can view it anymore
– Kevin Nasto
Nov 12 at 2:40
1
1
Instead of incrementing every entry, perhaps keep a list of increments, and when you
pop()
apply pending increments. And this would probably be better on Code Review, I think.– Ken Y-N
Nov 12 at 2:40
Instead of incrementing every entry, perhaps keep a list of increments, and when you
pop()
apply pending increments. And this would probably be better on Code Review, I think.– Ken Y-N
Nov 12 at 2:40
I didn't even think of that.. maybe that's what it was
– Kevin Nasto
Nov 12 at 2:42
I didn't even think of that.. maybe that's what it was
– Kevin Nasto
Nov 12 at 2:42
1
1
There was another question on SO about this same problem and they too could not pass all tests. stackoverflow.com/questions/51001791/…
– SilverSlash
Nov 12 at 2:52
There was another question on SO about this same problem and they too could not pass all tests. stackoverflow.com/questions/51001791/…
– SilverSlash
Nov 12 at 2:52
|
show 5 more comments
active
oldest
votes
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',
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%2f53255283%2fincrease-performance-of-simple-push-pop-stack%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53255283%2fincrease-performance-of-simple-push-pop-stack%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
Can you link to the original question?
– SilverSlash
Nov 12 at 2:38
It was a timed question for a company, I don't think I can view it anymore
– Kevin Nasto
Nov 12 at 2:40
1
Instead of incrementing every entry, perhaps keep a list of increments, and when you
pop()
apply pending increments. And this would probably be better on Code Review, I think.– Ken Y-N
Nov 12 at 2:40
I didn't even think of that.. maybe that's what it was
– Kevin Nasto
Nov 12 at 2:42
1
There was another question on SO about this same problem and they too could not pass all tests. stackoverflow.com/questions/51001791/…
– SilverSlash
Nov 12 at 2:52