Coffee, Code & Numbers - Codewars Challenges #10
Key Takeaways
The video solves Codewars challenges using Python, including ISBN validation and finding the next largest number, and demonstrates a raw thought process with mistakes and reasoning, showcasing the use of regex, Python functions, and permutation techniques.
Full Transcript
what is going on guys welcome back to another episode of the code Wars challenges today again I'm going to solve or try to solve code Wars challenges without preparation without chat GPT without Googling the solution and without cutting out mistakes or trying to appear like I can solve it immediately I'm going to show you my raw thought process my mistakes and my whole reasoning process to a hopefully correct solution so let us get right into it [Music] all right so the two most important things for a coding session water and coffee all right so same rules as always I'm not allowed to Google the exact solution I'm not allowed to use large language models uh I am allowed to Google documentation stack Overflow stuff if it's not the solution if it's just something about python or some basic stuff that is not going to immediately give me the solution as I said um and what we're going to do now is we're going to say that we want to have a certain difficulty let's start this time I think usually I start with six Q so let's go and start with 5 q today uh but I am going to sort by uh either popularity or most completed or let's go with popularity let's see what we have here now these are already solved by me so let's see actually I solved quite quite a bit of those already Pete the baker let's open up a couple of them first non-re repeating character sounds Familia I'm not sure if I didn't already look at this one let's scroll down a little bit or actually let's go and say sort by most completed probably it's going to be similar so let's scroll down a little bit to the to the ones that I have not seen yet per ation sounds interesting common denominator sounds interesting ISBN 10 validation actually this sounds quite simple ISBN identifiers are 10 digits long maybe it's a warmup here let's see if that's uh that's easy ISBN identifiers are 10 digits long the first nine characters are digits nine uh 0 to 9 the last digit can be 0 to 9 or X to indicate a value of 10 an ISBN number is valid if the sum of the digits multiplied by their position Modelo 11 equal 0 yeah okay that should be super simple I don't know why that is 5q I don't want to be again too cocky because maybe it's actually more difficult than I think but it seems like it's super easy so so what's the input again we get nine digits and we get an extra digit or an X and the whole thing modulo 11 has to be zero for it to be valid so basically we just just have to see if the first nine digits are digits because here in the sample tests we see that there are examples um actually why can I not scroll down here seems like I have to reload the page somehow displayed oh there you go um so I have some examples here where I get letters or where I get the X in the beginning these are of course invalid so in those I have to immediately return false but if the structure itself is valid then I want to see if the whole thing modulo um 11 is actually zero and X is equal to 10 so I have to treat X like 10 so how do I check if the first nine are actually digits I can do that probably with a regex but I don't know how to do a regex off the top of my head so I have to probably uh Google about how to do AIX a very stupid way probably to do it is to just try to typ cast the first nine characters into a number and if it works then it's just digits and if it doesn't work then the format is invalid um and of course the whole thing has to have a certain length I'm not sure if yeah actually for length I also have to check this so let's start with a very basic um check here which is now is this enabled or not I hope so uh let's say if the length of the ISBN is not equal to 10 then we return false then we can try also um what else is a good what else is a good check I mean we have we can try to typ cast it just let's let's get a solution that works and then we can make it cleaner so let's say try this is now really not a best practice way to do this but let's just try and typ cast ISBN uh the first nine characters that should throw an exception if uh or an error if um it contains characters so if I get an exception return false otherwise I'm going to assume that they are in fact uh digits and then I can say [Music] um if ISBN 9 in and then I can provide a string here uh 0 1 2 3 4 5 6 7 8 9 x then I consider that a valid thing potentially otherwise I return false um and if the structure is valid in general then I just have to sum up the individual positions so again an easy way to do that is to say um let me think about this an easy way to say that here again with a messy solu solution is to just iterate over the characters over the first nine characters to sum them up and then if maybe um actually let's do it that way I can say if that's part of this then I can do one thing otherwise if ISBN 9 is equal to X then we're going to do something else but we're still going to consider this as potentially uh potentially valid but in this case here what I can do is I can just say um I can just say if sum of and then int character for character in ISBN if the sum of that and actually I need to use I need to call the function here if the sum of that modulo 11 is equal to zero return true otherwise return false and then down here what I can do is I can say uh basically the same thing but not exactly almost we just do the same thing but up until the last so we don't um we do up until the nth and then we also add to this uh plus 10 so I actually have to do it like this and that should actually work now I'm not sure if I didn't think about something okay false should equal true where am I returning false um can I actually see that oh here actually so this is the first one fails why does this fail let me actually go to the desktop and open this as a script main py let me copy paste this I don't think I can do it in Vim mode I think I have to go into insert mode no this is actually from another video so how do I actually copy this can I do it with with my register is this possible no still doesn't work how do I copy the code from the code Wars block here I think I did this in the past do I always have to use my mouse maybe there you go and now let's say validor ISBN and then let's call this on actually print call this on um 11 one 22 2 3 3 39 false why do I return a false let me think about this let me maximize this let me zoom in 111 so definitely it's type castable so it's not going to throw an exception um the ninth character should actually be part of this string so in this case it would be Nine and Nine is part of 0 1 2 3 4 and so on so what I do is I sum it up I say 1 + 1 + 1+ 2 plus two and so on isn't this I mean if I open up Python 3 if I say 1 + 1 + 1 + 2 + 2 + 2 + 3 + 9 27 modulo 11 is 5 didn't did I get the assignment wrong maybe I didn't read the instructions properly 10 digits long the first nine characters are digits n 0 to 9 the last digit can be 0 to 9 or X to indicate a value of 10 and ISBN 10 number is valid if the sum of the oh multiplied by their position okay um that shouldn't be too difficult okay you should read the assignment before you jump to conclusions like like I do so that is going to be just an enumerate here and the enumerate uh what actually happened here oh yeah enumerate ISBN this is going to give us now not just a c but also the I and then we're going to say it's going to be this time I and besides that it's going to be the same thing here for I and c and ISBN numerate uh I have a new keyboard so I'm going to mistype a bit more frequently than usual because now I don't have arrow keys I have to use uh hjkl I force myself to do it uh in vim and for the 10 it's not 10 but it's probably 100 right shouldn't be 100 because it's an X in the 10th position so I think that should always add 100 not sure about this but let's see if this runs um through the tests correctly no okay FAL should equal true so or is it I + one oh it's probably I + 1 because of course we don't start counting from zero if we're normal people what's the problem here I fail B I always return false it seems actually not here I'm returning true okay uh let me just see what I'm doing wrong here again I have ISBN 9 is part of this works okay sum I have to take the value so in this case uh or what was the example 1 1 1 so it would be 1 * 1 1 * 2 1 * 3 2 * four if I understood this correctly that should be the pattern so in this case this is exactly what I'm doing first integer * 1 2 * 2 modulo 11 has to be zero and if it's X I have to add um but actually that's not the thing the X is not the problem because if X was the problem I should at least get the correct solution here so before we focus on the X stuff let's see why this is not working out I want to know that so I'm going to copy paste this again into my into my neovim here and I want to know why this is still a problem I also want to know what the sum is actually so maybe I [Music] can H right or actually let me just run this false okay yeah I know that already but if I do this manually now if I use let's say I use speed crunch for this um can I clear no okay doesn't matter uh but basically I have 1 + 2 + 3 right and then I have um 8 + 10 plus um 2 * 6 12 and then plus 3 * 7 21 plus 3 * 8 uh which is 24 plus and then uh [Music] 27 and then I have 9 * 10 so I have 90 and that is [Music] 198 modulo 11 or how do I do it here 11 okay how do I use mod and speed crunch doesn't matter okay I know what the number is the number is 198 modulo 11 zero okay so what am I doing wrong in a calculation this is then really a stupid problem here I'm taking the integer time I + 1 what am I doing wrong here [Music] I mean I would like to know what kind of fals I'm getting here so maybe let's let's just just print some let's do to cheap debugging of printing a here and printing B down here and then [Music] printing C down here and then printing it here again I get an A okay that's at least at least good because that's the calculation now what the hell am I doing wrong here this is probably so simple and I'm not seeing it because I'm recording as always dropping the IQ points when I'm pressing on record I want to see what the sum is I'm going to say now the sum is equal to zero or actually no not to zero I'm going to take this here oh actually wrong key take this here copy it paste it remove the zero here that's the sum and what I want to do here is I want to say the sum module of 11 print the sum what is the sum why doesn't this work 181 why for every character in ISBN I take the character and I multiply it times I + one which makes sense of course because I don't want to do it with zero we saw that the calculations actually correct so what the hell am I doing wrong here I don't I really don't know what I'm doing wrong here h okay I have to do it explicitly then I have to do it in a full loop for I see in enumerate IBN I'm going to be so disappointed by what the problem is it's going to be so stupid and I know you guys are probably screaming already into your screen uh into your into your screen so the sum is zero the sum plus equals c C or int c times oh boy oh this is so stupid this is so stupid this is so stupid this is so absolutely stupid okay got it of course I have to put parentheses around this because otherwise it's going to take it times I and add one to it this is is obviously a problem okay so let's oh come on let's do it like this and like this and now let's see if it works okay got all the tests let's see if I can pass a solution okay it works now I would like to at least a little bit uh make this a little bit more uh professional and I think the best way to do that is using regex because I don't know how otherwise you would want to uh check efficiently if it fits a pattern so regex not regits regex let's see if we can build a regex that allows me to find the the problem is I don't know I don't know the reg syntax on the top of my head and I don't want to use a large language model but maybe we can say I think it's 0 to 9 right but you got to have it how do I how do I say I want to have it exactly nine times so regex exactly n times oh okay so basically I would just do let me try this again regx why am I typing Z all the time regex if I say I want to have um 0 to9 then I would just say that and then here I would put nine right and then I want to have now we're learning regx here um if I have let let me enter some numbers here okay this is recognized already this should be recognized and then I want to have this here so let's see if it finds that uh so it would be nine numbers followed by and now I would have to have again 0 to 9 or X so how do I do a regex or reg X or so it would be basically um parenthesis I would say this pipe symbol can I just do X does it work like that oh it works and I want to have that exactly once okay this seems to work so that's my expression now let's see uh python regex check if string matches I compile the regx pattern match so let me open this here like this let me open this like this let's go back to our example here and now I'm going to say to make this more professional uh import re and I'm going to say the regx pattern is pattern equals re. compile and I think this um this hat symbol so to say is for the start of the for the start of the string so then I put my regx here and then I use the dollar for the end of the string not sure if this works um but then I would say pattern match and then ISBN is this correct I'm going to copy this and I'm going to say I'm going to try to to do this here in my uh local version so let's just go down here import r e and then let's paste this let's do it like this and now let's see if I can print or actually I want to print pattern match one11 222 3339 true okay now the big test is does it also work if I add some to it so this should still work but for example this should not and this should also not let's see match match oh actually okay this is a match and then I have no match okay perfect so if I have a match it's a match but how do I check for a true false value How can I turn this into uh how can I turn this into a yes or no can I just say if yeah of course because yeah and let's go if and now I can get rid of all this right I can get rid of all this the only difference here is I have to still consider what I'm doing with the X so this still has to [Music] be here but I don't need I can just have an else here and I can return false and I can have if that is true mhm so can I make this somehow more elegant can I say uh n digigit sum is equal to two and then I can do this comprehension here basically so I can start here and I can say that this here is what I'm looking for but only up until the last character so colon 9 and then I just have to say match can I use match here I can say ISBN n and then case how is the syntax actually of match case I've never used this in Python I think yeah basically what I wanted to type so match case and I can say the case case is either uh or actually the case doesn't make a lot of sense I basically it's way better to say if if the ISBN 9 is equal to X do something otherwise do something else and do something is take the nine digigit sum or actually just return n digigit [Music] sum n digit sum + 100 modulo 11 is equal to zero and here return nine digits sum plus int ISBN 9 * 10 if that thing mod modulo 11 equals 0 it's true not sure if that's so much more elegant I think the solutions that I'm going to look at here in a second are way better now I get a false even though it should be true so I'm messing up something what's the issue I get uh this this this what's the problem here it seems to work for enough cases but it doesn't work for my favorite case the first one so let's see actually if I just redefine this down here false okay I know that it's false but what's the problem what's the [Music] sum print n digigit sum 108 so then I add to this 9 * yeah 108 shouldn't it actually be correct then I mean it would be then no actually that's not the number that we had before right no actually it should be 198 modulo 11 should be correct so it's not that it goes in here uh yeah of course that's a problem because of course I'm taking the string I'm repeating the string 10 times and then I'm taking the integer version of that that's absolute nonsense let's go there you go attempt and we have done it so let's submit this and let's see what the other Solutions are we took quite some time for this trivial task here what does he do Here length has to be 10 and oh there is an is digit function this was I think I actually made a video about this this isdigit function in Python is quite new isn't it I think I covered this in one of my what's new in Python version whatever I think that this was a new thing not sure about this okay I didn't think about this at all so we have an is digit function we can just call on the individual characters we want to know if the length is 10 and if the last digit is um if the last character is a digit or if it's X oh if it's not okay yeah um return false and then modulo you just sum up 10 if x is X otherwise in X okay before yeah okay it's not that different from what I did here but he doesn't use a regex he just uses his digit which probably I don't know what's more efficient this guy here uses ax and he sums up the index D * I for enumerate yeah okay makes sense all right let's move on let's do another one let's actually do a 4 q one I think I've not done one yet on video so let's go for 4q but I'm going to filter it or I'm going to pick one that's seems easy okay I want to I want to not fail too hard here so let's try to pick one where I think I should be able to find a solution quite um easily oh actually I have one here human readable duration format I think this is one that I did on camera but let's go Matrix determinant no thank you simple maze exponentials is fractions Pathfinder AI on Labyrinth game big big big padan number problem of points let's actually go for um oh this is actually sorted by newest I didn't want to S sort by newest I want it sort by popularity okay actually I have snail done human readable format range extraction next bigger number with the same digits this sounds actually like it shouldn't be extremely complicated so where is it now next biggest number with the same digit so we're getting a number and we have to find the next bigger number that can be formed by rearranging its digits okay now the Brute Force approach for this is super simple you go with permutation and you just get the max of the of the values but I think that probably the input is going to be large enough for this to not make sense I want to do this one now oh actually I have done this already train again or have I I'm not sure I mean I didn't complete it I have looked at it already but I'm going to be honest I don't remember this so don't think that I prepared for this I have no idea I've never solved this but I think that the solution should be quite straightforward especially if these are the values you get I'm not sure if I'm again too naive about this but I think that all you have to do is you have to call permutations on it you have to typ cast all the permutations so you basically take the number turn into a string uh get all the permutations turn them into integers and then you just pick the max value out of there and if the length of the permutation list is one you basically return negative one here because it says if you can't rearrange the numbers uh yeah I don't know why this shouldn't work so let's just try it uh python permutations iter tools permutations how did this work iter tools permutations yeah so what I do is basically next bigger I say um numbers is going to be equal [Music] to from iter tools import permutations next bigger so numbers is going to be equal to permutations of string of n and then basically I say or actually I can do it in a list comprehension here already I can say int num for Num in permutations and then I can just return Max numbers if length numbers is not equal to one else 1 I don't know if that's it but I think this should work no okay in argument must be a [Music] string not two okay let me just see what this outputs I'm not sure so let's delete all of this next bigger off let's say one two three oh no sorry it's not it's not Max it's the next biggest number it's not the largest number okay but still it shouldn't be that more difficult to do that uh but what I want to see here is what happens can I print numbers or this does not work so if I run this no okay this happens actually already up here so let me see what happens when I print permutations Str Str of n so permutations object okay but what happens if I say num for Num in permutations what do I get then okay yeah I basically have to join this together then so I basically have to say join number and then I should get yeah this works okay but now it doesn't return of course the it doesn't give me what I want to have so it gives gives me the largest number so what I want to have is I want to have a sort on that list and I want to see where this is and then then I want to get the basically what I have to do is I don't have to return Max numbers but I want to do numbers and then I want to have the the index of the numbers so numbers index num + one if it exists num is not defined uh n obviously okay that is the correct solution the problem is of course if I already have the largest number this is going to throw an exception no it doesn't oh I didn't sort it sorry I need to say numbers is equal to sort it numbers and then it should give me an exception yes out of range but it should work if I have anything else so if I have something like 1 two three it should give me the correct yeah okay this works um so basically if length is not one and Max numbers is not equal to n so that works and and if I now have um if I now have 32 1 that gives me ne1 not sure if that is actually what we even need to do but let's see okay this one does not work what's the input 414 returns okay what does it return in my case 414 returns 414 why is that oh because I have to remove the duplicates um how do I remove duplicates from a list in Python probably there's a function for it I am allowed to Google stuff like this remove duplicates list python oh obviously I'm stupid I've done this countless times why do I have to look this up set of course um yeah that should fix it so sorted set numbers that should now be the solution but I'm going to have the problem that running through the permutations is not very efficient so I'm going to probably get a timeout now and I have to throw away my solution there you go execution timed out um all right that's probably where it gets difficult because that is the Brute Force approach easy don't have to do anything about it um what do you do if you don't have now you now you have to actually use intelligence to solve this because if I have a number like this what is the next largest number this requires now a more intelligent approach because now I have to think okay how can I move the numbers to cause the smallest increase and this now is where you have to turn on your brain because up until now this was super trivial let's use a smaller number as an example here um if I have let's say this number or this number what do I have to do to increase the value a tiny bit well I have to somehow increase the whole value how do I do that so you obviously don't want to decrease the value of the highest value or of the highest number so you don't want to swap this one with something because you're going to decrease the value so this has to stay let's maybe change this what what if I have a two here or a three then I could increase the value of course by swapping with the five but that is too much of an increase I can also increase the value by doing it a little bit so to increase the value just a little bit I have to probably increase the last digit however I have to increase it in a way that doesn't decrease the value so for example I can swap two and four but that will decrease the value so this doesn't make a lot of sense um so I should actually what I think about is I should move some value from the right to the left where the right value is larger so for example I could swap two and one because two and one are so two is larger than one but two is to the right of one so isn't it just the first occurrence where I can swap a value on the right so what if I have something like um what if I have something like I don't know this and then I have 1 9 2 what's the next biggest number well I could swap two and one yeah I mean isn't that the thing you just swap the number but what if for example this is not one but this is three and I have two here then I would of course not do that but what if I have a one here for example then I would try to to find a way to H I need the closest pair of numbers I think from right to left the first close how do I Define this because now swapping the two and the one is not the best way so I cannot just go for the first thing you know looking at the last digit and when can I swap it here I can swap it so I would increase the value because actually what I should be doing is I should swapping the N I should be swapping the actually I should be swapping even no I should be swapping the nine and three I think that the correct answer here is saying nine and saying three that would be the next largest number next biggest number but how do I put this into code I have to go from right to left and see if I have I mean one approach would be I say four distance in range I start with a distance of one and I [Music] go or actually I could say I could say done equals false while while not done and I could say I have distance equals 1 and then I could go from right to left and see um for I in range length n string N I go through the string and I say give me the last or go from right to left and see if your neighbor but this is also not always the case because what if I have or is it always the case I think it could actually be always the case let me just just try this and then I can think about this in more detail but if I say now if I need to do a different thing I need to say string num is if I have a number uh let's pick the one from above 1 3 3 92 whatever if I have a number like this I have to say string of N and down here I can say now string number and I can say if string number netive I is larger than string number [Music] I I + 1 then swap them no not negative I + [Music] 1 because I need to go with distance so it's going to be one in the beginning but not all the time distance swap them so I can say string num I or negative I String num i+ [Music] distance is and now basically the same thing but the other way around so that would be that and then we would say done equals true and break because then we're done otherwise if we get out of this for Loop we can say else if we don't break out of the for Loop distance plus equals one print Sr n I'm very curious to see what happens here so string object does not support item assignment great awesome why not why can I what do you mean string object doesn't support item assignment okay then uh don't turn it into a string turn it into a list of characters uh turn it into string of position for position in string off N I don't know okay this is obviously wrong this is obviously wrong why though I say two and nine no two is not larger than nine okay two and three no actually not two and three nine and three why is this not triggering immediately probably my Loop is not correct so shouldn't this be what's happening here I mean shouldn't the first thing that happens be oh obviously I have a problem here but I'm not sure if that's going to solve the problem the problem is I'm comparing stuff here that is not an integer so I don't know if that is an issue but I think that actually in terms of asky codes it shouldn't be that much of a difference so my question is I have the first comparison should be take the last element no it's not or is it no it should actually work like that so if I say two is larger than nine no so we don't do that we just go to the next iteration now I is one so I go n is larger than whatever this position is plus distance so this why is nine not larger than three I don't understand this because then in this case it should swap the values and it should stop the loop immediately that should actually be the solution so why doesn't it do that I don't know but I want to find out so I want to print here string number negative I and string number I + distance and I want to see what exactly you're doing here four and two why four and two four and two I don't I I don't understand this why four do I not understand something about I mean I'm I'm very confused right now oh oh I got it I have to start with one I have to start with one because negative 0 is zero that's the that's the reason but shouldn't that basically work all the time shouldn't this work for any number not sure right now but if I say this is my function what was the function called Next bigger if I call it next bigger n and I say this is what I'm doing here and I mean for some values I'm never going to get I have to to return negative 1 sometimes when do I return negative 1 if I don't get to a solution so if I I mean I can try until I get an exception right or not let's just try a couple of things so let's just take the default case if everything works so I take a number I do all this in the end I end up with a list so I just have to return the in version of uh join string number that should work that should be my function so let's see if it works at least for some cases because I think it should no um cannot access oh oh actually I forgot a couple of things here of course we need to delete the N thing here but besides that and we also don't need this uh print statement but that should be now it let's see it works for some instances at least uh examples I'm logging something am I printing something here I don't think that I'm printing something actually unexpected exception yeah of course um now I'm not sure if we can solve this by just saying minus one or minus 2 okay now I'm going into an endless loop I think this is the while not done at some point you should terminate what's that point when the distance let's just say if distance is larger than the length of uh string number we want to say done equals true just as a hard to terminate here at some point okay we get some tests uh tests passed 12 should equal 21 again let me just copy paste this run this locally print next bigger what was it 12 okay 12 is 12 why is 12 12 shouldn't it immediately we start with a distance of one okay doesn't um doesn't trigger this and then I say the last digit here oh I'm actually I am actually printing stuff shouldn't be printing stuff or actually I do want to print stuff let me let me check this again but it doesn't print stuff which means I don't get into this at all so let me print that um let me print this up here oh mhm doesn't get in the loop because we only have two and one uh okay the problem is sometimes I get otherwise uh H I can't just do it like this okay now it works but then I get the problem again with the with the exception so I can run it like this test but then I get the exception in the case of 21 why is that because for 21 I don't find a solution so if I type 21 oh actually I didn't have to do this index out of range because what happens is doesn't find anything distance is two um how many times do I go into this Loop if I have I have this okay I run it twice so what if I just say this no also not possible or actually it could be possible if I just say um in this case I would have to say break maybe in combination now with the thing that I tried four is just a solution if I say just break and add this okay 21 should equal yeah I'm not returning negative 1 obviously so what should happen in this case is actually not break actually don't have to break I don't have to break I can return negative one there you go let's see if it solves all the problems no okay we get a couple of wrong answers here a lot of them actually uh let me just check something on my phone I don't want to cut the video here uh I just got a message and I don't want to necessarily stop recording because of that okay maybe I have to make a cut in 15 minutes uh I promise I'm not going to Google the solution in that time uh so let's see what's the problem here 7847 32 should equal 787 234 what's the input testing for sometimes oh yeah the problem is I'm returning sometimes the same number so maybe I can do a hard code solution I can say [Music] um now actually is this the input I want to see what I get for this input okay so here I'm applying my strategy of finding the first case of swapping the numbers now what would be the correct solution the correct solution would be 787 234 oh okay this is a whole thing that I didn't consider because what we need to do now is of course I find that but then I have to minimize oh this gets now super complicated because now it's not just done when I swap that because what I have here is I I apply my strategy which is correct so I have to find the first um time where I can switch that so I have to basically say Okay seven it's larger than uh than four found the first occurrence so swap them but now we're not done because now after this position I have to minimize the order so I have to say once I do a switch I have to now take everything from that position until the end and arrange arange it in the minimal we which I could do with a sort which is not very clean but let's say I have um this is super messy now I do the swap the swap is correct but now what I have to do is I have to say from the position so basically string number is string number from this position onward so from negative i+ distance or actually no it's string number up until this position oh this gets crazy now uh I plus distance I take it now the way it is plus sorted string number everything after disposition does this make sense I'm not sure but yeah no in in ascending order that's fine I have no idea if this makes eight okay perfect this is not correct um actually before I assign anything here um this doesn't make any sense it's just sorting the whole number so show me what string num looks like here this is what string num looks like right now but that's not even true that that's not even true right because we saw right now this was not what I was returning did I change something here oh sorry um I changed the input uh what was the input 7847 32 7847 32 that was the input 7874 32 this is actually correct isn't it no it's not correct but that here 787 78 787 okay so if I say minus one here plus sort it oh and this is just a character this is of course not correct but um if I say this until the end basically so I say minus one does this make sense now yes that's correct uh so that is what I want to do I want to say string number is equal to exactly that let's see if this is a good solution um very curious right now 21 should still equal 1 ah yeah this was because I didn't return negative 1 here but that is not an issue return1 so I still pass all the tests but I don't pass this stuff still but I pass way more so there are some other problems what are the other problems I really don't want to solve them um let's look for example I mean index out of range is probably not that much of a big deal here let's look at this one I get this number and I should get get this number so again what do I actually get I get this I get this should get this okay our strategy what was it you take a number the first time you see a number larger than the neighbor you swap it in this case it would be um in this case we would swap 9 and one correct so I would say 91 is this correct no again let me let me see we have the first occurrence is this the first occurrence zero is not larger than two two is not larger than three 3 3 5 8 8 9 9 1 91 9 is larger than one so we'll swap it but that's not what's Happening Here What's Happening Here is I should get this so no this doesn't make any sense yeah of course because I shouldn't be swapping nine and one I should be swapping a smaller number always uh yeah I have to make a quick cut in the video um not not to look up the solution I probably am going to uh give up on this but I have to cut the video for a second okay I'm back again um I don't really know if the approach that I have up until this point is not completely FL lot because the whole strategy of replace the first actually maybe what if it makes more sense no actually no the problem is not the distance at all the problem is that two makes more sense than nine so maybe I should keep I should be going from H maybe it's not that bad I have to find the first occurrence where I can make a swap but then I should ask is this the best swap I can make so maybe I should find the first occurrence of a possible swap but then I should go for the smallest number that I can swap but in this case this wouldn't be true either because I'm not going to actually swap the small number so if I go to this point here I say 9 and one oh the smallest number that would be an increase yeah that would make that would make sense so if I say go through the numbers find the first occurrence where this happens but then don't replace it immediately by that but do another loop for example this gets messy now in range from this point until the end uh this is now how do I do this or I should say four yeah 4 J in range uh now this doesn't make sense so in this case here I find nine and one are the ones that I can swap but now what I want to do is I want to say is nine really the best candidate so look at all these and find the smallest number that's larger than the one I want to swap with so string number I plus distance is the thing I want to move to the right so the thing that I'm looking for has to be larger than that but also the smallest value that's larger than that so I would have to basically say let's add some more space around this here I would basically have to say four J in range and now we want to go from I until i+ no actually from 1 until I this this make sense smallest is equal to uh I don't know actually let's put this to 10 so we cannot have a better value go through the numbers if string number negative J is larger than string number this is so messy right now J or actually no this stays the same so basically I do this here uh now I messed this up uh if that's the case if string number negative J is long larger or actually less than smallest if we turn it into an integer we need to turn all of this into an integer obviously then set smallest equal to in string num negative J and also smallest index is equal to none we could say smallest index is equal to J if that's the case the replacement happens this is so messy right now this is so messy I have to think to this again otherwise I'm going to make a mistake so what I do is I check if there is a swap then I say smallest is 10 because there's no digit that's larger than 10 index none I go through all the digits that are yeah I go from right to left smallest digit that is still larger than the thing so I need to actually say here again int int that int that and then if it's smaller than smallest change this and then what you do is you say that this at position J this at position there you go there you go and then you still do this boy I have no idea if this works is this the correct number no it's not but why not shouldn't this actually print Str strr num minus J I want to know what happens here yeah okay it recognizes that so that should actually be the thing that I'm swapping with oh of course not J but there you go smallest index and then smallest index oh this is correct now come on oh boy if I can solve this now I didn't believe in myself now I do for a second at least okay uh what do I do if I get a none if smallest index is none smallest index is whatever it was before what was it before I let me just copy paste the full code again so that's that there you go copy [Music] paste test oh boy come on H oh we almost pass all of them okay there's some exceptions here which is always an index error this is perfect okay so where do I get the index error I'm almost done with this we're going to do this I get an index error here why do I get an index error with this one what does happen here um well if there are no Replacements when does this happen when I go through the loop we can we can I'm going to do this now in a very simple way to counteract this if string of n is equal to sorted string of n then I'm going to just return negative one that's the cheap way to do this but I think it should work let's see still works oh we still get a [Music] problem this shouldn't even happen I mean if I have 8872 oh okay I know why reverse equals true reverse equals true we still got a problem I don't know what this is here we have 982 this should actually not go into that Loop is this why is this not happening I mean if I have a number like this it shouldn't even get past this check here or do I have to join this is that the problem yeah okay that's a problem I need to join this string join this is not going to be the most beautiful solution but it's going to be a solution right now come on test attempt there you go we have it oh my God okay I'm going to submit this and now let's see this is way more beautiful than the I programmed okay let's see what he does or she iter tools list string numbers okay makes sense four range length s - 2 -1 - one so step size minus one we go from right to left if there is any number larger than the neighboring number number m is min filter Lambda okay I I don't have any remaining capacities to wrap my brain around this probably if you're looking at this now it makes sense but for me I I don't have the nerves to this this looks smooth what does he do if the string is EMP if the string is equal to okay this is basically what I did this is exactly what I did now but that a equals n while true a okay come on this is a super stupid solution but it's faster than the permutation stuff that I did this is like extremely stupid he's just increasing the number by one until he gets to a point where where he has the same digits okay well sure why not okay I want to explain my solution again for those of you who are interested just briefly I want to then stop recording because this is now uh too long already but the basic idea is if you have the string already in the largest possible permutation there's not going to be a larger permutation so if you have only descending numbers you're not going to have anything that you can rearrange to make this larger so you return negative one otherwise what I do is I say um I'm not even sure if this distance stuff makes sense not even sure if I ever run through this Loop more than once uh but basically what I do is I get the digits I go from right to left I find the first uh situation where I can swap two digits to make the uh number larger but before I just swap them I see is there any other number that is smaller than the one I'm trying to swap is there any smaller number where I can do that if that's the case I swap that number and then I sort everything afterwards in the sending order to get the minimal uh result possible I think actually this is a cool solution it's probably way too inefficient that I can optimize it which I'm not going to do now but I think this is not not a bad solution so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free all that thank you much for watching see you in the next video and bye
Original Description
Today we do some more Codewars challenges.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
💼 Services 💼
💻 Freelancing & Tutoring: https://www.neuralnine.com/services
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
Timestamps:
(0:00) Intro
(0:32) ISBN-10 Validation (5 kyu)
(30:17) Next Bigger Number (4 kyu)
(1:21:42) Outro
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →Related AI Lessons
🎓
Tutor Explanation
DeepCamp AI