Given two integers, X and Y , identify whether X < Y or X > Y or X = Y.
Exactly one of the following lines:
- X is less than Y
- X is greater than Y
- X is equal to Y
read -r X
read -r Y
if [[ X -lt Y ]]; then echo X is less than Y; fi
if [[ X -gt Y ]]; then echo X is greater than Y; fi
if [[ X -eq Y ]]; then echo X is equal to Y; fi
Getting Started with Conditionals
Read in one character from STDIN. If the character is 'Y' or 'y' display "YES". If the character is 'N' or 'n' display "NO". No other character will be provided as input.
read -r input;
if [[ $input == "y" || $input == "Y" ]]; then echo YES; fi;
if [[ $input == "n" || $input == "N" ]]; then echo NO; fi;
More On Conditionals
Given three integers (, , and ) representing the three sides of a triangle, identify whether the triangle is scalene, isosceles, or equilateral.
If all three sides are equal, output EQUILATERAL.
Otherwise, if any two sides are equal, output ISOSCELES.
A mathematical expression containing +,-,*,^, / and parenthesis will be provided. Read in the expression, then evaluate it. Display the result rounded to decimal places.
read -r exp
printf %.3f $(echo "$exp" | bc -l)
Compute the Average
Given N integers, compute their average, rounded to three decimal places.
read -r N
declare -i total
for ((i = 0; i < $N; i++)); do
read -r temp
((total += temp))
done
printf %.3f $(echo "$total / $N" | bc -l)
Functions and Fractals - Recursive Trees - Bash!
This challenge involves the construction of trees, in the form of ASCII Art.
We have to deal with real world constraints, so we cannot keep repeating the pattern infinitely. So, we will provide you a number of iterations, and you need to generate the ASCII version of the Fractal Tree for only those many iterations (or, levels of recursion).
declare -A matrix
grid()
{
for(( i=0; i < $1; i++ )) do
for(( j=0; j<$2; j++ )); do
matrix[$i,$j]="_"
done
done
}
print()
{
for(( i=0; i < $1; i++ )) do
for(( j=0; j<$2; j++ )); do
printf "%s" ${matrix[$i,$j]}
done
printf "\n"
done
}
fractal()
{
local r=$1
local c=$2
local itr=$3
local depth=$(($4 - 1))
if (( depth == 0 )); then
return 0;
fi
#make the trunk
for(( i=0; i<$itr; i++ )); do
matrix[$(($r - $i)),$c]="1"
done
#left diagonal
for(( i=0; i<$itr; i++ )); do
matrix[$(($r - $itr - $i)),$(($c - $i - 1))]="1";
done
#right diagonal
for(( i=0; i<$itr; i++ )); do
matrix[$(($r - $itr - $i)),$(($c + $i + 1))]="1";
done
#make Y in left diagonal
fractal $(($r - 2 * $itr)) $(($c - $itr)) $(($itr / 2)) $depth
#make Y in right diagonal
fractal $(($r - 2 * $itr)) $(($c + $itr)) $(($itr / 2)) $depth
}
main()
{
ROWS=63
COLS=100
grid $ROWS $COLS
read depth
fractal 62 49 16 $(($depth + 1))
print $ROWS $COLS
}
main