united-coders

TwitterFacebookGoogleRSS
  • Home
  • Authors
    • Christian Harms
    • Nico Heid
  • Newsletter
Home » Q*Bert chrismas tree puzzle

Q*Bert chrismas tree puzzle

Posted on December 19, 2011 by Christian Harms Posted in Uncategorized 9 Comments

This is a short coding puzzle with q*bert (who is q*bert?) and an chrismas tree.

the qbert question

qbert chrismas tree coding puzzle
The task: Our 2011 q*bert can only jump down. He start from the top and has to find the path to the bottom of the chrismas tree with collecting the most points available (while jumping left-or-right down). Find the way with the highest sum.

Input:

chrismasTree = [[75],
              [95,64],
            [17,47,82],
          [18,35,87,10],
        [20, 4,82,47,65],
      [19, 1,23,75, 3,34] ]
print qbertRun(chrismasTree)

The sum is 465 – try to solve it with a little program. The brute force method will be possible with this small tree.

Does your solution solve the task with larger data?

Project Euler offers the same question (Problem 67) with a larger triangle. Does work your solution with this data set?

tri = []
peUrl = "http://projecteuler.net/project/triangle.txt"
for line in urllib.urlopen(peUrl).readlines():
    tri.append([int(x) for x in line.strip().split(" ")])
print qbertRun(tri)

If you solved the larger data set and had fun register on project euler and try out other problems. Feel free to link your solution from you blog to this article and write a comment. My solution will published this year.

  • Bio
  • Latest Posts
My Google+ profile

Christian Harms

I am working as a software developer and project manager in germany. I have a strong background with python and javascript coding and design/develop web services with all aspects starting with http headers, scaling and fail-over issues. I use my knowledge as project manager and team motivator in the successful german automotive branche.

Latest posts by Christian Harms (see all)

  • google code jam 2013 – tic-tac-toe-Tomek solution - April 16, 2013
  • Google code jam 2013 – the lawnmower - April 14, 2013
  • code puzzles and permutations - April 11, 2013
code puzzle project euler python
« Automated frontend testing for the lazy. With Firefox, Selenium, Webdriver and Hudson/Jenkins. Now with 50% less programming.
facebook Hacker Cup 2012 Qualification Round »

9 thoughts on “Q*Bert chrismas tree puzzle”

  1. Robin Houston says:
    December 19, 2011 at 12:18

    How do you get 244 for the small example? I make it 465: 75 + 64 + 82 + 87 + 82 + 75. What have I got wrong?

  2. Christian says:
    December 19, 2011 at 12:46

    you are right! Best way is 75+64+82+87+82+75 = 465, second best way is 75+95+47+87+82+75 = 461.

  3. undu says:
    December 19, 2011 at 13:06

    Yes, I get the same here.

  4. Anonymous says:
    December 19, 2011 at 13:50
    print r'''Kqrs d(gerr):K  fby = [0]*(yra(gerr)+1)K  sbe ynlre va erirefrq(gerr):K    fby = [k + znk(fby[v:v+2]) sbe (v,k) va rahzrengr(ynlre)]K  erghea fby[0]K'''.decode('rot13').replace('X', '\n')
    
  5. Marius Gedminas says:
    December 19, 2011 at 20:15

    It looks like this should be efficiently solvable using dynamic programming. For every position the best score

    S(r, c) = max(S(r-1, c), S(r-1, c-1)) + christmasTree[r][c]

    and to handle corner cases

    S(r, 0) = S(r-1, c) + christmasTree[r][0]
    S(0, 0) = christmasTree[0][0]

    and the final answer is max(S(r, c) for c in range(r + 1)) where r = len(christmasTree) – 1. All you need to do is calculate those S(r, c) in the right order (i.e. increasing r).

  6. Matthias Reuter says:
    December 20, 2011 at 07:04
    christmastree.reduceRight(function (a, b) {
        return b.map(function (n, i) {
            return n + Math.max(a[i], a[i+1]);
        });
    })[0];
    
  7. Willyfrog says:
    December 23, 2011 at 08:33

    What you are asking in your problem, and the one for projet euler are in fact different problems, being yours slightly more difficult. The difference would be that yours is asking for the path to get the maximum sum, and PE’s one is only asking for the actual sum. Obviuosly not taking into account the size of the data sample.
    Almost the same, but not quite ;)

  8. Nico Heid says:
    December 30, 2011 at 16:14

    As I’m a real fan up bottom up for this solution, I’ll just add the max of the tow numbers to the row above and work my way up. I left the recursion just because I can, not because it’s necessary. :D (otherwise just iterate over the tree row times)

    public class Treecalc {
        public static void main(String[] args) {
    
            int[][] tree = {{75},
                    {95, 64},
                    {17, 47, 82},
                    {18, 35, 87, 10},
                    {20, 4, 82, 47, 65},
                    {19, 1, 23, 75, 3, 34}};
    
            new Treecalc().calculate(tree, tree.length - 1);
    
        }
    
    
        public void calculate(int[][] tree, int row) {
            if (row == 0) {
                System.out.println(tree[0][0]);
            } else {
                for (int i = 0; i < tree[row - 1].length; i++) {
                    tree[row - 1][i] += Math.max(tree[row][i], tree[row][i + 1]);
                }
                calculate(tree, row - 1);
            }
        }
    }
    
    
  9. Nico Heid says:
    January 12, 2012 at 08:46

    gung’f n ovg areql ;)

Leave a comment Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tags

android code jam code puzzle hackercup hosting java javascript linux permutations project euler python server

Recent Comments

  • http://underarmwhitening.Info on Number crunching with javascript – a tutorial
  • Nico Heid on A scalable, affordable WordPress hosting, lessons learned
  • Per Quested Aronsson on A scalable, affordable WordPress hosting, lessons learned
  • Christian Harms on A scalable, affordable WordPress hosting, lessons learned
  • Yoda Conditions | Pack 6 – Palo Alto on What are yoda conditions?

Recent Posts

  • All you need to know about Raspberry Pi colocation offers
  • A scalable, affordable WordPress hosting, lessons learned
  • google code jam 2013 – tic-tac-toe-Tomek solution
  • Google code jam 2013 – the lawnmower
  • code puzzles and permutations

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Copyright

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
© united-coders
  • This is a short coding puzzle with q*bert (who is q*bert?) and an chrismas tree. the qbert question The t