Add lots of things(including tests)

This commit is contained in:
Rohit Tanwar
2018-02-16 16:09:18 +05:30
parent c7810b00bb
commit fb9176aa91
51 changed files with 308 additions and 7 deletions

5
.gitignore vendored
View File

@ -1,5 +1,4 @@
__pycache__/
tape.py
test/
pytest.ini
website/
ms.py
tape*

2
confg.ini Normal file
View File

@ -0,0 +1,2 @@
[tape]
python_files=*.test.py

View File

@ -10,15 +10,15 @@ for snippet in snippets:
content = f.read()
code = re.search(codeRe,content).group(1).strip()
file_to_write_to = open(f'test/{snippet}/{snippet}.py','w')
test_file = open(f'test/{snippet}/{snippet}_test.py','w')
test_file = open(f'test/{snippet}/{snippet}.test.py','w')
file_to_write_to.write(code)
file_to_write_to.close()
test_file.write('''
test_file.write(f'''
import types,functools
from tape import test
from {snippet} import {snippet}
def {snippet}_test(t):
t.true(isinstance({snippet}, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'{snippet} is a function')
test('Testing {snippet}',{snippet}_test)
'''.format(snippet = snippet).strip())
'''.strip())
test_file.close()

2
test/average/average.py Normal file
View File

@ -0,0 +1,2 @@
def average(*args):
return sum(args, 0.0) / len(args)

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from average import average
def average_test(t):
t.true(isinstance(average, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'average is a function')
test('Testing average',average_test)

View File

@ -0,0 +1,2 @@
def byte_size(string):
return(len(string.encode('utf-8')))

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from byte_size import byte_size
def byte_size_test(t):
t.true(isinstance(byte_size, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'byte_size is a function')
test('Testing byte_size',byte_size_test)

View File

@ -0,0 +1,2 @@
def capitalize(string, lower_rest=False):
return string[:1].upper() + (string[1:].lower() if lower_rest else string[1:])

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from capitalize import capitalize
def capitalize_test(t):
t.true(isinstance(capitalize, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'capitalize is a function')
test('Testing capitalize',capitalize_test)

View File

@ -0,0 +1,2 @@
def capitalize_every_word(string):
return string.title()

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from capitalize_every_word import capitalize_every_word
def capitalize_every_word_test(t):
t.true(isinstance(capitalize_every_word, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'capitalize_every_word is a function')
test('Testing capitalize_every_word',capitalize_every_word_test)

7
test/chunk/chunk.py Normal file
View File

@ -0,0 +1,7 @@
from math import ceil
def chunk(arr, size):
return list(
map(lambda x: arr[x * size:x * size + size],
list(range(0, ceil(len(arr) / size)))))

6
test/chunk/chunk.test.py Normal file
View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from chunk import chunk
def chunk_test(t):
t.true(isinstance(chunk, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'chunk is a function')
test('Testing chunk',chunk_test)

2
test/compact/compact.py Normal file
View File

@ -0,0 +1,2 @@
def compact(arr):
return list(filter(lambda x: bool(x), arr))

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from compact import compact
def compact_test(t):
t.true(isinstance(compact, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'compact is a function')
test('Testing compact',compact_test)

View File

@ -0,0 +1,6 @@
def count_by(arr, fn=lambda x: x):
key = {}
for el in map(fn, arr):
key[el] = 0 if not el in key else key[el]
key[el] += 1
return key

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from count_by import count_by
def count_by_test(t):
t.true(isinstance(count_by, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'count_by is a function')
test('Testing count_by',count_by_test)

View File

@ -0,0 +1,4 @@
def count_occurences(arr, val):
return reduce(
(lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0),
arr)

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from count_occurences import count_occurences
def count_occurences_test(t):
t.true(isinstance(count_occurences, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'count_occurences is a function')
test('Testing count_occurences',count_occurences_test)

View File

@ -0,0 +1,5 @@
import re
def count_vowels(str):
return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE)))

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from count_vowels import count_vowels
def count_vowels_test(t):
t.true(isinstance(count_vowels, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'count_vowels is a function')
test('Testing count_vowels',count_vowels_test)

View File

@ -0,0 +1,2 @@
def decapitalize(string, upper_rest=False):
return str[:1].lower() + (str[1:].upper() if upper_rest else str[1:])

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from decapitalize import decapitalize
def decapitalize_test(t):
t.true(isinstance(decapitalize, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'decapitalize is a function')
test('Testing decapitalize',decapitalize_test)

View File

@ -0,0 +1,15 @@
def spread(arg):
ret = []
for i in arg:
if isinstance(i, list):
ret.extend(i)
else:
ret.append(i)
return ret
def deep_flatten(arr):
result = []
result.extend(
spread(list(map(lambda x: deep(x) if type(x) == list else x, arr))))
return result

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from deep_flatten import deep_flatten
def deep_flatten_test(t):
t.true(isinstance(deep_flatten, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'deep_flatten is a function')
test('Testing deep_flatten',deep_flatten_test)

View File

@ -0,0 +1,3 @@
def difference(a, b):
b = set(b)
return [item for item in a if item not in b]

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from difference import difference
def difference_test(t):
t.true(isinstance(difference, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'difference is a function')
test('Testing difference',difference_test)

View File

@ -0,0 +1,3 @@
def difference_by(a, b, fn):
b = set(map(fn, b))
return [item for item in a if fn(item) not in b]

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from difference_by import difference_by
def difference_by_test(t):
t.true(isinstance(difference_by, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'difference_by is a function')
test('Testing difference_by',difference_by_test)

View File

@ -0,0 +1,5 @@
def factorial(num):
if not ((num >= 0) & (num % 1 == 0)):
raise Exception(
f"Number( {num} ) can't be floating point or negative ")
return 1 if num == 0 else num * factorial(num - 1)

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from factorial import factorial
def factorial_test(t):
t.true(isinstance(factorial, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'factorial is a function')
test('Testing factorial',factorial_test)

21
test/gcd/gcd.py Normal file
View File

@ -0,0 +1,21 @@
from functools import reduce
def spread(arg):
ret = []
for i in arg:
if isinstance(i, list):
ret.extend(i)
else:
ret.append(i)
return ret
def gcd(*args):
numbers = []
numbers.extend(spread(list(args)))
def _gcd(x, y):
return x if not y else gcd(y, x % y)
return reduce((lambda x, y: _gcd(x, y)), numbers)

6
test/gcd/gcd.test.py Normal file
View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from gcd import gcd
def gcd_test(t):
t.true(isinstance(gcd, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'gcd is a function')
test('Testing gcd',gcd_test)

View File

@ -0,0 +1,2 @@
def is_lower_case(str):
return str == str.lower()

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from is_lower_case import is_lower_case
def is_lower_case_test(t):
t.true(isinstance(is_lower_case, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'is_lower_case is a function')
test('Testing is_lower_case',is_lower_case_test)

View File

@ -0,0 +1,2 @@
def is_upper_case(str):
return str == str.upper()

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from is_upper_case import is_upper_case
def is_upper_case_test(t):
t.true(isinstance(is_upper_case, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'is_upper_case is a function')
test('Testing is_upper_case',is_upper_case_test)

24
test/lcm/lcm.py Normal file
View File

@ -0,0 +1,24 @@
from functools import reduce
def spread(arg):
ret = []
for i in arg:
if isinstance(i, list):
ret.extend(i)
else:
ret.append(i)
return ret
def lcm(*args):
numbers = []
numbers.extend(spread(list(args)))
def _gcd(x, y):
return x if not y else gcd(y, x % y)
def _lcm(x, y):
return x * y / _gcd(x, y)
return reduce((lambda x, y: _lcm(x, y)), numbers)

6
test/lcm/lcm.test.py Normal file
View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from lcm import lcm
def lcm_test(t):
t.true(isinstance(lcm, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'lcm is a function')
test('Testing lcm',lcm_test)

8
test/max_n/max_n.py Normal file
View File

@ -0,0 +1,8 @@
from copy import deepcopy
def max_n(arr, n=1):
numbers = deepcopy(arr)
numbers.sort()
numbers.reverse()
return numbers[:n]

6
test/max_n/max_n.test.py Normal file
View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from max_n import max_n
def max_n_test(t):
t.true(isinstance(max_n, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'max_n is a function')
test('Testing max_n',max_n_test)

7
test/min_n/min_n.py Normal file
View File

@ -0,0 +1,7 @@
from copy import deepcopy
def min_n(arr, n=1):
numbers = deepcopy(arr)
numbers.sort()
return numbers[:n]

6
test/min_n/min_n.test.py Normal file
View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from min_n import min_n
def min_n_test(t):
t.true(isinstance(min_n, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'min_n is a function')
test('Testing min_n',min_n_test)

View File

@ -0,0 +1,4 @@
def palindrome(string):
from re import sub
s = sub('[\W_]', '', string.lower())
return s == s[::-1]

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from palindrome import palindrome
def palindrome_test(t):
t.true(isinstance(palindrome, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'palindrome is a function')
test('Testing palindrome',palindrome_test)

12
test/shuffle/shuffle.py Normal file
View File

@ -0,0 +1,12 @@
from copy import deepcopy
from random import randint
def shuffle(arr):
temp_arr = deepcopy(arr)
m = len(temp_arr)
while (m):
m -= 1
i = randint(0, m)
temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m]
return temp_arr

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from shuffle import shuffle
def shuffle_test(t):
t.true(isinstance(shuffle, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'shuffle is a function')
test('Testing shuffle',shuffle_test)

8
test/spread/spread.py Normal file
View File

@ -0,0 +1,8 @@
def spread(arg):
ret = []
for i in arg:
if isinstance(i, list):
ret.extend(i)
else:
ret.append(i)
return ret

View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from spread import spread
def spread_test(t):
t.true(isinstance(spread, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'spread is a function')
test('Testing spread',spread_test)

8
test/zip/zip.py Normal file
View File

@ -0,0 +1,8 @@
def zip(*args, fillvalue=None):
max_length = max([len(arr) for arr in args])
result = []
for i in range(max_length):
result.append([
args[k][i] if i < len(args[k]) else None for k in range(len(args))
])
return result

6
test/zip/zip.test.py Normal file
View File

@ -0,0 +1,6 @@
import types,functools
from tape import test
from zip import zip
def zip_test(t):
t.true(isinstance(zip, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'zip is a function')
test('Testing zip',zip_test)