From 57907bcda1fd7019858e06d149473c926902bbbe Mon Sep 17 00:00:00 2001 From: merwanehamadi Date: Mon, 4 Sep 2023 16:31:01 -0700 Subject: [PATCH] Url shortener challenge (#353) Signed-off-by: Merwane Hamadi --- .../3_url_shortener/artifacts_out/__init__.py | 0 .../3_url_shortener/artifacts_out/test.py | 15 ++++++++ .../artifacts_out/url_shortener.py | 37 +++++++++++++++++++ .../verticals/code/3_url_shortener/data.json | 21 +++++++++++ 4 files changed, 73 insertions(+) create mode 100644 agbenchmark/challenges/verticals/code/3_url_shortener/artifacts_out/__init__.py create mode 100644 agbenchmark/challenges/verticals/code/3_url_shortener/artifacts_out/test.py create mode 100644 agbenchmark/challenges/verticals/code/3_url_shortener/artifacts_out/url_shortener.py create mode 100644 agbenchmark/challenges/verticals/code/3_url_shortener/data.json diff --git a/agbenchmark/challenges/verticals/code/3_url_shortener/artifacts_out/__init__.py b/agbenchmark/challenges/verticals/code/3_url_shortener/artifacts_out/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/agbenchmark/challenges/verticals/code/3_url_shortener/artifacts_out/test.py b/agbenchmark/challenges/verticals/code/3_url_shortener/artifacts_out/test.py new file mode 100644 index 00000000..94fcac02 --- /dev/null +++ b/agbenchmark/challenges/verticals/code/3_url_shortener/artifacts_out/test.py @@ -0,0 +1,15 @@ +import unittest +from url_shortener import shorten_url, retrieve_url + +class TestURLShortener(unittest.TestCase): + def test_url_retrieval(self): + # Shorten the URL to get its shortened form + shortened_url = shorten_url('https://www.example.com') + + # Retrieve the original URL using the shortened URL directly + retrieved_url = retrieve_url(shortened_url) + + self.assertEqual(retrieved_url, 'https://www.example.com', "Retrieved URL does not match the original!") + +if __name__ == "__main__": + unittest.main() diff --git a/agbenchmark/challenges/verticals/code/3_url_shortener/artifacts_out/url_shortener.py b/agbenchmark/challenges/verticals/code/3_url_shortener/artifacts_out/url_shortener.py new file mode 100644 index 00000000..985f9afe --- /dev/null +++ b/agbenchmark/challenges/verticals/code/3_url_shortener/artifacts_out/url_shortener.py @@ -0,0 +1,37 @@ +import argparse +import sys +import base64 + +URL_MAPPING = {} + +def shorten_url(url): + # Convert the URL to base64 + encoded_url = base64.b64encode(url.encode()).decode() + # Take the first 8 characters of the encoded URL as our shortened URL + short_url = encoded_url[:8] + # Map the shortened URL back to the original + URL_MAPPING[short_url] = url + return short_url + +def retrieve_url(short_url): + return URL_MAPPING.get(short_url, "URL not found") + +def main(): + parser = argparse.ArgumentParser(description="URL Shortener") + parser.add_argument('-s', '--shorten', type=str, help="URL to be shortened") + parser.add_argument('-r', '--retrieve', type=str, help="Short URL to be retrieved") + + args = parser.parse_args() + + if args.shorten: + shortened_url = shorten_url(args.shorten) + print(shortened_url) + # Directly retrieve after shortening, using the newly shortened URL + print(retrieve_url(shortened_url)) + elif args.retrieve: + print(retrieve_url(args.retrieve)) + else: + print("No valid arguments provided.") + +if __name__ == "__main__": + main() diff --git a/agbenchmark/challenges/verticals/code/3_url_shortener/data.json b/agbenchmark/challenges/verticals/code/3_url_shortener/data.json new file mode 100644 index 00000000..e39fa5f6 --- /dev/null +++ b/agbenchmark/challenges/verticals/code/3_url_shortener/data.json @@ -0,0 +1,21 @@ +{ + "name": "TestUrlShortener", + "category": ["code"], + "task": "Build a basic URL shortener using a python CLI. Here are the specifications.\n\nFunctionality: The program should have two primary functionalities.\n\nShorten a given URL.\nRetrieve the original URL from a shortened URL.\n\nCLI: The command-line interface should accept the URL to be shortened as its first input. After shortening, it should display ONLY the shortened URL, and it will prompt a url to access.\n\nYour primary requirements are:\n\nPrompt the user for the long url.\nReturn the shortened url.\nPrompt the user for a shortened url.\nReturn the long url.\n\nTechnical specifications:\nBuild a file called url_shortener.py. This file will be called through command lines.\n\nEdge cases:\nFor the sake of simplicity, there will be no edge cases, you can assume the input is always correct and the user immediately passes the shortened version of the url he just shortened.\n\nYou will be expected to create a python file called url_shortener.py that will run through command lines by using python url_shortener.py.\n\nThe url_shortener.py game will be tested this way:\n```\nimport unittest\nfrom url_shortener import shorten_url, retrieve_url\n\nclass TestURLShortener(unittest.TestCase):\n def test_url_retrieval(self):\n # Shorten the URL to get its shortened form\n shortened_url = shorten_url('https://www.example.com')\n\n # Retrieve the original URL using the shortened URL directly\n retrieved_url = retrieve_url(shortened_url)\n\n self.assertEqual(retrieved_url, 'https://www.example.com', \"Retrieved URL does not match the original!\")\n\nif __name__ == \"__main__\":\n unittest.main()\n```", + "dependencies": [], + "cutoff": 150, + "ground": { + "answer": "The correct python file for a basic url shortener CLI", + "should_contain": [], + "should_not_contain": [], + "files": ["test.py"], + "eval": { + "type": "python" + } + }, + "info": { + "difficulty": "basic", + "description": "Tests ability for the agent to create a URL shortener.", + "side_effects": [] + } +}