String Processing in Python: Check Permutation

  Рет қаралды 6,575

LucidProgramming

LucidProgramming

Күн бұрын

Пікірлер: 39
@mightyprogrammer2899
@mightyprogrammer2899 2 жыл бұрын
i think everyone is little bit confused in the second method so let me clear this out. why we were decrement the value of d[i] in first loop coz we are checking only one string in first loop i got it after putting print statement in that and take a look on this and i hope you will understand :) code : def is_perm_2(str_1, str_2): str_1 = str_1.lower() str_2 = str_2.lower() if len(str_1) != len(str_2): return False d = dict() for i in str_1: if i in d: d[i] -= 1 else: d[i] = 1 print(d[i]) print(" ") for i in str_2: if i in d: d[i] -= 1 else: d[i] = 1 print(d[i]) return all(value == 0 for value in d.values()) not_permutation_1 = "google" not_permutation_2 = "ooggle" print(is_perm_2(not_permutation_1, not_permutation_2)) Output : 1 1 0 0 1 1 -1 -2 -1 -2 0 0 False at the first we were looping, great As when we decrement the value so we get our value not equal to zero which is not true so thats why Ans is False so let me give you second example which is correct code: def is_perm_2(str_1, str_2): str_1 = str_1.lower() str_2 = str_2.lower() if len(str_1) != len(str_2): return False d = dict() for i in str_1: if i in d: d[i] += 1 else: d[i] = 1 print(d[i]) print(" ") for i in str_2: if i in d: d[i] -= 1 else: d[i] = 1 print(d[i]) return all(value == 0 for value in d.values()) not_permutation_1 = "google" not_permutation_2 = "ooggle" print(is_perm_2(not_permutation_1, not_permutation_2)) Output: 1 1 2 2 1 1 1 0 1 0 0 0 True lemme explain this str_1 : "google" str_2 : "ooggle" first time "g" have seen so the value is 1 and then "o" which is also first time so value is 1 move ahead the "o" have seen again so value is 2 move ahead the "g" have seen again so value is 2 move ahead "l" have seen first time 1 move ahead "e" have seen first time 1 second loop there is "o" already present in dict so decrement the value is 1 again we have meet the "o" so decremnt again the value is 0 there is "g" already present in dict so decrement the value is 1 again we have meet the "g" so decremnt again the value is 0 move ahead we can seen the "l" already present so value is 0 move ahead we can see the "e" already present so the value is 0 hence ANSWER is True hope you understand :) Thanks man LUCID for your DS
@mightyprogrammer2899
@mightyprogrammer2899 2 жыл бұрын
kindly ignore that string name which i gave (not_permutation_1) and (not_permutation_2) istead of (permutation_1 and 2) which is totally fine if you ignore but the explanation which i gave is correct
@shreyaagrawal6706
@shreyaagrawal6706 6 жыл бұрын
The second method might not work out for words like google or ooggle Basically words which will have repeated alphabets. Or will it?
@LucidProgramming
@LucidProgramming 6 жыл бұрын
Hey Shreya. You know, you're absolutely correct, and that oversite was my fault. I will be sure, at the very least, the update the code on the repository for this video to correct that error. Thanks for pointing that observation out, and thank you for watching as well. Have a nice day, and thanks again!
@LucidProgramming
@LucidProgramming 6 жыл бұрын
Thanks, Shreya, I appreciate that. You can most certainly make apps and games with Python. For games, I would recommend you check out the "pygame" module. For apps, you can use something like "kivy" to create Android apps, although, I think for that it's best to use something like Java or Kotlin as those languages offer much more complete support for developing those applications. If you want to develop a desktop app using Python, I'd recommend looking at modules such as TKinter. At the moment, I don't have many tutorials on any of the above subjects, but knowing there is a desire for that content is helpful as I use that feedback to help me decide what content to create. Thanks again for watching. I hope those suggestions are helpful!
@peninelly4022
@peninelly4022 6 жыл бұрын
@@LucidProgramming The second method approach on the repo is still incorrect for strings with repeated alphabets
@LucidProgramming
@LucidProgramming 6 жыл бұрын
@@peninelly4022 If you like, you can create a pull request for the code on the repo with your corrections. That way, I can update the code. Thanks for letting me know!
@peninelly4022
@peninelly4022 6 жыл бұрын
@@LucidProgramming I would just use set() and make the strings unique, that can get rid of the repeated characters, which solves the problem. right??
@SomeOfOthers
@SomeOfOthers 5 жыл бұрын
I'm a big fan of your content! It's been really helpful to me and I appreciate it, man. I have a question about your second approach though: Why decrement the count in the first loop? If you were to increment the count and populate the dictionary with letters and counts, then in the second loop, if all the letters in the second string are present in the first, it would bring all the values to zero, wouldn't it?
@LucidProgramming
@LucidProgramming 5 жыл бұрын
Hi SomeOfOthers! Thanks for your comment, I'm happy that the content has been useful to you :) With respect to your question, I believe that bringing the values to zero is the signal that we need to indicate whether or not there is a permutation. That is the point of the second approach and what we indeed check in the return statement. Hope that makes sense and answered your question. Cheers!
@vamsimanubolu9174
@vamsimanubolu9174 4 жыл бұрын
Using dictionary: def permu(s1,s2): s1=s1.replace(' ','').lower() s2=s2.replace(' ','').lower() d1={} for i in s1: if i in d1: d1[i]+=1 d1[i]=1 for i in s2: if i in d1: d1[i]-=1 else: return False else: return True
@LucidProgramming
@LucidProgramming 4 жыл бұрын
Cool, thanks for sharing!
@captain_vegan
@captain_vegan 2 жыл бұрын
Line 42 (11:29) in the first for loop should be "+=" not "-="
@shonnoronha
@shonnoronha 4 жыл бұрын
At 5:39 , instead of using a for loop for checking if the elements are not equal , we could check if str_1 = str_2 then return True , or else return False . What is the edge case with this one or it can be used. Please explain! Thank you soo much , love your videos!
@LucidProgramming
@LucidProgramming 4 жыл бұрын
Your run time is potentially worse in this case though.
@abc00gh
@abc00gh 3 жыл бұрын
I have the same question
@LucidProgramming
@LucidProgramming 3 жыл бұрын
@@abc00gh Did you read my response?
@Dev-dk3ez
@Dev-dk3ez 6 жыл бұрын
The second method doesn't look right to me. The value of key has to be assign 0 in case the key is already present instead of subtracting. See below: for i in str_1: if i in d: d[i] = 0 else: d[i] = 1 for i in str_2: if i in d: d[i] = 0 else: d[i] = 1
@LucidProgramming
@LucidProgramming 6 жыл бұрын
Hi Dev. Your method won't work as you're not decrementing the value each time you see it, you appear to just be setting the entry at position "i" equal to "0". I believe the above code should work on all cases. Is there a test case that you noticed my code failing for? If so, please provide it, and I will fix. In any case, thanks for your comment.
@Dev-dk3ez
@Dev-dk3ez 6 жыл бұрын
@@LucidProgramming Hi, I ran your code available on github and got True, False, False, False. Is the result expected?
@LucidProgramming
@LucidProgramming 6 жыл бұрын
@@Dev-dk3ez Yes, I believe so. You should be able to eye-ball this as well and determine if this is the case so long as you understand what the problem is asking. Hope that helps. Cheers.
@Samcreature
@Samcreature 6 жыл бұрын
For is_perm_1, couldn't you just return "str_1 == str_2" after doing the join? I don't see the need to loop through each character afterwards and confirm that they are equal
@LucidProgramming
@LucidProgramming 6 жыл бұрын
Hi Samcreature. You're absolutely correct, you could indeed just do a simple compare which makes that part cleaner. Thanks for the comment and for pointing that out! Cheers! :)
@KippiExplainsStuff
@KippiExplainsStuff 5 жыл бұрын
@@LucidProgramming in fact, as far as I can see, even the "join" statements are unnecessary (and correct me if I'm wrong). Running "sorted" on a string returns a sorted list. So in fact, all we need to do after making sure they are the same size is: return (sorted(str_1) == sorted(str_2)) Seems to work for me.
@LucidProgramming
@LucidProgramming 5 жыл бұрын
@@KippiExplainsStuff Right seems a bit unnecessary on my part. Not incorrect, but probably something that you could do without. Thanks for the comment! :)
@203bigd
@203bigd 4 жыл бұрын
@@KippiExplainsStuff why even bother checking length? if two sorted things equal each other, they are by definition the same length.
@AnhChimXanh
@AnhChimXanh 6 жыл бұрын
What's the different between anagram and permutation? , if you write print(is_perm_1('fairy tales', 'rail safety')) it return True, doesn't that mean anagram is permutation,and btw the github link is no longer valid :D
@LucidProgramming
@LucidProgramming 6 жыл бұрын
Hello Đức. Thanks for letting me know about the broken link. I went ahead and fixed that. Every anagram is a permutation, but every permutation is not an anagram. That is, when you rearrange the letters in a word if it spells another valid word, it's an anagram. If it is just a rearrangement of the letters without the rearrangement being a valid word, then it is a permutation, but not an anagram. Hope that makes sense!
@AnhChimXanh
@AnhChimXanh 6 жыл бұрын
@@LucidProgrammingThanks for your explaination , got it now :D, your whole series about data structure and algorithms is very well explained for even a non cs background like me :D, keep up the good work !!! Really hope to see a web framework tutorial for python like django/flask :D
@LucidProgramming
@LucidProgramming 6 жыл бұрын
@@AnhChimXanh Thank you very much, I appreciate your kind words on my data structures and algorithms series. It's great to know that this is accessible to someone outside of the CS field. Django and Flask would be great to focus on, but at this time, there might be some other things coming down the pipe. Thanks for the suggestion!
4 жыл бұрын
yeah the 2nd has a logical error...dude man
@LucidProgramming
@LucidProgramming 4 жыл бұрын
What would that logical error be, dude man?
4 жыл бұрын
@@LucidProgramming do you think it will work for all combinations of string inputs? think about tot and pop for instance?
@LucidProgramming
@LucidProgramming 4 жыл бұрын
@ Yes. Unless you can give me a reason to not think that would be the case. I just tested these cases locally, and they work as expected.
4 жыл бұрын
@@LucidProgramming I did already, but let me add a couple more: dod & kok or mom & lol
@LucidProgramming
@LucidProgramming 4 жыл бұрын
@ Those all seem to pass just fine. Don't think there is any logical error, at least any I can see.
String Processing in Python: Is Unique
11:54
LucidProgramming
Рет қаралды 8 М.
String Processing in Python: Is Palindrome Permutation
11:59
LucidProgramming
Рет қаралды 7 М.
快乐总是短暂的!😂 #搞笑夫妻 #爱美食爱生活 #搞笑达人
00:14
朱大帅and依美姐
Рет қаралды 14 МЛН
Lazy days…
00:24
Anwar Jibawi
Рет қаралды 7 МЛН
Python Decorators in 15 Minutes
15:14
Kite
Рет қаралды 452 М.
String Processing in Python: Spreadsheet Encoding
12:51
LucidProgramming
Рет қаралды 4,5 М.
String Processing in Python: Is Anagram
11:34
LucidProgramming
Рет қаралды 15 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
Python - All Possible Permutations w/ Recursion
17:55
Wrt Tech
Рет қаралды 42 М.
The Algorithm Behind Spell Checkers
13:02
b001
Рет қаралды 419 М.
2 Years of C++ Programming
8:20
Zyger
Рет қаралды 9 М.
快乐总是短暂的!😂 #搞笑夫妻 #爱美食爱生活 #搞笑达人
00:14
朱大帅and依美姐
Рет қаралды 14 МЛН