We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#You can find the code without comments in the next comment.`IhopemycommentswillhelpyoutounderstandhowIthought.Havefunandgoodluckwithyourwork!`#!/bin/bashdeclare-Amatrixreaditeration#-------fill in the matrix ------------for((i=1;i<=63;i++));dofor((j=1;j<=100;j++));domatrix[$i,$j]="_"donedone#------where the tree will start to grow------------startRow=64startcolumn=50# -----------number of iterations---o=$((2**(5-$iteration)))# --- function --- x=nr of the charachets in lenght. You can measure and you will find that the completed tree covers a square with lenght of a side of 63. All the measures are in reltion with powers of two.I started with 16 (number of trunk's lenght end its slanting segemnts. With each iteration the number of the lenght is halved. )for((x=16;x>=$o;x/=2));do#-------------verticale-------------------for((a=1;a<=x;a++));dostartRow=$((startRow-1))column=$startcolumnn=0while(($column<82));do#below, in the next comment I will write why I use 82. 82 is the edge of the squarematrix[$startRow,$(($column))]=1column=$(($column+(4*$x)))donedone#-------------------slanting segments (branches left and right)----------------#after each interation the place where the trunk (column) is ending is actually the start of the new branch. End of course at every row of the each iteration we have various number of trunks and branches in a straight relantionship with iteration. I use 82 to delimitate the repetion. The repetition is in relation with de number of the segment. branchleft=$startcolumnbranchright=$branchleftfor((a=1;a<=x;a++));dostartRow=$((startRow-1))n=0# I use increment for the both brancheswhile(($branchleft-$a+$n<82&&$branchright+$a+$n<82))domatrix[$startRow,$(($branchleft-$a+$n))]=1matrix[$startRow,$(($branchright+$a+$n))]=1n=$(($n+(4*$x)))donedone# that is the end of the writting branches and the start of the new interation. From this point new column are growing startcolumn=$(($branchleft-$x))done#------print the result-------------for((i=1;i<=63;i++));dofor((j=1;j<=100;j++));doecho-ne${matrix[$i,$j]}doneechodone``
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Functions and Fractals - Recursive Trees - Bash!
You are viewing a single comment's thread. Return to all comments →