Ad
Algorithms
Logic

Get every nth word in the string.

Code
Diff
  • def get_nth_words(string, n):
        if n < 1: 
            return ""
        else:
            return ' '.join(string.split()[n-1::n])
    • def get_nth_words(string, n):
    • if n < 1: return ""
    • return ' '.join(['',*string.split()][::n][1:])
    • if n < 1:
    • return ""
    • else:
    • return ' '.join(string.split()[n-1::n])

Write code that converts number(0-9) to string(zero-nine)

Code
Diff
  • def converter(number):
        if 0<=number<10:    
            return ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"][int(number)]
        else:
            return None
    
    • def converter(number):
    • if number == 0:
    • return "zero"
    • elif number == 5:
    • return "five"
    • return number
    • if 0<=number<10:
    • return ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"][int(number)]
    • else:
    • return None