-
AlgorithmsLogic
Code get_nth_words = lambda string, n: "" if n<=0 else " ".join(string.split()[n-1::n])
Test Cases import codewars_test as test from solution import get_nth_words basictests = [ ["Hello World", 2, "World"], ["1 2 3 4 5 6 7 8 9", 3, "3 6 9"], ["Lorem ipsum dolor sit amet", 1, "Lorem ipsum dolor sit amet"], ["And a one and a two and a three and a four", 3, "one two three four"] ] notenoughwords = [ ["There aren't enough words.", 5, ""], ["What if there weren't enough words?", 15, ""], ["What are the chances of there being enough words?", 150, ""], ["", 5000, ""] ] attempterrors = [ ["", 1, ""], ["error 1 2 3 4", 0, ""], ["Hmm... What about this?", -1, ""], ["", -5, ""] ] .describe("Basic Tests") def basic_test_group(): .it("Basic Tests") def basic_tests(): for tst in basictests: test.assert_equals(get_nth_words(tst[0], tst[1]), tst[2]) .it("Not Enough Words") def not_enough_words(): for tst in notenoughwords: test.assert_equals(get_nth_words(tst[0], tst[1]), tst[2]) .it("Exception Handling") def cause_errors(): for tst in attempterrors: test.assert_equals(get_nth_words(tst[0], tst[1]), tst[2])
Output:
-
Code def get_nth_words(string, n):return " ".join(string.split()[n-1::n]) if n>0 else ""- get_nth_words = lambda string, n: "" if n<=0 else " ".join(string.split()[n-1::n])
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}