mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 14:56:27 -04:00
abstracted structures fixed
This commit is contained in:
parent
3fdbc2a605
commit
01703751f1
98 changed files with 305 additions and 856 deletions
45
src/USEFUL/basic_examples/example_fractions.py
Normal file
45
src/USEFUL/basic_examples/example_fractions.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
from fractions import Fraction
|
||||
|
||||
def rounding_floats(number1, places):
|
||||
return round(number1, places)
|
||||
|
||||
|
||||
def float_to_fractions(number):
|
||||
return Fraction(*number.as_integer_ratio())
|
||||
|
||||
|
||||
def get_denominator(number1, number2):
|
||||
a = Fraction(number1, number2)
|
||||
return a.denominator
|
||||
|
||||
|
||||
def get_numerator(number1, number2):
|
||||
a = Fraction(number1, number2)
|
||||
return a.numerator
|
||||
|
||||
|
||||
def test_testing_floats(module_name='this module'):
|
||||
number1 = 1.25
|
||||
number2 = 1
|
||||
number3 = -1
|
||||
number4 = 5/4
|
||||
number6 = 6
|
||||
assert(rounding_floats(number1, number2) == 1.2)
|
||||
assert(rounding_floats(number1*10, number3) == 10)
|
||||
assert(float_to_fractions(number1) == number4)
|
||||
assert(get_denominator(number2, number6) == number6)
|
||||
assert(get_numerator(number2, number6) == number2)
|
||||
|
||||
s = 'Tests in {name} have {con}!'
|
||||
print(s.format(name=module_name, con='passed'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_testing_floats()
|
||||
|
||||
|
60
src/USEFUL/basic_examples/example_numpy.py
Normal file
60
src/USEFUL/basic_examples/example_numpy.py
Normal file
|
@ -0,0 +1,60 @@
|
|||
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
import time
|
||||
import numpy as np
|
||||
|
||||
def testing_numpy():
|
||||
''' tests many features of numpy '''
|
||||
ax = np.array([1,2,3])
|
||||
ay = np.array([3,4,5])
|
||||
print(ax)
|
||||
print(ax*2)
|
||||
print(ax+10)
|
||||
print(np.sqrt(ax))
|
||||
print(np.cos(ax))
|
||||
print(ax-ay)
|
||||
print(np.where(ax<2, ax, 10))
|
||||
|
||||
m = np.matrix([ax, ay, ax])
|
||||
print(m)
|
||||
print(m.T)
|
||||
|
||||
grid1 = np.zeros(shape=(10,10), dtype=float)
|
||||
grid2 = np.ones(shape=(10,10), dtype=float)
|
||||
print(grid1)
|
||||
print(grid2)
|
||||
print(grid1[1]+10)
|
||||
print(grid2[:,2]*2)
|
||||
|
||||
|
||||
def trad_version():
|
||||
t1 = time.time()
|
||||
X = range(10000000)
|
||||
Y = range(10000000)
|
||||
Z = []
|
||||
for i in range(len(X)):
|
||||
Z.append(X[i] + Y[i])
|
||||
return time.time() - t1
|
||||
|
||||
def numpy_version():
|
||||
t1 = time.time()
|
||||
X = np.arange(10000000)
|
||||
Y = np.arange(10000000)
|
||||
Z = X + Y
|
||||
return time.time() - t1
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
testing_numpy()
|
||||
print(trad_version())
|
||||
print(numpy_version())
|
||||
|
||||
|
||||
'''
|
||||
3.23564291
|
||||
0.0714290142059
|
||||
'''
|
27
src/USEFUL/basic_examples/example_random.py
Normal file
27
src/USEFUL/basic_examples/example_random.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
import random
|
||||
|
||||
def testing_random():
|
||||
''' testing the module random'''
|
||||
values = [1, 2, 3, 4]
|
||||
print(random.choice(values))
|
||||
print(random.choice(values))
|
||||
print(random.choice(values))
|
||||
print(random.sample(values, 2))
|
||||
print(random.sample(values, 3))
|
||||
|
||||
''' shuffle in place '''
|
||||
random.shuffle(values)
|
||||
print(values)
|
||||
|
||||
''' create random integers '''
|
||||
print(random.randint(0,10))
|
||||
print(random.randint(0,10))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
testing_random()
|
||||
|
37
src/USEFUL/basic_examples/example_sets.py
Normal file
37
src/USEFUL/basic_examples/example_sets.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
def difference(l1):
|
||||
""" return the list with duplicate elements removed """
|
||||
return list(set(l1))
|
||||
|
||||
def intersection(l1, l2):
|
||||
""" return the intersection of two lists """
|
||||
return list(set(l1) & set(l2))
|
||||
|
||||
def union(l1, l2):
|
||||
""" return the union of two lists """
|
||||
return list(set(l1) | set(l2))
|
||||
|
||||
|
||||
def test_sets_operations_with_lists():
|
||||
l1 = [1,2,3,4,5,9,11,15]
|
||||
l2 = [4,5,6,7,8]
|
||||
l3 = []
|
||||
assert(difference(l1) == [1, 2, 3, 4, 5, 9, 11, 15])
|
||||
assert(difference(l2) == [8, 4, 5, 6, 7])
|
||||
assert(intersection(l1, l2) == [4,5])
|
||||
assert(union(l1, l2) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 15])
|
||||
assert(difference(l3) == [])
|
||||
assert(intersection(l3, l2) == l3)
|
||||
assert(sorted(union(l3, l2)) == sorted(l2))
|
||||
print('Tests passed!')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_sets_operations_with_lists()
|
||||
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue