def max_sequence(arr): # Code to find maximum sum of subarray l_a = len(arr) sum_b = sum(arr) for i in range(l_a): for k in range(l_a-i+1): sum_c = sum(arr[k:k+i]) if sum_c > sum_b: sum_b = sum_c return(sum_b)
- def max_sequence(arr):
- # Code to find maximum sum of subarray
- l_a = len(arr)
counter = 0for m in arr:if m < 0:counter = counterelse:counter += 1if counter == 0:return(0)- sum_b = sum(arr)
- for i in range(l_a):
if i == 0:sum_b = sum(arr)else:for k in range(l_a-i+1):sum_c = sum(arr[k:k+i])if sum_c > sum_b: sum_b = sum_c- for k in range(l_a-i+1):
- sum_c = sum(arr[k:k+i])
- if sum_c > sum_b: sum_b = sum_c
- return(sum_b)
import codewars_test as test from solution import max_sequence @test.describe("Max Sequence Tests") def test_group(): @test.it("Case 1") def test_case_1(): test.assert_equals(max_sequence([0,1,2,3,4,5,6,7,8,9,10]), 55) test.assert_equals(max_sequence([-22,1,2,3,4,5,6,7,8,9,10]), 55) test.assert_equals(max_sequence([0,1,2,3,4,5,-6,7,8,9,10]), 43) test.assert_equals(max_sequence([0,0,0,0,0,0,0,0,0,99,-1, 23, 43]), 164) test.assert_equals(max_sequence([0] * 10), 0) test.assert_equals(max_sequence([-1] * 10), 0) test.assert_equals(max_sequence([1] * 10), 10)
- import codewars_test as test
- from solution import max_sequence
- @test.describe("Max Sequence Tests")
- def test_group():
- @test.it("Case 1")
- def test_case_1():
- test.assert_equals(max_sequence([0,1,2,3,4,5,6,7,8,9,10]), 55)
- test.assert_equals(max_sequence([-22,1,2,3,4,5,6,7,8,9,10]), 55)
- test.assert_equals(max_sequence([0,1,2,3,4,5,-6,7,8,9,10]), 43)
- test.assert_equals(max_sequence([0,0,0,0,0,0,0,0,0,99,-1, 23, 43]), 164)
- test.assert_equals(max_sequence([0] * 10), 0)
- test.assert_equals(max_sequence([-1] * 10), 0)
- test.assert_equals(max_sequence([1] * 10), 10)
29/2 - Added Tests
import codewars_test as test from solution import max_sequence @test.describe("Max Sequence Tests") def test_group(): @test.it("Case 1") def test_case_1(): test.assert_equals(max_sequence([0,1,2,3,4,5,6,7,8,9,10]), 55) test.assert_equals(max_sequence([-22,1,2,3,4,5,6,7,8,9,10]), 55) test.assert_equals(max_sequence([0,1,2,3,4,5,-6,7,8,9,10]), 43) test.assert_equals(max_sequence([0,0,0,0,0,0,0,0,0,99,-1, 23, 43]), 164)
- import codewars_test as test
# TODO Write testsimport solution # or from solution import example- from solution import max_sequence
# test.assert_equals(actual, expected, [optional] message)@test.describe("Example")- @test.describe("Max Sequence Tests")
- def test_group():
@test.it("test case")def test_case():test.assert_equals(1 + 1, 2)- @test.it("Case 1")
- def test_case_1():
- test.assert_equals(max_sequence([0,1,2,3,4,5,6,7,8,9,10]), 55)
- test.assert_equals(max_sequence([-22,1,2,3,4,5,6,7,8,9,10]), 55)
- test.assert_equals(max_sequence([0,1,2,3,4,5,-6,7,8,9,10]), 43)
- test.assert_equals(max_sequence([0,0,0,0,0,0,0,0,0,99,-1, 23, 43]), 164)
Tables
Data Structures