Skip to content

Commit

Permalink
Merge pull request #3 from exercism/master
Browse files Browse the repository at this point in the history
Pulling in most recent exercise changes
  • Loading branch information
BethanyG authored May 23, 2019
2 parents bcdd67f + 29f9814 commit e848fbf
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 124 deletions.
2 changes: 1 addition & 1 deletion exercises/binary-search/binary_search.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
def binary_search(list_of_numbers, number):
def find(search_list, value):
pass
25 changes: 12 additions & 13 deletions exercises/binary-search/binary_search_test.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
import unittest

from binary_search import binary_search
from binary_search import find

# Tests adapted from `problem-specifications//canonical-data.json` @ v1.3.0


class BinarySearchTest(unittest.TestCase):
def test_finds_value_in_array_with_one_element(self):
self.assertEqual(binary_search([6], 6), 0)
self.assertEqual(find([6], 6), 0)

def test_finds_value_in_middle_of_array(self):
self.assertEqual(binary_search([1, 3, 4, 6, 8, 9, 11], 6), 3)
self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 6), 3)

def test_finds_value_at_beginning_of_array(self):
self.assertEqual(binary_search([1, 3, 4, 6, 8, 9, 11], 1), 0)
self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 1), 0)

def test_finds_value_at_end_of_array(self):
self.assertEqual(binary_search([1, 3, 4, 6, 8, 9, 11], 11), 6)
self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 11), 6)

def test_finds_value_in_array_of_odd_length(self):
self.assertEqual(
binary_search([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634],
144), 9)
find([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634], 144), 9)

def test_finds_value_in_array_of_even_length(self):
self.assertEqual(
binary_search([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], 21),
find([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], 21),
5)

def test_identifies_value_missing(self):
with self.assertRaisesWithMessage(ValueError):
binary_search([1, 3, 4, 6, 8, 9, 11], 7)
find([1, 3, 4, 6, 8, 9, 11], 7)

def test_value_smaller_than_arrays_minimum(self):
with self.assertRaisesWithMessage(ValueError):
binary_search([1, 3, 4, 6, 8, 9, 11], 0)
find([1, 3, 4, 6, 8, 9, 11], 0)

def test_value_larger_than_arrays_maximum(self):
with self.assertRaisesWithMessage(ValueError):
binary_search([1, 3, 4, 6, 8, 9, 11], 13)
find([1, 3, 4, 6, 8, 9, 11], 13)

def test_empty_array(self):
with self.assertRaisesWithMessage(ValueError):
binary_search([], 1)
find([], 1)

def test_nothing_is_found_when_left_and_right_bounds_cross(self):
with self.assertRaisesWithMessage(ValueError):
binary_search([1, 2], 0)
find([1, 2], 0)

# Utility functions
def setUp(self):
Expand Down
2 changes: 1 addition & 1 deletion exercises/binary-search/example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def binary_search(search_list, value):
def find(search_list, value):
low = 0
high = len(search_list) - 1
while low <= high:
Expand Down
2 changes: 1 addition & 1 deletion exercises/bob/bob.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
def hey(phrase):
def response(hey_bob):
pass
55 changes: 28 additions & 27 deletions exercises/bob/bob_test.py
Original file line number Diff line number Diff line change
@@ -1,100 +1,101 @@
import unittest

from bob import hey
from bob import response


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.4.0

class BobTest(unittest.TestCase):
def test_stating_something(self):
self.assertEqual(hey("Tom-ay-to, tom-aaaah-to."), "Whatever.")
self.assertEqual(response("Tom-ay-to, tom-aaaah-to."), "Whatever.")

def test_shouting(self):
self.assertEqual(hey("WATCH OUT!"), "Whoa, chill out!")
self.assertEqual(response("WATCH OUT!"), "Whoa, chill out!")

def test_shouting_gibberish(self):
self.assertEqual(hey("FCECDFCAAB"), "Whoa, chill out!")
self.assertEqual(response("FCECDFCAAB"), "Whoa, chill out!")

def test_asking_a_question(self):
self.assertEqual(
hey("Does this cryogenic chamber make me look fat?"), "Sure.")
response("Does this cryogenic chamber make me look fat?"), "Sure.")

def test_asking_a_numeric_question(self):
self.assertEqual(hey("You are, what, like 15?"), "Sure.")
self.assertEqual(response("You are, what, like 15?"), "Sure.")

def test_asking_gibberish(self):
self.assertEqual(hey("fffbbcbeab?"), "Sure.")
self.assertEqual(response("fffbbcbeab?"), "Sure.")

def test_talking_forcefully(self):
self.assertEqual(
hey("Let's go make out behind the gym!"), "Whatever.")
response("Let's go make out behind the gym!"), "Whatever.")

def test_using_acronyms_in_regular_speech(self):
self.assertEqual(
hey("It's OK if you don't want to go to the DMV."),
response("It's OK if you don't want to go to the DMV."),
"Whatever.")

