Python

Discussion on other languages at the COJ. This is the place to clear your doubts about these languages, and to share with the community the new things you learn about them.
User avatar
Phantom
Posts: 58
Joined: 7 years ago
Location: Cuba
Gender: None specified

Re: Python

Post by Phantom » 6 years ago

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):
a=raw_input()

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:

range(3)
#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)



User avatar
Robbin
Posts: 38
Joined: 7 years ago
Location: Santiago de Cuba
Gender: None specified

Re: Python

Post by Robbin » 6 years ago

Yes, I see that..

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

AH!!
I've discovered a new question:

-what about trees?

User avatar
Robbin
Posts: 38
Joined: 7 years ago
Location: Santiago de Cuba
Gender: None specified

Re: Python

Post by Robbin » 6 years ago

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

User avatar
Phantom
Posts: 58
Joined: 7 years ago
Location: Cuba
Gender: None specified

Re: Python

Post by Phantom » 6 years ago

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
------------return
--------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
--------else:
------------majMin = self.__left.__Major()
------------majMin.__RemoveNode()
------------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
----#abb.Remove(1)
----#for i in abb.AmongOrder(): print i
----print abb.Search(1)

if __name__ == "__main__": Main()
Last edited by Phantom on Mon Jun 11, 2012 12:37 am, edited 1 time in total.

User avatar
Robbin
Posts: 38
Joined: 7 years ago
Location: Santiago de Cuba
Gender: None specified

Re: Python

Post by Robbin » 6 years ago

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)

User avatar
Robbin
Posts: 38
Joined: 7 years ago
Location: Santiago de Cuba
Gender: None specified

Re: Python

Post by Robbin » 6 years ago

ok, got a question:
why do I have to enter an object(self) in the functions of the tree?
example:
when I write "def __init__(self, value...)", what's the purpose of self?

User avatar
Phantom
Posts: 58
Joined: 7 years ago
Location: Cuba
Gender: None specified

Re: Python

Post by Phantom » 6 years ago

---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:
@staticmethod
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:

User avatar
Robbin
Posts: 38
Joined: 7 years ago
Location: Santiago de Cuba
Gender: None specified

Re: Python

Post by Robbin » 6 years ago

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.. ;)

User avatar
Phantom
Posts: 58
Joined: 7 years ago
Location: Cuba
Gender: None specified

Re: Python

Post by Phantom » 6 years ago

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:

@property
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

@Vector
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:
3
7
8
5
10
4

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.

User avatar
Robbin
Posts: 38
Joined: 7 years ago
Location: Santiago de Cuba
Gender: None specified

Re: Python

Post by Robbin » 6 years ago

Ehh..
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??

Post Reply

Return to “Other languages”