You will be given a vector of string(s). You must sort it alphabetically (case-sensitive!!) and then return the first value.

The returned value must be a string and have “***” between each of its letters.

You should not remove or add elements from/to the array.

Above is another problem in codewars, besides asking us to sort the array list and returning the first value in that list, we also need to insert stars within the characters.

def two_sort(array):
    
    array.sort()
    first_string = array[0]
    first_star_string = ''
    limit = len(first_string)

    for i in range(0, limit):
        if i == 0:
            first_star_string += first_string[i]
        else:
            first_star_string +='***'+first_string[i]
            
    return first_star_string

The python solution above is straight forward but needs further improvement if possible, do write down your own answer in the comment box below this post.

Click to rate this post!
[Total: 0 Average: 0]
Please follow and like us:
error0

4 Comments

  1. Much simpler:

    >>> def two_sort(array):
    … array.sort()
    … return ‘***’.join(array[0])

    >>> two_sort([‘foo’, ‘bar’, ‘baz’])
    ‘b***a***r’

    And even simpler:

    >>> def two_sort(array):
    … return ‘***’.join(min(array))

    (if the array doesn’t need to be sorted)

  2. To insert the ‘***’ between each character, you can use `’***’.join(first_string)`. Also, I prefer not mutating the original input array:

    “`
    def two_sort(array):
    first_string = sorted(array)[0]
    return ‘***’.join(first_string)
    “`

  3. Unless we want array to be sorted after calling the function (as a side effect) then all we need is the first string alphabetically, with its characters separated by ‘***’:

    “`
    def two_sort(array):
    ‘***’.join(min(array))
    “`

  4. If you don’t want to mutate the array, you don’t even have to sort it since you’re only interested in the first string in sorted order anyway, which can be done with min(array) instead of sorted(array[0])

Leave a Reply

Your email address will not be published. Required fields are marked *