Skip to content

Instantly share code, notes, and snippets.

@andreescocard
Created March 15, 2024 02:28
Show Gist options
  • Save andreescocard/699fd88e31b16dda90f87304d9003bf0 to your computer and use it in GitHub Desktop.
Save andreescocard/699fd88e31b16dda90f87304d9003bf0 to your computer and use it in GitHub Desktop.
Sort text in arrow style
# List of strings
texts = [
"Big Size",
"Easy to fab",
"Can be storage outside",
"No fillers and cracks",
"Nice bookmatch",
"Requires minimum maintenance",
"Affordable price"
]
def sort_texts_arrow_pattern_v7(texts):
# Find the longest text (to be placed in the middle)
longest_text = max(texts, key=len)
# Remove the longest text from the original list
remaining_texts = [text for text in texts if text != longest_text]
# Sort the remaining texts by length
remaining_texts_sorted = sorted(remaining_texts, key=len)
# Split the sorted texts into two halves, ensuring the larger texts are distributed between the groups
first_half = [remaining_texts_sorted[i] for i in range(len(remaining_texts_sorted)) if i % 2 == 0]
second_half = [remaining_texts_sorted[i] for i in range(len(remaining_texts_sorted)) if i % 2 != 0]
# Sort the first half in ascending order and the second half in descending order
first_half_sorted = sorted(first_half)
second_half_sorted = sorted(second_half, key=lambda x: (len(x), -second_half.index(x)), reverse=True)
# Arrange the texts with the first half, then the longest, then the second half
arrow_text = first_half_sorted + [longest_text] + second_half_sorted
return arrow_text
# Apply the function and get the arrow pattern text
arrow_pattern_text = sort_texts_arrow_pattern_v7(texts)
# Prepare the text block in the 'arrow' pattern
arrow_text_block = "\n".join(arrow_pattern_text)
print(arrow_text_block)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment