From 76dec73df29fbcc294c6d8bb75dde9afa12a2073 Mon Sep 17 00:00:00 2001 From: Siddharth1605 Date: Tue, 2 Jan 2024 09:10:11 +0530 Subject: [PATCH 01/11] wiki has been updated --- Python.wiki/Home.md | 6 ++ Python.wiki/Sorting-Algorithms.md | 93 +++++++++++++++++++++++++++++++ Python.wiki/_Footer.md | 1 + 3 files changed, 100 insertions(+) create mode 100644 Python.wiki/Home.md create mode 100644 Python.wiki/Sorting-Algorithms.md create mode 100644 Python.wiki/_Footer.md diff --git a/Python.wiki/Home.md b/Python.wiki/Home.md new file mode 100644 index 000000000000..765420b7b2ae --- /dev/null +++ b/Python.wiki/Home.md @@ -0,0 +1,6 @@ +## Python Algorithms +- All the algorithms are implemented in **Python** for **education purpose** only.
+- These are just for **demonstration** purpose.
+- There are many implementations of different algorithms in the Python standard library that are much better for performance reasons.
+ +List of Algorithms diff --git a/Python.wiki/Sorting-Algorithms.md b/Python.wiki/Sorting-Algorithms.md new file mode 100644 index 000000000000..709cea5d4204 --- /dev/null +++ b/Python.wiki/Sorting-Algorithms.md @@ -0,0 +1,93 @@ +

index

+ +
+ +
    +
  1. Selection Sort
  2. +
  3. Bubble Sort
  4. +
  5. Recursive Bubble Sort
  6. +
  7. Insertion Sort
  8. +
  9. Recursive Insertion Sort
  10. +
  11. Merge Sort
  12. +
  13. Iterative Merge Sort
  14. +
  15. Quick Sort
  16. +
  17. Iterative Quick Sort
  18. +
  19. Heap Sort
  20. +
  21. Counting Sort
  22. +
  23. Radix Sort
  24. +
  25. Bucket Sort
  26. +
  27. ShellSort
  28. +
  29. TimSort
  30. +
  31. Comb Sort
  32. +
  33. Pigeonhole Sort
  34. +
  35. Cycle Sort
  36. +
  37. Cocktail Sort
  38. +
  39. Strand Sort
  40. +
  41. Bitonic Sort
  42. +
  43. Pancake sorting
  44. +
  45. Binary Insertion Sort
  46. +
  47. BogoSort or Permutation Sort
  48. +
  49. Gnome Sort
  50. +
  51. Sleep Sort – The King of Laziness / Sorting while Sleeping
  52. +
  53. Structure Sorting (By Multiple Rules)
  54. +
  55. Stooge Sort
  56. +
  57. Tag Sort (To get both sorted and original)
  58. +
  59. Tree Sort
  60. +
  61. Cartesian Tree Sorting
  62. +
  63. Odd-Even Sort / Brick Sort
  64. +
  65. QuickSort on Singly Linked List
  66. +
  67. QuickSort on Doubly Linked List
  68. +
  69. 3-Way QuickSort (Dutch National Flag)
  70. +
  71. Merge Sort for Linked Lists
  72. +
  73. Merge Sort for Doubly Linked List
  74. +
  75. 3-way Merge Sort
  76. +
+ +*** + +

Selection sort

