Skip to content

Commit 133838c

Browse files
committed
Merge branch 'master' of https://github.com/TheAlgorithms/Python
2 parents b4e33ab + 6a39545 commit 133838c

File tree

4 files changed

+111
-13
lines changed

4 files changed

+111
-13
lines changed

digital_image_processing/change_contrast.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@
1111
from PIL import Image
1212

1313

14-
def change_contrast(img: Image, level: float) -> Image:
14+
def change_contrast(img: Image, level: int) -> Image:
1515
"""
1616
Function to change contrast
1717
"""
1818
factor = (259 * (level + 255)) / (255 * (259 - level))
1919

20-
def contrast(c: int) -> float:
20+
def contrast(c: int) -> int:
2121
"""
2222
Fundamental Transformation/Operation that'll be performed on
2323
every bit.
2424
"""
25-
return 128 + factor * (c - 128)
25+
return int(128 + factor * (c - 128))
2626

2727
return img.point(contrast)
2828

project_euler/problem_112/__init__.py

Whitespace-only changes.

project_euler/problem_112/sol1.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""
2+
Problem 112: https://projecteuler.net/problem=112
3+
4+
Working from left-to-right if no digit is exceeded by the digit to its left it is
5+
called an increasing number; for example, 134468.
6+
Similarly if no digit is exceeded by the digit to its right it is called a decreasing
7+
number; for example, 66420.
8+
We shall call a positive integer that is neither increasing nor decreasing a "bouncy"
9+
number, for example, 155349.
10+
Clearly there cannot be any bouncy numbers below one-hundred, but just over half of
11+
the numbers below one-thousand (525) are bouncy. In fact, the least number for which
12+
the proportion of bouncy numbers first reaches 50% is 538.
13+
Surprisingly, bouncy numbers become more and more common and by the time we reach
14+
21780 the proportion of bouncy numbers is equal to 90%.
15+
16+
Find the least number for which the proportion of bouncy numbers is exactly 99%.
17+
"""
18+
19+
20+
def check_bouncy(n: int) -> bool:
21+
"""
22+
Returns True if number is bouncy, False otherwise
23+
>>> check_bouncy(6789)
24+
False
25+
>>> check_bouncy(-12345)
26+
False
27+
>>> check_bouncy(0)
28+
False
29+
>>> check_bouncy(6.74)
30+
Traceback (most recent call last):
31+
...
32+
ValueError: check_bouncy() accepts only integer arguments
33+
>>> check_bouncy(132475)
34+
True
35+
>>> check_bouncy(34)
36+
False
37+
>>> check_bouncy(341)
38+
True
39+
>>> check_bouncy(47)
40+
False
41+
>>> check_bouncy(-12.54)
42+
Traceback (most recent call last):
43+
...
44+
ValueError: check_bouncy() accepts only integer arguments
45+
>>> check_bouncy(-6548)
46+
True
47+
"""
48+
if not isinstance(n, int):
49+
raise ValueError("check_bouncy() accepts only integer arguments")
50+
return "".join(sorted(str(n))) != str(n) and "".join(sorted(str(n)))[::-1] != str(n)
51+
52+
53+
def solution(percent: float = 99) -> int:
54+
"""
55+
Returns the least number for which the proportion of bouncy numbers is
56+
exactly 'percent'
57+
>>> solution(50)
58+
538
59+
>>> solution(90)
60+
21780
61+
>>> solution(80)
62+
4770
63+
>>> solution(105)
64+
Traceback (most recent call last):
65+
...
66+
ValueError: solution() only accepts values from 0 to 100
67+
>>> solution(100.011)
68+
Traceback (most recent call last):
69+
...
70+
ValueError: solution() only accepts values from 0 to 100
71+
"""
72+
if not 0 < percent < 100:
73+
raise ValueError("solution() only accepts values from 0 to 100")
74+
bouncy_num = 0
75+
num = 1
76+
77+
while True:
78+
if check_bouncy(num):
79+
bouncy_num += 1
80+
if (bouncy_num / num) * 100 >= percent:
81+
return num
82+
num += 1
83+
84+
85+
if __name__ == "__main__":
86+
from doctest import testmod
87+
88+
testmod()
89+
print(f"{solution(99)}")

sorts/bubble_sort.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@ def bubble_sort(collection):
88
Examples:
99
>>> bubble_sort([0, 5, 2, 3, 2])
1010
[0, 2, 2, 3, 5]
11-
12-
>>> bubble_sort([])
13-
[]
14-
15-
>>> bubble_sort([-2, -45, -5])
16-
[-45, -5, -2]
17-
18-
>>> bubble_sort([-23, 0, 6, -4, 34])
19-
[-23, -4, 0, 6, 34]
20-
11+
>>> bubble_sort([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2])
12+
True
13+
>>> bubble_sort([]) == sorted([])
14+
True
15+
>>> bubble_sort([-2, -45, -5]) == sorted([-2, -45, -5])
16+
True
2117
>>> bubble_sort([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34])
2218
True
19+
>>> bubble_sort(['d', 'a', 'b', 'e', 'c']) == sorted(['d', 'a', 'b', 'e', 'c'])
20+
True
21+
>>> import random
22+
>>> collection = random.sample(range(-50, 50), 100)
23+
>>> bubble_sort(collection) == sorted(collection)
24+
True
25+
>>> import string
26+
>>> collection = random.choices(string.ascii_letters + string.digits, k=100)
27+
>>> bubble_sort(collection) == sorted(collection)
28+
True
2329
"""
2430
length = len(collection)
2531
for i in range(length - 1):
@@ -34,8 +40,11 @@ def bubble_sort(collection):
3440

3541

3642
if __name__ == "__main__":
43+
import doctest
3744
import time
3845

46+
doctest.testmod()
47+
3948
user_input = input("Enter numbers separated by a comma:").strip()
4049
unsorted = [int(item) for item in user_input.split(",")]
4150
start = time.process_time()

0 commit comments

Comments
 (0)