Skip to content

Commit d94358b

Browse files
authored
[Linting]: Lint practice stub files (4 / ?) (#2791)
* Ending before L * nvm lol
1 parent cb83d99 commit d94358b

File tree

12 files changed

+78
-79
lines changed

12 files changed

+78
-79
lines changed

exercises/practice/go-counting/.meta/example.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
BLACK = "B"
3-
WHITE = "W"
4-
NONE = ""
2+
BLACK = 'B'
3+
WHITE = 'W'
4+
NONE = ''
55
STONES = [BLACK, WHITE]
66
DIRECTIONS = [(0, 1), (0, -1), (1, 0), (-1, 0)]
77

@@ -12,23 +12,25 @@ def __init__(self, board):
1212
self.width = len(self.board[0])
1313
self.height = len(self.board)
1414

15-
def valid(self, x, y):
16-
return x >= 0 and x < self.width and y >= 0 and y < self.height
17-
18-
def walk(self, x, y,
19-
visited_territory=[],
20-
visited_coords=[],
21-
visited_stones=[]):
22-
if not (x, y) in visited_coords and self.valid(x, y):
23-
s = self.board[y][x]
24-
if s in STONES:
25-
if s not in visited_stones:
26-
return (visited_territory, visited_stones + [s])
15+
def valid(self, width, height):
16+
return self.width > width >= 0 and self.height > height >= 0
17+
18+
def walk(self, width, height, visited_territory=None, visited_coords=None, visited_stones=None):
19+
# Pylint gives W0102 warning if list used as default argument, because list is mutable.
20+
visited_territory = [] if visited_territory is None else visited_territory
21+
visited_coords = [] if visited_coords is None else visited_coords
22+
visited_stones = [] if visited_stones is None else visited_stones
23+
24+
if (width, height) not in visited_coords and self.valid(width, height):
25+
stone = self.board[height][width]
26+
if stone in STONES:
27+
if stone not in visited_stones:
28+
return (visited_territory, visited_stones + [stone])
2729
else: # s is empty
28-
for d in DIRECTIONS:
29-
visited = self.walk(x + d[0], y + d[1],
30-
visited_territory + [(x, y)],
31-
visited_coords + [(x, y)],
30+
for direction in DIRECTIONS:
31+
visited = self.walk(width + direction[0], height + direction[1],
32+
visited_territory + [(width, height)],
33+
visited_coords + [(width, height)],
3234
visited_stones)
3335
visited_territory = visited[0]
3436
visited_stones = visited[1]
@@ -50,12 +52,12 @@ def territory(self, x, y):
5052

5153
def territories(self):
5254
owners = STONES + [NONE]
53-
result = dict([(owner, set()) for owner in owners])
55+
result = {owner:set() for owner in owners}
5456
visited = set()
55-
for y in range(self.height):
56-
for x in range(self.width):
57-
if not (x, y) in visited:
58-
owner, owned_territories = self.territory(x, y)
57+
for row in range(self.height):
58+
for column in range(self.width):
59+
if not (column, row) in visited:
60+
owner, owned_territories = self.territory(column, row)
5961
result[owner].update(owned_territories)
6062
visited.update(owned_territories)
6163

exercises/practice/grade-school/.meta/example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
class School:
5-
def __init__(self,):
5+
def __init__(self):
66
self.db = {}
77
self.add = []
88

exercises/practice/grains/.meta/example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
def square(number):
22
if number == 0:
3-
raise ValueError("square must be between 1 and 64")
3+
raise ValueError('square must be between 1 and 64')
44
elif number < 0:
5-
raise ValueError("square must be between 1 and 64")
5+
raise ValueError('square must be between 1 and 64')
66
elif number > 64:
7-
raise ValueError("square must be between 1 and 64")
7+
raise ValueError('square must be between 1 and 64')
88

99
return 2 ** (number - 1)
1010

exercises/practice/grep/.meta/example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def format_lines(matched_lines, flags, files):
2727
result = []
2828

2929
for file_name, line_number, line in matched_lines:
30-
line_result = ""
30+
line_result = ''
3131

3232
if len(files) > 1:
3333
line_result += file_name + ':'
@@ -46,7 +46,7 @@ def grep(pattern, flags, files):
4646
matched_lines = []
4747

4848
for file_name in files:
49-
with open(file_name) as f:
49+
with open(file_name, encoding='utf-8') as f:
5050
for line_number, line in enumerate(f.readlines(), start=1):
5151
if matches(line, pattern, flags):
5252
matched_lines.append((file_name, line_number, line))
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
def distance(s1, s2):
2-
if len(s1) != len(s2):
3-
raise ValueError("Strands must be of equal length.")
1+
def distance(strand_a, strand_b):
2+
if len(strand_a) != len(strand_b):
3+
raise ValueError('Strands must be of equal length.')
44

5-
return sum(a != b for a, b in zip(s1, s2))
5+
return sum(a_part != b_part for a_part, b_part in zip(strand_a, strand_b))

exercises/practice/hangman/.meta/example.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
STATUS_WIN = "win"
2-
STATUS_LOSE = "lose"
3-
STATUS_ONGOING = "ongoing"
1+
STATUS_WIN = 'win'
2+
STATUS_LOSE = 'lose'
3+
STATUS_ONGOING = 'ongoing'
44

55

66
class Hangman:
@@ -10,24 +10,24 @@ def __init__(self, word):
1010
self.word = word
1111
self.masked_word = ''
1212
self.guesses = []
13-
for i in self.word:
13+
for _ in self.word:
1414
self.masked_word += '_'
1515

1616
def guess(self, char):
1717
if self.status != STATUS_ONGOING:
18-
raise ValueError("The game has already ended.")
18+
raise ValueError('The game has already ended.')
1919

2020
self.update_remaining_guesses(char)
2121
self.update_masked_word()
2222
self.update_status()
2323

2424
def update_masked_word(self):
2525
self.masked_word = ''
26-
for i in self.word:
27-
if i not in self.guesses:
26+
for idx in self.word:
27+
if idx not in self.guesses:
2828
self.masked_word += '_'
2929
else:
30-
self.masked_word += i
30+
self.masked_word += idx
3131

3232
def update_remaining_guesses(self, char):
3333
if char not in self.word or char in self.guesses:

exercises/practice/hangman/hangman.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Game status categories
22
# Change the values as you see fit
3-
STATUS_WIN = "win"
4-
STATUS_LOSE = "lose"
5-
STATUS_ONGOING = "ongoing"
3+
STATUS_WIN = 'win'
4+
STATUS_LOSE = 'lose'
5+
STATUS_ONGOING = 'ongoing'
66

77

88
class Hangman:
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from functools import reduce
22

33

4-
def hexa(hex_str):
5-
hex_str = hex_str.lower()
6-
if set(hex_str) - set('0123456789abcdef'):
4+
def hexa(hex_string):
5+
hex_string = hex_string.lower()
6+
if set(hex_string) - set('0123456789abcdef'):
77
raise ValueError('Invalid hexadecimal string')
8-
digits = [ord(c) - ord('a') + 10 if c in 'abcdef' else ord(c) - ord('0')
9-
for c in hex_str]
10-
return reduce(lambda x, y: x * 16 + y, digits, 0)
8+
digits = [ord(letter) - ord('a') + 10 if letter in 'abcdef' else ord(letter) - ord('0')
9+
for letter in hex_string]
10+
return reduce(lambda var_1, var_2: var_1 * 16 + var_2, digits, 0)
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
parts = [('lay in', 'the house that Jack built.'),
1+
PARTS = [('lay in', 'the house that Jack built.'),
22
('ate', 'the malt'),
33
('killed', 'the rat'),
44
('worried', 'the cat'),
@@ -13,14 +13,11 @@
1313

1414

1515
def verse(verse_num):
16-
v = ['This is {}'.format(parts[verse_num][1])]
17-
v.extend(['that {0} {1}'.format(*parts[i])
18-
for i in range(verse_num - 1, -1, -1)])
19-
return ' '.join(v)
16+
verse = [f'This is {PARTS[verse_num][1]}']
17+
verse.extend(['that {0} {1}'.format(*PARTS[idx])
18+
for idx in range(verse_num - 1, -1, -1)])
19+
return ' '.join(verse)
2020

2121

2222
def recite(start_verse, end_verse):
23-
result = []
24-
for verse_num in range(start_verse-1, end_verse):
25-
result.append(verse(verse_num))
26-
return result
23+
return [verse(verse_num) for verse_num in range(start_verse-1, end_verse)]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
def is_isogram(string):
2-
characters_lower = [c.lower() for c in string if c.isalpha()]
2+
characters_lower = [char.lower() for char in string if char.isalpha()]
33
return len(set(characters_lower)) == len(characters_lower)

0 commit comments

Comments
 (0)