Stack | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Stack

You need to make a back button for a browser. You use a stack to store the website links visited. Each new link visited is pushed onto the stack. The back button needs to pop the top link from the stack and navigate to it. The given code declares a Browser class as a stack and implements some of its methods. Then, some links are pushed onto the stack. A while loop is then used to go back to all links and print them. Implement the required pop() method for the Browser, so that the given code works as expected. Note, that the pop() method needs to return the value, so that it can be printed. class Browser: def __init__(self): self.links = [] def is_empty(self): return self.links == [] def push(self, link): self.links.insert(0, link) x = Browser() x.push('about:blank') x.push('www.sololearn.com') x.push('www.sololearn.com/courses/') x.push('www.sololearn.com/courses/JUMP_LINK__&&__python__&&__JUMP_LINK/') while not x.is_empty(): print(x.pop()) not able to solve problem as unable to understand stack class in python.

1st Jul 2021, 9:29 AM
Dharmendra Singh
Dharmendra Singh - avatar
11 Answers
+ 7
class Browser: def __init__(self): self.links = [] def is_empty(self): return self.links == [] def push(self, link): self.links.insert(0, link) def pop(self): return self.links.pop(0) x = Browser() x.push('about:blank') x.push('www.sololearn.com') x.push('www.sololearn.com/courses/') x.push('www.sololearn.com/courses/JUMP_LINK__&&__python__&&__JUMP_LINK/') while not x.is_empty(): print(x.pop())
21st Sep 2021, 8:03 PM
Hasnae BOUHMADY
Hasnae BOUHMADY - avatar
+ 3
class Browser: def __init__(self): self.links = [] def is_empty(self): return self.links == [] def push(self, link): self.links.insert(0, link) def pop(self): return self.links.pop(0) x = Browser() x.push('about:blank') x.push('www.sololearn.com') x.push('www.sololearn.com/courses/') x.push('www.sololearn.com/courses/JUMP_LINK__&&__python__&&__JUMP_LINK/') while not x.is_empty(): print(x.links.pop(0)) It works my friends. Please check it.
5th Nov 2021, 4:38 PM
Siva Sai Narayana
Siva Sai Narayana - avatar
+ 2
In browser class def pop(self): return self.links.pop(0)
1st Jul 2021, 10:18 AM
Oma Falk
Oma Falk - avatar
+ 2
Lothar I agree with you. But here and there it might be a good idea to provide the code. In this case I think he stood on the pipe. So he will sich vor den Kopf schlagen and say.. Oh....so easy was it? 10 minutes studying a good solution might be as good as trying 1 hour without result. But...it depends from case to case.
1st Jul 2021, 10:56 AM
Oma Falk
Oma Falk - avatar
+ 1
pop method will remove the last element in stack . So you can implement a pop method with the following functionality , return self.links.pop() Here pop() is a list method in python that removes the last element (which is what pop should do).
1st Jul 2021, 9:40 AM
Abhay
Abhay - avatar
+ 1
print(x.pop(0))
1st Jul 2021, 9:56 AM
Oma Falk
Oma Falk - avatar
+ 1
Dharmendra Singh , the community will not provide complete solutions. This does not complying with the mission of sololearn. and it does not help you developing your coding knowledge and your problem solving ability. it is not a problem when someone can not solve a coding task instantly, as you can repeat some parts of the tutorial and then try it again. happy coding and good success!
1st Jul 2021, 10:44 AM
Lothar
Lothar - avatar
+ 1
oh.. ,is that true, i need to go for python intermediate first & then python data structure, may be that's why im feeling difficulty in solving problems,i have completed python for beginners.
1st Jul 2021, 1:53 PM
Dharmendra Singh
Dharmendra Singh - avatar
0
can u please provide complete solution
1st Jul 2021, 10:10 AM
Dharmendra Singh
Dharmendra Singh - avatar
0
print(x.links.pop(0))
16th Aug 2022, 1:29 PM
Tsion Hailye Tasew
Tsion Hailye Tasew - avatar
- 1
I think this program is a solution for the problem: if (evenNums.Peek() % 2 != 0) { Console.WriteLine(num + ": Removed"); evenNums.Pop(); } foreach (var evenNum in evenNums) { Console.Write(evenNum + " "); }
22nd Dec 2021, 2:04 PM
San khatkmou
San khatkmou - avatar