Double Generator Expression

Ακρώνυμο
Ακρώνυμο στη διαδρομή Python
from string import ascii_letters


VALID_CHARS = {" ", "-"} | set(ascii_letters)


def abbreviate(to_abbreviate):
    to_abbreviate = "".join(" " if char == "-" else char
                            for char in to_abbreviate
                            if char in VALID_CHARS)

    return "".join(word[0] for word in to_abbreviate.split()).upper()

One way someone might try to increase performce is to use a single generator expression to clean the input, rather than using multiple calls to str.replace(). However, this approach is actually amongst the slower ones. (See the performance article for more detail.)

In this approach, the VALID_CHARS constant is first defined using string.ascii_letters, a space, and a hyphen. In abbreviate(), the first generator expression iterates over to_abbreviate, excluding any code points that are not a member of the VALID_CHARS set. For each code point that is not excluded, the expression passes it into str.join() (unless it is a hyphen, in which case it replaces the hyphen with a space). to_abbreviate is then set to the result of the str.join(), preparing it for the next step.

Next, to_abbreviate.split() is used to split to_abbreviate into words separated by whitespace — we can ignore the case of hyphens as we already replaced all of them with spaces. Now the second generator expression iterates over the list returned by to_abbreviate.split(), yeilding the first code point in each word. These code points are passed to another str.join(), which is then chained to str.upper(). Now that both steps are complete, we return the result of str.upper() directly on the same line.

Translation missing: el.number.nth.ordinalized Sep 2026 · Σου φάνηκε χρήσιμη;

Άλλες προσεγγίσεις για την άσκηση Ακρώνυμο στη διαδρομή Python

Άλλοι τρόποι με τους οποίους η κοινότητά μας έλυσε αυτή την άσκηση
from functools import reduce

def abbreviate(to_abbreviate):
    phrase = to_abbreviate.replace("-", " ").replace("_", " ").upper().split()

    return reduce(lambda start, word: start + word[0], phrase, "")
Functools Reduce

Use functools.reduce() to form an acronym from text cleaned using str.replace().

def abbreviate(to_abbreviate):
    phrase = to_abbreviate.replace("-", " ").replace("_", " ").upper().split()

    # Note the lack of square brackets around the comprehension.
    return "".join(word[0] for word in phrase)
Generator Expression

Use a generator expression with str.join() to form an acronym from text cleaned using str.replace().

def abbreviate(to_abbreviate):
    phrase = to_abbreviate.replace("-", " ").replace("_", " ").upper().split()

    return "".join([word[0] for word in phrase])
List Comprehension

Use a list comprehension with str.join() to form an acronym from text cleaned using str.replace().

def abbreviate(to_abbreviate):
    phrase = to_abbreviate.replace("-", " ").replace("_", " ").upper().split()
    acronym = ""

    for word in phrase:
        acronym += word[0]

    return acronym
Loop

Use str.replace() to clean the input string and a loop with string concatenation to form the acronym.

def abbreviate(to_abbreviate):
    phrase = to_abbreviate.replace("-", " ").replace("_", " ").upper().split()
    
    return "".join(map(lambda word: word[0], phrase))
Map Built-in

Use the built-in map() function to form an acronym after cleaning the input string with str.replace().

import re

def abbreviate(phrase):
    removed = re.findall(r"[a-zA-Z']+", phrase)

    return "".join(word[0] for word in removed).upper()
Regex join

Use regex to clean the input string and form the acronym with str.join().

import re

def abbreviate_regex_sub(to_abbreviate):
    pattern = re.compile(r"(?<!_)\B[\w']+|[ ,\-_]")

    return re.sub(pattern, "", to_abbreviate.upper())
Regex Sub

Use re.sub() to clean the input string and create the acronym in one step.