Skip to content

Commit e155a1c

Browse files
J08KBethanyG
authored andcommitted
Finished all the Ps
1 parent e5425ab commit e155a1c

File tree

11 files changed

+105
-93
lines changed

11 files changed

+105
-93
lines changed

exercises/practice/pascals-triangle/.meta/example.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ def rows(row_count):
33
return None
44
elif row_count == 0:
55
return []
6-
rows = []
7-
for i in range(row_count):
6+
row_list = []
7+
for idx in range(row_count):
88
ronald = [1]
9-
for j in range(i):
10-
ronald.append(sum(rows[-1][j:j+2]))
11-
rows.append(ronald)
12-
return rows
9+
for edx in range(idx):
10+
ronald.append(sum(row_list[-1][edx:edx+2]))
11+
row_list.append(ronald)
12+
return row_list

exercises/practice/perfect-numbers/.meta/example.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12

23
def divisor_generator(number):
34
"""Returns an unordered list of divisors for n (1 < number).
@@ -6,7 +7,7 @@ def divisor_generator(number):
67
:return: list of int divisors
78
"""
89

9-
for index in range(2, int(number ** 0.5) + 1):
10+
for index in range(2, int(math.sqrt(number)) + 1):
1011
if number % index == 0:
1112
yield index
1213
if index * index != number:
@@ -21,13 +22,13 @@ def classify(number):
2122
"""
2223

2324
if number <= 0:
24-
raise ValueError("Classification is only possible for positive integers.")
25+
raise ValueError('Classification is only possible for positive integers.')
2526

2627
aliquot_sum = sum(divisor_generator(number)) + (1 if number > 1 else 0)
2728

2829
if aliquot_sum < number:
29-
return "deficient"
30+
return 'deficient'
3031
elif aliquot_sum == number:
31-
return "perfect"
32+
return 'perfect'
3233
else:
33-
return "abundant"
34+
return 'abundant'

exercises/practice/phone-number/.meta/example.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,44 @@ def __init__(self, number):
1010
self.subscriber_number = self.number[-4:]
1111

1212
def pretty(self):
13-
return f"({self.area_code})-{self.exchange_code}-{self.subscriber_number}"
13+
return f'({self.area_code})-{self.exchange_code}-{self.subscriber_number}'
1414

1515
def _clean(self, number):
16-
preprocess = re.sub(r"[() +-.]", "", number)
16+
preprocess = re.sub(r'[() +-.]', '', number)
1717

1818
if any(item for item in preprocess if item.isalpha()):
19-
raise ValueError("letters not permitted")
19+
raise ValueError('letters not permitted')
2020

2121
if any(item for item in preprocess if item in punctuation):
22-
raise ValueError("punctuations not permitted")
22+
raise ValueError('punctuations not permitted')
2323

2424
return self._normalize(preprocess)
2525

2626
def _normalize(self, number):
2727
if len(number) < 10:
28-
raise ValueError("incorrect number of digits")
28+
raise ValueError('incorrect number of digits')
2929

3030
if len(number) > 11:
31-
raise ValueError("more than 11 digits")
32-
33-
if len(number) == 10 or len(number) == 11 and number.startswith("1"):
34-
if number[-10] == "0":
35-
raise ValueError("area code cannot start with zero")
36-
elif number[-10] == "1":
37-
raise ValueError("area code cannot start with one")
38-
elif number[-7] == "0":
39-
raise ValueError("exchange code cannot start with zero")
40-
elif number[-7] == "1":
41-
raise ValueError("exchange code cannot start with one")
31+
raise ValueError('more than 11 digits')
32+
33+
if len(number) == 10 or len(number) == 11 and number.startswith('1'):
34+
if number[-10] == '0':
35+
raise ValueError('area code cannot start with zero')
36+
elif number[-10] == '1':
37+
raise ValueError('area code cannot start with one')
38+
elif number[-7] == '0':
39+
raise ValueError('exchange code cannot start with zero')
40+
elif number[-7] == '1':
41+
raise ValueError('exchange code cannot start with one')
4242
else:
43-
valid = number[-10] in "23456789" and number[-7] in "23456789"
43+
valid = number[-10] in '23456789' and number[-7] in '23456789'
4444

4545
else:
4646
valid = False
47-
if number[0] in "023456789":
48-
raise ValueError("11 digits must start with 1")
47+
if number[0] in '023456789':
48+
raise ValueError('11 digits must start with 1')
4949

5050
if valid:
5151
return number[-10:]
52+
53+
return None # [Pylint]: R1710;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
def hamming_distance(strand1, strand2):
2-
return sum(x != y for (x, y) in zip(strand1, strand2))
2+
return sum(idx != edx for (idx, edx) in zip(strand1, strand2))

exercises/practice/poker/.meta/example.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@ def best_hands(hands):
44

55
def allmax(iterable, key=None):
66
result, maxval = [], None
7-
key = key or (lambda x: x)
8-
for x in iterable:
9-
xval = key(x)
7+
key = key or (lambda idx: idx)
8+
for idx in iterable:
9+
xval = key(idx)
1010
if not result or xval > maxval:
11-
result, maxval = [x], xval
11+
result, maxval = [idx], xval
1212
elif xval == maxval:
13-
result.append(x)
13+
result.append(idx)
1414
return result
1515

1616

1717
def hand_rank(hand):
18-
hand = hand.replace("10", "T").split()
19-
card_ranks = ["..23456789TJQKA".index(r) for r, s in hand]
20-
groups = [(card_ranks.count(i), i) for i in set(card_ranks)]
18+
hand = hand.replace('10', 'T').split()
19+
card_ranks = ['..23456789TJQKA'.index(idx) for idx, _ in hand]
20+
groups = [(card_ranks.count(idx), idx) for idx in set(card_ranks)]
2121
groups.sort(reverse=True)
2222
counts, ranks = zip(*groups)
2323
if ranks == (14, 5, 4, 3, 2):
2424
ranks = (5, 4, 3, 2, 1)
2525
straight = (len(counts) == 5) and (max(ranks) - min(ranks) == 4)
26-
flush = len(set([s for r, s in hand])) == 1
26+
flush = len({idx for _, idx in hand}) == 1
2727
return (9 if counts == (5,) else
2828
8 if straight and flush else
2929
7 if counts == (4, 1) else

exercises/practice/pov/.meta/example.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33

44
class Tree:
5-
def __init__(self, label, children=[]):
5+
def __init__(self, label, children=None):
66
self.label = label
7-
self.children = children
7+
self.children = children if children is not None else []
88

99
def __dict__(self):
1010
return {self.label: [member.__dict__() for member in sorted(self.children)]}
@@ -57,7 +57,7 @@ def from_pov(self, from_node):
5757
for child in tree.children:
5858
stack.append(child.add(tree.remove(child.label)))
5959

60-
raise ValueError("Tree could not be reoriented")
60+
raise ValueError('Tree could not be reoriented')
6161

6262

6363

@@ -69,8 +69,8 @@ def path_to(self, from_node, to_node):
6969
while path[-1] != to_node:
7070
try:
7171
tree = stack.pop()
72-
except IndexError:
73-
raise ValueError("No path found")
72+
except IndexError as error:
73+
raise ValueError('No path found') from error
7474
if to_node in tree:
7575
path.append(tree.label)
7676
stack = tree.children

exercises/practice/pov/pov.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33

44
class Tree:
5-
def __init__(self, label, children=[]):
5+
def __init__(self, label, children=None):
66
self.label = label
7-
self.children = children
7+
self.children = children if children is not None else []
88

99
def __dict__(self):
1010
return {self.label: [c.__dict__() for c in sorted(self.children)]}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
def factors(value):
2-
factors = []
2+
factor_list = []
33
divisor = 2
44
while value > 1:
55
while value % divisor == 0:
6-
factors.append(divisor)
6+
factor_list.append(divisor)
77
value /= divisor
88

99
divisor += 1
1010

11-
return factors
11+
return factor_list
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
CODONS = {'AUG': "Methionine", 'UUU': "Phenylalanine",
2-
'UUC': "Phenylalanine", 'UUA': "Leucine", 'UUG': "Leucine",
3-
'UCU': "Serine", 'UCC': "Serine", 'UCA': "Serine",
4-
'UCG': "Serine", 'UAU': "Tyrosine", 'UAC': "Tyrosine",
5-
'UGU': "Cysteine", 'UGC': "Cysteine", 'UGG': "Tryptophan",
6-
'UAA': "STOP", 'UAG': "STOP", 'UGA': "STOP"}
1+
CODONS = {'AUG': 'Methionine', 'UUU': 'Phenylalanine',
2+
'UUC': 'Phenylalanine', 'UUA': 'Leucine', 'UUG': 'Leucine',
3+
'UCU': 'Serine', 'UCC': 'Serine', 'UCA': 'Serine',
4+
'UCG': 'Serine', 'UAU': 'Tyrosine', 'UAC': 'Tyrosine',
5+
'UGU': 'Cysteine', 'UGC': 'Cysteine', 'UGG': 'Tryptophan',
6+
'UAA': 'STOP', 'UAG': 'STOP', 'UGA': 'STOP'}
77

88

99
def of_codon(codon):
1010
if codon not in CODONS:
11-
raise ValueError('Invalid codon: {}'.format(codon))
11+
raise ValueError(f'Invalid codon: {codon}')
1212
return CODONS[codon]
1313

1414

1515
def proteins(strand):
16-
proteins = []
16+
protein_list = []
1717
for codon in map(of_codon, _chunkstring(strand, 3)):
1818
if codon == 'STOP':
1919
break
20-
proteins.append(codon)
21-
return proteins
20+
protein_list.append(codon)
21+
return protein_list
2222

2323

24-
def _chunkstring(string, n):
25-
return (string[i:n + i] for i in range(0, len(string), n))
24+
def _chunkstring(string, number):
25+
return (string[idx:number + idx] for idx in range(0, len(string), number))
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
def proverb(rhyme_items):
22
if not rhyme_items:
3-
return ""
4-
phrases = ['For want of a {0} the {1} was lost.'.format(el1, el2)
5-
for el1, el2 in zip(rhyme_items, rhyme_items[1:])]
6-
phrases.append('And all for the want of a {0}.'.format(rhyme_items[0]))
3+
return ''
4+
phrases = [f'For want of a {element_1} the {element_2} was lost.'
5+
for element_1, element_2 in zip(rhyme_items, rhyme_items[1:])]
6+
phrases.append(f'And all for the want of a {rhyme_items[0]}.')
77
return '\n'.join(phrases)

0 commit comments

Comments
 (0)