Bash Add
add() {
echo $(( $1 + $2 ))
}
This just adds the two input numbers with Bash arithmetic and prints the sum.
Bash Add Border
add_border() {
local -n _pic="$1"
local -n _outPic="$2"
local _width=${#_pic[0]}
local _border
_border=$(printf '%*s' "$_width" '' | tr ' ' '*')
_outPic=("$_border")
local _line
for _line in "${_pic[@]}"; do
_outPic+=("*${_line}*")
done
_outPic+=("$_border")
}
This builds a new grid with a * border around every side. It adds a full top and bottom row, then wraps each existing row from left and right.
Bash Adjacent Elements Product
adjacent_elements_product() {
local -n _arr="$1"
local _max=-9223372036854775808
local _i _c=${#_arr[@]}
for ((_i = 0; _i < _c - 1; _i++)); do
local _p=$(( _arr[_i] * _arr[_i+1] ))
if (( _p > _max )); then _max=$_p; fi
done
echo "$_max"
}
This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.
Bash Almost Magic Square
almost_magic_square() {
local -n _a="$1"
local -n _out="$2"
local -a _rowSum=(0 0 0) _colSum=(0 0 0)
local -a _m
local _i _j
for ((_i = 0; _i < 3; _i++)); do
for ((_j = 0; _j < 3; _j++)); do
_m[_i*3+_j]=${_a[_i*3+_j]}
done
done
for ((_i = 0; _i < 3; _i++)); do
for ((_j = 0; _j < 3; _j++)); do
_rowSum[_i]=$(( _rowSum[_i] + _m[_i*3+_j] ))
_colSum[_i]=$(( _colSum[_i] + _m[_j*3+_i] ))
done
done
local _maxSum=0
for ((_i = 0; _i < 3; _i++)); do
if (( _rowSum[_i] > _maxSum )); then _maxSum=${_rowSum[_i]}; fi
if (( _colSum[_i] > _maxSum )); then _maxSum=${_colSum[_i]}; fi
done
_i=0; _j=0
while (( _i < 3 && _j < 3 )); do
local _diffR=$(( _maxSum - _rowSum[_i] ))
local _diffC=$(( _maxSum - _colSum[_j] ))
local _diff=$(( _diffR < _diffC ? _diffR : _diffC ))
_m[_i*3+_j]=$(( _m[_i*3+_j] + _diff ))
_rowSum[_i]=$(( _rowSum[_i] + _diff ))
_colSum[_j]=$(( _colSum[_j] + _diff ))
if (( _rowSum[_i] == _maxSum )); then ((_i++)); fi
if (( _colSum[_j] == _maxSum )); then ((_j++)); fi
done
_out=("${_m[@]}")
}
This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.
Bash Are Equally Strong
are_equally_strong() {
local _yourLeft=$1 _yourRight=$2 _friendsLeft=$3 _friendsRight=$4
local _maxYou=$(( _yourRight > _yourLeft ? _yourRight : _yourLeft ))
local _maxFriend=$(( _friendsLeft > _friendsRight ? _friendsLeft : _friendsRight ))
local _minYou=$(( _yourLeft < _yourRight ? _yourLeft : _yourRight ))
local _minFriend=$(( _friendsRight < _friendsLeft ? _friendsRight : _friendsLeft ))
if (( _maxYou == _maxFriend && _minYou == _minFriend )); then
echo true
else
echo false
fi
}
This compares each person’s strongest and weakest arm. If both pairs match, the result is true.
Bash Array Change
array_change() {
local -n _a="$1"
local _min=0 _k _c=${#_a[@]}
for ((_k = 0; _k < _c - 1; _k++)); do
if (( _a[_k] >= _a[_k+1] )); then
local _dif=$(( _a[_k] - _a[_k+1] + 1 ))
_a[_k+1]=$(( _a[_k+1] + _dif ))
_min=$(( _min + _dif ))
fi
done
echo "$_min"
}
This moves left to right and bumps values only when needed so the array becomes strictly increasing.
Bash Array Maximal Adjacement Difference
array_maximal_adjacent_difference() {
local -n _a="$1"
local _dif=0 _i _c=${#_a[@]}
for ((_i = 1; _i < _c - 1; _i++)); do
local _d1=$(( _a[_i] - _a[_i-1] )); (( _d1 < 0 )) && _d1=$(( -_d1 ))
local _d2=$(( _a[_i] - _a[_i+1] )); (( _d2 < 0 )) && _d2=$(( -_d2 ))
(( _d1 > _dif )) && _dif=$_d1
(( _d2 > _dif )) && _dif=$_d2
done
echo "$_dif"
}
This checks the gap between each pair of neighbors and returns the largest difference.
Variable
NAME="John"
echo $NAME
echo "$NAME"
echo "${NAME}
This shows the common ways to read a Bash variable. Quoting is usually the safe default because it keeps spaces and special characters from breaking your output.
Condition
if [[ -z "$string" ]]; then
echo "String is empty"
elif [[ -n "$string" ]]; then
echo "String is not empty"
fi
This is the standard Bash string check. -z means the value is empty, and -n means it has content.
Bash Binary Gap
binary_gap() {
local _n=$1
local _bin=""
if (( _n == 0 )); then
_bin="0"
else
local _x=$_n
while (( _x > 0 )); do
_bin="$(( _x % 2 ))$_bin"
_x=$(( _x / 2 ))
done
fi
_bin="${_bin#"${_bin%%[!0]*}"}"
_bin="${_bin%"${_bin##*[!0]}"}"
local _gap=0 _len=0
local -a _zeros
IFS='1' read -ra _zeros <<< "$_bin"
local _z
for _z in "${_zeros[@]}"; do
_len=${#_z}
(( _len > _gap )) && _gap=$_len
done
echo "$_gap"
}
This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.
Bash Bracket
bracket() {
local _s=$1
local -a _stack=()
local _i _c
for ((_i = 0; _i < ${#_s}; _i++)); do
_c=${_s:_i:1}
case "$_c" in
')')
if (( ${#_stack[@]} == 0 )) || [[ "${_stack[-1]}" != "(" ]]; then
echo 0; return
fi
unset '_stack[-1]'
;;
']')
if (( ${#_stack[@]} == 0 )) || [[ "${_stack[-1]}" != "[" ]]; then
echo 0; return
fi
unset '_stack[-1]'
;;
'}')
if (( ${#_stack[@]} == 0 )) || [[ "${_stack[-1]}" != "{" ]]; then
echo 0; return
fi
unset '_stack[-1]'
;;
*)
_stack+=("$_c")
;;
esac
done
if (( ${#_stack[@]} == 0 )); then echo 1; else echo 0; fi
}
This uses a simple stack approach: open brackets go in, matching closing brackets pop them out.