[关闭]
@ysongzybl 2015-04-30T18:39:17.000000Z 字数 4170 阅读 1599

Bash script to insert problem description from leetcode by Hao Chen

bash


Some Scripts

comments.sh

comments.sh - it's used to generate author and problem description comments

Notes:
- The comments.sh needs xidel to grab & parse the html contents with filtering out the HTML tags. The script would check the xidel whether installed or not in your Linux box, if not, it will download the Universal Linux Version .
- The comments.sh currently can automatically detect the problem's type, if the problem is algorithm type, it's going to create .cpp file, if the problem is shell type, the .sh file will be created.

1) Create a file named largestNumber.cpp, and add Copyright & Problem description

  1. ./comments.sh https://oj.leetcode.com/problems/largest-number/

2) Add Copyright & Problem description into existed file

  1. ./comments.sh https://oj.leetcode.com/problems/largest-number/ largestNumber.cpp

The comments would be generated by above examples as below:

  1. // Source : https://oj.leetcode.com/problems/largest-number/
  2. // Author : Hao Chen
  3. // Date : 2015-01-25
  4. /**********************************************************************************
  5. *
  6. * Given a list of non negative integers, arrange them such that they form the largest number.
  7. *
  8. * For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.
  9. *
  10. * Note: The result may be very large, so you need to return a string instead of an integer.
  11. *
  12. * Credits:Special thanks to @ts for adding this problem and creating all test cases.
  13. *
  14. **********************************************************************************/
  1. #!/bin/bash
  2. set -e
  3. AUTHOR="Hao Chen"
  4. LEETCODE_URL=https://leetcode.com/problems/
  5. LEETCODE_NEW_URL=https://leetcode.com/problems/
  6. LEETCODE_OLD_URL=https://oj.leetcode.com/problems/
  7. COMMENT_TAG="//"
  8. FILE_EXT=".cpp"
  9. function usage()
  10. {
  11. echo -e "Usage: ${0} [url] [source_file]"
  12. echo -e ""
  13. echo -e "Example:"
  14. echo -e ""
  15. echo -e " 1) Create a file named largestNumber.cpp, and add Copyright & Problem description"
  16. echo -e " ${0} https://oj.leetcode.com/problems/largest-number/"
  17. echo -e ""
  18. echo -e " 2) Add Copyright & Problem description into existed file"
  19. echo -e " ${0} https://oj.leetcode.com/problems/largest-number/ largestNumber.cpp"
  20. echo -e ""
  21. }
  22. function install_xidel()
  23. {
  24. echo "Install xidel ..."
  25. if [ ! -d ./xidel ]; then
  26. mkdir xidel
  27. fi
  28. cd xidel
  29. linux=`uname -m`
  30. xidel_tar=xidel-0.8.4.linux64.tar.gz
  31. case $linux in
  32. x86_64 ) xidel_tar=xidel-0.8.4.linux64.tar.gz
  33. ;;
  34. i686 ) xidel_tar=xidel-0.8.4.linux32.tar.gz
  35. ;;
  36. * ) echo "Cannot install xidel, please install it manually!"
  37. exit 1;
  38. esac
  39. if [ ! -f ${xidel_tar} ]; then
  40. echo "Downloading xidel....."
  41. curl -L http://softlayer-sng.dl.sourceforge.net/project/videlibri/Xidel/Xidel%200.8.4/${xidel_tar} -o ${xidel_tar}
  42. fi
  43. tar -zxvf ${xidel_tar}
  44. ./install.sh
  45. cd ..
  46. echo "Install xidel successfullly !"
  47. }
  48. if [ $# -lt 1 ] || [[ "${1}" != ${LEETCODE_NEW_URL}* ]] && [[ "${1}" != ${LEETCODE_OLD_URL}* ]]; then
  49. usage
  50. exit 255
  51. fi
  52. if [[ "${1}" == ${LEETCODE_OLD_URL}* ]]; then
  53. LEETCODE_URL=${LEETCODE_OLD_URL}
  54. fi
  55. IS_SHELL=`curl ${1} 2>/dev/null | grep Bash |wc -l`
  56. if [ ${IS_SHELL} -gt 0 ]; then
  57. COMMENT_TAG='#'
  58. FILE_EXT='.sh'
  59. fi
  60. leetcode_url=$1
  61. current_time=`date +%Y-%m-%d`
  62. if [ $# -gt 1 ] && [ -f $2 ]; then
  63. source_file=$2
  64. current_time=`stat -c %x ${source_file} | awk '{print \$1}'`
  65. else
  66. source_file=${1#${LEETCODE_URL}}
  67. source_file=${source_file::${#source_file}-1}
  68. source_file=`echo $source_file | awk -F '-' '{for (i=1; i<=NF; i++) printf("%s", toupper(substr($i,1,1)) substr($i,2)) }'`${FILE_EXT}
  69. if [ ! -f ${source_file} ]; then
  70. echo "Create a new file - ${source_file}."
  71. echo -e "\n" > ${source_file}
  72. current_time=`date +%Y-%m-%d`
  73. else
  74. current_time=`stat -c %x ${source_file} | awk '{print \$1}'`
  75. fi
  76. fi
  77. #adding the Copyright Comments
  78. if ! grep -Fq "${COMMENT_TAG} Author :" $source_file ; then
  79. sed -i '1i\'"${COMMENT_TAG} Source : ${leetcode_url}" $source_file
  80. sed -i '2i\'"${COMMENT_TAG} Author : ${AUTHOR}" $source_file
  81. sed -i '3i\'"${COMMENT_TAG} Date : ${current_time}\n" $source_file
  82. fi
  83. #grab the problem description and add the comments
  84. xidel=`type -P xidel || /bin/true`
  85. if [ -z "${xidel}" ]; then
  86. echo "xidel not found !"
  87. install_xidel
  88. fi
  89. case $FILE_EXT in
  90. .cpp ) xidel ${leetcode_url} -q -e "css('div.question-content')" | \
  91. grep -v ' ' |sed '/^$/N;/^\n$/D' | \
  92. sed 's/^/ * /' | sed "1i/*$(printf '%.0s*' {0..80}) \n * " | \
  93. sed "\$a \ $(printf '%.0s*' {0..80})*/\n" > /tmp/tmp.txt
  94. ;;
  95. .sh ) xidel ${leetcode_url} -q -e "css('div.question-content')" | \
  96. grep -v ' ' |sed '/^$/N;/^\n$/D' | \
  97. sed 's/^/# /' | sed "1i#$(printf '%.0s#' {0..80}) \n# " | \
  98. sed "\$a \#$(printf '%.0s#' {0..80})\n" > /tmp/tmp.txt
  99. ;;
  100. * ) echo "Bad file extension!"
  101. exit 1;
  102. esac
  103. sed -i '4 r /tmp/tmp.txt' ${source_file}
  104. rm -f /tmp/tmp.txt
  105. echo "${source_file} updated !"

Source:

https://github.com/haoel/leetcode.git

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注