This commit is contained in:
Xevion
2019-07-13 04:16:23 -05:00
parent f10c9ee99c
commit d558cf2d6c
15 changed files with 90 additions and 1 deletions

5
.gitignore vendored
View File

@@ -1,2 +1,5 @@
*
!**/*.py
!**/**/
!**/**/*.py
*_test.py
**/**/__pycache

View File

@@ -0,0 +1,2 @@
def is_armstrong_number(number):
return sum([int(digit) ** len(str(number)) for digit in str(number)]) == number

View File

@@ -0,0 +1,4 @@
def distance(strand_a, strand_b):
if len(strand_a) != len(strand_b):
raise ValueError(f'strand_a (length {len(strand_a)}) has a different length from strand_b (length {len(strand_b)}), hamming distance cannot be computed.')
return len(strand_a) - len([char for index, char in enumerate(strand_a) if char == strand_b[index]])

View File

@@ -0,0 +1,2 @@
def hello():
return "Hello, World!"

View File

@@ -0,0 +1,10 @@
def latest(scores):
return scores[-1]
def personal_best(scores):
return max(scores)
def personal_top_three(scores):
return sorted(scores, reverse=True)[:3]

View File

@@ -0,0 +1,2 @@
def is_isogram(string):
return len(list(set(string))) == len(string)

2
python/leap/leap.py Normal file
View File

@@ -0,0 +1,2 @@
def leap_year(year):
return (year % 100 == 0 and year % 400 == 0) or (year % 100 != 0 and year % 4 == 0)

11
python/matrix/matrix.py Normal file
View File

@@ -0,0 +1,11 @@
class Matrix(object):
def __init__(self, matrix_string):
self.matrix_string = matrix_string
self.rows = [list(map(int, row.split(' '))) for row in self.matrix_string.split('\n')]
self.columns = [list(column) for column in zip(*(row for row in self.rows))]
def row(self, index):
return self.rows[index - 1]
def column(self, index):
return self.columns[index - 1]

View File

@@ -0,0 +1,6 @@
import string
charset = string.ascii_lowercase
def is_pangram(sentence):
dictionary = {char : True if char in sentence.lower() else False for char in charset}
return all(dictionary.values())

View File

@@ -0,0 +1,19 @@
from itertools import takewhile
# Processing of data into a dictionary from a list taken from the site.
# I would have preferred being able to process the data from a module, or maybe a text file,
# but that's a whole lot of effort for something as simple as this.
data = [['AUG', 'Methionine'],
['UUU', 'UUC', 'Phenylalanine'],
['UUA', 'UUG', 'Leucine'],
['UCU', 'UCC', 'UCA', 'UCG', 'Serine'],
['UAU', 'UAC', 'Tyrosine'],
['UGU', 'UGC', 'Cysteine'],
['UGG', 'Tryptophan'],
['UAA', 'UAG', 'UGA', 'STOP']]
protein_definitions = {codon : definition for definition, codons in [(subarray[-1], subarray[:-1]) for subarray in data] for codon in codons }
def proteins(strand):
strand = [strand[index * 3:(index * 3)+3] for index, char in enumerate(strand[::3])]
strand = takewhile(lambda x : x != 'STOP', (protein_definitions[codon] for codon in strand))
return list(strand)

View File

@@ -0,0 +1,2 @@
def convert(n):
return ''.join(['Pling' if n % 3 == 0 else '', 'Plang' if n % 5 == 0 else '', 'Plong' if n % 7 == 0 else '']) or str(n)

View File

@@ -0,0 +1,2 @@
def reverse(text):
return text[::-1]

View File

@@ -0,0 +1,17 @@
from random import choices
from string import ascii_uppercase
from itertools import product
from random import shuffle
nums = (str(x).zfill(3) for x in range(1000))
front = (''.join(fr) for fr in product(ascii_uppercase, ascii_uppercase))
names = [l + n for l, n in product(front, nums)]
shuffle(names)
name_iter = iter(names)
class Robot(object):
def __init__(self):
self.reset()
def reset(self):
self.name = next(name_iter)

5
python/series/series.py Normal file
View File

@@ -0,0 +1,5 @@
from itertools import takewhile
def slices(series, length):
if length > len(series) or length <= 0:
raise ValueError('[length] parameter cannot be {}.'.format('longer than [series]' if length > len(series) else 'shorter than zero'))
return list(takewhile(lambda x : len(x) == length, (series[index:index+length] for index in range(len(series)))))

View File

@@ -0,0 +1,2 @@
def two_fer(name='you'):
return f'One for {name}, one for me.'