Python - All Possible Permutations w/ Recursion

  Рет қаралды 42,707

Wrt Tech

Wrt Tech

Күн бұрын

Пікірлер: 100
@user_at180
@user_at180 10 ай бұрын
Struggling many years to understand this,finally gotcha your video and u made my day bro
@alasdairmacintyre9383
@alasdairmacintyre9383 3 жыл бұрын
Dude thank you for that simple explanation "2nd function call, first recursive call." I have been trying to wrap my mind around how code like this works but you really simplified it
@wrttech422
@wrttech422 3 жыл бұрын
I am glad it was helpful!
@lindgren.bjorn1
@lindgren.bjorn1 4 жыл бұрын
Thank a lot, especially for visualizing the execution of the code. It made me fully understand the solution. I have struggled with permutations and have seen a couple of other explanations/solutions, but yours is the only one that walked through the execution in detail and thus made me really understand the code.
@wrttech422
@wrttech422 4 жыл бұрын
Glad it helped! I have many more python/ java tutorials with many more to come in the future.
@yiqiongxiao5255
@yiqiongxiao5255 4 жыл бұрын
i struggled so much to understand permutation and this video is so far the best. detailed explanation! awesome and thank you so much
@wrttech422
@wrttech422 4 жыл бұрын
Glad it helped!
@zeroanims4113
@zeroanims4113 3 жыл бұрын
@Gertrudis Gahn bruh
@peytonfrancis4869
@peytonfrancis4869 3 жыл бұрын
sorry to be so offtopic but does anybody know a method to get back into an Instagram account? I was dumb forgot the login password. I love any help you can offer me!
@augustryker6822
@augustryker6822 3 жыл бұрын
@Peyton Francis instablaster :)
@peytonfrancis4869
@peytonfrancis4869 3 жыл бұрын
@August Ryker thanks for your reply. I found the site thru google and im waiting for the hacking stuff now. Looks like it's gonna take quite some time so I will get back to you later when my account password hopefully is recovered.
@crowsnsuch
@crowsnsuch Жыл бұрын
Dude, I seriously cannot thank you enough for this video. I was struggling with the idea of recursion and you filled in the gaps in my knowledge. Thanks a million
@wrttech422
@wrttech422 Жыл бұрын
Great to hear!
@MuratJumashev
@MuratJumashev 2 жыл бұрын
This video deserves more views! You nailed it!!!
@CrushOfSiel
@CrushOfSiel Жыл бұрын
Damn, I was so close to the answer. I just couldn't write the whole thing in any correct order. I had the base case, I knew I needed a loop somewhere with perm[:i] + char + perm[i:] and I knew I needed the function to call itself but I just could'nt put it all together >< so frustrating. Your solution makes perfect sense but I don't know if I could ever get that to all come out of my head the correct way. It seems so complicated, building a list, doing the permutations, going through the list to add more complex permutations...
@wrttech422
@wrttech422 Жыл бұрын
It just takes a lot of practice. Eventually, problems like this will come a lot easier. If you're new to recursion you should be proud you got as far as you did, because it takes most people a long time before they can solve recursion problems on their own.
@CrushOfSiel
@CrushOfSiel Жыл бұрын
@@wrttech422 Thank you for the encouragement. I think I'm slowly getting it more and more. I coded Pascal's Triangle today with recursion. My first attempt worked but it was horrendously slow if I wanted a row larger than n=12. I found another version online after trying and failing to make mine run faster with a dictionary (memoization). The solution I found was very similar to this solution so I guess with practice the pattern will just become easier.
@opinions892
@opinions892 3 жыл бұрын
Your solution rocks my friend ... returns love from India...
@harshsaxena7716
@harshsaxena7716 4 жыл бұрын
this solution deserves a like ..best explanation
@wrttech422
@wrttech422 4 жыл бұрын
Glad you think so!
@hoanghuynh4110
@hoanghuynh4110 11 ай бұрын
Love it, due to u with visual explanation, i comprehend now
@kevinyuan5232
@kevinyuan5232 Жыл бұрын
best video explanation of recursion, bro deserves a W👍
@wrttech422
@wrttech422 Жыл бұрын
Glad you liked it!
@Strawlocka
@Strawlocka 7 ай бұрын
you're a life saver. Thank you for making this video
@wrttech422
@wrttech422 7 ай бұрын
Glad it helped!
@vivekshinde2107
@vivekshinde2107 4 жыл бұрын
banged head hundred times to understand this..thank u for making it so simple
@wrttech422
@wrttech422 4 жыл бұрын
Glad it helped!
@AlI-xy9jx
@AlI-xy9jx 2 жыл бұрын
Your explanation was the best! Thank you so much. I haave been stuck on this for so long.
@kushalhu7189
@kushalhu7189 4 жыл бұрын
THE best Explanation in youtube..............
@victor.novorski
@victor.novorski 3 жыл бұрын
Saw you in that Rabbit getting scared by its own fart....... Now your subscriber
@fardeendingankar7318
@fardeendingankar7318 2 жыл бұрын
Trust me man you just nailed this i was trying for long to get being from non-Cs background it was difficult to digest but hats off to you
@amitupadhyay6511
@amitupadhyay6511 3 жыл бұрын
Perfect explaination, you sit, take a bow
@mrKnown01
@mrKnown01 2 жыл бұрын
This is best available code and explaination i guess
@TutonicKnight
@TutonicKnight 4 жыл бұрын
Thanks for the video. I've been racking my brain on this for a few days because i was under the impression that the return needed to be the function call... My brain kept coming back to having the recursive call go into a variable, but thought it wasn't allowed in a recursive function. Turns out i failed because i was too strict about what i thought the "rules" were... lol Here's an updoot and a sub :P
@rajrathod3228
@rajrathod3228 4 жыл бұрын
You made the rock edible for me
@ahyungrocks5509
@ahyungrocks5509 10 ай бұрын
Wonderful explanation. The question is how am I suppose come up with this if I haven't seen your video? Recursion is very difficult for folks who don't program everyday.
@chakoladavidpaul3920
@chakoladavidpaul3920 Жыл бұрын
Thank you, the video has been really helpful, just wanted to point out one error if I may , i.e. at 17:06 for bc would be (abc,bac,*bca instead of cab)
@wrttech422
@wrttech422 Жыл бұрын
yes, you are correct
@tigranhayrapetyan8357
@tigranhayrapetyan8357 2 жыл бұрын
Tnank you! It was very helpful explanation how it works
@xaoqi1157
@xaoqi1157 3 жыл бұрын
thanks for this video,the explanation is amazing!
@dbz1132
@dbz1132 3 жыл бұрын
Very well Explained 🔥🔥
@ajitdhobale6826
@ajitdhobale6826 2 жыл бұрын
Thanks for this video - great explanation! One request - can you please add time and space complexity to this video?
@tigranhayrapetyan8357
@tigranhayrapetyan8357 2 жыл бұрын
I think time coplexity is O(n!)
@sidharthpunathil
@sidharthpunathil 4 жыл бұрын
Keep going! :) please don't stop.
@cincyohio1946
@cincyohio1946 4 жыл бұрын
Great video! Very Informative
@glennarandilla5193
@glennarandilla5193 4 жыл бұрын
Hi Wrt Tech I'm new in python and my cousin looking for the solution on this kind of assignment. you might solve this. Please help. I'm trying your video tutorial on this matter. PERMUTATION OF LIST OF STRINGS Given a list of strings such as arr = ["abc", "de", "fgh"], cycle through/print all the combinations starting from the first word to the last. Implement the recursive function as f(index) and implement a global variable called build for your solution. You may also use global variables to track the number of strings in the list, and the list of strings itself. Input Format The first line of the input contains n, the number of strings to read The next n lines contains strings composed of small-case alphabetical characters only Constraints 1
@sebastianvasquez805
@sebastianvasquez805 2 жыл бұрын
Thank you so much for this video!
@vinimoreira16
@vinimoreira16 Жыл бұрын
dont know why i can solve really large and complex problems but when it comes to recursion, no matter the complexity, my brain just freezes LoL
@jonsentio3150
@jonsentio3150 Жыл бұрын
man i can truly relate to that! shi is scaring me
@walaamaklad181
@walaamaklad181 3 жыл бұрын
can you do with repetition of the char
@wagishasingh_2203
@wagishasingh_2203 3 жыл бұрын
You r amazing sir thank you very much
@walaamaklad181
@walaamaklad181 3 жыл бұрын
what should we change
@vladkojancar4007
@vladkojancar4007 3 жыл бұрын
does it also cover cases like: aabc ? wont there be like twice as much permutations than there should be ?
@wrttech422
@wrttech422 3 жыл бұрын
The function returns all permutations of the string you provide as input. a and a are separate characters and will be included in the output list. There will be 4! permutations just like there will be 4! permutations of any other string of length 4.
@vladkojancar4007
@vladkojancar4007 3 жыл бұрын
@@wrttech422 I understand, so the goal isn't to find all possible permutation for mashed-up string, but for characters in it. BTW very thanks! Your video helped me. Not just with this permutating problem, but also with imrpoving my implementation skills of recursion-typed algorithms 😂🙏.
@everyzan-m2q
@everyzan-m2q 2 жыл бұрын
You are an absolute chad
@julietgenshin5193
@julietgenshin5193 4 жыл бұрын
how do I do it if i want every permutation of a specific length? If my string is ABCS but only wants the permutations of length 3?
@wrttech422
@wrttech422 4 жыл бұрын
Thats a bit more complicated, but ill be making a video on it soon.
@julietgenshin5193
@julietgenshin5193 4 жыл бұрын
@@wrttech422 thank you sir Im looking forward to it. Also, what if i want certain conditions? For example, the third digit needs to be 2?, what happends then?
@danirieg9910
@danirieg9910 6 ай бұрын
thanks so much! just a thought: this doesn't account for repetition of letters. for example if you input 'aba', it will give you 6 solutions but there are only three.
@wrttech422
@wrttech422 6 ай бұрын
Yeah forsure! This is a solution for all possible permutations of every character irrespective of frequency. You're correct; 'aba' will return 6 permutations since there are 3 characters.(3!)
@lepuzki
@lepuzki Жыл бұрын
This is a nice solution for a few letters but what would be a better algorithm than this? obviously it cant find permutations for let's say 12 characters.
@wrttech422
@wrttech422 Жыл бұрын
There is no super efficient algorithm to solve this problem (ie: O(n), O(n log n), O(n^2) etc)
@elizabethmoukit8482
@elizabethmoukit8482 4 жыл бұрын
Can a recursive funtion use a for loop? I thought recursion was used instead of a for or while loop.( sorry if this is a dumb question, i'm new to this lol.)
@natnaelberhane3141
@natnaelberhane3141 2 жыл бұрын
I was thinking the exact same thing!
@wrttech422
@wrttech422 2 жыл бұрын
Recursion and for loops are 2 completely different things. They can be used together, or independent of one another.
@jackzhao4302
@jackzhao4302 2 жыл бұрын
Thank you for this. Very helpful! A quick question, when you got result=['bc', 'cb'], then why perms become ['bc', 'cb']?
@myfuzzyanimals67
@myfuzzyanimals67 2 жыл бұрын
Try watching this video on GCD with recursion. kzbin.info/www/bejne/lZnGdGykbc99j7s Its his video, but he explains call stacks a bit better in it. Start at the visualization part around 4:20.
@vishakhasangtani3354
@vishakhasangtani3354 2 жыл бұрын
Can someone please help me with time and space complexities here?
@emmanuelc.opeter
@emmanuelc.opeter 3 жыл бұрын
Nice video, but for the second iteration, why did you use the range function and added +1
@wrttech422
@wrttech422 3 жыл бұрын
I explain it in the video and again in the visualization of the code, but it is because we need to append the char variable in every possible position. the range function is non inclusive which means for the last iteration, the char variable will be appended to the end .
@LNMLucasMasiero
@LNMLucasMasiero 4 жыл бұрын
Thanks you sooooo soooo much. I was wondering how to do that... I first start looking for patterns. Like for example, using numpy.... with word 'cold'... which is a 4 letter's word... so it means 24 posibble permutations... then i divided by lenght of the word (4) and got that every letter would have to be on the first posittion 6 times... so it would be something like below.... and then repeating thesame for each colum... but it needs to use conditionals to don't take the letter that was in the first column to aviod repetitions.... so i decide to look for something more simple and the answer was tooo much simple but not for me easy to understand it... Thanks you sooo mch ♥♥♥ c x x x c x x x c x x x c x x x c x x x c x x x o x x x o x x x o x x x o x x x o x x x o x x x l x x x l x x x l x x x l x x x l x x x l x x x d x x x d x x x d x x x d x x x d x x x d x x x
@wrttech422
@wrttech422 4 жыл бұрын
Absolutely. Recursion is particularly useful when we have problems that can be broken down into smaller repetitive problems. It is especially good for working on things that have many possible branches and are too complex for an iterative approach. So instead of finding all possible permutations of the word cold, length 4, we could instead find all possible permutations of the last letter(length 1). All possible permutations of d, is just d. We could then find all possible permutations of 'ld' by just appending l in every possible position in our list of all possible permutations of d. That would give us [ld, dl]. We keep doing this until we have reached the first letter in our word, giving us all possible permutations of the word cold. So this is what it would look like with the word cold: -all possible permutations of d: result = ['d'] -all possible permutations of ld: (by appending l to all possible positions in our result list) result = ['ld', 'dl'] -all possible permutations of old: (by appending o to all possible positions in our result list) result = ['old', 'lod', 'ldo', 'odl', 'dol', 'dlo'] -all possible permutations of cold: (by appending c to all possible positions in our result list) result = ['cold', 'ocld', 'olcd', 'oldc', 'clod', 'lcod', 'locd', 'lodc', 'cldo', 'lcdo', 'ldco', 'ldoc', 'codl', 'ocdl', 'odcl', 'odlc', 'cdol', 'dcol', 'docl', 'dolc', 'cdlo', 'dclo', 'dlco', 'dloc'] and this works for words of any length.
@LNMLucasMasiero
@LNMLucasMasiero 4 жыл бұрын
@@wrttech422 awesome. Thanks you again. ❤️❤️❤️
@jodecibirkenfeld1943
@jodecibirkenfeld1943 3 жыл бұрын
What coding program is this and where do I download it?
@aradog3213
@aradog3213 3 жыл бұрын
its called pycharm its much better nowadays
@bhimeshk4866
@bhimeshk4866 2 жыл бұрын
Thank you so much!!!
@janghyukboo6113
@janghyukboo6113 4 жыл бұрын
Thanks a lot
@donpilarsuarezgudino5160
@donpilarsuarezgudino5160 2 жыл бұрын
If I don`t find the result by my own, should I quit programming learning?
@wrttech422
@wrttech422 2 жыл бұрын
Definitely not. You should keep practicing and eventually you'll be able to do problems harder than this on your own. Most people cant solve problems like this at first. It takes lots of practice.
@Alkarid2
@Alkarid2 4 жыл бұрын
Hi I think at 17:05 the string should be ‘bca’ and not ‘cab’
@wrttech422
@wrttech422 4 жыл бұрын
Youre correct. Thats what i was meaning to write
@kspv2806
@kspv2806 3 жыл бұрын
thanks a lot man
@keebdude
@keebdude 2 жыл бұрын
Thank you 8^)
@vitorpmh
@vitorpmh Жыл бұрын
this is fucking great, thx
@abemanyukumar8709
@abemanyukumar8709 Жыл бұрын
give me a code pls
@aradog3213
@aradog3213 3 жыл бұрын
it doesnt word anymore i dont know why here is my code:
@aradog3213
@aradog3213 3 жыл бұрын
def permutations(word): if len(word) == 1: return [word] perms = word[1:] letter = word[0] result = [] for perm in perms: for x in range(len(perm) + 1): result.append(perm[:x] + letter + perm[x:]) return result print(permutations("abcd"))
@wrttech422
@wrttech422 3 жыл бұрын
Because you're not recursively calling the permutations function. line 4 should be perms = permutations(word[1:]) not perms = word[1:]
@aradog3213
@aradog3213 3 жыл бұрын
@@wrttech422 oh ok thanks!
@ألشعوبتحيابالعلم-ق6ز
@ألشعوبتحيابالعلم-ق6ز 3 жыл бұрын
Peace be upon you. Can you add the Arabic translation to your videos please🙃🌹
@ulaitor4186
@ulaitor4186 2 жыл бұрын
I'm getting this instead; ['ca', 'cac', 'cacb', 'ca', 'cac', 'cacb', 'cacbc']
@wrttech422
@wrttech422 2 жыл бұрын
paste your code below and ill fix it and tell you what's wrong.
@ulaitor4186
@ulaitor4186 2 жыл бұрын
@@wrttech422 def do_permutation(word): if len(word) == 1: return [word] perms = do_permutation(word[1:]) char = word[0] result = [] for perm in perms: for i in range(len(perm)+1): result.append(perm[:1]+ char + perm[:i]) return result print(do_permutation("abc"))
@wrttech422
@wrttech422 2 жыл бұрын
@@ulaitor4186 you just had a small issue on the line that contains: result.append(perm[:1]+ char + perm[:i]) perm[:1] should = perm[:i] The perm[:i] after the concatenation of the char variable should = perm[i:] The correct code is below. Copy and paste it into your IDE/text editor def do_permutation(word): if len(word) == 1: return [word] perms = do_permutation(word[1:]) char = word[0] result = [] for perm in perms: for i in range(len(perm)+1): result.append(perm[:i]+ char + perm[i:]) return result print(do_permutation("abc")) For a better understanding of the code inside the for loop, I suggest you look at the visualization of the code in the video that starts at 6:54
@ulaitor4186
@ulaitor4186 2 жыл бұрын
@@wrttech422 You're simply the best, wish we could talk further, you can drop any of your available handles, I'll hit you up.
@wrttech422
@wrttech422 2 жыл бұрын
@@ulaitor4186 I appreciate that! At this time I am only available on KZbin, but I intend to branch out further once I start making higher quality machine learning, data structures, and algorithms videos.
Recursion Simply Explained
27:58
NeuralNine
Рет қаралды 8 М.
Backtracking: Permutations - Leetcode 46 - Python
9:43
NeetCode
Рет қаралды 366 М.
SIZE DOESN’T MATTER @benjaminjiujitsu
00:46
Natan por Aí
Рет қаралды 7 МЛН
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 14 МЛН
Don’t Choose The Wrong Box 😱
00:41
Topper Guild
Рет қаралды 31 МЛН
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 332 М.
Please Master These 10 Python Functions…
22:17
Tech With Tim
Рет қаралды 222 М.
Recursion Example in Python | Find All Combinations of a List
14:14
Matthew M Young
Рет қаралды 6 М.
How to Code Combinations Using Recursion
22:26
Coderbyte
Рет қаралды 90 М.
Permutations - Leetcode 46 - Recursive Backtracking (Python)
9:42
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
Python itertools - The key to mastering iteration
20:03
mCoding
Рет қаралды 35 М.
L12. Print all Permutations of a String/Array | Recursion | Approach - 1
19:07