Page 2 of 4

Re: Python

Posted: Thu May 17, 2012 10:01 pm
by Phantom
Robbin wrote:hey:

I got another question:
how can I read an entire string on a cicle? :?:

if I put:

for i in range(range):

it doesn't work
help me
of course!! How you expect that work??
range is a function that return a list of values beginning in 0 and ending in the predecessor of the number you pass, for example:

#It return:
[0, 1, 2]

you understand now?? you pass to range a number not a function. What you expect "range(range)" returns??

As the function range return a list, it is slowest than if you use a "while" loop(but the difference is little, I suppose)

Re: Python

Posted: Fri May 18, 2012 12:49 am
by Robbin
Yes, I see that..

You know a lot of python..
when did you begin to learn python?

I've discovered a new question:

-what about trees?

Re: Python

Posted: Fri May 18, 2012 1:11 am
by Robbin
Hey, I sent the message three times, by a mistake, connection was failing..

Yes, I saw that thing about the range.. I never thought to do that on a while loop.. ha, it's funny

Re: Python

Posted: Wed May 23, 2012 7:53 pm
by Phantom
Jajajaja My brother was the first person in talk to me about Python and it’s strong. Since I began to learn Python, I decided be a Python prophet :D
I had something hidden for you. You wanted a tree, and I implemented for you a Binary Search Tree with everything: Insert, Remove, Search and the different travels in the tree (ToWidthOrder, AmongOrder, PreOrder and PosOrder) I implemented a HEAP too, if you are interested.

Code: Select all

class ABB(object):
----def __init__(self, value=None, __father=None):
--------self.__left = self.__right = None
--------self.__count, self.__father = 0, __father
--------if value is not None: self.__value, self.__count = value, 1

----def Insert(self, value):
--------if not self.__count:
------------self.__count += 1
------------self.__value = value
--------comp = cmp(value, self.__value)
--------if not comp: self.__count += 1
--------elif comp > 0:
------------if self.__right: self.__right.Insert(value)
------------else: self.__right = ABB(value, self)
--------elif self.__left: self.__left.Insert(value)
--------else: self.__left = ABB(value, self)

----def Search(self, value):
--------if not self.__count: return False
--------comp = cmp(value, self.__value)
--------if comp > 0: return self.__right.Search(value) if self.__right else False
--------if comp < 0: return self.__left.Search(value) if self.__left else False
--------return True

----def Remove(self, value):
--------if not self.__count: return
--------comp = cmp(value, self.__value)
--------if not comp:
------------self.__count -= 1
------------if not self.__count: self.__RemoveNode()
--------elif comp > 0: self.__right.Remove(value)
--------else: self.__left.Remove(value)

----def __RemoveNode(self):
--------cmpParent = cmp(self.__value, self.__father.__value) if self.__father else 0
--------if not self.__right:
------------if cmpParent > 0: self.__father.__right = self.__left
------------elif cmpParent: self.__father.__left = self.__left
------------elif self.__left: self.__CopyProp(self.__left)
--------elif not self.__left:
------------if cmpParent > 0: self.__father.__right = self.__right
------------elif cmpParent: self.__father.__left = self.__right
------------majMin = self.__left.__Major()
------------majMin.__father = self.__father
------------majMin.__left, majMin.__right = self.__left, self.__right
------------if cmpParent > 0: self.__father.__right = majMin
------------elif cmpParent: self.__father.__left = majMin
------------else: self.__CopyProp(majMin)

----def __CopyProp(self, other):
--------self.__left, self.__right = other.__left, other.__right
--------self.__count, self.__value = other.__count, other.__value

----def __Major(self): return self if not self.__right else self.__right.__Major()

----def IsLeaf(self): return not(self.__left or self.__right)

----def ToWidthOrder(self):
--------queue = [self]
--------while queue:
------------act = queue.pop(0)
------------count = act.__count
------------while count:
----------------yield act.__value
----------------count -= 1
------------if act.__left: queue.append(act.__left)
------------if act.__right: queue.append(act.__right)

----def AmongOrder(self):
--------if self.__left:
------------for i in self.__left.AmongOrder(): yield i
--------count = self.__count
--------while count:
------------yield self.__value
------------count -= 1
--------if self.__right:
------------for i in self.__right.AmongOrder(): yield i

