-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions_and_fractals.sh
59 lines (53 loc) · 1.4 KB
/
functions_and_fractals.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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). A few samples are provided below."""
declare -A matrix
num_rows=63
num_columns=100
declare -a roots
roots[0]=50
for ((i=1;i<=num_rows;i++)) do
for ((j=1;j<=num_columns;j++)) do
matrix[$i,$j]='_'
done
done
read n
j=63
len=16
for ((i=1; i<=n; i++)) do
lim=$((${#roots[@]}-1))
elems=${#roots[@]}
old_j=$j
for((k=0; k<=lim; k++)) do
pos=${roots[$k]}
#print the trunk
for((m=0; m<=len-1; m++)) do
matrix[$j,$pos]='1'
((j--))
done
#print the branches
for((m=1; m<=len; m++)) do
matrix[$j,$((pos-m))]='1'
matrix[$j,$((pos+m))]='1'
((j--))
done
roots=("${roots[@]}" "$((pos-m+1))" "$((pos+m-1))" )
if (( $k < $lim ))
then
j=$old_j
fi
done
for((k=0; k<$elems; k++)) do
unset roots[$k]
done
roots=( "${roots[@]}" )
len=$((len/2))
done
for ((i=1;i<=num_rows;i++)) do
for ((j=1;j<=num_columns;j++)) do
printf ${matrix[$i,$j]}
done
printf "\n"
done