mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-24 18:08:39 +00:00
Compare commits
4 Commits
209a59ee56
...
793e564e1d
Author | SHA1 | Date | |
---|---|---|---|
|
793e564e1d | ||
|
6939538a41 | ||
|
997d56fb63 | ||
|
44aa17fb86 |
@ -716,6 +716,7 @@
|
|||||||
* [Lru Cache](other/lru_cache.py)
|
* [Lru Cache](other/lru_cache.py)
|
||||||
* [Magicdiamondpattern](other/magicdiamondpattern.py)
|
* [Magicdiamondpattern](other/magicdiamondpattern.py)
|
||||||
* [Maximum Subarray](other/maximum_subarray.py)
|
* [Maximum Subarray](other/maximum_subarray.py)
|
||||||
|
* [Maximum Subsequence](other/maximum_subsequence.py)
|
||||||
* [Nested Brackets](other/nested_brackets.py)
|
* [Nested Brackets](other/nested_brackets.py)
|
||||||
* [Password](other/password.py)
|
* [Password](other/password.py)
|
||||||
* [Quine](other/quine.py)
|
* [Quine](other/quine.py)
|
||||||
|
37
maths/remove_digit.py
Normal file
37
maths/remove_digit.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
def remove_digit(num: int) -> int:
|
||||||
|
"""
|
||||||
|
|
||||||
|
returns the biggest possible result
|
||||||
|
that can be achieved by removing
|
||||||
|
one digit from the given number
|
||||||
|
|
||||||
|
>>> remove_digit(152)
|
||||||
|
52
|
||||||
|
>>> remove_digit(6385)
|
||||||
|
685
|
||||||
|
>>> remove_digit(-11)
|
||||||
|
1
|
||||||
|
>>> remove_digit(2222222)
|
||||||
|
222222
|
||||||
|
>>> remove_digit("2222222")
|
||||||
|
Traceback (most recent call last):
|
||||||
|
TypeError: only integers accepted as input
|
||||||
|
>>> remove_digit("string input")
|
||||||
|
Traceback (most recent call last):
|
||||||
|
TypeError: only integers accepted as input
|
||||||
|
"""
|
||||||
|
|
||||||
|
if not isinstance(num, int):
|
||||||
|
raise TypeError("only integers accepted as input")
|
||||||
|
else:
|
||||||
|
num_str = str(abs(num))
|
||||||
|
num_transpositions = [list(num_str) for char in range(len(num_str))]
|
||||||
|
for index in range(len(num_str)):
|
||||||
|
num_transpositions[index].pop(index)
|
||||||
|
return max(
|
||||||
|
int("".join(list(transposition))) for transposition in num_transpositions
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
__import__("doctest").testmod()
|
42
other/maximum_subsequence.py
Normal file
42
other/maximum_subsequence.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
from collections.abc import Sequence
|
||||||
|
|
||||||
|
|
||||||
|
def max_subsequence_sum(nums: Sequence[int] | None = None) -> int:
|
||||||
|
"""Return the maximum possible sum amongst all non - empty subsequences.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: when nums is empty.
|
||||||
|
|
||||||
|
>>> max_subsequence_sum([1,2,3,4,-2])
|
||||||
|
10
|
||||||
|
>>> max_subsequence_sum([-2, -3, -1, -4, -6])
|
||||||
|
-1
|
||||||
|
>>> max_subsequence_sum([])
|
||||||
|
Traceback (most recent call last):
|
||||||
|
. . .
|
||||||
|
ValueError: Input sequence should not be empty
|
||||||
|
>>> max_subsequence_sum()
|
||||||
|
Traceback (most recent call last):
|
||||||
|
. . .
|
||||||
|
ValueError: Input sequence should not be empty
|
||||||
|
"""
|
||||||
|
if nums is None or not nums:
|
||||||
|
raise ValueError("Input sequence should not be empty")
|
||||||
|
|
||||||
|
ans = nums[0]
|
||||||
|
for i in range(1, len(nums)):
|
||||||
|
num = nums[i]
|
||||||
|
ans = max(ans, ans + num, num)
|
||||||
|
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
|
|
||||||
|
# Try on a sample input from the user
|
||||||
|
n = int(input("Enter number of elements : ").strip())
|
||||||
|
array = list(map(int, input("\nEnter the numbers : ").strip().split()))[:n]
|
||||||
|
print(max_subsequence_sum(array))
|
61
sorts/binary_insertion_sort.py
Normal file
61
sorts/binary_insertion_sort.py
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
"""
|
||||||
|
This is a pure Python implementation of the binary insertion sort algorithm
|
||||||
|
|
||||||
|
For doctests run following command:
|
||||||
|
python -m doctest -v binary_insertion_sort.py
|
||||||
|
or
|
||||||
|
python3 -m doctest -v binary_insertion_sort.py
|
||||||
|
|
||||||
|
For manual testing run:
|
||||||
|
python binary_insertion_sort.py
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def binary_insertion_sort(collection: list) -> list:
|
||||||
|
"""Pure implementation of the binary insertion sort algorithm in Python
|
||||||
|
:param collection: some mutable ordered collection with heterogeneous
|
||||||
|
comparable items inside
|
||||||
|
:return: the same collection ordered by ascending
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
>>> binary_insertion_sort([0, 4, 1234, 4, 1])
|
||||||
|
[0, 1, 4, 4, 1234]
|
||||||
|
>>> binary_insertion_sort([]) == sorted([])
|
||||||
|
True
|
||||||
|
>>> binary_insertion_sort([-1, -2, -3]) == sorted([-1, -2, -3])
|
||||||
|
True
|
||||||
|
>>> lst = ['d', 'a', 'b', 'e', 'c']
|
||||||
|
>>> binary_insertion_sort(lst) == sorted(lst)
|
||||||
|
True
|
||||||
|
>>> import random
|
||||||
|
>>> collection = random.sample(range(-50, 50), 100)
|
||||||
|
>>> binary_insertion_sort(collection) == sorted(collection)
|
||||||
|
True
|
||||||
|
>>> import string
|
||||||
|
>>> collection = random.choices(string.ascii_letters + string.digits, k=100)
|
||||||
|
>>> binary_insertion_sort(collection) == sorted(collection)
|
||||||
|
True
|
||||||
|
"""
|
||||||
|
|
||||||
|
n = len(collection)
|
||||||
|
for i in range(1, n):
|
||||||
|
val = collection[i]
|
||||||
|
low = 0
|
||||||
|
high = i - 1
|
||||||
|
|
||||||
|
while low <= high:
|
||||||
|
mid = (low + high) // 2
|
||||||
|
if val < collection[mid]:
|
||||||
|
high = mid - 1
|
||||||
|
else:
|
||||||
|
low = mid + 1
|
||||||
|
for j in range(i, low, -1):
|
||||||
|
collection[j] = collection[j - 1]
|
||||||
|
collection[low] = val
|
||||||
|
return collection
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
user_input = input("Enter numbers separated by a comma:\n").strip()
|
||||||
|
unsorted = [int(item) for item in user_input.split(",")]
|
||||||
|
print(binary_insertion_sort(unsorted))
|
108
strings/string_switch_case.py
Normal file
108
strings/string_switch_case.py
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
"""
|
||||||
|
general info:
|
||||||
|
https://en.wikipedia.org/wiki/Naming_convention_(programming)#Python_and_Ruby
|
||||||
|
|
||||||
|
pascal case [ an upper Camel Case ]: https://en.wikipedia.org/wiki/Camel_case
|
||||||
|
|
||||||
|
camel case: https://en.wikipedia.org/wiki/Camel_case
|
||||||
|
|
||||||
|
kebab case [ can be found in general info ]:
|
||||||
|
https://en.wikipedia.org/wiki/Naming_convention_(programming)#Python_and_Ruby
|
||||||
|
|
||||||
|
snake case: https://en.wikipedia.org/wiki/Snake_case
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
# assistant functions
|
||||||
|
def split_input(str_: str) -> list:
|
||||||
|
"""
|
||||||
|
>>> split_input("one two 31235three4four")
|
||||||
|
[['one', 'two', '31235three4four']]
|
||||||
|
"""
|
||||||
|
return [char.split() for char in re.split(r"[^ a-z A-Z 0-9 \s]", str_)]
|
||||||
|
|
||||||
|
|
||||||
|
def to_simple_case(str_: str) -> str:
|
||||||
|
"""
|
||||||
|
>>> to_simple_case("one two 31235three4four")
|
||||||
|
'OneTwo31235three4four'
|
||||||
|
"""
|
||||||
|
string_split = split_input(str_)
|
||||||
|
return "".join(
|
||||||
|
["".join([char.capitalize() for char in sub_str]) for sub_str in string_split]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def to_complex_case(text: str, upper: bool, separator: str) -> str:
|
||||||
|
"""
|
||||||
|
>>> to_complex_case("one two 31235three4four", True, "_")
|
||||||
|
'ONE_TWO_31235THREE4FOUR'
|
||||||
|
>>> to_complex_case("one two 31235three4four", False, "-")
|
||||||
|
'one-two-31235three4four'
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
string_split = split_input(text)
|
||||||
|
if upper:
|
||||||
|
res_str = "".join(
|
||||||
|
[
|
||||||
|
separator.join([char.upper() for char in sub_str])
|
||||||
|
for sub_str in string_split
|
||||||
|
]
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
res_str = "".join(
|
||||||
|
[
|
||||||
|
separator.join([char.lower() for char in sub_str])
|
||||||
|
for sub_str in string_split
|
||||||
|
]
|
||||||
|
)
|
||||||
|
return res_str
|
||||||
|
except IndexError:
|
||||||
|
return "not valid string"
|
||||||
|
|
||||||
|
|
||||||
|
# main content
|
||||||
|
def to_pascal_case(text: str) -> str:
|
||||||
|
"""
|
||||||
|
>>> to_pascal_case("one two 31235three4four")
|
||||||
|
'OneTwo31235three4four'
|
||||||
|
"""
|
||||||
|
return to_simple_case(text)
|
||||||
|
|
||||||
|
|
||||||
|
def to_camel_case(text: str) -> str:
|
||||||
|
"""
|
||||||
|
>>> to_camel_case("one two 31235three4four")
|
||||||
|
'oneTwo31235three4four'
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
res_str = to_simple_case(text)
|
||||||
|
return res_str[0].lower() + res_str[1:]
|
||||||
|
except IndexError:
|
||||||
|
return "not valid string"
|
||||||
|
|
||||||
|
|
||||||
|
def to_snake_case(text: str, upper: bool) -> str:
|
||||||
|
"""
|
||||||
|
>>> to_snake_case("one two 31235three4four", True)
|
||||||
|
'ONE_TWO_31235THREE4FOUR'
|
||||||
|
>>> to_snake_case("one two 31235three4four", False)
|
||||||
|
'one_two_31235three4four'
|
||||||
|
"""
|
||||||
|
return to_complex_case(text, upper, "_")
|
||||||
|
|
||||||
|
|
||||||
|
def to_kebab_case(text: str, upper: bool) -> str:
|
||||||
|
"""
|
||||||
|
>>> to_kebab_case("one two 31235three4four", True)
|
||||||
|
'ONE-TWO-31235THREE4FOUR'
|
||||||
|
>>> to_kebab_case("one two 31235three4four", False)
|
||||||
|
'one-two-31235three4four'
|
||||||
|
"""
|
||||||
|
return to_complex_case(text, upper, "-")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
__import__("doctest").testmod()
|
Loading…
x
Reference in New Issue
Block a user