----def PreOrder(self):
--------count = self.__count
--------while count:
------------yield self.__value
------------count -= 1
--------if self.__left:
------------for i in self.__left.PreOrder(): yield i
--------if self.__right:
------------for i in self.__right.PreOrder(): yield i

----def PosOrder(self):
--------if self.__left:
------------for i in self.__left.PreOrder(): yield i
--------if self.__right:
------------for i in self.__right.PreOrder(): yield i
--------count = self.__count
--------while count:
------------yield self.__value
------------count -= 1

----Value = property(lambda self: self.__value if self.__count else None)

def Main():
----abb = ABB()
----values = [6, 3, 8, 4, 5, 9, 3, 2, 1, 33]
----for i in values: abb.Insert(i)
----#for i in abb.AmongOrder(): print i
----#for i in abb.AmongOrder(): print i
----print abb.Search(1)

if __name__ == "__main__": Main()

Re: Python

Posted: Wed May 23, 2012 8:45 pm
by Robbin
That's great!!
In c++ you most write about 300 lines for an abb, but, it seems in python it's not the same..
I will test your gift(it's not a real test, i just wanna know how does it work..)
that's cool..

when i had finish, i will ask you another thinggs(huffman, etcetera)

Re: Python

Posted: Fri Jun 01, 2012 9:24 pm
by Robbin
ok, got a question:
why do I have to enter an object(self) in the functions of the tree?
when I write "def __init__(self, value...)", what's the purpose of self?

Re: Python

Posted: Sun Jun 03, 2012 11:32 pm
by Phantom
---This “self” mean that the method, is an instance method. This “self” is like the “this” in C#, C++, Java, etc. to refer to the actual instance.
---Now, In Python you can do “static methods” too. If you want that, you write:
def pepe(val…): … #in this case you have not put the “self”

--- @staticmethod is a decorator :D
Do you know what is a decorator?? :twisted:

Re: Python

Posted: Tue Jun 05, 2012 7:02 pm
by Robbin
ok, I have to recognize that I don´t know what´s a decorator,
but I would not be angry if you tell me something about it.. ;)

Re: Python

Posted: Thu Jun 07, 2012 12:36 am
by Phantom
Ok, as always... You always learn something new with me!! :D

See the decorators as a function that transforms other functions. For example, @staticmethod, or...
If you want a property with "get" only, you can do this:

def Prop(self):
----"""This is an example of a "get" of a property""" #Documentation of the function, and in this case, the documentation of the property too.
----return <value>

Now, you have in your class a property named "Prop" with "get" onlt, It mean you can't change the value of "Prop".
Now... how can I do a decorator?
Very easy!! Is easier that you imagine. A decorator is a function too. But is a function that receives another function.
For example, do you remember that you can do something like this…?

Prop = property(Prop)

Ok, sedate, we will do our first decorator:

def Vector(func):
----def Res(values):
--------for i in values:yield func(i)
----return Res

def IncTwo(value):return value + 2

-->>This is equivalent to:

IncTwo = Vector(IncTwo)

Now, if you haven’t put “@Vector” above the declaration of “IncTwo”...:
If you do:

IncTwo(3) #It return 5
IncTwo(28) #It return 30

But… as “@Vector” is above the declaration of “IncTwo”… now “IncTwo” receive an iterable, and returns an iterable, applying the old “IncTwo” in each value of the iterable . For example:

for i in IncTwo((1, 5, 6, 3, 8, 2)):
----print i

#It print:

I have not talked with you about decorators in a beginning because it is a powerful tool that only Python have, and it can be shocking for you in a beginning because you haven’t seen ever something like this. I have more surprises for you very interesting one of them are decorators with parameters.

Re: Python

Posted: Thu Jun 07, 2012 2:02 pm
by Robbin
well: I´m shure it´s too easy for you to see that I didn´t understand anything, rigth?
I´m looking this "new thing", and it´s wonderful, but, I don´t understand yet how does it work..
a question: what´s for the ´@´??
I understand it´s a requisite, but, can I use the @ in other parts of a program??