This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu -o pipefail | |
# Given a direction and a number of columns, write a function that outputs | |
# an arrow of asterisks | |
if [ $# -ne 2 ] || | |
! ( ( [ $( echo $1 | tr [':upper:'] [':lower:'] ) == 'left' ] || [ $( echo $1 | tr [':upper:'] [':lower:'] ) == 'right' ] ) && [ -z "$(printf '%s\n' "$2" | sed 's/[0-9]//g')" ] ); then | |
echo "$(basename $0): enter 'left' or 'right' and a positive integer, e.g. 'left 4' or 'right 3'" | |
exit 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu -o pipefail | |
# Imagine your users are all typing slightly incorrectly, in that they shifted their | |
# hands one key to the right. Write a function that translates what they mean to say. | |
if [ $# -ne 1 ]; then | |
echo "$(basename $0): enter a mis-typed string, e.g. ';p; epe'" | |
exit 1 | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu -o pipefail | |
# Given an IPv4 address and a netmask in CIDR notation, return a boolean | |
# specifying whether the IP address is inside the given range. | |
# requirements: | |
# # bash version >= 4 | |
# # 'grepcidr' command (http://www.pc-tools.net/unix/grepcidr/) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu -o pipefail | |
# Given a positive integer n, write a function that finds the number of zeros | |
# at the end of n! in base 10. | |
if [ $# -eq 0 ] || [ -n "$(printf '%s\n' "$1" | sed 's/[0-9]//g')" ]; then | |
echo "$(basename "$0"): enter a positive integer" | |
exit 1 | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu -o pipefail | |
# You have to order wrapping paper for presents. Given the length, width, and height of | |
# the boxes you need to wrap, return the number of square feet (or whatever units you | |
# want) of wrapping paper you need to order. Extra credit: allow for other shapes of | |
# presents and their dimensions! | |
if [ $# -ne 3 ]; then | |
echo "$(basename $0): enter three nonzero positive integers (length, width, depth)" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu -o pipefail | |
# Given a file named merry-christmas.txt that has a single integer x in it, write a script | |
# to generate a Christmas tree with asterisks (*) and output it as happy-holidays.txt. The | |
# tree should have a height of x, and if merry-christmas.txt is empty, the height should | |
# be 25 asterisks tall. | |
IN_FILE="merry-christmas.txt" | |
OUT_FILE="happy-holidays.txt" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu -o pipefail | |
# return true if input is a perfect square | |
isPerfectSquare () { | |
square_root=$( bc <<< "sqrt ($1)" ) | |
return $( [ $(( $square_root * $square_root )) == $1 ] ) | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu -o pipefail | |
# substitute asterisks for all but first and last characters of string | |
obscureString () { | |
str="$1" | |
ret="${str:0:1}$( printf "%0.s*" $( seq 1 $(( ${#str}-2 )) ) )${str: -1}" | |
echo $ret | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu -o pipefail | |
# initialize vars | |
runningTotal="0" | |
# normalized grade names to point values | |
A="4" AMINUS="3.7" | |
BPLUS="3.3" B="3" BMINUS="2.7" | |
CPLUS="2.3" C="2" CMINUS="1.7" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu -o pipefail | |
# initialize vars | |
ordinal='' | |
# check input | |
ARGC="$#" | |
if [[ "$ARGC" -ne 1 ]]; then | |
echo "Please enter an integer number, e.g. 123" |