+ +**Selection sort**, algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from the unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. +
    +
  1. The subarray which is already sorted.
  2. +
  3. Remaining subarray which is unsorted.
  4. + +In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray. + +#### Properties: +- Worst-case performance O(n^2) +- Best-case performance O(n) +- Average case performance O(n^2) + +**read more at: [Wikipedia](https://en.wikipedia.org/wiki/Selection_sort)**
    +**code: [Selection sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/selection_sort.py)** + +*** + + +

    Bubble sort

    + +**Bubble sort**, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort. It can be practical if the input is usually in sorted order but may occasionally have some out-of-order elements nearly in position. + +#### Properties: +- Worst-case performance O(n^2) +- Best-case performance O(n) +- Average case performance O(n^2) + +**read more at: [Wikipedia](https://en.wikipedia.org/wiki/Bubble_sort)**
    +**code: [Bubble sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/bubble_sort.py)** + +*** + + +

    Insertion sort

    + +**Insertion sort** is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. + +#### Properties: +- Worst-case performance O(n^2) +- Best-case performance O(n) +- Average case performance O(n^2) + +**read more at: [Wikipedia](https://en.wikipedia.org/wiki/Insertion_sort)**
    +**code: [Insertion sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/insertion_sort.py)** diff --git a/Python.wiki/_Footer.md b/Python.wiki/_Footer.md new file mode 100644 index 000000000000..b304535c2357 --- /dev/null +++ b/Python.wiki/_Footer.md @@ -0,0 +1 @@ +

    Contribution | join at Gitter!

    \ No newline at end of file From cc28eed98a0404f41616e827d7ce2bf7a1f7f056 Mon Sep 17 00:00:00 2001 From: Charlie Miller <35689954+MarlieChiller@users.noreply.github.com> Date: Tue, 2 Jan 2024 10:36:54 +0000 Subject: [PATCH 02/11] Update get_top_billionaires.py to fix doctest failure --- web_programming/get_top_billionaires.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/get_top_billionaires.py b/web_programming/get_top_billionaires.py index 6f986acb9181..13de55264738 100644 --- a/web_programming/get_top_billionaires.py +++ b/web_programming/get_top_billionaires.py @@ -29,7 +29,7 @@ def calculate_age(unix_date: float) -> str: >>> from datetime import datetime, UTC >>> years_since_create = datetime.now(tz=UTC).year - 2022 >>> int(calculate_age(-657244800000)) - years_since_create - 73 + 72 >>> int(calculate_age(46915200000)) - years_since_create 51 """ From 7d55e21dae73cb2d4dfcdfe8ef6cc6d0612ecb57 Mon Sep 17 00:00:00 2001 From: Charlie Miller <35689954+MarlieChiller@users.noreply.github.com> Date: Tue, 2 Jan 2024 10:49:06 +0000 Subject: [PATCH 03/11] Update get_top_billionaires.py --- web_programming/get_top_billionaires.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/get_top_billionaires.py b/web_programming/get_top_billionaires.py index 13de55264738..0b6473fd6217 100644 --- a/web_programming/get_top_billionaires.py +++ b/web_programming/get_top_billionaires.py @@ -31,7 +31,7 @@ def calculate_age(unix_date: float) -> str: >>> int(calculate_age(-657244800000)) - years_since_create 72 >>> int(calculate_age(46915200000)) - years_since_create - 51 + 50 """ # Convert date from milliseconds to seconds unix_date /= 1000 From 553eb820b5a880f109ce646197f6a05e6c606534 Mon Sep 17 00:00:00 2001 From: Charlie Miller <35689954+MarlieChiller@users.noreply.github.com> Date: Tue, 2 Jan 2024 10:51:50 +0000 Subject: [PATCH 04/11] Update get_top_billionaires.py --- web_programming/get_top_billionaires.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/get_top_billionaires.py b/web_programming/get_top_billionaires.py index 0b6473fd6217..13de55264738 100644 --- a/web_programming/get_top_billionaires.py +++ b/web_programming/get_top_billionaires.py @@ -31,7 +31,7 @@ def calculate_age(unix_date: float) -> str: >>> int(calculate_age(-657244800000)) - years_since_create 72 >>> int(calculate_age(46915200000)) - years_since_create - 50 + 51 """ # Convert date from milliseconds to seconds unix_date /= 1000 From c7866491ec3de74bc7f55c9b1cf69896645f727e Mon Sep 17 00:00:00 2001 From: Charlie Miller <35689954+MarlieChiller@users.noreply.github.com> Date: Tue, 2 Jan 2024 10:55:59 +0000 Subject: [PATCH 05/11] Update get_top_billionaires.py --- web_programming/get_top_billionaires.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/get_top_billionaires.py b/web_programming/get_top_billionaires.py index 13de55264738..0b6473fd6217 100644 --- a/web_programming/get_top_billionaires.py +++ b/web_programming/get_top_billionaires.py @@ -31,7 +31,7 @@ def calculate_age(unix_date: float) -> str: >>> int(calculate_age(-657244800000)) - years_since_create 72 >>> int(calculate_age(46915200000)) - years_since_create - 51 + 50 """ # Convert date from milliseconds to seconds unix_date /= 1000 From aae2164cb23a3c5733f2a90542fa705a58fdcbd0 Mon Sep 17 00:00:00 2001 From: Charlie Miller <35689954+MarlieChiller@users.noreply.github.com> Date: Wed, 3 Jan 2024 11:50:54 +0000 Subject: [PATCH 06/11] Update get_top_billionaires.py Fixed calculate_age function such that is returns the age correctly from a given timestamp. Fixed doctests to be robust when year changes --- web_programming/get_top_billionaires.py | 37 +++++++++++-------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/web_programming/get_top_billionaires.py b/web_programming/get_top_billionaires.py index 0b6473fd6217..6120742efd32 100644 --- a/web_programming/get_top_billionaires.py +++ b/web_programming/get_top_billionaires.py @@ -20,36 +20,31 @@ ) -def calculate_age(unix_date: float) -> str: +def calculate_age(unix_timestamp: float) -> str: """Calculates age from given unix time format. Returns: Age as string >>> from datetime import datetime, UTC - >>> years_since_create = datetime.now(tz=UTC).year - 2022 - >>> int(calculate_age(-657244800000)) - years_since_create - 72 - >>> int(calculate_age(46915200000)) - years_since_create - 50 + >>> 2024 - int(calculate_age(946684800)) + 2000 + >>> 2024 - int(calculate_age(-2145703316)) + 1902 + >>> 2024 - int(calculate_age(2209202284)) + 2040 """ # Convert date from milliseconds to seconds - unix_date /= 1000 - - if unix_date < 0: - # Handle timestamp before epoch - epoch = datetime.fromtimestamp(0, tz=UTC) - seconds_since_epoch = (datetime.now(tz=UTC) - epoch).seconds - birthdate = ( - epoch - timedelta(seconds=abs(unix_date) - seconds_since_epoch) - ).date() - else: - birthdate = datetime.fromtimestamp(unix_date, tz=UTC).date() - return str( - TODAY.year - - birthdate.year - - ((TODAY.month, TODAY.day) < (birthdate.month, birthdate.day)) + birth_date = datetime.fromtimestamp(unix_timestamp, tz=UTC) + + # Calculate the age + current_date = datetime.now(tz=UTC) + age = ( + current_date.year - + birth_date.year - + ((current_date.month, current_date.day) < (birth_date.month, birth_date.day)) ) + return str(age) def get_forbes_real_time_billionaires() -> list[dict[str, str]]: From 9c0424c75bf990dc236beb68b5caa5ccb4c9b96a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 3 Jan 2024 11:51:25 +0000 Subject: [PATCH 07/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/get_top_billionaires.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/web_programming/get_top_billionaires.py b/web_programming/get_top_billionaires.py index 6120742efd32..976084e2db45 100644 --- a/web_programming/get_top_billionaires.py +++ b/web_programming/get_top_billionaires.py @@ -40,9 +40,9 @@ def calculate_age(unix_timestamp: float) -> str: # Calculate the age current_date = datetime.now(tz=UTC) age = ( - current_date.year - - birth_date.year - - ((current_date.month, current_date.day) < (birth_date.month, birth_date.day)) + current_date.year + - birth_date.year + - ((current_date.month, current_date.day) < (birth_date.month, birth_date.day)) ) return str(age) From aa5885e92afb2c3c99049fd8f3cbad33e2a78b03 Mon Sep 17 00:00:00 2001 From: Charlie Miller <35689954+MarlieChiller@users.noreply.github.com> Date: Wed, 3 Jan 2024 11:52:40 +0000 Subject: [PATCH 08/11] removed unused import removed unused import --- web_programming/get_top_billionaires.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/get_top_billionaires.py b/web_programming/get_top_billionaires.py index 976084e2db45..41da95a00b2e 100644 --- a/web_programming/get_top_billionaires.py +++ b/web_programming/get_top_billionaires.py @@ -3,7 +3,7 @@ This works for some of us but fails for others. """ -from datetime import UTC, datetime, timedelta +from datetime import UTC, datetime import requests from rich import box From 655419a7f73ca4c1d4a0e4cbfeb8a648e7f00ab4 Mon Sep 17 00:00:00 2001 From: Charlie Miller <35689954+MarlieChiller@users.noreply.github.com> Date: Wed, 3 Jan 2024 13:48:48 +0000 Subject: [PATCH 09/11] removed redundant comment --- web_programming/get_top_billionaires.py | 1 - 1 file changed, 1 deletion(-) diff --git a/web_programming/get_top_billionaires.py b/web_programming/get_top_billionaires.py index 41da95a00b2e..1b27c9379213 100644 --- a/web_programming/get_top_billionaires.py +++ b/web_programming/get_top_billionaires.py @@ -34,7 +34,6 @@ def calculate_age(unix_timestamp: float) -> str: >>> 2024 - int(calculate_age(2209202284)) 2040 """ - # Convert date from milliseconds to seconds birth_date = datetime.fromtimestamp(unix_timestamp, tz=UTC) # Calculate the age From d8b299d671c8eca9ff7fc0e2679aab0dc7a17e6b Mon Sep 17 00:00:00 2001 From: Charlie Miller <35689954+MarlieChiller@users.noreply.github.com> Date: Mon, 15 Jan 2024 09:24:18 +0000 Subject: [PATCH 10/11] Update get_top_billionaires.py --- web_programming/get_top_billionaires.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/web_programming/get_top_billionaires.py b/web_programming/get_top_billionaires.py index 281dceec4997..ec9d41f8225a 100644 --- a/web_programming/get_top_billionaires.py +++ b/web_programming/get_top_billionaires.py @@ -26,11 +26,11 @@ def years_old(unix_timestamp: float) -> str: Age as string >>> from datetime import datetime, UTC - >>> 2024 - int(calculate_age(946684800)) + >>> 2024 - int(years_old(946684800)) 2000 - >>> 2024 - int(calculate_age(-2145703316)) + >>> 2024 - int(years_old(-2145703316)) 1902 - >>> 2024 - int(calculate_age(2209202284)) + >>> 2024 - int(years_old(2209202284)) 2040 """ birth_date = datetime.fromtimestamp(unix_timestamp, tz=UTC) From 72c1ae367430def0c1732f1ecfec1aa21825dfd7 Mon Sep 17 00:00:00 2001 From: Charlie Miller <35689954+MarlieChiller@users.noreply.github.com> Date: Mon, 15 Jan 2024 14:03:15 +0000 Subject: [PATCH 11/11] Update get_top_billionaires.py --- web_programming/get_top_billionaires.py | 47 ++++++++++++++----------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/web_programming/get_top_billionaires.py b/web_programming/get_top_billionaires.py index ec9d41f8225a..703b635eef82 100644 --- a/web_programming/get_top_billionaires.py +++ b/web_programming/get_top_billionaires.py @@ -3,7 +3,7 @@ This works for some of us but fails for others. """ -from datetime import UTC, datetime +from datetime import UTC, date, datetime import requests from rich import box @@ -19,30 +19,35 @@ ) -def years_old(unix_timestamp: float) -> str: - """Calculates age from given unix time format. +def years_old(birth_timestamp: int, today: date | None = None) -> int: + """ + Calculate the age in years based on the given birth date. Only the year, month, + and day are used in the calculation. The time of day is ignored. + + Args: + birth_timestamp: The date of birth. + today: (useful for writing tests) or if None then datetime.date.today(). Returns: - Age as string - - >>> from datetime import datetime, UTC - >>> 2024 - int(years_old(946684800)) - 2000 - >>> 2024 - int(years_old(-2145703316)) - 1902 - >>> 2024 - int(years_old(2209202284)) - 2040 + int: The age in years. + + Examples: + >>> today = date(2024, 1, 12) + >>> years_old(birth_timestamp=datetime(1959, 11, 20).timestamp(), today=today) + 64 + >>> years_old(birth_timestamp=datetime(1970, 2, 13).timestamp(), today=today) + 53 + >>> all( + ... years_old(datetime(today.year - i, 1, 12).timestamp(), today=today) == i + ... for i in range(1, 111) + ... ) + True """ - birth_date = datetime.fromtimestamp(unix_timestamp, tz=UTC) - - # Calculate the age - current_date = datetime.now(tz=UTC) - age = ( - current_date.year - - birth_date.year - - ((current_date.month, current_date.day) < (birth_date.month, birth_date.day)) + today = today or TODAY.date() + birth_date = datetime.fromtimestamp(birth_timestamp, tz=UTC).date() + return (today.year - birth_date.year) - ( + (today.month, today.day) < (birth_date.month, birth_date.day) ) - return str(age) def get_forbes_real_time_billionaires() -> list[dict[str, int | str]]: