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.
I looked at the top few, and most basically hardcoded the tree. That's weak.
I was happy with my fairly unique answer. I can be cleaned up better, but I've already spent a few hours on this.
The basic idea was to form each tree based on a base row. At first the tree function was done in 3 parts - trunk, branch left, and branch right - but I was able to roll them into the same loop after I had all three working independantly.
From the base row, the trunk just copies the previous row (if there was a 1 at "base, col)" then you put a 1 at "trunkcur,col"). For the branches you look at the base row plus the offset so "base, col +/- i" (where i is the row difference from branchcur to base) to put a 1 in "branchcur, col +/- (base - branchcur)".
You add an additional 64th row that is just a 1 at "64,50" and now the trees can be built off that single starting point. frac() takes the number of iterations, starting line, and depth of the tree (starting line is always 63 and depth always starts at 16). Each recursive call decreaments iteration number (base case of 0 where nothing is printed at it returns), calculates a new starting row (decrementing 2 * length of last tree), and sets the next tree length to half the previous. So frac 5 63 16 calls frac 4 31 8 which calls frac 3 15 4 ...
Yes, this can all be done iteratively, but I was just too lazy to refactor it for such a small n. The nice thing about this approach is that all the constants are easily adjustable to make it work on any size board with any length trees and any number of branches even.
When the table is printed at the end, just remeber to leave off the 64th starting base row and you're good.
declare-Atabforrin{1..64};doforcin{1..100};dotab["$r,$c"]="_"donedonetab["64,50"]="1"functionprtab{forrin{1..63};doforcin{1..100};doecho-n${tab["$r,$c"]}doneechodone}functiontree{line=$1len=$2base=$(($line+1))foriin$(seq0$(($len-1)));dotrunkcur=$(($line-$i))branchcur=$(($line-$len-$i))forcin{1..100};do# trunkiftest"1"=${tab["$base,$c"]-_};thentab["$trunkcur,$c"]="1"fi# left branchright=$(($c+$i+1))iftest"1"=${tab["$base,$right"]-_};thentab["$branchcur,$c"]="1"fi# right branchleft=$(($c-$i-1))iftest"1"=${tab["$base,$left"]-_};thentab["$branchcur,$c"]="1"fidonedone}functionfrac{iter=$1start=$2len=$3if[$iter-eq0];thenreturnfitree$start$lenfrac$((iter-1))$((start-len*2))$((len/2))}readnfrac$n6316prtab
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 →
I looked at the top few, and most basically hardcoded the tree. That's weak.
I was happy with my fairly unique answer. I can be cleaned up better, but I've already spent a few hours on this.
The basic idea was to form each tree based on a base row. At first the tree function was done in 3 parts - trunk, branch left, and branch right - but I was able to roll them into the same loop after I had all three working independantly.
From the base row, the trunk just copies the previous row (if there was a 1 at "base, col)" then you put a 1 at "trunkcur,col"). For the branches you look at the base row plus the offset so "base, col +/- i" (where i is the row difference from branchcur to base) to put a 1 in "branchcur, col +/- (base - branchcur)".
You add an additional 64th row that is just a 1 at "64,50" and now the trees can be built off that single starting point.
frac()
takes the number of iterations, starting line, and depth of the tree (starting line is always 63 and depth always starts at 16). Each recursive call decreaments iteration number (base case of 0 where nothing is printed at it returns), calculates a new starting row (decrementing 2 * length of last tree), and sets the next tree length to half the previous. Sofrac 5 63 16
callsfrac 4 31 8
which callsfrac 3 15 4
...Yes, this can all be done iteratively, but I was just too lazy to refactor it for such a small n. The nice thing about this approach is that all the constants are easily adjustable to make it work on any size board with any length trees and any number of branches even.
When the table is printed at the end, just remeber to leave off the 64th starting base row and you're good.