{"id":878,"date":"2012-01-03T23:53:26","date_gmt":"2012-01-04T05:53:26","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=878"},"modified":"2012-01-04T00:08:01","modified_gmt":"2012-01-04T06:08:01","slug":"interview-programming-challenge-print-me-a-tree","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2012\/01\/interview-programming-challenge-print-me-a-tree\/","title":{"rendered":"Interview Programming Challenge: Print me a tree"},"content":{"rendered":"<p>Last Thursday I had a interview with a software company. During the interview that asked me write out a program which I was unable to complete within 10 minutes time. But I think am OK because their goal seemed to be to determine how I approached the problem rather than if I could produce a working program. <\/p>\n<p>Anyhow, I decided to finished the program during my spare time in case if it every comes up again. And here it is.<\/p>\n<p><b>Question:<\/b> (Not exact wording)<br \/>\nDesign a program in any language that will print a tree using only UNICODE or\/and ANSI characters. The program should accept user input an integer value to set the base of the tree.<br \/>\nExample:<\/p>\n<pre lang=\"perl\">\r\n\/\/ base length is 7\r\n   *\r\n  ***\r\n *****\r\n*******\r\n   *\r\n<\/pre>\n<p><b>My Program<\/b><br \/>\nI decided to use Groovy and Test Driven Development to solve this problem. After a little bit of thinking I managed to come up with the following two files that create a working program.<br \/>\nTree.groovy &#8211; Program that prints the tree<br \/>\nTreeTest.groovy &#8211; Test cases for Tree.groovy<\/p>\n<p><b>TreeTest.groovy<\/b> (Test Cases)<\/p>\n<pre lang=\"java\">\r\n\/**\r\n* @author Larry Battle\r\n* @date 1\/3\/2011\r\n* @purpose Provide test cases for the Tree class.\r\n*\/\r\nimport Tree;\r\n\r\nclass TreeTest extends GroovyTestCase{\r\n    private Tree tree = new Tree();\r\n    private testValues = [\r\n        [baseLength:-1, level:-1, length:0],\r\n        [baseLength:1, level:0, length:1],\r\n\t[baseLength:1, level:0, length:1],\r\n\t[baseLength:1, level:5, length:0],\r\n        [baseLength:1, level:9, length:0],\r\n        [baseLength:2, level:0, length:1],\r\n        [baseLength:2, level:2, length:1],\r\n\t[baseLength:2, level:5, length:0],\r\n\t[baseLength:6, level:0, length:1],\r\n\t[baseLength:6, level:2, length:5],\r\n        [baseLength:6, level:3, length:6],\r\n        [baseLength:6, level:4, length:1],\r\n        [baseLength:6, level:5, length:0],\r\n        [baseLength:7, level:0, length:1],\r\n        [baseLength:7, level:2, length:5],\r\n        [baseLength:7, level:3, length:7],\r\n        [baseLength:7, level:4, length:1],\r\n        [baseLength:7, level:5, length:0]\r\n    ];\r\n    void testHeight(){\r\n        def vals = [ 0:0, 1:1, 2:3, 3:3, 4:4, 7:5, 40:22 ];\r\n        def msg;\r\n        vals.each{\r\n            msg = \"baseLength of {$it.key} should have height of {$it.value}\";\r\n            assert tree.getHeight( it.key as short ) == it.value : msg;\r\n        }\r\n    }\r\n    void testTreeBranchLength(){\r\n        def msg; \r\n        def length;\r\n        testValues.each{ obj ->\r\n            length = tree.getBranchLength( obj.level, obj.baseLength );\r\n            msg = \"BaseLength $obj.baseLength at Level $obj.level should be $obj.length, not $length\";\r\n            assert length == obj.length : msg;\r\n        }\r\n    }\r\n\/\/ Other test cases not included.\r\n}<\/pre>\n<p><b>Tree.groovy<\/b><\/p>\n<pre lang=\"java\">\r\n\/**\r\n* @author Larry Battle\r\n* @date 1\/3\/2011\r\n* @purpose Tree Interview Question: Display a tree in ansi format with a specified integer base length.\r\n*\/\r\n\/\/ function documentation excluded.\r\nclass Tree{\r\n    short getHeight( baseLength ){\r\n        short height = 0;\r\n        if( baseLength > 0 ){\r\n            height = 1;\r\n            if( baseLength > 1 ){\r\n                height += Math.floor( baseLength \/ 2 ) + 1;\r\n            }\r\n        }\r\n        return height;\r\n    }\r\n    short getBranchLength( levelNum, baseLength ){\r\n        short length = 0;\r\n        short height = getHeight( baseLength );\r\n        if( levelNum > -1 && height > levelNum ){\r\n            if( height - levelNum == 2 ){\r\n                length = baseLength;\r\n            }else{\r\n                if( height - levelNum == 1 ){\r\n                    length = 1;\r\n                }else{\r\n                    length = 2 * levelNum + 1;\r\n                }\r\n            }\r\n        }\r\n        return length;\r\n    }\r\n    short getWhiteSpaceOnLeftNum( levelNum, baseLength ){\r\n        short i = 0;\r\n        short height = getHeight( baseLength );\r\n        if( levelNum > -1 && height > levelNum ){\r\n            if( height - levelNum == 2 ){\r\n                i = 0;\r\n            }else{\r\n                if( height - levelNum == 1){\r\n                    levelNum = 0;\r\n                }\r\n                i = Math.floor( baseLength \/ 2 ) - levelNum;\r\n            }\r\n        }\r\n        return i;\r\n    }\r\n    String getTreeBranch( levelNum, baseLength ){\r\n        char x = '*';\r\n        short xNum = getBranchLength( levelNum, baseLength );\r\n        short spaceNum = getWhiteSpaceOnLeftNum( levelNum, baseLength );\r\n        String str = \"\";\r\n        spaceNum.times{\r\n            str += ' ';\r\n        }\r\n        xNum.times{\r\n            str += x;\r\n        }\r\n        return str;\r\n    }\r\n    String[] getTreeAsStrArr( short baseLength ){\r\n        short height = getHeight( baseLength );\r\n        return (0..height-1).collect{\r\n            return getTreeBranch( it, baseLength );\r\n        }\r\n    }\r\n    void printTree( short baseLength ){    \r\n        if( 1 > baseLength || baseLength > 49 ){\r\n            throw new Error( \"BaseLength must be bigger than 1 and less than 50.\" );\r\n        }\r\n        String[] strArr = getTreeAsStrArr( baseLength );\r\n        println strArr.join( '\\n' );\r\n        println \"------Base length = $baseLength -------\";\r\n    }\r\n    static void main( String[] args ){\r\n        Tree a = new Tree();\r\n\tdef vals = [1,2,6,7,49];\r\n\tif( args.length ){\r\n\t\tvals = [];\r\n\t\targs.each{\r\n\t\t\tvals.push( Integer.parseInt( it ) );\r\n\t\t}\r\n\t}\r\n\tvals.each{\r\n\t\ta.printTree( it as short );\r\n\t}\r\n    }\r\n}<\/pre>\n<p><b>Output<\/b> ( Run Tree.groovy 2 7 12 > output.txt )<\/p>\n<pre lang=\"perl\">\r\n *\r\n**\r\n *\r\n------Base length = 2 -------\r\n   *\r\n  ***\r\n *****\r\n*******\r\n   *\r\n------Base length = 7 -------\r\n      *\r\n     ***\r\n    *****\r\n   *******\r\n  *********\r\n ***********\r\n************\r\n      *\r\n------Base length = 12 -------\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Last Thursday I had a interview with a software company. During the interview that asked me write out a program which I was unable to complete within 10 minutes time. But I think am OK because their goal seemed to be to determine how I approached the problem rather than if I could produce a &hellip; <a href=\"https:\/\/bateru.com\/news\/2012\/01\/interview-programming-challenge-print-me-a-tree\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Interview Programming Challenge: Print me a tree<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62],"tags":[108,180,107],"class_list":["post-878","post","type-post","status-publish","format-standard","hentry","category-questions-and-answers","tag-answer","tag-groovy","tag-interview-question"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=878"}],"version-history":[{"count":9,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/878\/revisions"}],"predecessor-version":[{"id":884,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/878\/revisions\/884"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}