def test_forceful_question(self):
self.assertEqual(
hey("WHAT THE HELL WERE YOU THINKING?"),
response("WHAT THE HELL WERE YOU THINKING?"),
"Calm down, I know what I'm doing!"
)

def test_shouting_numbers(self):
self.assertEqual(hey("1, 2, 3 GO!"), "Whoa, chill out!")
self.assertEqual(response("1, 2, 3 GO!"), "Whoa, chill out!")

def test_no_letters(self):
self.assertEqual(hey("1, 2, 3"), "Whatever.")
self.assertEqual(response("1, 2, 3"), "Whatever.")

def test_question_with_no_letters(self):
self.assertEqual(hey("4?"), "Sure.")
self.assertEqual(response("4?"), "Sure.")

def test_shouting_with_special_characters(self):
self.assertEqual(
hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"),
response("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"),
"Whoa, chill out!")

def test_shouting_with_no_exclamation_mark(self):
self.assertEqual(hey("I HATE THE DMV"), "Whoa, chill out!")
self.assertEqual(response("I HATE THE DMV"), "Whoa, chill out!")

def test_statement_containing_question_mark(self):
self.assertEqual(
hey("Ending with ? means a question."), "Whatever.")
response("Ending with ? means a question."), "Whatever.")

def test_non_letters_with_question(self):
self.assertEqual(hey(":) ?"), "Sure.")
self.assertEqual(response(":) ?"), "Sure.")

def test_prattling_on(self):
self.assertEqual(
hey("Wait! Hang on. Are you going to be OK?"), "Sure.")
response("Wait! Hang on. Are you going to be OK?"), "Sure.")

def test_silence(self):
self.assertEqual(hey(""), "Fine. Be that way!")
self.assertEqual(response(""), "Fine. Be that way!")

def test_prolonged_silence(self):
self.assertEqual(hey(" "), "Fine. Be that way!")
self.assertEqual(response(" "), "Fine. Be that way!")

def test_alternate_silence(self):
self.assertEqual(hey("\t\t\t\t\t\t\t\t\t\t"), "Fine. Be that way!")
self.assertEqual(response("\t\t\t\t\t\t\t\t\t\t"),
"Fine. Be that way!")

def test_multiple_line_question(self):
self.assertEqual(
hey("\nDoes this cryogenic chamber make me look fat?\nNo."),
"Whatever.")
response("\nDoes this cryogenic chamber make me look fat?\n"
"No."), "Whatever.")

def test_starting_with_whitespace(self):
self.assertEqual(hey(" hmmmmmmm..."), "Whatever.")
self.assertEqual(response(" hmmmmmmm..."), "Whatever.")

def test_ending_with_whitespace(self):
self.assertEqual(
hey("Okay if like my spacebar quite a bit? "), "Sure.")
response("Okay if like my spacebar quite a bit? "), "Sure.")

def test_other_whitespace(self):
self.assertEqual(hey("\n\r \t"), "Fine. Be that way!")
self.assertEqual(response("\n\r \t"), "Fine. Be that way!")

def test_non_question_ending_with_whitespace(self):
self.assertEqual(
hey("This is a statement ending with whitespace "),
response("This is a statement ending with whitespace "),
"Whatever.")


Expand Down
24 changes: 12 additions & 12 deletions exercises/bob/example.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
def hey(stimulus):
stimulus = stimulus.strip()
def response(hey_bob):
hey_bob = hey_bob.strip()

if _is_silence(stimulus):
if _is_silence(hey_bob):
return 'Fine. Be that way!'
if _is_shouting(stimulus):
if _is_question(stimulus):
if _is_shouting(hey_bob):
if _is_question(hey_bob):
return "Calm down, I know what I'm doing!"
else:
return 'Whoa, chill out!'
elif _is_question(stimulus):
elif _is_question(hey_bob):
return 'Sure.'
else:
return 'Whatever.'


def _is_silence(stimulus):
return stimulus == ''
def _is_silence(hey_bob):
return hey_bob == ''


def _is_shouting(stimulus):
return stimulus.isupper()
def _is_shouting(hey_bob):
return hey_bob.isupper()


def _is_question(stimulus):
return stimulus.endswith('?')
def _is_question(hey_bob):
return hey_bob.endswith('?')
2 changes: 1 addition & 1 deletion exercises/collatz-conjecture/collatz_conjecture.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
def collatz_steps(number):
def steps(number):
pass
22 changes: 11 additions & 11 deletions exercises/collatz-conjecture/collatz_conjecture_test.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import unittest

from collatz_conjecture import collatz_steps
from collatz_conjecture import steps


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.2.1

class CollatzConjectureTest(unittest.TestCase):

class CollatzConjectureTest(unittest.TestCase):
def test_zero_steps_for_one(self):
self.assertEqual(collatz_steps(1), 0)
self.assertEqual(steps(1), 0)

def test_divide_if_even(self):
self.assertEqual(collatz_steps(16), 4)
self.assertEqual(steps(16), 4)

def test_even_and_odd_steps(self):
self.assertEqual(collatz_steps(12), 9)
self.assertEqual(steps(12), 9)

