Advertisement
Guest User

Untitled

a guest
May 25th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -o nounset
  4. set -o noclobber
  5. set -o errexit
  6. set -o pipefail
  7.  
  8. main() {
  9. if (( "${#}" != 2)); then
  10. printf '%s' 'Usage: hamming.sh <string1> <string2>'
  11. exit 1
  12. fi
  13.  
  14. : 'check two strings that have the same length' && {
  15. printf '%s\n%s' "${1}" "${2}" |
  16. awk '{ print length }' |
  17. uniq |
  18. (( $(wc -l) != 1)) &&
  19. {
  20. printf '%s' 'left and right strands must be of equal length'
  21. exit 1
  22. }
  23. }
  24.  
  25. : 'calculate hamming distance' && {
  26. paste <(printf '%s' "${1}" | fold -w 1) \
  27. <(printf '%s' "${2}" | fold -w 1) |
  28. grep -v '\(.\)[[:cntrl:]]\1' |
  29. wc -l ||
  30. true # FIXME
  31. }
  32. }
  33.  
  34. main "$@"
  35.  
  36. # vim:set tabstop=3 shiftwidth=3 :
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement