fork download
  1. #!/bin/bash
  2.  
  3. # ideone boilerplate - we can't write files in the home directory;
  4. # so create a temporary directory for our files instead
  5. t=$(mktemp -d -t ideone.XXXXXXXXXXXX) || exit
  6. trap 'rm -rf "$t"' ERR EXIT
  7. cd "$t"
  8.  
  9. cat <<\: >test_final.txt
  10. here are words in some sort of order 1234 hello123
  11. here are more words
  12. :
  13.  
  14. cat <<\: >demo.py
  15.  
  16. import random
  17. import re
  18. # import sys # unused import
  19.  
  20.  
  21. # Changed the regex so that it can actually match words
  22. mama = r"[a-zA-Z]+\d*"
  23.  
  24. # Python variables should prefer snake_case over headlessCamelCase
  25. list_of_words = []
  26. # Specify encoding here, too
  27. with open("test_final.txt", "r", encoding="utf-8") as f:
  28. for line in f:
  29. for word in line.rstrip('\n').split():
  30. # Does this line match the regular expression?
  31. if re.match(mama, word):
  32. list_of_words.append(word)
  33. else:
  34. # Print a warning when we are discarding a line
  35. print('# discarding', word)
  36.  
  37. print("Result: ", list_of_words)
  38.  
  39. random.shuffle(list_of_words)
  40. print("Shuffled stuff: ", list_of_words)
  41.  
  42. finalString = " ".join(list_of_words)
  43. print(finalString)
  44.  
  45. with open("test_final.txt", "w", encoding = 'utf-8') as file:
  46. file.write(finalString)
  47. :
  48.  
  49. echo '** before'
  50. cat test_final.txt
  51.  
  52. echo '** run'
  53. python3 demo.py
  54.  
  55. echo '** after'
  56. cat test_final.txt
  57.  
Success #stdin #stdout 0.03s 12004KB
stdin
Standard input is empty
stdout
** before
here are words in some sort of order 1234 hello123
here are more words
** run
# discarding 1234
Result:  ['here', 'are', 'words', 'in', 'some', 'sort', 'of', 'order', 'hello123', 'here', 'are', 'more', 'words']
Shuffled stuff:  ['sort', 'more', 'here', 'order', 'in', 'of', 'here', 'words', 'are', 'are', 'hello123', 'words', 'some']
sort more here order in of here words are are hello123 words some
** after
sort more here order in of here words are are hello123 words some