Pascal with arbitrary rules help

This is the place for queries that don't fit in any of the other categories.

Pascal with arbitrary rules help

Postby XndrK » Tue Jan 14, 2014 6:32 pm

I'm trying to make a program that makes Pascal's Triangle with an arbitrary ruleset (e.g. you could multiply instead of adding or do the opposite of the hailstone equation). This is what I have so far.

Code: Select all
'''
Created on Jan 13, 2014

@author: XndrK
'''

def default_rule(num1, num2):
    return num1 + num2

class PascalTriangle:
   
    def __init__(self, height, rule=default_rule, identity=0):
        """
        Initiates PascalTriangle class
       
        Note: Set self.identity to 0 if the default rule is additive,
        1 if it's multiplicative.
       
        Unless, of course, you're being adventurous.
        """
        self.height = height
        self.rule = rule
        self.identity = identity
        self.triangle = [[1]] # the list in a list is on purpose
   
    def make_triangle(self):
        """
        Makes Pascal triangle
        """
        for i in range(self.height): # repeater
            current_row = self.triangle[i]
            new_row = []
            for index, item in enumerate(current_row):
                if index < len(current_row) - 1:
                    new_row.append(self.rule(item, current_row[index+1]))
                else:
                    new_row.append(self.rule(item, 1))
            self.triangle.append(new_row)
               
           

basic_pascal = PascalTriangle(6)
basic_pascal.make_triangle()
print basic_pascal.triangle


And this is what it returns right now.

Code: Select all
[[1], [2], [3], [4], [5], [6], [7]]


What am I doing wrong?
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Pascal with arbitrary rules help

Postby stranac » Tue Jan 14, 2014 7:28 pm

You need to start and end each row with ones.
Something like this should do the job:
Code: Select all
new_row = [1]
# pairwise loop
for a, b in zip(current_row, current_row[1:]):
    new_row.append(whatever)
new_row.append(1)
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1097
Joined: Thu Feb 07, 2013 3:42 pm

Re: Pascal with arbitrary rules help

Postby XndrK » Tue Jan 14, 2014 8:18 pm

Thanks for the zip() trick. Fixed it.

Code: Select all
'''
Created on Jan 13, 2014

Pascal Triangle generation program and class

@author: XndrK
'''

class Pascal:
   
    def __init__(self, height, rule=(lambda a, b: a+b)):
        self.height = height
        self.rule = rule
        self.triangle = [[1]]
        self.pascal()
   
    def pascal(self):
        for i in range(self.height):
            new_row = [1]
            current_row = self.triangle[i]
            print current_row
            for a, b in zip(current_row, current_row[1:]):
                new_row.append(a+b)
            new_row.append(1)
            self.triangle.append(new_row)
   
    def __repr__(self):
        return str(self.triangle)

pascal = Pascal(6)
print pascal
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests