Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

3.9 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69b1028d6e265413d0198a2f Challenge 236: Browser History 29 challenge-236

--description--

Given an array of browser commands, return an array with two values: the history as an array of URLs, and the index of the current page.

Valid commands are:

  • "URL" - Where URL is a web address ("freecodecamp.org" for example). Navigates to the given URL, adds it to the history at the next position, and discards any forward history.
  • "Back" - moves to the previous page in history, or stays on the current page if there isn't one.
  • "Forward" - moves to the next page in history, or stays on the current page if there isn't one.

For example, given ["freecodecamp.org", "freecodecamp.org/learn", "Back"], return [["freecodecamp.org", "freecodecamp.org/learn"], 0].

--hints--

get_browser_history(["freecodecamp.org", "freecodecamp.org/learn", "Back"]) should return [["freecodecamp.org", "freecodecamp.org/learn"], 0].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["freecodecamp.org", "freecodecamp.org/learn", "Back"]), [["freecodecamp.org", "freecodecamp.org/learn"], 0])`)
}})

get_browser_history(["example.com", "example.com/about", "example.com/contact", "example.com/blog"]) should return [["example.com", "example.com/about", "example.com/contact", "example.com/blog"], 3].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["example.com", "example.com/about", "example.com/contact", "example.com/blog"]), [["example.com", "example.com/about", "example.com/contact", "example.com/blog"], 3])`)
}})

get_browser_history(["example.com", "example.com/about", "Back", "example.com/contact", "example.com/blog", "Back", "Back", "Forward"]) should return [["example.com", "example.com/contact", "example.com/blog"], 1].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["example.com", "example.com/about", "Back", "example.com/contact", "example.com/blog", "Back", "Back", "Forward"]), [["example.com", "example.com/contact", "example.com/blog"], 1])`)
}})

get_browser_history(["example.com", "example.com/about", "example.com/contact", "example.com/blog", "Back", "Back", "Forward", "freecodecamp.org"]) should return [["example.com", "example.com/about", "example.com/contact", "freecodecamp.org"], 3].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["example.com", "example.com/about", "example.com/contact", "example.com/blog", "Back", "Back", "Forward", "freecodecamp.org"]), [["example.com", "example.com/about", "example.com/contact", "freecodecamp.org"], 3])`)
}})

get_browser_history(["example.com", "example.com/about", "Back", "Back"]) should return [["example.com", "example.com/about"], 0].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["example.com", "example.com/about", "Back", "Back"]), [["example.com", "example.com/about"], 0])`)
}})

get_browser_history(["example.com", "example.com/about", "Forward"]) should return [["example.com", "example.com/about"], 1].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["example.com", "example.com/about", "Forward"]), [["example.com", "example.com/about"], 1])`)
}})

--seed--

--seed-contents--

def get_browser_history(commands):

    return commands

--solutions--

def get_browser_history(commands):
    stack = []
    index = -1

    for command in commands:
        if command == "Back":
            if index > 0:
                index -= 1
        elif command == "Forward":
            if index < len(stack) - 1:
                index += 1
        else:
            del stack[index + 1:]
            stack.append(command)
            index += 1

    return [stack, index]