Ad
Arrays
Sorting
Code
Diff
  • import re
    
    def longest_words(array, num):
        valid_words = [re.sub(r'[^A-Za-z]', '', word) for word in array if word and re.sub(r'[^A-Za-z]', '', word)]
        return sorted(valid_words, key=len, reverse=True)[:num] if num <= len(valid_words) else 'Invalid Parameters'
    • import re
    • def longest_words(array, num):
    • cleaned_words = [re.sub(r'[^A-Za-z]', '', word) for word in array]
    • valid_words = [word for word in cleaned_words if word]
    • return sorted(valid_words, key=len, reverse=True)[:num] if num <= len(valid_words) else 'Invalid Parameters'
    • valid_words = [re.sub(r'[^A-Za-z]', '', word) for word in array if word and re.sub(r'[^A-Za-z]', '', word)]
    • return sorted(valid_words, key=len, reverse=True)[:num] if num <= len(valid_words) else 'Invalid Parameters'