From cbc74e5d4c39ff3e4e3c31194b2b058e9611022e Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 25 Oct 2021 12:42:30 +0000 Subject: [PATCH 01/15] updating DIRECTORY.md --- DIRECTORY.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 13a360ab67f3..268bbbbd2d2e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -10,6 +10,11 @@ * [Newton Raphson](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/newton_raphson.py) * [Secant Method](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/secant_method.py) +## Audio Filters + * [Butterworth Filter](https://github.com/TheAlgorithms/Python/blob/master/audio_filters/butterworth_filter.py) + * [Iir Filter](https://github.com/TheAlgorithms/Python/blob/master/audio_filters/iir_filter.py) + * [Show Response](https://github.com/TheAlgorithms/Python/blob/master/audio_filters/show_response.py) + ## Backtracking * [All Combinations](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_combinations.py) * [All Permutations](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_permutations.py) @@ -60,6 +65,7 @@ * [Base64 Encoding](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base64_encoding.py) * [Base85](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base85.py) * [Beaufort Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/beaufort_cipher.py) + * [Bifid](https://github.com/TheAlgorithms/Python/blob/master/ciphers/bifid.py) * [Brute Force Caesar Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/brute_force_caesar_cipher.py) * [Caesar Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/caesar_cipher.py) * [Cryptomath Module](https://github.com/TheAlgorithms/Python/blob/master/ciphers/cryptomath_module.py) @@ -106,8 +112,8 @@ ## Conversions * [Binary To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/binary_to_decimal.py) - * [Binary To Octal](https://github.com/TheAlgorithms/Python/blob/master/conversions/binary_to_octal.py) * [Binary To Hexadecimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/binary_to_hexadecimal.py) + * [Binary To Octal](https://github.com/TheAlgorithms/Python/blob/master/conversions/binary_to_octal.py) * [Decimal To Any](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_any.py) * [Decimal To Binary](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_binary.py) * [Decimal To Binary Recursion](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_binary_recursion.py) @@ -352,6 +358,7 @@ * [Multi Heuristic Astar](https://github.com/TheAlgorithms/Python/blob/master/graphs/multi_heuristic_astar.py) * [Page Rank](https://github.com/TheAlgorithms/Python/blob/master/graphs/page_rank.py) * [Prim](https://github.com/TheAlgorithms/Python/blob/master/graphs/prim.py) + * [Random Graph Generator](https://github.com/TheAlgorithms/Python/blob/master/graphs/random_graph_generator.py) * [Scc Kosaraju](https://github.com/TheAlgorithms/Python/blob/master/graphs/scc_kosaraju.py) * [Strongly Connected Components](https://github.com/TheAlgorithms/Python/blob/master/graphs/strongly_connected_components.py) * [Tarjans Scc](https://github.com/TheAlgorithms/Python/blob/master/graphs/tarjans_scc.py) From def3bf6c4627f6749a45b4fc8d00429f5479f432 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 26 Oct 2021 08:03:57 +0000 Subject: [PATCH 02/15] updating DIRECTORY.md --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 268bbbbd2d2e..170515d7cdfa 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -53,6 +53,7 @@ ## Cellular Automata * [Conways Game Of Life](https://github.com/TheAlgorithms/Python/blob/master/cellular_automata/conways_game_of_life.py) * [Game Of Life](https://github.com/TheAlgorithms/Python/blob/master/cellular_automata/game_of_life.py) + * [Nagel Schrekenberg](https://github.com/TheAlgorithms/Python/blob/master/cellular_automata/nagel_schrekenberg.py) * [One Dimensional](https://github.com/TheAlgorithms/Python/blob/master/cellular_automata/one_dimensional.py) ## Ciphers @@ -944,6 +945,7 @@ * [Capitalize](https://github.com/TheAlgorithms/Python/blob/master/strings/capitalize.py) * [Check Anagrams](https://github.com/TheAlgorithms/Python/blob/master/strings/check_anagrams.py) * [Check Pangram](https://github.com/TheAlgorithms/Python/blob/master/strings/check_pangram.py) + * [Credit Card Validator](https://github.com/TheAlgorithms/Python/blob/master/strings/credit_card_validator.py) * [Detecting English Programmatically](https://github.com/TheAlgorithms/Python/blob/master/strings/detecting_english_programmatically.py) * [Frequency Finder](https://github.com/TheAlgorithms/Python/blob/master/strings/frequency_finder.py) * [Indian Phone Validator](https://github.com/TheAlgorithms/Python/blob/master/strings/indian_phone_validator.py) @@ -961,6 +963,7 @@ * [Rabin Karp](https://github.com/TheAlgorithms/Python/blob/master/strings/rabin_karp.py) * [Remove Duplicate](https://github.com/TheAlgorithms/Python/blob/master/strings/remove_duplicate.py) * [Reverse Letters](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_letters.py) + * [Reverse Long Words](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_long_words.py) * [Reverse Words](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_words.py) * [Split](https://github.com/TheAlgorithms/Python/blob/master/strings/split.py) * [Upper](https://github.com/TheAlgorithms/Python/blob/master/strings/upper.py) From 9cec6fc16f12d963b20286cac0e85be261d41646 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 26 Oct 2021 13:36:15 +0530 Subject: [PATCH 03/15] Create get_top_hn_posts.py --- web_programming/get_top_hn_posts.py | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 web_programming/get_top_hn_posts.py diff --git a/web_programming/get_top_hn_posts.py b/web_programming/get_top_hn_posts.py new file mode 100644 index 000000000000..14265bbd390f --- /dev/null +++ b/web_programming/get_top_hn_posts.py @@ -0,0 +1,30 @@ +import requests +from texttable import Texttable + + +def stories(): + top_stories = "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty" + + data = requests.get(top_stories) + top_10 = data.json()[:10] + + table_data = [ + ["Title", "URL"], + ] + + for story_id in top_10: + story_url = ( + f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty" + ) + story_content = requests.get(story_url).json() + content = [story_content["title"], story_content["url"]] + table_data.append(content) + + table = Texttable() + table.set_cols_dtype(["t", "a"]) + table.add_rows(table_data) + print(table.draw()) + + +if __name__ == "__main__": + stories() From c69cd40952368c0a8b7263d6016ad262d4c5ada1 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 26 Oct 2021 08:06:35 +0000 Subject: [PATCH 04/15] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 170515d7cdfa..e08729d3ed60 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -988,6 +988,7 @@ * [Fetch Jobs](https://github.com/TheAlgorithms/Python/blob/master/web_programming/fetch_jobs.py) * [Get Imdb Top 250 Movies Csv](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_imdb_top_250_movies_csv.py) * [Get Imdbtop](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_imdbtop.py) + * [Get Top Hn Posts](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_top_hn_posts.py) * [Giphy](https://github.com/TheAlgorithms/Python/blob/master/web_programming/giphy.py) * [Instagram Crawler](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_crawler.py) * [Instagram Pic](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_pic.py) From 36794a6f96785dd16cc0864920bef2468c67fcf3 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 26 Oct 2021 13:43:41 +0530 Subject: [PATCH 05/15] Add return type and desc --- web_programming/get_top_hn_posts.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/web_programming/get_top_hn_posts.py b/web_programming/get_top_hn_posts.py index 14265bbd390f..0148158bb491 100644 --- a/web_programming/get_top_hn_posts.py +++ b/web_programming/get_top_hn_posts.py @@ -2,7 +2,13 @@ from texttable import Texttable -def stories(): +def stories() -> None: + """ + Get the top 10 latest posts from HackerNews and display + them as a table inside the terminal + https://news.ycombinator.com/ + """ + top_stories = "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty" data = requests.get(top_stories) From b94c541836e2548e5e20cb502c786b055dd45941 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 26 Oct 2021 13:50:00 +0530 Subject: [PATCH 06/15] Add texttable --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 7c2672ae25d3..c28238a0774f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,6 +14,7 @@ sklearn statsmodels sympy tensorflow +texttable tweepy types-requests xgboost From 704856ac28b6aab991ffd8dd0f55770d0ba654b8 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 26 Oct 2021 14:43:56 +0530 Subject: [PATCH 07/15] Update web_programming/get_top_hn_posts.py Co-authored-by: Christian Clauss --- web_programming/get_top_hn_posts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/get_top_hn_posts.py b/web_programming/get_top_hn_posts.py index 0148158bb491..8033b5236a3e 100644 --- a/web_programming/get_top_hn_posts.py +++ b/web_programming/get_top_hn_posts.py @@ -2,7 +2,7 @@ from texttable import Texttable -def stories() -> None: +def hackernews_top_stories(max_stories: int = 10) -> dict: """ Get the top 10 latest posts from HackerNews and display them as a table inside the terminal From dc822cdca6fdfab99b6c8edcd2b26e551dbc9c58 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 26 Oct 2021 14:47:37 +0530 Subject: [PATCH 08/15] Update web_programming/get_top_hn_posts.py Co-authored-by: Christian Clauss --- web_programming/get_top_hn_posts.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/web_programming/get_top_hn_posts.py b/web_programming/get_top_hn_posts.py index 8033b5236a3e..ab8482955c62 100644 --- a/web_programming/get_top_hn_posts.py +++ b/web_programming/get_top_hn_posts.py @@ -11,8 +11,7 @@ def hackernews_top_stories(max_stories: int = 10) -> dict: top_stories = "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty" - data = requests.get(top_stories) - top_10 = data.json()[:10] + top_10 = requests.get(top_stories).json()[:10] table_data = [ ["Title", "URL"], From 2f1a5cfb0baa01ad380eabdfefe6b94a9bc5d144 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 26 Oct 2021 14:53:11 +0530 Subject: [PATCH 09/15] Get top 10 posts --- web_programming/get_top_hn_posts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/get_top_hn_posts.py b/web_programming/get_top_hn_posts.py index ab8482955c62..a271e2c801e3 100644 --- a/web_programming/get_top_hn_posts.py +++ b/web_programming/get_top_hn_posts.py @@ -4,7 +4,7 @@ def hackernews_top_stories(max_stories: int = 10) -> dict: """ - Get the top 10 latest posts from HackerNews and display + Get the top 10 posts from HackerNews and display them as a table inside the terminal https://news.ycombinator.com/ """ From 78abd6b1cf67ea207fbf92d6d1d906b534cd8102 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 26 Oct 2021 15:03:35 +0530 Subject: [PATCH 10/15] Update get_top_hn_posts.py --- web_programming/get_top_hn_posts.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web_programming/get_top_hn_posts.py b/web_programming/get_top_hn_posts.py index a271e2c801e3..553880a210ab 100644 --- a/web_programming/get_top_hn_posts.py +++ b/web_programming/get_top_hn_posts.py @@ -11,7 +11,7 @@ def hackernews_top_stories(max_stories: int = 10) -> dict: top_stories = "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty" - top_10 = requests.get(top_stories).json()[:10] + top_10 = requests.get(top_stories).json()[:max_stories] table_data = [ ["Title", "URL"], @@ -32,4 +32,4 @@ def hackernews_top_stories(max_stories: int = 10) -> dict: if __name__ == "__main__": - stories() + hackernews_top_stories() From db1682f657431da45e261c6065095a933b7bfc00 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 26 Oct 2021 16:21:12 +0530 Subject: [PATCH 11/15] Don't use texttable --- web_programming/get_top_hn_posts.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/web_programming/get_top_hn_posts.py b/web_programming/get_top_hn_posts.py index 553880a210ab..b56a1a231a6f 100644 --- a/web_programming/get_top_hn_posts.py +++ b/web_programming/get_top_hn_posts.py @@ -1,8 +1,7 @@ import requests -from texttable import Texttable -def hackernews_top_stories(max_stories: int = 10) -> dict: +def hackernews_top_stories(max_stories: int = 10) -> list: """ Get the top 10 posts from HackerNews and display them as a table inside the terminal @@ -13,9 +12,7 @@ def hackernews_top_stories(max_stories: int = 10) -> dict: top_10 = requests.get(top_stories).json()[:max_stories] - table_data = [ - ["Title", "URL"], - ] + table_data = [] for story_id in top_10: story_url = ( @@ -25,10 +22,10 @@ def hackernews_top_stories(max_stories: int = 10) -> dict: content = [story_content["title"], story_content["url"]] table_data.append(content) - table = Texttable() - table.set_cols_dtype(["t", "a"]) - table.add_rows(table_data) - print(table.draw()) + for row in table_data: + print(f"{'-' * 150} \n | {row[0]} \n | {row[1]}") + + return table_data if __name__ == "__main__": From 836a7840c7bdfeb78b4d8687f9eb5c8f459c33ff Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 26 Oct 2021 16:42:30 +0530 Subject: [PATCH 12/15] Setup doctest --- web_programming/get_top_hn_posts.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/web_programming/get_top_hn_posts.py b/web_programming/get_top_hn_posts.py index b56a1a231a6f..58832780a436 100644 --- a/web_programming/get_top_hn_posts.py +++ b/web_programming/get_top_hn_posts.py @@ -1,11 +1,19 @@ import requests -def hackernews_top_stories(max_stories: int = 10) -> list: +def hackernews_top_stories(max_stories: int = 10) -> int: """ Get the top 10 posts from HackerNews and display them as a table inside the terminal https://news.ycombinator.com/ + + >>> max_stories = 10 + >>> hackernews_top_stories(max_stories) + 10 + + >>> max_stories = 5 + >>> hackernews_top_stories(max_stories) + 5 """ top_stories = "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty" @@ -25,7 +33,7 @@ def hackernews_top_stories(max_stories: int = 10) -> list: for row in table_data: print(f"{'-' * 150} \n | {row[0]} \n | {row[1]}") - return table_data + return len(table_data) if __name__ == "__main__": From 5aac414efb3d3059269a3720b3440c7fd5d3fcaa Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 26 Oct 2021 16:47:19 +0530 Subject: [PATCH 13/15] Fix pre-commit issues --- web_programming/get_top_hn_posts.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web_programming/get_top_hn_posts.py b/web_programming/get_top_hn_posts.py index 58832780a436..ecbf7e8b670e 100644 --- a/web_programming/get_top_hn_posts.py +++ b/web_programming/get_top_hn_posts.py @@ -6,11 +6,11 @@ def hackernews_top_stories(max_stories: int = 10) -> int: Get the top 10 posts from HackerNews and display them as a table inside the terminal https://news.ycombinator.com/ - + >>> max_stories = 10 >>> hackernews_top_stories(max_stories) 10 - + >>> max_stories = 5 >>> hackernews_top_stories(max_stories) 5 From 0607d979ae0cb26cbadb46d449ea56c075d9d939 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 26 Oct 2021 17:00:38 +0530 Subject: [PATCH 14/15] Remove print statement --- web_programming/get_top_hn_posts.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/web_programming/get_top_hn_posts.py b/web_programming/get_top_hn_posts.py index ecbf7e8b670e..f1494d8d5cf5 100644 --- a/web_programming/get_top_hn_posts.py +++ b/web_programming/get_top_hn_posts.py @@ -30,9 +30,6 @@ def hackernews_top_stories(max_stories: int = 10) -> int: content = [story_content["title"], story_content["url"]] table_data.append(content) - for row in table_data: - print(f"{'-' * 150} \n | {row[0]} \n | {row[1]}") - return len(table_data) From 77922cac3db34002bbba0214b209b2903f3cc25e Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 26 Oct 2021 15:07:57 +0200 Subject: [PATCH 15/15] Add hackernews_top_stories_as_markdown() --- web_programming/get_top_hn_posts.py | 41 +++++++++++------------------ 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/web_programming/get_top_hn_posts.py b/web_programming/get_top_hn_posts.py index f1494d8d5cf5..fbb7c051a88e 100644 --- a/web_programming/get_top_hn_posts.py +++ b/web_programming/get_top_hn_posts.py @@ -1,37 +1,26 @@ +from __future__ import annotations + import requests -def hackernews_top_stories(max_stories: int = 10) -> int: - """ - Get the top 10 posts from HackerNews and display - them as a table inside the terminal - https://news.ycombinator.com/ +def get_hackernews_story(story_id: str) -> dict: + url = f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty" + return requests.get(url).json() - >>> max_stories = 10 - >>> hackernews_top_stories(max_stories) - 10 - >>> max_stories = 5 - >>> hackernews_top_stories(max_stories) - 5 +def hackernews_top_stories(max_stories: int = 10) -> list[dict]: """ + Get the top max_stories posts from HackerNews - https://news.ycombinator.com/ + """ + url = "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty" + story_ids = requests.get(url).json()[:max_stories] + return [get_hackernews_story(story_id) for story_id in story_ids] - top_stories = "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty" - - top_10 = requests.get(top_stories).json()[:max_stories] - - table_data = [] - - for story_id in top_10: - story_url = ( - f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty" - ) - story_content = requests.get(story_url).json() - content = [story_content["title"], story_content["url"]] - table_data.append(content) - return len(table_data) +def hackernews_top_stories_as_markdown(max_stories: int = 10) -> str: + stories = hackernews_top_stories(max_stories) + return "\n".join("* [{title}]({url})".format(**story) for story in stories) if __name__ == "__main__": - hackernews_top_stories() + print(hackernews_top_stories_as_markdown())