Skip to content

Commit 644e9ca

Browse files
authored
Merge branch 'TheAlgorithms:master' into master
2 parents 5be2a1f + 598f6a2 commit 644e9ca

File tree

4 files changed

+171
-50
lines changed

4 files changed

+171
-50
lines changed
+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
"""
2+
Author : Alexander Pantyukhin
3+
Date : November 1, 2022
4+
5+
Task:
6+
Given a list of days when you need to travel. Each day is integer from 1 to 365.
7+
You are able to use tickets for 1 day, 7 days and 30 days.
8+
Each ticket has a cost.
9+
10+
Find the minimum cost you need to travel every day in the given list of days.
11+
12+
Implementation notes:
13+
implementation Dynamic Programming up bottom approach.
14+
15+
Runtime complexity: O(n)
16+
17+
The implementation was tested on the
18+
leetcode: https://leetcode.com/problems/minimum-cost-for-tickets/
19+
20+
21+
Minimum Cost For Tickets
22+
Dynamic Programming: up -> down.
23+
"""
24+
25+
from functools import lru_cache
26+
27+
28+
def mincost_tickets(days: list[int], costs: list[int]) -> int:
29+
"""
30+
>>> mincost_tickets([1, 4, 6, 7, 8, 20], [2, 7, 15])
31+
11
32+
33+
>>> mincost_tickets([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [2, 7, 15])
34+
17
35+
36+
>>> mincost_tickets([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [2, 90, 150])
37+
24
38+
39+
>>> mincost_tickets([2], [2, 90, 150])
40+
2
41+
42+
>>> mincost_tickets([], [2, 90, 150])
43+
0
44+
45+
>>> mincost_tickets('hello', [2, 90, 150])
46+
Traceback (most recent call last):
47+
...
48+
ValueError: The parameter days should be a list of integers
49+
50+
>>> mincost_tickets([], 'world')
51+
Traceback (most recent call last):
52+
...
53+
ValueError: The parameter costs should be a list of three integers
54+
55+
>>> mincost_tickets([0.25, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [2, 90, 150])
56+
Traceback (most recent call last):
57+
...
58+
ValueError: The parameter days should be a list of integers
59+
60+
>>> mincost_tickets([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [2, 0.9, 150])
61+
Traceback (most recent call last):
62+
...
63+
ValueError: The parameter costs should be a list of three integers
64+
65+
>>> mincost_tickets([-1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [2, 90, 150])
66+
Traceback (most recent call last):
67+
...
68+
ValueError: All days elements should be greater than 0
69+
70+
>>> mincost_tickets([2, 367], [2, 90, 150])
71+
Traceback (most recent call last):
72+
...
73+
ValueError: All days elements should be less than 366
74+
75+
>>> mincost_tickets([2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [])
76+
Traceback (most recent call last):
77+
...
78+
ValueError: The parameter costs should be a list of three integers
79+
80+
>>> mincost_tickets([], [])
81+
Traceback (most recent call last):
82+
...
83+
ValueError: The parameter costs should be a list of three integers
84+
85+
>>> mincost_tickets([2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [1, 2, 3, 4])
86+
Traceback (most recent call last):
87+
...
88+
ValueError: The parameter costs should be a list of three integers
89+
"""
90+
91+
# Validation
92+
if not isinstance(days, list) or not all(isinstance(day, int) for day in days):
93+
raise ValueError("The parameter days should be a list of integers")
94+
95+
if len(costs) != 3 or not all(isinstance(cost, int) for cost in costs):
96+
raise ValueError("The parameter costs should be a list of three integers")
97+
98+
if len(days) == 0:
99+
return 0
100+
101+
if min(days) <= 0:
102+
raise ValueError("All days elements should be greater than 0")
103+
104+
if max(days) >= 366:
105+
raise ValueError("All days elements should be less than 366")
106+
107+
days_set = set(days)
108+
109+
@lru_cache(maxsize=None)
110+
def dynamic_programming(index: int) -> int:
111+
if index > 365:
112+
return 0
113+
114+
if index not in days_set:
115+
return dynamic_programming(index + 1)
116+
117+
return min(
118+
costs[0] + dynamic_programming(index + 1),
119+
costs[1] + dynamic_programming(index + 7),
120+
costs[2] + dynamic_programming(index + 30),
121+
)
122+
123+
return dynamic_programming(1)
124+
125+
126+
if __name__ == "__main__":
127+
import doctest
128+
129+
doctest.testmod()

other/check_strong_password.py

-47
This file was deleted.

other/password_generator.py renamed to other/password.py

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
"""Password Generator allows you to generate a random password of length N."""
21
import secrets
32
from random import shuffle
4-
from string import ascii_letters, digits, punctuation
3+
from string import ascii_letters, ascii_lowercase, ascii_uppercase, digits, punctuation
54

65

76
def password_generator(length: int = 8) -> str:
87
"""
8+
Password Generator allows you to generate a random password of length N.
9+
910
>>> len(password_generator())
1011
8
1112
>>> len(password_generator(length=16))
@@ -62,6 +63,45 @@ def random_characters(chars_incl, i):
6263
pass # Put your code here...
6364

6465

66+
# This Will Check Whether A Given Password Is Strong Or Not
67+
# It Follows The Rule that Length Of Password Should Be At Least 8 Characters
68+
# And At Least 1 Lower, 1 Upper, 1 Number And 1 Special Character
69+
def strong_password_detector(password: str, min_length: int = 8) -> str:
70+
"""
71+
>>> strong_password_detector('Hwea7$2!')
72+
'This is a strong Password'
73+
74+
>>> strong_password_detector('Sh0r1')
75+
'Your Password must be at least 8 characters long'
76+
77+
>>> strong_password_detector('Hello123')
78+
'Password should contain UPPERCASE, lowercase, numbers, special characters'
79+
80+
>>> strong_password_detector('Hello1238udfhiaf038fajdvjjf!jaiuFhkqi1')
81+
'This is a strong Password'
82+
83+
>>> strong_password_detector('0')
84+
'Your Password must be at least 8 characters long'
85+
"""
86+
87+
if len(password) < min_length:
88+
return "Your Password must be at least 8 characters long"
89+
90+
upper = any(char in ascii_uppercase for char in password)
91+
lower = any(char in ascii_lowercase for char in password)
92+
num = any(char in digits for char in password)
93+
spec_char = any(char in punctuation for char in password)
94+
95+
if upper and lower and num and spec_char:
96+
return "This is a strong Password"
97+
98+
else:
99+
return (
100+
"Password should contain UPPERCASE, lowercase, "
101+
"numbers, special characters"
102+
)
103+
104+
65105
def main():
66106
length = int(input("Please indicate the max length of your password: ").strip())
67107
chars_incl = input(

requirements.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
beautifulsoup4
2-
cython>=0.29.28 # For statsmodels on Python 3.11
32
fake_useragent
43
keras
54
lxml

0 commit comments

Comments
 (0)