Ad
Algorithms
Arrays
Strings
Fundamentals

Pluralising English

English is known for some oddities, and one of them is the rules for pluralisation.

Generally, a noun is made plural by adding an "s" on the end of the word, but for words ending in "s", "x" or "ch", the word is pluralised by adding an "es"

English also has some strange words that are spelled the same in the singular and the plural, like "moose" or "deer".

Some words have multiple plurals, like "octupus"

Since this is meant to be a simple challenge, we will ignore words with identical spellings and words like "octopus" will get a regular plural.

Sometime in the future, I might make a more complex challenge with all these irregularities included.

The kata

You will receive a string containing one or more space-separated words. Pluralise each word in the string and return it as a space-separated string.

Examples
---------------
cat        --> cats
peach      --> peaches
queen king --> queens kings
def pluralise(strng):
    if strng == "":
        return ""
    
    result = []
    
    for i in strng.split(" "):
        match i[len(i) - 1]:
            case "s":
                result.append(i + "es")  
            case "x":
                result.append(i + "es")  
            case "h":
                if i[len(i) - 2:] == "ch":
                    result.append(i + "es")
                else:
                    result.append(i + "s")
            case _:
                result.append(i + "s")  
    return " ".join(result)
Fundamentals
Code
Diff
  • class OrdinalNumbers:
        def __init__(self, n):
            self.n = n
            
        def solution(self):
            ht = {1: 'st', 2: 'nd', 3: 'rd'}
            last_digit = self.n % 10
            
            if self.n % 100 in [11, 12, 13] or last_digit not in [1,2,3]:
                return str(self.n) + 'th'
            elif last_digit in [1,2,3]:
                return str(self.n) + ht[last_digit]
                
        
           
                
    
    • class OrdinalNumbers:
    • def __init__(self, n):
    • self.n = n
    • def solution(self):
    • ht = {1: 'st', 2: 'nd', 3: 'rd'}
    • if len(str(self.n)) < 2 or self.n in [11, 12, 13]:
    • suffix = ht.get(self.n, 'th')
    • return f'{self.n}{suffix}'
    • else:
    • last_digit = self.n % 10
    • suffix = ht.get(last_digit, 'th')
    • return f'{self.n}{suffix}'
    • last_digit = self.n % 10
    • if self.n % 100 in [11, 12, 13] or last_digit not in [1,2,3]:
    • return str(self.n) + 'th'
    • elif last_digit in [1,2,3]:
    • return str(self.n) + ht[last_digit]