Kata –

Complete the method so that it formats the words into a single comma separated value. The last word should be separated by the word ‘and’ instead of a comma. The method takes in an array of strings and returns a single formatted string. Empty string values should be ignored. Empty arrays or null/nil values being passed into the method should result in an empty string being returned.

formatWords(['ninja', 'samurai', 'ronin']) // should return "ninja, samurai and ronin"
formatWords(['ninja', '', 'ronin']) // should return "ninja and ronin"
formatWords([]) // should return ""

My Solution

def format_words(words):
    if not words:
        return ''
    words = [a for a in words if a]
    lw = len(words)
    if lw==0:
        return ''
    elif lw>1:
        return ', '.join(words[:-1])+' and '+words[-1:][0]
        return words[0]

Tests"Basic tests")
Test.assert_equals(format_words(['one', 'two', 'three', 'four']), 'one, two, three and four', "formatWords(['one', 'two', 'three', 'four'] should return 'one, two, three and four'")
Test.assert_equals(format_words(['one']), 'one', "formatWords(['one']) should return 'one'")
Test.assert_equals(format_words(['one', '', 'three']), 'one and three', "formatWords(['one', '', 'three']) should return 'one and three'")
Test.assert_equals(format_words(['', '', 'three']), 'three', "formatWords(['', '', 'three']) should return 'three'")
Test.assert_equals(format_words(['one', 'two', '']), 'one and two', "formatWords(['one', 'two', '']) should return 'one and two'")
Test.assert_equals(format_words([]), '', 'formatWords([]) should return ""')
Test.assert_equals(format_words(None), '', 'formatWords(null) should return ""')
Test.assert_equals(format_words(['']), '', 'formatWords([""]) should return ""')