Skip to content

Commit 036bedb

Browse files
Merge branch 'TheAlgorithms:master' into master
2 parents e27c028 + 533767f commit 036bedb

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
Author : Sanjay Muthu <https://github.com/XenoBytesX>
3+
4+
This is a pure Python implementation of Dynamic Programming solution to the longest
5+
increasing subsequence of a given sequence.
6+
7+
The problem is:
8+
Given an array, to find the longest and increasing sub-array in that given array and
9+
return it.
10+
11+
Example:
12+
``[10, 22, 9, 33, 21, 50, 41, 60, 80]`` as input will return
13+
``[10, 22, 33, 50, 60, 80]`` as output
14+
"""
15+
16+
from __future__ import annotations
17+
18+
import copy
19+
20+
21+
def longest_subsequence(array: list[int]) -> list[int]:
22+
"""
23+
Some examples
24+
25+
>>> longest_subsequence([10, 22, 9, 33, 21, 50, 41, 60, 80])
26+
[10, 22, 33, 50, 60, 80]
27+
>>> longest_subsequence([4, 8, 7, 5, 1, 12, 2, 3, 9])
28+
[1, 2, 3, 9]
29+
>>> longest_subsequence([9, 8, 7, 6, 5, 7])
30+
[7, 7]
31+
>>> longest_subsequence([28, 26, 12, 23, 35, 39])
32+
[12, 23, 35, 39]
33+
>>> longest_subsequence([1, 1, 1])
34+
[1, 1, 1]
35+
>>> longest_subsequence([])
36+
[]
37+
"""
38+
n = len(array)
39+
# The longest increasing subsequence ending at array[i]
40+
longest_increasing_subsequence = []
41+
for i in range(n):
42+
longest_increasing_subsequence.append([array[i]])
43+
44+
for i in range(1, n):
45+
for prev in range(i):
46+
# If array[prev] is less than or equal to array[i], then
47+
# longest_increasing_subsequence[prev] + array[i]
48+
# is a valid increasing subsequence
49+
50+
# longest_increasing_subsequence[i] is only set to
51+
# longest_increasing_subsequence[prev] + array[i] if the length is longer.
52+
53+
if array[prev] <= array[i] and len(
54+
longest_increasing_subsequence[prev]
55+
) + 1 > len(longest_increasing_subsequence[i]):
56+
longest_increasing_subsequence[i] = copy.copy(
57+
longest_increasing_subsequence[prev]
58+
)
59+
longest_increasing_subsequence[i].append(array[i])
60+
61+
result: list[int] = []
62+
for i in range(n):
63+
if len(longest_increasing_subsequence[i]) > len(result):
64+
result = longest_increasing_subsequence[i]
65+
66+
return result
67+
68+
69+
if __name__ == "__main__":
70+
import doctest
71+
72+
doctest.testmod()

graphics/butterfly_pattern.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
def butterfly_pattern(n: int) -> str:
2+
"""
3+
Creates a butterfly pattern of size n and returns it as a string.
4+
5+
>>> print(butterfly_pattern(3))
6+
* *
7+
** **
8+
*****
9+
** **
10+
* *
11+
>>> print(butterfly_pattern(5))
12+
* *
13+
** **
14+
*** ***
15+
**** ****
16+
*********
17+
**** ****
18+
*** ***
19+
** **
20+
* *
21+
"""
22+
result = []
23+
24+
# Upper part
25+
for i in range(1, n):
26+
left_stars = "*" * i
27+
spaces = " " * (2 * (n - i) - 1)
28+
right_stars = "*" * i
29+
result.append(left_stars + spaces + right_stars)
30+
31+
# Middle part
32+
result.append("*" * (2 * n - 1))
33+
34+
# Lower part
35+
for i in range(n - 1, 0, -1):
36+
left_stars = "*" * i
37+
spaces = " " * (2 * (n - i) - 1)
38+
right_stars = "*" * i
39+
result.append(left_stars + spaces + right_stars)
40+
41+
return "\n".join(result)
42+
43+
44+
if __name__ == "__main__":
45+
n = int(input("Enter the size of the butterfly pattern: "))
46+
print(butterfly_pattern(n))

other/doomsday.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def get_week_day(year: int, month: int, day: int) -> str:
4646
) % 7
4747
day_anchor = (
4848
DOOMSDAY_NOT_LEAP[month - 1]
49-
if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0)
49+
if year % 4 != 0 or (centurian == 0 and year % 400 != 0)
5050
else DOOMSDAY_LEAP[month - 1]
5151
)
5252
week_day = (dooms_day + day - day_anchor) % 7

0 commit comments

Comments
 (0)