Ad
Arrays
Sorting
Code
Diff
  • 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'
    
    • import re
    • def longest_words(array, num):
    • new = [k for k in sorted([''.join(re.findall(r"[A-Za-z]", j)) for j in array], key=lambda x: len(x), reverse=True) if k != '']
    • return new[:num] if num <= len(new) else 'Invalid Parameters'
    • 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'