Question

Use python 3 to solve: Every web browser has both a back and a forward button...

Use python 3 to solve:

Every web browser has both a back and a forward button which allows the user to navigate to previously seen web pages.

Your task is to implement this functionality using two stacks:

·        The input to your program will be a sequence of commands. The ">" and "<" tokens will be used to indicate forward and back, respectively. All other input will be web addresses (you can assume they will always be valid).

·        You will not actually have to do any web navigation and you will simply be keeping track of the current page by outputting the given address.

·        There are a few situations you should take care to handle.

o   First, if a user enters "<" or ">" without there being a previous or next page to go to, you should output a corresponding error message but allow the user to continue browsing.

o   Secondly, if a user enters "<" and then a new web address, the user's previous browsing history in the ">" direction should be erased.

o   For example, if a user is on "www.google.com", enters "<", and then enters "www.yahoo.com", the history of visiting "www.google.com" should be lost. This means that if the user then enters ">", this should generate an error message and not "www.google.com". This behaviour can also be seen below in the sample data. You can also test an actual web browser (specifically Firefox or Chrome) if you feel there is other ambiguity. Also, keep in mind that navigation should start with a home page which we will set as "www.cs.ualberta.ca".

You may choose how your program receives the input.

Sample Input

www.google.com
<
<
>
www.yahoo.com
www.ualberta.ca
www.beartracks.ualberta.ca
>
<
<
>
<
www.microsoft.com
>
<
<

Sample Output

Starting at Home Page: www.cs.ualberta.ca
Current page: www.google.com
Current page: www.cs.ualberta.ca
< is an invalid action
Current page: www.google.com
Current page: www.yahoo.com
Current Page: www.ualberta.ca
Current Page: www.beartracks.ualberta.ca
> is an invalid action
Current Page: www.ualberta.ca
Current page: www.yahoo.com
Current Page: www.ualberta.ca
Current page: www.yahoo.com
Current page: www.microsoft.com
> is an invalid action
Current page: www.yahoo.com
Current Page: www.google.com

0 0
Add a comment Improve this question Transcribed image text
Answer #1

back_stack=['www.cs.ualberta.ca']
forward_stack=[]

def main():
   print ('Starting at Home Page: www.cs.ualberta.ca')

   while True:
       input_str = input('')
       if(input_str=='<'):
           if(len(back_stack)<=1):
               print ('< is an invalid action')
           else:
               url = back_stack.pop()
               forward_stack.append(url)
               print ('Current Page: '+back_stack[-1])

       elif input_str=='>':
           if(len(forward_stack)==0):
               print ('> is an invalid action')
           else:
               url = forward_stack.pop()
               back_stack.append(url)
               print ('Current Page: '+back_stack[-1])  
       else:
           back_stack.append(input_str)
           forward_stack=[]
           print ('Current Page: '+input_str)

if __name__ == '__main__':
   main()

Add a comment
Know the answer?
Add Answer to:
Use python 3 to solve: Every web browser has both a back and a forward button...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CODE MUST BE IN PYTHON 3 The objective of this lab is to solve problems using...

    CODE MUST BE IN PYTHON 3 The objective of this lab is to solve problems using the Stack ADT Q1. Write a program that uses the Stack implementation given to you browser has both a back and a forward button which allows the user to in Stack.py to simulate navigation in a web browser. Every web navigate to previously seen web pages. Your task is to implement this functionality using two Stack objects called forward stack and back stack. The...

  • Could I get some help with the following Task? The program should be implemented in JavaScript and running on Firefox, a web browser independent to operating systems. The client has specified the foll...

    Could I get some help with the following Task? The program should be implemented in JavaScript and running on Firefox, a web browser independent to operating systems. The client has specified the following requirements for the functionality of the program: 1. The program should be running without errors throughout two Phases: Information Gathering and Information Presenting. 2. Information Gathering is to gather the information such as state/territory name, the population and population change over previous year for calculation of APG;...

  • Question 1 ​ A URL is a ____-part addressing scheme a. ​two b. ​four c. ​three...

    Question 1 ​ A URL is a ____-part addressing scheme a. ​two b. ​four c. ​three d. ​one Question 2 ​ A text file that contains HTML tags is called a(n) ____. a. ​server b. ​HTML document c. ​browser d. ​IP address Question 3 ​ A(n) ____ comes into existence as soon as a work is placed into a tangible form. a. ​open-source project b. ​copyright c. ​reproduction notice d. ​lien Question 4 ​ A(n) ____ is a small file...

  • Design an original, professional web site following the specifications listed below. This web sit...

    Design an original, professional web site following the specifications listed below. This web site will be for a business you plan to set up for yourself or for someone else. The following is a detailed list of the requirements for your web site. READ them carefully. Instructions - Web Site Requirements for the web site: General: You will thoroughly test all your pages in more than one browser. All links MUST work. All graphics must show on the page. All...

  • Description: In this assignment, you will be launching a denial of service attack on a web...

    Description: In this assignment, you will be launching a denial of service attack on a web server. We will be using hping3, a command-line oriented network security tool inside Kali Linux (an advanced penetration testing Linux distribution). Setting up the victim machine Download the Windows XP virtual machine with WebGoat server installed, using the following link. We will use this machine as the victim machine and launch a DoS attack on the WebGoat server.https://drive.google.com/open?id=0BwCbaZv8DevUejBPWlNHREFVc2s Open the victim machine and launch...

  • Easy Javascript questions You can use window.history to retrieve the history object. Using the history object, what methods can you call to navigate backwards and forward to web pages that have been...

    Easy Javascript questions You can use window.history to retrieve the history object. Using the history object, what methods can you call to navigate backwards and forward to web pages that have been visited recently? The answer is not in the book. See https://developer.mozilla.org/en-US/docs/Web/API/History. Think about the situation where the alert message displays “your reply was false.” Describe the type of person who would generate that output—someone who always tells the truth, someone who always lies, or some other type of...

  • NOTE: Write the Program in python as mentioned below and use while loop and flags as...

    NOTE: Write the Program in python as mentioned below and use while loop and flags as necessary. And please write the code following the program description given below. Directions: Read the program description below carefully. All requirements must be met by your program to receive full credit. Thus, pay particular attention to input and output requirements, structural requirements, and so on. Furthermore, code that contains syntax errors will not receive any credit, so be sure that your code interprets correctly...

  • The first script is validate.sh. This is a simple form validation script that will be used...

    The first script is validate.sh. This is a simple form validation script that will be used to verify the inputs given. Normally, this would be done to validate input from a website or another program before entry into a database or other record storage. In this case, we will keep it simple and only focus on the input validation step. In particular, the script should prompt the user for four values: first name, last name, zip code, and email address....

  • NOTE: Write the Program in python as mentioned below and use while loop and flags as...

    NOTE: Write the Program in python as mentioned below and use while loop and flags as necessary. And please write the code following the program description given below. DON'T use Function concept or any other concept except while loop,if-else. Directions: Read the program description below carefully. All requirements must be met by your program to receive full credit. Thus, pay particular attention to input and output requirements, structural requirements, and so on. Furthermore, code that contains syntax errors will not...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT