[Tutor] Luke, thanks a lot , here is the perfect code (as per ur suggestion)
Asrarahmed Kadri
ajkadri at googlemail.com
Thu Oct 5 16:36:45 CEST 2006
#Implementation of Pascal Triangle
num_of_lines = input("How many lines you want to display")
list1 = []
for i in range(num_of_lines):
flag = 0
tmp = []
for j in range(i+1):
if flag == 0 or j == i:
tmp.append(1) # for the first or teh last element of the line
flag = 1
else:
tmp.append(list1[i-1][j-1]+list1[i-1][j]) # for rest, add teh
numbers in previous row
list1.append(tmp)
# this code prints the Pascal triangle
for i in range(num_of_lines):
for j in range(i+1):
print list1[i][j],
print
Output for num_lines = 5:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
-----------------------------
THNAKS ONCE MORE.
On 10/5/06, Asrarahmed Kadri <ajkadri at googlemail.com> wrote:
>
> Thank you so much.
> I am trying to implement. I hope I can do it.
>
> A gentle note: This is not a homework question.
> I am learning python as a part of my Project work.
>
>
>
> On 10/5/06, Luke Paireepinart <rabidpoobear at gmail.com> wrote:
> >
> > Asrarahmed Kadri wrote:
> > > Its something very close.
> > > What I want is:
> > >
> > > 1
> > > 1 1
> > > 1 2 1
> > > 1 3 3 1
> > > 1 4 6 4 1
> > >
> > > This can be done using arrays, right or is there any other way??
> > They're not arrays, they're lists!
> > arrays imply contiguous memory blocks and single data types.
> > Lists are neither of these.
> > > My logic in C is:
> > >
> > > int arr[5][5];
> > > for (i=0;i<5;i++)
> > > {
> > > flag = 0;
> > > for (j=0;j<=i;j++)
> > > {
> > > if (flag == 0 || j == i) // print 1 if the array element is the
> > > first or the last number
> > > {
> > > arr[i][j] = 1;
> > > flag = 1;
> > > }
> > > arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
> > > }
> > Yes, you could do this in Python.
> > I'm guessing it'd be something like this:
> >
> > lst = []
> > for x in range(5):
> > tmp = []
> > for y in range(x):
> > if y == 0:
> > tmp.append(1)
> > else:
> > tmp.append(tmp[y-1]) #this line would be different
> > lst.append(tmp)
> >
> > That's the best I can do.
> >
> > HTH,
> > -Luke
> >
>
>
>
> --
> To HIM you shall return.
>
--
To HIM you shall return.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061005/e23e0a9e/attachment.html
More information about the Tutor
mailing list