Created
October 19, 2017 17:44
-
-
Save Bovojon/0e77b3ae9bd4caeee3494afcf0034c0d to your computer and use it in GitHub Desktop.
Given a stream of href links, backward operations, or forward operations, output a stream of the full url the browser should visit after each event in the input stream.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Given a stream of href links, backward operations, or forward operations, output | |
a stream of the full url the browser should visit after each event in the input stream. | |
''' | |
def full_url(paths): | |
''' | |
In this implementation, I am assuming that a user cannot go backwards more | |
than once consequentively. | |
''' | |
if paths == '': | |
print("Please provide the strings") | |
else: | |
path_list = paths.split("\n") | |
for path in path_list: | |
if "https://" in path: | |
visits = [] | |
visits.append(path) | |
print path | |
elif path == "BACK": | |
try: | |
if len(visits) > 0: | |
previous = len(visits) - 2 | |
back = visits[previous] | |
visits.append("B") | |
print back | |
except: | |
print "Sorry, nothing to go back to..." | |
elif path == "FORWARD": | |
try: | |
previous = len(visits) - 1 | |
if len(visits) > 0 and visits[previous] == "B": | |
forward = visits[previous-1] | |
print forward | |
except: | |
print "Sorry, nothing to go forward to..." | |
visits.pop(previous) | |
else: | |
previous = len(visits) - 1 | |
if visits[previous] == "B": | |
visits.pop(previous) | |
if path[0] == "/": | |
new_path = visits[0] + path | |
visits.append(new_path) | |
print new_path | |
else: | |
latest = len(visits) | |
new_path = visits[latest-1] + "/" + path | |
visits.append(new_path) | |
print new_path | |
if __name__ == '__main__': | |
full_url("https://google.com\n/search\nquery\nterm/kittens\n/calendar/today\nevents/mine\nhttps://reddit.com\nr/kittens\nBACK\nFORWARD\nnew") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment