Day 10 - Python

Problem Statement: Browser Navigation Simulator

You are required to simulate a basic web browser navigation system. The browser will initially start with an empty history and forward history. Users will input a sequence of websites to visit, followed by a sequence of actions to perform on the browser.

Class to Implement:

Implement a class Browser with the following methods:

__init__(self): Initializes the browser with empty history and forward history lists.

navigate(self, url): Adds the given URL to the history list, simulating navigation to that URL.

back(self): Simulates pressing the back button in the browser. Moves back one step in history if possible.

forward(self): Simulates pressing the forward button in the browser. Moves forward one step in history if possible.

print_current_page(self): Prints the URL of the current page being viewed.

Input:

The first line of input contains an integer n (1 ≤ n ≤ 100), indicating the number of websites the user intends to visit.

The second line contains n space-separated strings representing the URLs of the websites to visit.

The third line contains a sequence of space-separated integers, representing the actions to perform on the browser. Each integer corresponds to the following actions:

1: Print the URL of the current page.

2: Move forward in the browsing history.

3: Move backward in the browsing history.

Output:

For each action '1', '2' or '3' in the sequence, perform the corresponding action as described above.

Sample Input:

5

www.google.com    www.mastercodingfromscratch.blogspot.com    www.facebook.com www.youtube.com  www.instagram.com

1 2 3 3 3 1

Sample Output:

Navigating to www.google.com

Navigating to www.mastercodingfromscratch.blogspot.com

Navigating to www.facebook.com

Navigating to www.youtube.com

Navigating to www.instagram.com


Current page: www.instagram.com


Going forward..

No more pages to go forward


Going backward..

Going back to www.youtube.com


Going backward..

Going back to www.facebook.com


Going backward..

Going back to www.mastercodingfromscratch.blogspot.com


Current page: www.mastercodingfromscratch.blogspot.com


SOLUTION:

class Browser:

    def __init__(self):

        self.history = []

        self.forward_history = []


    def navigate(self, url):

        print("Navigating to", url)

        self.history.append(url)


    def back(self):

        print("Going backward..")

        if len(self.history) > 1:

            current_page = self.history.pop()

            self.forward_history.append(current_page)

            print("Going back to", self.history[-1])

        else:

            print("No more pages to go back")

        print()


    def forward(self):

        print("Going forward..")

        if self.forward_history:

            next_page = self.forward_history.pop()

            self.history.append(next_page)

            print("Going forward to", next_page)

        else:

            print("No more pages to go forward")

        print()


    def print_current_page(self):

        print("Current page:", self.history[-1])

        print()


browser = Browser()


n = int(input())

websites = input().split()


for i in websites[:n]:

    browser.navigate(i)


actions = input("").split()


for i in actions:

    if i == '1':

        browser.print_current_page()

    elif i == '2':

        browser.forward()

    elif i == '3':

        browser.back()

    else:

        print("Invalid action.")

Insights:

  • The browser class maintains history and forward_history lists to keep track of visited pages and forward navigation.
  • The navigate, back, and forward methods handle page navigation.
  • The print_current_page method prints the URL of the current page.
  • When going forward, it checks if there are pages in forward_history to navigate forward. When going back, it checks if there are pages in history to navigate backward.
  • The history list acts as a stack to track visited pages, enabling the back function to pop the last visited page, simulating backward navigation. 
  • A list is used for the stack due to its flexibility and ease of implementation in Python, offering built-in methods like append and pop for stack operations.
Code and Enjoy! :)


Comments