Coding Help

2007-11-10 Thread rishiyoor
I need help coding my flowchart. The C's are conditions, the S's are
statements. The statements do not affect the conditions except for S5
which is an increment for C0. The left is True, and the right is
False.

I would probably use a while loop (or for loop without S5) for the
first condition C0, and I am thinking of evaluating the rest of the
conditions using if statements. But if I use if-statements, I am not
able to accomodate the breaks.

Program
   ├───┐
   ┌─< C0 >─┐  │
   │|  │
   │┌< C1 >──┐ │
   │  ┌───< C2 >┐   ┌──< C3 >─┐│
   │  │┌─< C4 >─┐  [S1]   ││
   │  │   [S2] [S3] └┬┘│
   │  │││┌─< C4 >─┐│
   │  │││   [S4]  ││
   │  │││└───┬┘│
   │  └───┬┴┴┘ │
   │ [S5]  │
   │  └┘
Program
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Coding Help

2007-11-10 Thread rishiyoor
Well, its not homework, and if you don't want to be a tutor, you can
be a consultant and earn a lot of money.

Here is how I thought of solving this, but it does not work:

While C0 is false
  if C1
if C2

now what?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Coding Help

2007-11-10 Thread rishiyoor
On Nov 10, 3:20 pm, Carsten Haese <[EMAIL PROTECTED]> wrote:

>
> Maybe it would help you if you added an S6 that does nothing underneath
> the "C2 is true" branch of your flow chart.
>
> --
> Carsten Haesehttp://informixdb.sourceforge.net

Think I found a sequence of statements that would work

if C1
  if C2
  else
if C4
  S2
else
  S3
else
  if C3
S1
  if C5  # This should be C5. There is an error in the chart.
S4

-- 
http://mail.python.org/mailman/listinfo/python-list


Dynamic or not?

2007-12-12 Thread rishiyoor
I'm trying to write a program that will find the distance between two
groups of points in space, which have cartesian co-ordinates X,Y and
Z.

I need to find the distances between each point in one group and every
point in the other group. So if group 1 has 6 points and group 2 had 8
points, I will calculate 6 x 8 = 48 distances. But I do not know the
number of points the user will want to use. It is typically between 50
and 500 in both groups combined, but may be more. Since the memory
required for the distances will be much larger than the points
themselves, I am wondering if I will have to allocate memory
dynamically or if there are easier ways of solving such problems in
Python.

Suggestions for amateur programmer will be appreciated.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamic or not?

2007-12-13 Thread rishiyoor
On Dec 12, 11:33 pm, Steven D'Aprano <[EMAIL PROTECTED]
cybersource.com.au> wrote:
> On Wed, 12 Dec 2007 19:18:20 -0800, rishiyoor wrote:
> > I'm trying to write a program that will find the distance between two
> > groups of points in space, which have cartesian co-ordinates X,Y and Z.
>
> > I need to find the distances between each point in one group and every
> > point in the other group. So if group 1 has 6 points and group 2 had 8
> > points, I will calculate 6 x 8 = 48 distances. But I do not know the
> > number of points the user will want to use. It is typically between 50
> > and 500 in both groups combined, but may be more. Since the memory
> > required for the distances will be much larger than the points
> > themselves, I am wondering if I will have to allocate memory dynamically
> > or if there are easier ways of solving such problems in Python.
>
> Writing in Python, you never need to manually allocate memory. Sometimes,
> for especially complicated data structures, you need to take care about
> _de_allocating memory, but that's rare.
>
> In this case, you may not have enough memory to calculate all the
> combinations at once, so you should consider an iterator-based solution.
> Read up on generators and iterators.
>
> points1 = [ (1.2, 3.4), (5.7, 9.2) ]
> points2 = [ (-6.3, 0.0), (14.1, -7.8), (2.6, 12.8) ]
>
> import math
> def distance(p1, p2):
> return math.hypot(p1[0]-p2[0], p1[1]-p2[1])
>
> def distances(list1, list2):
> """Yield the distances from every pair of points."""
> for pt1 in list1:
> for pt2 in list2:
> yield distance(pt1, pt2)
>
> Now, if you want the distances one at a time you do this:
>
> >>> D = distances(points1, points2)
> >>> for d in D:
>
> ... print d
> ...
> 8.23468275042
> 17.0836178838
> 9.50368349641
> 15.1208465371
> 18.9620673978
> 4.75078940809
>
> and if you want them all at once, you can do this:
>
> >>> list(distances(points1, points2))
>
> [8.2346827504160718, 17.083617883809037, 9.5036834964133785,
> 15.120846537148639, 18.962067397834023, 4.7507894080878819]
>
> --
> Steven

Thanks, that helps.

When you say python automatically allocates memory, what would you do
if you don't know the size of the list of, say for example, the
nearest pairs between the two groups. I would probably iterate over
all the pairs and create a new list. I did a similar operation in
another program, but I had to initialize the list (statically) with x
= [0]*50 before I could use it in the for loop.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamic or not?

2007-12-13 Thread rishiyoor
On Dec 13, 10:24 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Thu, 13 Dec 2007 06:51:23 -0800, rishiyoor wrote:
> > When you say python automatically allocates memory, what would you do
> > if you don't know the size of the list of, say for example, the
> > nearest pairs between the two groups. I would probably iterate over
> > all the pairs and create a new list. I did a similar operation in
> > another program, but I had to initialize the list (statically) with x
> > = [0]*50 before I could use it in the for loop.
>
> Only if you used an index instead of starting with an empty list and
> appending values to it.  Or in simple cases a list comprehension might
> replace a ``for`` loop.
>
> "Bad" and "unpythonic" example:
>
> new = [0] * len(old)
> for i in xrange(len(old)):
> new[i] = do_something(old[i])
>
> Better written as:
>
> new = list()
> for item in old:
> new.append(do_something(item))
>
> Or as list comprehension:
>
> new = [do_something(item) for item in old]
>
> Or:
>
> new = map(do_something, old)
>
> Ciao,
> Marc 'BlackJack' Rintsch

Thanks Marc.
-- 
http://mail.python.org/mailman/listinfo/python-list