def test_large_number_of_even_and_odd_steps(self):
self.assertEqual(collatz_steps(1000000), 152)
self.assertEqual(steps(1000000), 152)

def test_zero_is_invalid_input(self):
def test_zero_is_an_error(self):
with self.assertRaisesWithMessage(ValueError):
collatz_steps(0)
steps(0)

def test_negative_number_is_invalid_input(self):
def test_negative_value_is_an_error(self):
with self.assertRaisesWithMessage(ValueError):
collatz_steps(-15)
steps(-15)

# Utility functions
def setUp(self):
Expand All @@ -38,5 +38,5 @@ def assertRaisesWithMessage(self, exception):
return self.assertRaisesRegex(exception, r".+")


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion exercises/collatz-conjecture/example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def collatz_steps(n):
def steps(n):
if n <= 0:
raise ValueError("input should be positive")

Expand Down
20 changes: 11 additions & 9 deletions exercises/saddle-points/example.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
def saddle_points(m):
if not m:
return set()
if any(len(r) != len(m[0]) for r in m):
def saddle_points(matrix):
if not matrix:
return [{}]
if any(len(row) != len(matrix[0]) for row in matrix):
raise ValueError('irregular matrix')
mmax = [max(r) for r in m]
mmin = [min(c) for c in zip(*m)]
points = [(i+1, j+1) for i in range(len(m))
for j in range(len(m[0])) if mmax[i] == mmin[j]]
mmax = [max(row) for row in matrix]
mmin = [min(col) for col in zip(*matrix)]
points = [{"row": index + 1, "column": col_index + 1}
for index in range(len(matrix))
for col_index in range(len(matrix[0]))
if mmax[index] == mmin[col_index]]

return set(points)
return points or [{}]
25 changes: 15 additions & 10 deletions exercises/saddle-points/saddle_points_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,50 @@
class SaddlePointsTest(unittest.TestCase):
def test_identify_single_saddle_point(self):
matrix = [[9, 8, 7], [5, 3, 2], [6, 6, 7]]
self.assertEqual(saddle_points(matrix), set([(2, 1)]))
self.assertEqual(saddle_points(matrix), [{"row": 2, "column": 1}])

def test_empty_matrix_has_no_saddle_points(self):
self.assertEqual(saddle_points([]), set())
self.assertEqual(saddle_points([]), [dict()])

def test_matrix_with_one_elem_has_single_saddle_point(self):
matrix = [[1]]
self.assertEqual(saddle_points(matrix), set([(1, 1)]))
self.assertEqual(saddle_points(matrix), [{"row": 1, "column": 1}])

def test_identify_lack_of_saddle_points_when_there_are_none(self):
matrix = [[1, 2, 3], [3, 1, 2], [2, 3, 1]]
self.assertEqual(saddle_points(matrix), set())
self.assertEqual(saddle_points(matrix), [dict()])

def test_identify_multiple_saddle_points_in_column(self):
matrix = [[4, 5, 4], [3, 5, 5], [1, 5, 4]]
expected = set([(1, 2), (2, 2), (3, 2)])
expected = [{"row": 1, "column": 2}, {"row": 2, "column": 2},
{"row": 3, "column": 2}]
self.assertEqual(saddle_points(matrix), expected)

def test_identify_multiple_saddle_points_in_row(self):
matrix = [[6, 7, 8], [5, 5, 5], [7, 5, 6]]
expected = set([(2, 1), (2, 2), (2, 3)])
expected = [{"row": 2, "column": 1}, {"row": 2, "column": 2},
{"row": 2, "column": 3}]
self.assertEqual(saddle_points(matrix), expected)

def test_identify_saddle_point_in_bottom_right_corner(self):
matrix = [[8, 7, 9], [6, 7, 6], [3, 2, 5]]
expected = set([(3, 3)])
expected = [{"row": 3, "column": 3}]
self.assertEqual(saddle_points(matrix), expected)

def test_non_square_matrix_with_2_saddle_points(self):
matrix = [[3, 1, 3], [3, 2, 4]]
self.assertEqual(saddle_points(matrix), set([(1, 3), (1, 1)]))
self.assertEqual(saddle_points(matrix), [{"row": 1, "column": 1},
{"row": 1, "column": 3}])

def test_single_column_matrix_has_saddle_point_min_value(self):
matrix = [[2], [1], [4], [1]]
self.assertEqual(saddle_points(matrix), set([(2, 1), (4, 1)]))
self.assertEqual(saddle_points(matrix), [{"row": 2, "column": 1},
{"row": 4, "column": 1}])

def test_single_row_matrix_has_saddle_point_in_max_value(self):
matrix = [[2, 5, 3, 5]]
self.assertEqual(saddle_points(matrix), set([(1, 2), (1, 4)]))
self.assertEqual(saddle_points(matrix), [{"row": 1, "column": 2},
{"row": 1, "column": 4}])

# Additional tests for this track

Expand Down
Loading

0 comments on commit e848fbf

Please sign in to comment.