I am doing some Codewars practice and got this problem. The goal is to determine if the word provided “text” ends with “ending”. The solution i have works with every test case but (sumo, omo). For some reason, the for loop returns to 0 after 1. This does not occur with any other test case.
def solution(text, ending):
text_list = list(text)
ending_list = list(ending)
results_list = []
text_list.reverse()
ending_list.reverse()
if len(text_list) >= len(ending_list):
for i in ending_list:
num = ending_list.index(i)
if ending_list[num] == text_list[num]:
results_list.append(0)
print(num)
else:
results_list.append(1)
if sum(results_list) == 0:
return True
else:
return False
else:
return False
Above is my Code
from solution import solution
import codewars_test as test
from random import randint
fixed_tests_True = (
( "samurai", "ai" ),
( "ninja", "ja" ),
( "sensei", "i" ),
( "abc", "abc" ),
( "abcabc", "bc" ),
( "fails", "ails" ),
)
fixed_tests_False = (
( "sumo", "omo" ),
( "samurai", "ra" ),
( "abc", "abcd" ),
( "ails", "fails" ),
( "this", "fails" ),
( "spam", "eggs" )
)
@test.describe("Fixed Tests")
def test_group():
@test.it("True Cases")
def test_case():
for text, ending in fixed_tests_True:
test.assert_equals(solution(text, ending), True)
@test.it("False Cases")
def test_case():
for text, ending in fixed_tests_False:
test.assert_equals(solution(text, ending), False)
Above is the test case provided.