Jump to content

what is def tri_recursion, or what is recursion really, in python, it says its a defined function that can call itself. How is it different from this?

 

def main():
    print("y for start, n for quit")
    z = input()
    if z == "y":
        start()
    elif z == "n":
            exit()
    elif z != "n" and z != "y":
            print("y or n only")
            main()
main()

in this code, if input() is anything that isn't y or n, it will return itself or the entire code

Link to comment
https://linustechtips.com/topic/966098-what-is-def-tri_recursion/
Share on other sites

Link to post
Share on other sites

def sum(n):
    if n == 0:
        return 0
    return n + sum(n - 1)

This is a recursive function that calculates the sum of numbers. It will call itself recursively until n is 0, in which case it terminates. Any function that calls itself is recursive, so your main() example is also recursive.

Link to post
Share on other sites

I'm not sure what "tri-recursion" is (is that a function that's missing in this code snippet?), but recursion in general is exactly what you said: some method calling itself. The example you gave is a case of recursion, though in this case you are only doing recursive calls if the input is bad.

 

Another example of recursion might be traversing a tree and listing every element in a tree structure starting with a root node (in this case it's depth first traversal):

def traverseTree(node):
    print(node.getName())
    for child in node.getChildren():
        traverseTree(child)

 

(where node is defined below)

class Node:  
    def __init__(self, name, children):
        self.children = children
        self.name = name
    
    def getName(self):
        return self.name
    
    def getChildren(self):
        return self.children

 

The major thing to consider when dealing with recursion is the call stack. The top recursive call won't "end" until all of the subsequent calls are finished. For example, in your provided code snippet, there's nothing stopping a user from typing in incorrect input over, and over, and over, which is going to grow the recursive call stack each time. If the user gave bad input long enough, your application could crash with a stack overflow.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to post
Share on other sites

7 minutes ago, reniat said:

I'm not sure what "tri-recursion" is (is that a function that's missing in this code snippet?), but recursion in general is exactly what you said: some method calling itself. The example you gave is a case of recursion, though in this case you are only doing recursive calls if the input is bad.

 

Another example of recursion might be traversing a tree and listing every element in a tree structure starting with a root node:


def traverseTree(node):
    print(node.getName())
    for child in node.getChildren():
        traverseTree(child)

 

(where node is defined below)


class Node:  
    def __init__(self, name, children):
        self.children = children
        self.name = name
    
    def getName(self):
        return self.name
    
    def getChildren(self):
        return self.children

 

im not in the node part and class part yet but, recursion is basically looping itself until it gets a good value?

 

So cool tho, people actually replied pretty fast In LTT.com with the proper and really good answer....so cool

Link to post
Share on other sites

10 minutes ago, DARK0717 said:

im not in the node part and class part yet but, recursion is basically looping itself until it gets a good value?

It's looping itself until a base case is reached, whatever that base case is. In the case of traversing a tree, the base case is when you don't have any more children to traverse. User input is usually a very small portion of code, so don't think about it in terms of "bad" vs "good" value, think of it instead in terms of the structure of the data.

 

Let's do another simple example of recursion that simply takes a list and goes through it, printing every item:

def traverseList(array):
    if (len(array) == 0):
        return
    
    print(array[0])
    del array[0]
    traverseList(array)

array = [1,2,3,4,5]
traverseList(array)

The above code takes in a list, prints the first item, removes that item, then passes the now smaller list to itself. If you run that code you get:

1
2
3
4
5

In this the base case is when the list that gets passed in is empty (len(array) == 0), so we don't need to go through it anymore and can just stop the function with no new recursive calls. 

 

If you laid you the call stack, you might represent it like this:

traverseList([1,2,3,4,5]) which calls traverseList([1,2,3,4]) which calls traverseList([1,2,3]) which calls traverseList([1,2]) which calls traverseList([1]) which calls traverseList([]) which doesn't call anything, so the call stack stops there and execution starts to go back up the stack..

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to post
Share on other sites

4 minutes ago, reniat said:

It's looping itself until a base case is reached, whatever that base case is. In the case of traversing a tree, the base case is when you don't have any more children to traverse. User input is usually a very small portion of code, so don't think about it in terms of "bad" vs "good" value, think of it instead in terms of the structure of the data.

 

Let's do another simple example of recursion that simply takes a list and goes through it, printing every item:


def traverseList(array):
    if (len(array) == 0):
        return
    
    print(array[0])
    del array[0]
    traverseList(array)

array = [1,2,3,4,5]
traverseList(array)

The above code takes in a list, prints the first item, removes that item, then passes the now smaller list to itself. If you run that code you get:


1
2
3
4
5

In this the base case is when the list that gets passed in is empty (len(array) == 0), so we don't need to go through it anymore and can just stop the function with no new recursive calls. 

I think I get it. So recursion is looping through itself until a case is complete in which in ur case is when there is nothing left to list from the array, it will stop. Dunno if I used the right term or words tho

Link to post
Share on other sites

1 minute ago, DARK0717 said:

I think I get it. So recursion is looping through itself until a case is complete in which in ur case is when there is nothing left to list from the array, it will stop. Dunno if I used the right term or words tho

That's pretty much it :D

The only terminology you're missing in that statement is "base case", which is the case where the function won't do any more recursive calls. If we're being super picky, it isn't exactly "looping" through itself, but conceptually it can be useful while learning to think of it like looping.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to post
Share on other sites

2 minutes ago, reniat said:

That's pretty much it :D

The only terminology you're missing in that statement is "base case", which is the case where the function won't do any more recursive calls. If we're being super picky, it isn't exactly "looping" through itself, but conceptually it can be useful while learning to think of it like looping.

MAN, THANKS, ok ok ok I actually get it now, thankssssss, Got really confused about it at first as I cant relly tell the difference between a recursion and a for loop, or even just a bunch of if and elif statements, I guess pretty similar with if statements tho.

Link to post
Share on other sites

1 minute ago, DARK0717 said:

MAN, THANKS, ok ok ok I actually get it now, thankssssss, Got really confused about it at first as I cant relly tell the difference between a recursion and a for loop, or even just a bunch of if and elif statements, I guess pretty similar with if statements tho.

just practice lol. I guarantee that you'll have to think through it again next time you come across recursion again and will end up with the same "AHA!" moment, but it will happen faster. The more code you write the more ingrained this stuff gets.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to post
Share on other sites

1 minute ago, reniat said:

just practice lol. I guarantee that you'll have to think through it again next time you come across recursion again and will end up with the same "AHA!" moment, but it will happen faster. The more code you write the more ingrained this stuff gets.

Thanks! Im looking forward to that moment in time xD

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×