Last active
February 17, 2025 09:45
-
-
Save JonasMoss/31c5489d25998ce07bf01d5b68bf2078 to your computer and use it in GitHub Desktop.
Longest words in alphabetical and reverse alphabetical order.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # Prompt to o3-mini-high: The file url = "https://raw.githubusercontent.com/dwyl/english-words/master/words.txt" contains "all" english words. I want a Python script that prints out the longest word with letters in alphabetical order (e.g., "abc") AND reverse alphabetical order (e.g., "cba"). Convert all words to lowercase and replace hyphens with nothing before doing this. The script should be executable immediately, so you need to provide code for downloading the file. | |
| import urllib.request | |
| def is_alphabetical(word): | |
| return list(word) == sorted(word) | |
| def is_reverse_alphabetical(word): | |
| return list(word) == sorted(word, reverse=True) | |
| def main(): | |
| url = "https://raw.githubusercontent.com/dwyl/english-words/master/words.txt" | |
| # Download the file | |
| with urllib.request.urlopen(url) as response: | |
| text = response.read().decode("utf-8") | |
| words = text.splitlines() | |
| longest_alpha = "" | |
| longest_rev_alpha = "" | |
| for word in words: | |
| # Preprocess the word: trim, convert to lowercase, and remove hyphens | |
| word = word.strip().lower().replace("-", "") | |
| if not word: | |
| continue | |
| if is_alphabetical(word) and len(word) > len(longest_alpha): | |
| longest_alpha = word | |
| if is_reverse_alphabetical(word) and len(word) > len(longest_rev_alpha): | |
| longest_rev_alpha = word | |
| print("Longest alphabetical word:", longest_alpha) | |
| print("Longest reverse alphabetical word:", longest_rev_alpha) | |
| if __name__ == "__main__": | |
| main() | |
| # Output: | |
| # > Longest alphabetical word: aegilops | |
| # > Longest reverse alphabetical word: spoonfeed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment