From dffc2122be0be38046b30e69b80b9ee1ef9c26d4 Mon Sep 17 00:00:00 2001 From: Rayvant Sahni <38404580+rayvantsahni@users.noreply.github.com> Date: Mon, 3 Aug 2020 04:11:19 +0530 Subject: [PATCH 1/7] Josephus problem in Python Added the code for the josephus problem in python using circular linked lists. --- maths/josephus_problem.py | 88 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 maths/josephus_problem.py diff --git a/maths/josephus_problem.py b/maths/josephus_problem.py new file mode 100644 index 000000000000..dfbc78bab10c --- /dev/null +++ b/maths/josephus_problem.py @@ -0,0 +1,88 @@ +class Node: + def __init__(self, value): + self.value = value + self.next = None + + def get_value(self): + return self.value + + def get_next(self): + return self.next + +class CircularLinkedList: + def __init__(self, head = None): + self.head = head + + def append(self, new_value): + new_node = Node(new_value) + + if not self.head: + self.head = new_node + self.head.next = new_node + return + else: + new_node.next = self.head + current_node = self.head + while current_node.get_next() != self.head: + current_node = current_node.get_next() + current_node.next = new_node + + def remove_node(self, node_to_remove): + if self.head == node_to_remove and self.head.get_next() == self.head: + self.head.next = None + self.head = None + return + elif self.head == node_to_remove: + current_node = self.head + while current_node.get_next() != self.head: + current_node = current_node.get_next() + current_node.next = self.head.get_next() + self.head = self.head.get_next() + return + else: + current_node = self.head + while current_node.get_next() != node_to_remove: + current_node = current_node.get_next() + current_node.next = current_node.get_next().get_next() + return + + def length(self): + if not self.head: + return 0 + else: + count = 0 + current_node = self.head + while current_node.get_next() != self.head: + count += 1 + current_node = current_node.get_next() + return count + 1 + + def get_survivor(self): + return self.head.get_value() + + def josephus(self, step): + current_node = self.head + while self.length() > 1: + count = 1 + while count != step: + current_node = current_node.get_next() + count += 1 + print("KILLED", current_node.get_value()) + self.remove_node(current_node) + current_node = current_node.get_next() + + + +def main(): + n = int(input("Enter the number of people:\n")) + step = int(input("Enter the number of people to be skipped each time:\n")) + + c = CircularLinkedList() + for i in range(1, n + 1): + c.append(i) + + c.josephus(step) + print("The final survivor is:", c.get_survivor()) + + +main() From 0ded3121c8005121742b652de8a7924c06f57f9b Mon Sep 17 00:00:00 2001 From: Rayvant Sahni <38404580+rayvantsahni@users.noreply.github.com> Date: Mon, 3 Aug 2020 04:21:35 +0530 Subject: [PATCH 2/7] Update josephus_problem.py --- maths/josephus_problem.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/maths/josephus_problem.py b/maths/josephus_problem.py index dfbc78bab10c..372092713206 100644 --- a/maths/josephus_problem.py +++ b/maths/josephus_problem.py @@ -1,3 +1,9 @@ +''' +For more information about this prtoblem- +https://en.wikipedia.org/wiki/Josephus_problem#:~:text=In%20computer%20science%20and%20mathematics,a%20certain%20counting%2Dout%20game +https://www.geogebra.org/m/ExvvrBbR +''' + class Node: def __init__(self, value): self.value = value From 4f88bf17c2679181e4c92820fb705afabe695780 Mon Sep 17 00:00:00 2001 From: Rayvant Sahni <38404580+rayvantsahni@users.noreply.github.com> Date: Mon, 3 Aug 2020 04:38:13 +0530 Subject: [PATCH 3/7] Added World covid19 stats in web programming --- web_programming/world_covid19_stats.py | 36 ++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 web_programming/world_covid19_stats.py diff --git a/web_programming/world_covid19_stats.py b/web_programming/world_covid19_stats.py new file mode 100644 index 000000000000..1c59662b86c1 --- /dev/null +++ b/web_programming/world_covid19_stats.py @@ -0,0 +1,36 @@ +''' +This programs gives the latest statistics related to the situaion of Covid 19 all around the world. +The data is being scrapped from 'https://www.worldometers.info/coronavirus/'. +''' + +def get_stats(): + import requests + from bs4 import BeautifulSoup + + url = "https://www.worldometers.info/coronavirus/" + + page = requests.get(url) + page = page.text + soup = BeautifulSoup(page, 'html.parser') + + print("\033[1m" + "COVID-19 Status of the World" + "\033[0m\n") + + x1 = soup.findAll('h1') + x2 = soup.findAll("div", {"class": "maincounter-number"}) + + for i, j in zip(x1, x2): + print(i.text, j.text) + + x3 = soup.findAll("span", {"class": "panel-title"}) + x4 = soup.findAll("div", {"class": "number-table-main"}) + + for i, j in zip(x3, x4): + _i = i.text.strip() + _j = j.text.strip() + print(_i, _j, sep = ":\n", end = "\n\n") + + +def main(): + get_stats() + +main() From f5337425c1917444f0435a9f4f52d5caf9685b98 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 3 Aug 2020 06:31:49 +0200 Subject: [PATCH 4/7] Delete josephus_problem.py --- maths/josephus_problem.py | 94 --------------------------------------- 1 file changed, 94 deletions(-) delete mode 100644 maths/josephus_problem.py diff --git a/maths/josephus_problem.py b/maths/josephus_problem.py deleted file mode 100644 index 372092713206..000000000000 --- a/maths/josephus_problem.py +++ /dev/null @@ -1,94 +0,0 @@ -''' -For more information about this prtoblem- -https://en.wikipedia.org/wiki/Josephus_problem#:~:text=In%20computer%20science%20and%20mathematics,a%20certain%20counting%2Dout%20game -https://www.geogebra.org/m/ExvvrBbR -''' - -class Node: - def __init__(self, value): - self.value = value - self.next = None - - def get_value(self): - return self.value - - def get_next(self): - return self.next - -class CircularLinkedList: - def __init__(self, head = None): - self.head = head - - def append(self, new_value): - new_node = Node(new_value) - - if not self.head: - self.head = new_node - self.head.next = new_node - return - else: - new_node.next = self.head - current_node = self.head - while current_node.get_next() != self.head: - current_node = current_node.get_next() - current_node.next = new_node - - def remove_node(self, node_to_remove): - if self.head == node_to_remove and self.head.get_next() == self.head: - self.head.next = None - self.head = None - return - elif self.head == node_to_remove: - current_node = self.head - while current_node.get_next() != self.head: - current_node = current_node.get_next() - current_node.next = self.head.get_next() - self.head = self.head.get_next() - return - else: - current_node = self.head - while current_node.get_next() != node_to_remove: - current_node = current_node.get_next() - current_node.next = current_node.get_next().get_next() - return - - def length(self): - if not self.head: - return 0 - else: - count = 0 - current_node = self.head - while current_node.get_next() != self.head: - count += 1 - current_node = current_node.get_next() - return count + 1 - - def get_survivor(self): - return self.head.get_value() - - def josephus(self, step): - current_node = self.head - while self.length() > 1: - count = 1 - while count != step: - current_node = current_node.get_next() - count += 1 - print("KILLED", current_node.get_value()) - self.remove_node(current_node) - current_node = current_node.get_next() - - - -def main(): - n = int(input("Enter the number of people:\n")) - step = int(input("Enter the number of people to be skipped each time:\n")) - - c = CircularLinkedList() - for i in range(1, n + 1): - c.append(i) - - c.josephus(step) - print("The final survivor is:", c.get_survivor()) - - -main() From b2682cbe37c3eb6136fb69013a6b4abeb52db00e Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 3 Aug 2020 06:34:34 +0200 Subject: [PATCH 5/7] Type hints, algorithmic functions should not print Return a dict of world covid19 stats. Move all printing into the main functions. --- web_programming/world_covid19_stats.py | 45 +++++++++++--------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/web_programming/world_covid19_stats.py b/web_programming/world_covid19_stats.py index 1c59662b86c1..0104965aead8 100644 --- a/web_programming/world_covid19_stats.py +++ b/web_programming/world_covid19_stats.py @@ -1,36 +1,27 @@ +#!/usr/bin/env python3 + ''' This programs gives the latest statistics related to the situaion of Covid 19 all around the world. The data is being scrapped from 'https://www.worldometers.info/coronavirus/'. ''' -def get_stats(): - import requests - from bs4 import BeautifulSoup - - url = "https://www.worldometers.info/coronavirus/" +import requests +from bs4 import BeautifulSoup - page = requests.get(url) - page = page.text - soup = BeautifulSoup(page, 'html.parser') - - print("\033[1m" + "COVID-19 Status of the World" + "\033[0m\n") - x1 = soup.findAll('h1') - x2 = soup.findAll("div", {"class": "maincounter-number"}) +def world_covid19_stats(url: str="https://www.worldometers.info/coronavirus/") -> dict: + """ + Return a dict of world covid19 stats + """ + soup = BeautifulSoup(requests.get(url).text, 'html.parser') + keys = soup.findAll('h1') + values = soup.findAll("div", {"class": "maincounter-number"}) + keys += soup.findAll("span", {"class": "panel-title"}) + values += soup.findAll("div", {"class": "number-table-main"}) + return {key.text.strip(): value.text.strip() for key, value in zip(keys, values)} - for i, j in zip(x1, x2): - print(i.text, j.text) - x3 = soup.findAll("span", {"class": "panel-title"}) - x4 = soup.findAll("div", {"class": "number-table-main"}) - - for i, j in zip(x3, x4): - _i = i.text.strip() - _j = j.text.strip() - print(_i, _j, sep = ":\n", end = "\n\n") - - -def main(): - get_stats() - -main() +if __name__ == "__main__": + print("\033[1m" + "COVID-19 Status of the World" + "\033[0m\n") + for key, value in world_covid19_stats().items(): + print(f"{key}\n{value}\n") From 656f31125bdea34198a03f6c2a8667e59fcd0c3f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 3 Aug 2020 07:34:37 +0200 Subject: [PATCH 6/7] Update world_covid19_stats.py --- web_programming/world_covid19_stats.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/web_programming/world_covid19_stats.py b/web_programming/world_covid19_stats.py index 0104965aead8..ca3f75166c42 100644 --- a/web_programming/world_covid19_stats.py +++ b/web_programming/world_covid19_stats.py @@ -1,17 +1,17 @@ #!/usr/bin/env python3 ''' -This programs gives the latest statistics related to the situaion of Covid 19 all around the world. -The data is being scrapped from 'https://www.worldometers.info/coronavirus/'. +Provide the current worldwide COVID-19 statistics. +This data is being scrapped from 'https://www.worldometers.info/coronavirus/'. ''' import requests from bs4 import BeautifulSoup -def world_covid19_stats(url: str="https://www.worldometers.info/coronavirus/") -> dict: +def world_covid19_stats(url: str= "https://www.worldometers.info/coronavirus/") -> dict: """ - Return a dict of world covid19 stats + Return a dict of current worldwide COVID-19 statistics """ soup = BeautifulSoup(requests.get(url).text, 'html.parser') keys = soup.findAll('h1') From 87f928ed9d4ad7b953c852ac1ac130ea571ec9db Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 3 Aug 2020 07:39:09 +0200 Subject: [PATCH 7/7] Update world_covid19_stats.py --- web_programming/world_covid19_stats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/world_covid19_stats.py b/web_programming/world_covid19_stats.py index ca3f75166c42..1907ed5f35f7 100644 --- a/web_programming/world_covid19_stats.py +++ b/web_programming/world_covid19_stats.py @@ -9,7 +9,7 @@ from bs4 import BeautifulSoup -def world_covid19_stats(url: str= "https://www.worldometers.info/coronavirus/") -> dict: +def world_covid19_stats(url: str = "https://www.worldometers.info/coronavirus") -> dict: """ Return a dict of current worldwide COVID-19 statistics """