# -*- coding: utf-8 -*-
# ~ Jesse K. Rubin ~ Pretty Useful Python
from collections import Counter
from math import acos
from math import factorial
from math import pi
from math import sqrt
from operator import add
from operator import floordiv
from operator import methodcaller
from operator import sub
from operator import truediv
from typing import Any
from typing import Iterator
from typing import List
from typing import Optional
from typing import Set
from typing import Tuple
from typing import Type
from typing import Union
from pupy.decorations import cash_it
from pupy.foreign import iter_product
def partitions_gen(
numero: int, min_p: int = 1, max_p: Optional[int] = None
) -> Iterator[
Union[Tuple[int, int], Tuple[int, int, int], Tuple[int], Tuple[int, int, int, int]]
]:
"""Partitions generator
Adapted from: code.activestate.com/recipes/218332-generator-for-integer-partitions/min_p
:param numero: number for which to yield partiton tuples
:type numero: int
:param min_p: smallest part size (Default value = 1)
:type min_p: int
:param max_p: largest part size (Default value = None)
:type max_p: int
.. docstring::python
>>> list(partitions_gen(4))
[(4,), (1, 3), (1, 1, 2), (1, 1, 1, 1), (2, 2)]
>>> list(partitions_gen(4, min_p=1, max_p=2))
[(1, 1, 2), (1, 1, 1, 1), (2, 2)]
"""
if max_p is None or max_p >= numero:
yield (numero,)
for i in range(min_p, numero // 2 + 1):
for p in partitions_gen(numero - i, i, max_p):
yield (i,) + p
@cash_it
def rfactorial(n: int) -> int:
"""Recursive factorial function
:param n:
.. docstring::python
>>> from math import factorial
>>> rfactorial(1) == factorial(1)
True
>>> rfactorial(2) == factorial(2)
True
>>> rfactorial(3) == factorial(3)
True
>>> rfactorial(4) == factorial(4)
True
>>> rfactorial(5) == factorial(5)
True
>>> rfactorial(6) == factorial(6)
True
>>> rfactorial(7) == factorial(7)
True
>>> rfactorial(8) == factorial(8)
True
>>> rfactorial(9) == factorial(9)
True
"""
if n == 1:
return 1
else:
return rfactorial(n - 1) * n
def radians_2_degrees(rads: float) -> float:
"""Converts radians to degrees
:param rads:
.. doctest:: python
>>> from math import pi
>>> radians_2_degrees(2*pi) == 360.0
True
>>> radians_2_degrees(2*pi)
360.0
"""
return 180 * rads / pi
def degrees_2_radians(degs: float) -> float:
"""Converts degrees to radians
:param degs:
.. doctest:: python
>>> from math import pi
>>> degrees_2_radians(360.0) == 2*pi
True
"""
return degs * pi / 180
def power_mod(number: int, exponent: int, mod: int) -> int:
"""
:param number:
:param exponent:
:param mod:
.. doctest:: python
>>> power_mod(2, 4, 3)
2
>>> power_mod(12, 3, 4)
144
>>> power_mod(123, 2, 3)
123
>>> power_mod(120, 6, 10)
14400
>>> power_mod(120, 6, 1)
14400
"""
if exponent > 0:
if exponent % 2 == 0:
return power_mod(number, floordiv(exponent, 2), mod)
return power_mod(number, floordiv(exponent, 2), mod) * number
else:
return 1
def divisors_gen(n: int) -> Iterator[int]:
"""Divisors generator
:param n: number w/ divisors to be generated
:type n: int
.. doctest:: python
>>> list(divisors_gen(1))
[1]
>>> list(divisors_gen(4))
[1, 2, 4]
>>> list(divisors_gen(16))
[1, 2, 4, 8, 16]
"""
large_divisors = []
for i in range(1, int(sqrt(n) + 1)):
if n % i == 0:
yield i
if i * i != n:
large_divisors.append(n // i)
for divisor in reversed(large_divisors):
yield divisor
def gcd_it(a: int, b: int) -> int:
"""iterative gcd
:param a:
:param b:
>>> from pupy.maths import gcd_it
>>> from pupy.maths import gcd_r
>>> gcd_it(1, 4) == gcd_r(1, 4)
True
>>> gcd_it(2, 6) == gcd_r(2, 6)
True
>>> gcd_it(3, 14) == gcd_r(3, 14)
True
>>> gcd_it(4, 300) == gcd_r(4, 300)
True
"""
while a:
a, b = b % a, a
return b
@cash_it
def gcd_r(a: int, b: int) -> int:
"""recursive greatest common divisor
:param a:
:param b:
.. doctest:: python
>>> from pupy.maths import gcd_it
>>> from pupy.maths import gcd_r
>>> gcd_it(1, 4) == gcd_r(1, 4)
True
>>> gcd_it(2, 6) == gcd_r(2, 6)
True
>>> gcd_it(3, 14) == gcd_r(3, 14)
True
>>> gcd_it(4, 300) == gcd_r(4, 300)
True
"""
if b > a:
return gcd_r(b, a)
r = a % b
if r == 0:
return b
return gcd_r(r, b)
def reverse(n: int) -> int:
"""Reverses a number
:param n: number to be reversed
:type n: int
:returns: reversed of a number
:rtype: int
.. doctest:: python
>>> reverse(1)
1
>>> reverse(12345)
54321
>>> reverse(54321)
12345
>>> reverse(54321000)
12345
"""
reversed = 0
while n > 0:
reversed *= 10
reversed += n % 10
n //= 10
return reversed
@cash_it
def fib_r(n: int) -> int:
"""Recursively the nth fibonacci number
:param n: nth fibonacci sequence number
:type n: int
:returns: the nth fibonacci number
:rtype: int
.. doctest:: python
>>> fib_r(1)
1
>>> fib_r(2)
2
>>> fib_r(6)
13
"""
return n if n < 3 else fib_r(n - 1) + fib_r(n - 2)
def expo(d: int, n: int) -> int:
"""greatest exponent for a divisor of n
:param d: divisor
:type d: int
:param n: number be divided
:type n: int
:returns: number of times a divisor divides n
:rtype: int
.. doctest:: python
>>> expo(100, 2)
2
>>> expo(12, 5)
0
>>> expo(12, 2)
2
>>> expo(160, 4)
2
>>> expo(1000, 4)
1
"""
if n < d: # flip
d, n = n, d
c = n
divs = 0
while c % d == 0:
c //= d
divs += 1
return divs
def pytriple_gen(max_c: int) -> Iterator[Tuple[int, int, int]]:
"""primative pythagorean triples generator
special thanks to 3Blue1Brown's video on pythagorean triples
https://www.youtube.com/watch?v=QJYmyhnaaek&t=300s
:param max_c: max value of c to yeild triples up to
:type max_c: int
"""
for real_pts in range(2, int(sqrt(max_c)) + 1, 1):
for imag_pts in range(real_pts % 2 + 1, real_pts, 2):
comp = complex(real_pts, imag_pts)
sqrd = comp * comp
real = int(sqrd.real)
imag = int(sqrd.imag)
if abs(real - imag) % 2 == 1 and gcd_it(imag, real) == 1:
sea = int((comp * comp.conjugate()).real)
if sea > max_c:
break
else:
yield (imag, real, sea) if real > imag else (real, imag, sea)
def n_permutations_with_replacements(
it: Union[
Tuple[int, int, int, int],
Tuple[int, int, int, int, int, int, int],
Tuple[int, int, int, int, int],
Tuple[int, int, int, int, int, int],
]
) -> int:
"""
.. doctest:: python
>>> n_permutations_with_replacements((1, 2, 3, 4))
24
>>> n_permutations_with_replacements((1, 2, 3, 4, 3))
60
>>> n_permutations_with_replacements((1, 2, 3, 4, 3, 3))
120
>>> n_permutations_with_replacements((1, 2, 3, 4, 3, 4, 4))
420
"""
c = Counter(n for n in it)
a =
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共32个文件
py:17个
pyi:12个
pkg-info:1个
资源分类:Python库 所属语言:Python 资源全名:pupy-2.11.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
pupy-2.11.0.tar.gz (32个子文件)
pupy-2.11.0
PKG-INFO 566B
pyproject.toml 442B
LICENSE 1KB
setup.py 500B
pupy
foreign.pyi 932B
foreign.py 7KB
cli.pyi 220B
win.pyi 499B
string_cheese.pyi 467B
savings_n_loads.py 7KB
maths.py 17KB
decorations.py 6KB
_typing.py 213B
utils.py 8KB
cli.py 1KB
lin.pyi 539B
savings_n_loads.pyi 1KB
ui.pyi 317B
win.py 3KB
maths.pyi 4KB
__main__.py 155B
__init__.py 1KB
amazon_prime.pyi 1021B
string_cheese.py 4KB
sodoku.pyi 1KB
utils.pyi 2KB
_version.py 328B
amazon_prime.py 9KB
decorations.pyi 587B
sodoku.py 8KB
ui.py 2KB
lin.py 3KB
共 32 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功