language_chooser.sh 902 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/bin/sh
  2. #languages="C++
  3. #Python
  4. #TypeScript
  5. #C
  6. #Go
  7. #Ruby
  8. #PHP
  9. #Racket"
  10. #
  11. #echo "$languages" | shuf | head -n5
  12. prob="$1"
  13. test -n "$prob" || { echo "Pass the problem number"; exit 1;}
  14. lang_frek="$(find . -maxdepth 1 -mindepth 1 -not -path './.*' -type d \
  15. | parallel -j1 'find {} -maxdepth 1 -mindepth 1 -type f | wc -l | xargs echo {}' \
  16. | sort -k2)"
  17. lest_amount="$(echo "$lang_frek" | head -n1 | cut -d' ' -f2)"
  18. possibles="$(echo "$lang_frek" | grep "$lest_amount" | sed "s/ ${lest_amount}//g")"
  19. chosen="$(echo "$possibles" | shuf | head -n1)"
  20. newest_file="$(find "${chosen}" -type f -printf "%T@ %p\n" \
  21. | sort -n \
  22. | cut -d' ' -f 2- \
  23. | tail -n 1)"
  24. bn="$(basename "$newest_file")"
  25. ext="$(echo "$bn" | cut -d'.' -f2)"
  26. cp "$newest_file" "${chosen}/${prob}.${ext}"
  27. echo "Language ${chosen}"
  28. echo "File: '${chosen}/${prob}.${ext}'"