Search In
• More options...
Find results that contain...
Find results in...

# Python recursion/list help?

## Recommended Posts

Posted · Original PosterOP

I have a program im writing but I don't really know how to get what I want specifically

The function gives a list of combinations of letters based on the parameter

I have this:

```def comb(string):
if string == "" or len(string) == 1:
return list(string)

elif len(string) > 1:

scperm = []
for i in range(len(string)):

spt = string[:i] + string[i+1:]

for n in comb(spt):

scperm.append(string[i]+n)

return scperm```

Say if the input is "Hi", the output is ["Hi", "iH"], but I'm wanting to get it to be more like

`[ ["H" , "i" ], ["i", "H"] ]`

I've tried everything I can think of and havent rolled past anything that remotely worked. Any help is appreciated, thanks!

##### Share on other sites
16 minutes ago, Yellow_ said:

Say if the input is "Hi", the output is ["Hi", "iH"], but I'm wanting to get it to be more like

Something like this:

```def comb(string):
if len(string) < 1:
return list(string)
scperm = []
scperm2 = []
for i in range(len(string)):
scperm.append(string[i])
scperm2.insert(0, string[i])
return [scperm, scperm2]```

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

##### Share on other sites
Posted · Original PosterOP
3 minutes ago, WereCatf said:

Something like this:

That kiiiinda gets there?

IS there any way to globalize it a little better to take a string of any length? The goal of the project from this book is to use recursion- is there any decent way to do that?

As I had recursion in my initial code, and it almost got there

##### Share on other sites
11 minutes ago, Yellow_ said:

is there any decent way to do that﻿?

Decent? Well, no, because it'd be stupid to use recursion to achieve what you wanted.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

##### Share on other sites
Posted · Original PosterOP
Just now, WereCatf said:

Decent? Well, no, because it'd be stupid to use recursion to achieve what you wanted.

Yeah, thats true. Just trying to test myself.

Is there any way you know that would accomplish that?

##### Share on other sites
17 minutes ago, Yellow_ said:

Is there any way you know that would accomplish that?

Well, yes. All the ways of doing that are clunky and stupid, but yes. Do you want the complete solution or do you just want me to give you hints on how to do it?

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

##### Share on other sites

Hints: check what type of variable the comb() - function is receiving. For example, if insertyourvariable is str: would check that the variable is a string. Then make your function to behave differently based on that.

Full solution:

Spoiler
```
def comb(string):
if type(string) is str:
if len(string) < 1:
return [[],[],[]]
return comb([[],[],string])

if len(string[2]) > 0:
scperm = string[0]
scperm2 = string[1]
scperm.append(string[2][0])
scperm2.insert(0, string[2][0])
return comb([scperm, scperm2, string[2][1:]])

return [string[0], string[1]]```

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

##### Share on other sites
Posted · Original PosterOP
3 minutes ago, WereCatf said:

Well, yes. All the ways of doing that are clunky and stupid, but yes. Do you want the complete solution or do you just want me to give you hints on how to do it?

Hints would be perfect, I really do enjoy figuring these out myself. This question though.. Is recursion used really?

##### Share on other sites
1 minute ago, Yellow_ said:

This question though.. Is recursion used really?

Recursion is sometimes the only way of doing something, or doing something in an actually practical way, so yes, it is both used and useful. The thing is, most things don't require using recursion. It's not something you're likely to need in any actual real-world usecase for a good while, unless you delve into very heavy maths - problems.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

##### Share on other sites
2 minutes ago, Yellow_ said:

Is recursion used really?﻿

it's definitely used, but it has a time and place. Another aspect of recursion that makes it more usable is dynamic programming, which is basically "recursion plus memoization", where memoization is just a fancy way of saying "remembering the values you got". that way you are not re-computing values you already solved in the recursive chain.

I wouldn't worry about that just yet if you are just learning. Knowing the basics of using recursion is enough.

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

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

##### Share on other sites
Posted · Original PosterOP
3 minutes ago, WereCatf said:

Recursion is sometimes the only way of doing something, or doing something in an actually practical way, so yes, it is both used and useful. The thing is, most things don't require using recursion. It's not something you're likely to need in any actual real-world usecase for a good while, unless you delve into very heavy maths - problems.

I see, well I'll take a swing at what you threw me there. Thanks!

##### Share on other sites
9 hours ago, Yellow_ said:

Hints would be perfect, I really do enjoy figuring these out myself. This question though.. Is recursion used really?

I've used recursion for tree traversal, which has practical applications. But I'm also certain there are better methods.

##### Share on other sites

Why not just write a short helper function?

```if __name__ == "__main__":
def comb(string):
if string == "" or len(string) == 1:
return list(string)

elif len(string) > 1:

scperm = []
for i in range(len(string)):

spt = string[:i] + string[i + 1:]

for n in comb(spt):
scperm.append(string[i] + n)

return scperm

def combContainer(string):
# Get result of comb function
result = comb(string)
# Using list comprehension, convert each string permutation to a list of characters
return [list(item) for item in result]

print(combContainer("Hi"))

print(combContainer("abc"))```

Output:

```[['H', 'i'], ['i', 'H']]
[['a', 'b', 'c'], ['a', 'c', 'b'], ['b', 'a', 'c'], ['b', 'c', 'a'], ['c', 'a', 'b'], ['c', 'b', 'a']]```

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]