diff --git a/.gitignore b/.gitignore index f65f411..98c9279 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ * -!**/*.py \ No newline at end of file +!**/**/ +!**/**/*.py +*_test.py +**/**/__pycache \ No newline at end of file diff --git a/python/armstrong-numbers/armstrong_numbers.py b/python/armstrong-numbers/armstrong_numbers.py new file mode 100644 index 0000000..6094c92 --- /dev/null +++ b/python/armstrong-numbers/armstrong_numbers.py @@ -0,0 +1,2 @@ +def is_armstrong_number(number): + return sum([int(digit) ** len(str(number)) for digit in str(number)]) == number diff --git a/python/hamming/hamming.py b/python/hamming/hamming.py new file mode 100644 index 0000000..7907685 --- /dev/null +++ b/python/hamming/hamming.py @@ -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]]) \ No newline at end of file diff --git a/python/hello-world/hello_world.py b/python/hello-world/hello_world.py new file mode 100644 index 0000000..dea05ae --- /dev/null +++ b/python/hello-world/hello_world.py @@ -0,0 +1,2 @@ +def hello(): + return "Hello, World!" diff --git a/python/high-scores/high_scores.py b/python/high-scores/high_scores.py new file mode 100644 index 0000000..3a5179d --- /dev/null +++ b/python/high-scores/high_scores.py @@ -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] \ No newline at end of file diff --git a/python/isogram/isogram.py b/python/isogram/isogram.py new file mode 100644 index 0000000..c336281 --- /dev/null +++ b/python/isogram/isogram.py @@ -0,0 +1,2 @@ +def is_isogram(string): + return len(list(set(string))) == len(string) diff --git a/python/leap/leap.py b/python/leap/leap.py new file mode 100644 index 0000000..38c3674 --- /dev/null +++ b/python/leap/leap.py @@ -0,0 +1,2 @@ +def leap_year(year): + return (year % 100 == 0 and year % 400 == 0) or (year % 100 != 0 and year % 4 == 0) \ No newline at end of file diff --git a/python/matrix/matrix.py b/python/matrix/matrix.py new file mode 100644 index 0000000..8946dd3 --- /dev/null +++ b/python/matrix/matrix.py @@ -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] \ No newline at end of file diff --git a/python/pangram/pangram.py b/python/pangram/pangram.py new file mode 100644 index 0000000..c61bc3f --- /dev/null +++ b/python/pangram/pangram.py @@ -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()) diff --git a/python/protein-translation/protein_translation.py b/python/protein-translation/protein_translation.py new file mode 100644 index 0000000..c1ebff6 --- /dev/null +++ b/python/protein-translation/protein_translation.py @@ -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) \ No newline at end of file diff --git a/python/raindrops/raindrops.py b/python/raindrops/raindrops.py new file mode 100644 index 0000000..d27276d --- /dev/null +++ b/python/raindrops/raindrops.py @@ -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) diff --git a/python/reverse-string/reverse_string.py b/python/reverse-string/reverse_string.py new file mode 100644 index 0000000..0f0a63f --- /dev/null +++ b/python/reverse-string/reverse_string.py @@ -0,0 +1,2 @@ +def reverse(text): + return text[::-1] diff --git a/python/robot-name/robot_name.py b/python/robot-name/robot_name.py new file mode 100644 index 0000000..1fb3764 --- /dev/null +++ b/python/robot-name/robot_name.py @@ -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) \ No newline at end of file diff --git a/python/series/series.py b/python/series/series.py new file mode 100644 index 0000000..450e7e3 --- /dev/null +++ b/python/series/series.py @@ -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))))) \ No newline at end of file diff --git a/python/two-fer/two_fer.py b/python/two-fer/two_fer.py new file mode 100644 index 0000000..425d236 --- /dev/null +++ b/python/two-fer/two_fer.py @@ -0,0 +1,2 @@ +def two_fer(name='you'): + return f'One for {name}, one for me.'