Experiments with Knuth's 5,757 five letter words. https://charlesreid1.com/wiki/Five_Letter_Words
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.1 KiB

  1. """
  2. lexico.py
  3. Donald Knuth, Art of Computer Programming, Volume 4 Facsimile 0
  4. Exercise #30
  5. Each letter of the word "first" appears in correct lexicographic order.
  6. Find the first and last such words in the SGB words.
  7. """
  8. from get_words import get_words
  9. def in_sorted_order(word):
  10. chars = list(word)
  11. if(str(chars)==str(sorted(chars))):
  12. return True
  13. else:
  14. return False
  15. if __name__=="__main__":
  16. words = get_words()
  17. words = sorted(words)
  18. count = 0
  19. print("-"*40)
  20. print("ALL lexicographically sorted words:")
  21. for word in words:
  22. if(in_sorted_order(word)):
  23. print(word)
  24. count += 1
  25. print("{0:d} total.".format(count))
  26. print("-"*40)
  27. for word in words:
  28. if(in_sorted_order(word)):
  29. print("First lexicographically sorted word:")
  30. print(word)
  31. break
  32. words.reverse()
  33. print("-"*40)
  34. for word in words:
  35. if(in_sorted_order(word)):
  36. print("Last lexicographically sorted word:")
  37. print(word)
  38. break