示例#1
0
 def test_slice_length_is_too_large(self):
     with self.assertRaises(ValueError) as err:
         slices("12345", 6)
     self.assertEqual(type(err.exception), ValueError)
     self.assertEqual(
         err.exception.args[0], "slice length cannot be greater than series length"
     )
示例#2
0
 def test_slices_of_a_long_series(self):
     self.assertEqual(
         slices("918493904243", 5),
         [
             "91849", "18493", "84939", "49390", "93904", "39042", "90424",
             "04243"
         ],
     )
示例#3
0
 def test_slices_of_a_long_series(self):
     assert slices("918493904243", 5) == [
         "91849",
         "18493",
         "84939",
         "49390",
         "93904",
         "39042",
         "90424",
         "04243",
     ]
示例#4
0
 def test_does_not_accept_series_with_chars_other_than_digits(self):
     with self.assertRaisesWithMessage(ValueError):
         slices("123a3", 2)
     with self.assertRaisesWithMessage(ValueError):
         slices("l0123bba", 3)
     with self.assertRaisesWithMessage(ValueError):
         slices("series_of_digits", 4)
def largest_product(sequence, num_of_digits):
    if num_of_digits == 0:
        return 1
    if len(sequence) == 0:
        raise ValueError

    for sym in sequence:
        if not sym.isdigit():
            raise ValueError
    if num_of_digits < 0:
        raise ValueError

    subsequences = slices(sequence, num_of_digits)

    max_product = 0

    for subsequence in subsequences:
        current_product = 1
        for i in subsequence:
            current_product *= i
        if max_product < current_product:
            max_product = current_product

    return max_product
示例#6
0
 def test_slices_of_one_from_two(self):
     self.assertEqual(slices("12", 1), ["1", "2"])
示例#7
0
 def test_overly_long_slice(self):
     with self.assertRaises(ValueError):
         slices("012", 4)
示例#8
0
 def test_slices_of_four(self):
     self.assertEqual(
         slices("01234", 4),
         [[0, 1, 2, 3], [1, 2, 3, 4]],
     )
示例#9
0
 def test_slices_of_one(self):
     self.assertEqual(
         slices("01234", 1),
         [[0], [1], [2], [3], [4]],
     )
示例#10
0
 def test_overly_long_slice(self):
     with self.assertRaises(ValueError):
         slices("012", 4)
示例#11
0
 def test_slices_of_four(self):
     self.assertEqual([[0, 1, 2, 3], [1, 2, 3, 4]],
                      slices("01234", 4))
示例#12
0
 def test_empty_series_is_invalid(self):
     with self.assertRaisesWithMessage(ValueError):
         slices("", 1)
示例#13
0
 def test_slices_can_include_duplicates(self):
     self.assertEqual(slices("777777", 3), ["777", "777", "777", "777"])
示例#14
0
 def test_slices_of_two_overlap(self):
     self.assertEqual(slices("9142", 2), ["91", "14", "42"])
示例#15
0
 def test_slices_of_two(self):
     self.assertEqual(slices("35", 2), ["35"])
示例#16
0
 def test_slices_of_one_from_two(self):
     self.assertEqual(slices("12", 1), ["1", "2"])
示例#17
0
 def test_slices_of_one_from_one(self):
     self.assertEqual(slices("1", 1), ["1"])
示例#18
0
 def test_slices_of_one_from_two(self):
     assert slices("12", 1) == ["1", "2"]
示例#19
0
 def test_slices_of_one_from_one(self):
     assert slices("1", 1) == ["1"]
示例#20
0
 def test_slices_of_two_overlap(self):
     self.assertEqual(slices("9142", 2), ["91", "14", "42"])
示例#21
0
 def test_empty_series_is_invalid(self):
     with pytest.raises(ValueError):
         slices("", 1)
示例#22
0
 def test_slice_length_is_too_large(self):
     with self.assertRaisesWithMessage(ValueError):
         slices("12345", 6)
示例#23
0
 def test_slice_length_cannot_be_zero(self):
     with self.assertRaisesWithMessage(ValueError):
         slices("12345", 0)
示例#24
0
 def test_slice_length_cannot_be_negative(self):
     with self.assertRaisesWithMessage(ValueError):
         slices("123", -1)
示例#25
0
 def test_slices_of_five(self):
     self.assertEqual([[0, 1, 2, 3, 4]],
                      slices("01234", 5))
示例#26
0
 def test_empty_series_is_invalid(self):
     with self.assertRaisesWithMessage(ValueError):
         slices("", 1)
示例#27
0
 def test_overly_short_slice(self):
     with self.assertRaises(ValueError):
         slices("01234", 0)
示例#28
0
 def test_slices_can_deal_with_big_numbers(self):
     self.assertEqual(
         slices("1222912912831", 10),
         ["1222912912", "2229129128", "2291291283", "2912912831"])
示例#29
0
 def test_slices_of_three(self):
     self.assertEqual(
         slices("97867564", 3),
         [[9, 7, 8], [7, 8, 6], [8, 6, 7], [6, 7, 5], [7, 5, 6], [5, 6, 4]],
     )
示例#30
0
 def test_slices_can_deal_with_big_numbers_and_get_rid_of_useless_0s_in_the_beginning_of_created_numbers(
         self):
     self.assertEqual(slices("1000120001", 5),
                      ["10001", "12", "120", "1200", "12000", "20001"])
示例#31
0
 def test_slices_of_five(self):
     self.assertEqual(
         slices("01234", 5),
         [[0, 1, 2, 3, 4]],
     )
示例#32
0
 def test_slice_length_cannot_be_zero(self):
     with pytest.raises(ValueError):
         slices("12345", 0)
示例#33
0
 def test_overly_short_slice(self):
     with self.assertRaises(ValueError):
         slices("01234", 0)
示例#34
0
 def test_slices_of_a_series_of_3(self):
     self.assertEqual(slices("49142", 4), ["4914", "9142"])
示例#35
0
 def test_slices_of_two(self):
     self.assertEqual(slices("35", 2), ["35"])
示例#36
0
 def test_slice_length_cannot_be_zero(self):
     with self.assertRaisesWithMessage(ValueError):
         slices("12345", 0)
示例#37
0
 def test_slices_can_include_duplicates(self):
     self.assertEqual(slices("777777", 3), ["777", "777", "777", "777"])
示例#38
0
 def test_slice_length_cannot_be_negative(self):
     with pytest.raises(ValueError):
         slices("123", -1)
示例#39
0
 def test_slices_of_a_series_of_2(self):
     self.assertEqual(slices("49142", 2), ["49", "91", "14", "42"])
示例#40
0
 def test_slices_of_two(self):
     self.assertEqual(
         slices("97867564", 2),
         [[9, 7], [7, 8], [8, 6], [6, 7], [7, 5], [5, 6], [6, 4]], )
示例#41
0
 def test_slice_length_is_too_large(self):
     with self.assertRaisesWithMessage(ValueError):
         slices("12345", 6)
 def test_slices_of_two(self):
     self.assertEqual([[9, 7], [7, 8], [8, 6], [6, 7],
                       [7, 5], [5, 6], [6, 4]],
                      slices("97867564", 2))
示例#43
0
 def test_slice_length_cannot_be_negative(self):
     with self.assertRaisesWithMessage(ValueError):
         slices("123", -1)
示例#44
0
 def test_slices_of_one(self):
     self.assertEqual([[0], [1], [2], [3], [4]],
                      slices("01234", 1))
示例#45
0
 def test_slices_of_one_from_one(self):
     self.assertEqual(slices("1", 1), ["1"])
示例#46
0
 def test_slices_of_three(self):
     self.assertEqual([[9, 7, 8], [7, 8, 6], [8, 6, 7],
                       [6, 7, 5], [7, 5, 6], [5, 6, 4]],
                      slices("97867564", 3))