Leveling Up To 4 Kyu Challenges - Codewars Challenges #7

NeuralNine · Beginner ·💻 AI-Assisted Coding ·2y ago

Key Takeaways

The video covers attempting a 4 Kyu challenge on Codewars for the first time, specifically the Duration Formatting challenge, using Python programming skills and Codewars platform.

Full Transcript

what is going on guys welcome back to Let's code code Wars here on neural 9 the format where I basically do coding exercises on the fly without preparing for it without scripting without uh cutting out mistakes showing you the full raw unfiltered thought process so let us get right into it [Music] all right so the rules are the same as always I'm not allowed to use chat GPT I'm not allowed to communicate with other people and of course I'm not allowed to Google the exact solution to the problem I am however allowed to use Google in general I'm allowed to stack Overflow and I'm allowed to look up documentation and stuff like that so when it comes to Googling if the task is for example finding the first 20 primes of a sequence I would not be allowed to Google the exact solution to that task however I could Google how to work with lists in Python I could Google how to um do a certain operation in Python I would be allowed to Google what a prime number is if I don't know so stuff like that is allowed so that you can see the full unfiltered process of problem solving here and today my goal is to also do a 4q challenge we haven't done it yet I always did A6 and then a 5q we're going to start today with a 5q challenge for warm-up uh hopefully not too difficult because last time I think if I remember correctly I failed the 5q um but we're going to start with the 5K q and then hopefully we're going to go into a 4q challenge and for that we're going to go to practice here now um and we're going to filter here difficulty 5q do I have to apply it no okay so this one we have already done I think this is uh with this check mark stands for uh let's see what we have here and I'm trying to avoid here in this format two mathematical things not because I don't like them those are actually my favorite challenges the reason I avoid them for the videos usually is because I oftentimes end up especially because I'm recording and it's a different my mind functions in a different way when I'm recording and then the mathematical tasks end up being quite confusing to me and to the audience so I try to do less mathematical things here when recording these code Wars Episodes uh so let's see what we have here's Caesar Cipher string Generation by pattern I'm going to just open up a couple of them and we're going to choose one not very secure maybe regular Expressions this is scary train of dominoes vowel alternations kinda actually I wanted to open this in a new tab uh lazy repeater simple CSS selector comparison okay that should be enough let's look through them I'm not going to read through all of them I just gonna scroll through them briefly and if we find something that looks interesting we're going to read all of it um the action of a Caesar ciphers to a plane uh replace yeah we know that so basically Shifting the alphabet Pro program performs a variation of the Caesar shift increases by one for each character okay so if the shift is initially one the first character is uh shifted by one the second shifted by two the third one by three and so on okay um parameters in Return of function moving shift okay we got a string we get an initial shift and then this shift increases and then codes the entire string and then returns an array of strings containing the coded string in five parts five parts because to avoid what does that mean first codes the entire string then returns an array of strings containing the coded string in five parts to avoid more risks the coded message will be okay maybe we can look at some examples here do we have some examples for example the coded message has a length of 17. I understand the first part I'm not sure about the second part so what does it say if possible the message will be equally divided okay so we have five Runners we always have five right yeah okay so okay got it so basically we're going to divide by five if the number is not divisible by five we're going to divide by four and the remainder is going to be in the fifth one so length 17 is not divisible by five so we're going to divide it by so basically we're going to take 16 divided by 4 which is 4 so we have 4 times 4 then 1 for the remainder uh so that the first four parts are evenly split okay I think I got it length is 16. okay got it you will also Implement a d moving shift um possibly resulting okay we get an array from string array of strings and then we have to de-shift it basically decrypt it so example here U equals I should have known that you would have a perfect answer for me moving shift U1 so we we shift I by One we shift the space which is also character I guess or is the space ignored does it say anything about white spaces because it would be interesting for the S so um t would be one shift U would be two and V is actually three so I don't know what happened to the space because it is still the space but we have um q r s t u v so we actually shifted by three here and then the next one is H I J K L so this is actually then even one more okay let's just try it I think this is not too difficult let's let's give it a try now for this year I'm gonna um how am I going to do this I'm going to maximize this year or do we have some information here no right okay so I'm going to maximize this here I'm going to zoom in and the important thing is I want to activate Vim bindings this is a necessity and I'm going to also maximize my browser window all right so again to recap the idea is to basically shift the alphabet but to do it with momentum you could say so to do it increasing by one every time so if I have something like uh let's add a comment here if I have hello hello would be shifted if I say hello one so shift by one it would shift H by one so it would be I then e would not be shifted to F but to G right so it would be e f g then we would have uh l m n o but then it would be p and then o would be one more so I think that's the idea so we shift H by 1 which is I we shift e by 2 which is G we shift L by 3 which is O we shift L by 4 which is p and so on um I think that's the idea right so in general how do we shift values I think we basically and for this I'm going to open up my local python to play around a little bit I'm going to go to my desktop and I'm going to create a new file experiments.py um I'm gonna zoom in a little bit and we're going to try to just uh play around a little bit or actually we can go into to The Interpreter directly so maybe uh I think or of a character gives you the ASCII code right so if I say h for example it should give me 72 and if I say this plus one it is 73 and if I take this now and I turn it into a character this gives me I so that is how you shift basically you take the ordinal you increase it by something so you shift it and then you turn it back into a character uh that actually shouldn't be too difficult now the question is what happens when you do that with a white space so it seemed that with a white space nothing happened in the example so what if I say here white space it would turn it into an exclamation mark so actually I think that I need to say if it's a white space um ignore it does it actually specify that did I maybe not read that space there you go quotes added in order to okay no uh space okay no it doesn't say anything about space does it say anything about blank no but it seems like no does it seem like that yeah it seems like the white spaces stay the same so we're only actually shifting the characters however we're also considering this as a step so we're not shifting the white space but we're counting with it so it's one two three four and so on so the white space is also um counted so that's important at least this is what I assume I think that's the case so let's go ahead again uh sorry let's go ahead into the terminal again the idea is now to take the character and actually we can do that we can do that by the position so we don't even have to use a loop here we can basically just say take the position and add the index of that character to the initial shift so if I have a word let's say my word is hello then I would say that the resulting word so resulting would be and then I could do a list comprehension for example to um I would have to enumerate right what I have to enumerate I think so so I could say character what we do is we say uh the idea is to then say four I and C so for index and character in and then word or actually enumerate word that's the idea and what I want to do here is I want to say character ordinal and then of the character Plus um plus the initial value so the initial shift let's say 1 plus I and then yeah exactly that's exactly what I want but then of course to to can I turn this just into a string or do I have to join it no I have to join it so I would have to say just this dot join that okay and of course the important thing here is that we need to also say that the white spaces are important so if my word is hello world I need to say in this list comprehension here uh I would have to say do this if or actually here I would have to say this is what happens if the character is not equal to a white space and else it's going to be a white space so that should actually give me the result that I'm looking for I hope so not entirely sure or actually I'm not sure if that is because in his result here no okay this is interesting it seems like he only rotates uh maybe you're not seeing that sorry uh I'm gonna move my camera to the left here there you go um what he's actually doing is he's only rotating characters uh ASCII characters so letters basically white spaces are ignored but so are exclamation marks so I think what we have to do is we have to rotate within the space of characters so basically [Music] um okay I think we have import string and then we have string Dot letters right or was it ASCII letters yeah this is what we're rotating over so then what would I say I would say alphabet alphabet equals string dot ASCII letters and in this alphabet this alphabet has a certain length 52 I would have to rotate and take the modulus of 52. let's just write some code here let's get some basic structure going I'm going to say here import string I'm going to say alphabet equals string dot ASCII letters and then we're going to say that the resulting string we're going to return here um empty string dot join we're gonna have a list crumb range and we're gonna say character board um actually not ORD right because we want to have not the ordinal we want to have the position in the ASCII letters so actually we would have to say alphabet dot index I think this is how it works right so I would have to say alphabet dot index for example h yeah okay so I say alphabet index whatever the character is so C plus the initial shift plus I uh yeah that is Dad if then we would have to say yeah if C is in the alphabet else just C for i c in enumerate uh strict so s I think this makes sense because now what we're doing is we're checking is see a character is see a letter from the alphabet if yes shift it otherwise um otherwise just use the character itself so if it's a white space or if it's um if it's an exclamation mark for example just use the character itself otherwise shift it and that should actually work shouldn't it so let me just copy this here and let's see if it works here on the terminal but it's not string it's uh what was it worth right shift is not defined yet shift in my case was one why do we have bytes now why do we have bytes now uh that's interesting alphabet dot index Dot or index C plus one plus I let's say h for example yeah I know oh it's not defined yeah sure of course it isn't um let me just then use this comprehension here this list comprehension let's see if that somehow works okay this works but it gives me bites why does it give me bites what happens this is the index why is the index such a problem alphabet index h oh of course of course yeah I'm using alphabet index now I'm getting a different number than I would get with ORD but character works with or not with the alphabet index so we're going to actually have to say here alphabet and then take that index of course so it would be our index plus 1 plus I and then uh actually no parentheses here that would be it what's the problem now a modulo of course so we would have to say this whatever it is modulo the length of the alphabet in this case 52. there you go that's what we want okay so it's not character it's not a function that we apply to this it's the alphabet itself we look at this position we get what we get and of course we need to take that and we need to say modulo 52 so that we cannot go out of bounds and then it's just percent uh length alphabet all right so again to recap what we have here is we have the alphabet uh from the alphabet we go to the position that this character is at We shift it and we shift it also again depending on the position of this character which we get by numerating the string string and then we basically say if the character is part of the alphabet we shift it we take the new character otherwise we just keep the character that's the idea so for the D moving shift basically for the D moving shift don't we have to just do the same thing the other way around foreign so for example um I would have to shift but I would have to shift in a different way no actually would I so what I get here is a result it's a problem again oh okay so to return this I basically have to do the same thing right I have to don't I just have to subtract maybe I'm naive but I think I just have to subtract actually I need to run this one again and then I need to run this but I'm just say result yeah okay I think this should work no actually we forgot an important part we're not just working with a string we're actually working with the split as well so we need to take the result this one here and we need to split it into five boxes that are it can be spread evenly so what we do is we say the length of the result in this case is 11. 11 is not divisible by 5. so what we do is we say 11 floor division 5 to get how many or actually no we got to say first of all 11 modulo 5. is this zero no it's not so what we're going to do is we're going to take 11 uh minus what is not no how do we do that uh if 11 is not divisible by five what we do is we do 11 floor division four is this true 11th floor division four would mean two two two two and then whatever is left one that would not be enough because in this case in this case we only have eight so we need three more this doesn't make a lot of sense right or does it or does it I don't know let's pick a different example let's say I have 17. oh we actually had this example in D in the task so if I say 17 modulo 5 it doesn't work okay so because it doesn't work 17 floor division four okay four four four four and the remainder is going to be one okay let's just try this let's just say here now this is my result and what I want to do is I want to say return or actually I want to say if length results modulo 5 is 0. then of course we just split it up into equal parts so how do we do that um uh part length is going to be equal to length of result divided by 5. which should be an integer because it's divisible by 5 and then what we're going to do is we're just going to say what are we going to say uh can we do less comprehension so we want to have a list of equal parts so we want to have um actually what is the result exactly yeah so we just get strings okay we just get strings so we want to have the different strings here so we want to take the full strength string which is the result and want to get from that first of all the first part length character characters so let's say it's four we would get the first four characters then we would get the next four characters so actually would get from the position that we're currently at which would be I so we're going to use an i we're going to enumerate no we're not going to enumerate we're going to use a range the range is going to be 5. and for that we're going to say from I times um I times part length it's going to start with zero up until I Plus 1. times part length I think this makes sense does this make sense and here we're going to save four I in range 5. I'm not sure if this makes sense so I'm going to copy that or actually I'm gonna just write it here well we need to have some string that has 15 or 10 characters so let's just say the string test underscore string is going to be equal to hello world without the D because that should be 10 right okay so this now is can be split up into five by just saying two two two two two so we're going to say now that the part length is going to be equal to 10 in this case divided by 5. so 2. and then the result would be test string and we set here since the part length is 2 we would have here I times 2 up until I Plus 1. times two [Music] four no four we need to close the brackets here for I in range 5. yes this works this works yeah otherwise if it's not divisible by five we're going to say part length is going to be equal to length of the result divided by 4 then we're going to return basically almost the same thing we're going to say all of this here but we're going to do it four times and then we're going to say plus so we're going to append a new list to it plus and then we're going to say um we're going to just say here what are we going to say we're going to say result and then four times part length until the end that should make sense uh now I actually already tested so what happens here this should equal this but it doesn't why is that first of all I have uppercase characters here he only has lowercase characters okay I think I know how to solve that but the split is also not correct so what we have here is we have let me just copy this here um I hope I copied it no didn't I think I gotta be in insert mode to copy it okay it doesn't work at all but the string that we have here has a certain length I would like to figure out what that length is can I not copy from here this is kind of maybe I have to do it with right click there you go so if I say I want to know the length of this 66 okay so 66 is not divisible by five and 66 floor divided by 4 is 16. now how many characters do we have here this is my response this is what it should be one two three four five six seven eight nine ten eleven twelve thirteen fourteen why is it 14. why not 16. so if I say 16 times 4 maybe I didn't read the task properly so let me just read this part about the shift not about to shift about the split again so if the length is 16 the parts will be of length that okay yeah if the length is 11 equal parts would be of length 2 2.2 okay what is that okay I think I didn't read the assignment properly um what does it say here sorry um I'm not sure I understand what they want from me if possible the message will be equally divided by the message length between the five runners so basically taking the message length divided by five if this is not possible Parts one to five will have subsequently non-increasing lengths okay this makes sense because we had this example before where actually there were increasing so this is not allowed it should not be increasing such that Parts one to four are at least as long as when evenly divided but at most one longer so in this case when we say 66 divided by five that is 13.2 so at least 13.2 but at most one longer so basically the calculation is not not Florida Vision four but actually it is division five and then actually import math math dot oh sorry wrong place math dot seal that would then be 14. does this work uh it works from the split I think the split works now the only problem here now is that we have a lowercase and uppercase issues but I think that is the right split here now okay uh for the lowercase and uppercase it seems like the letter should keep their case so they should not shift across the alphabet so what I think we need to do here is we need to say lower alphabet and upper alphabet and we need to say let me just Google how this works python upper case letters string so python ASCII letters ASCII uppercase ASCII lowercase okay ASCII uppercase string ASCII lowercase okay so we have two alphabets now and which one we choose maybe we shouldn't do this as list comprehension but it already is one so I'm gonna keep it like that mod is this gets kind of stupid uh I could also do something completely unnecessary I could just keep it the way it was before and and this is really not reasonable to do it but what I could do in order to not have to change my list comprehension completely is I could have a separate list indicating whether a character is an uppercase or a lowercase character to then apply the whole thing on only lowercase letters and to then basically turn them back into uppercase if they were uppercase which is kind of stupid but why not let's just do it I don't care so let's say cases or let's say is is underscore uppercase what we're going to do here is we're going to say and I think there should be a function for this uh python is uppercase yeah I think actually the string itself has this function okay so we're going to say here uh C is upper is that the function how is it called is upper with an underscore no without an underscore is upper um for C in s and then what we're going to do is we're going to say here alphabet equals ASCII lowercase and we're gonna say alphabet index blah blah blah notice yeah actually it works but we need to say here after we have this list s is s dot lower and let me just see what happens here if I have something like hello world exclamation mark lower okay the symbols stay the same this is good let me just also check here real quick what happens if I say exclamation mark is slower or is Opera okay both are false this is great okay so is uppercase then s is lower and in the end what I'm gonna do is I'm going to say results equals and then um C upper if C or actually is uppercase I yeah if uppercase i else C for I see in enumerate s all right completely completely unnecessary but this should actually work I think so let's just give it a try a problem here with the seal function we didn't close this what is happening now oh we need to join this again now we have another problem oh okay what's the problem here now it doesn't do anything anymore the alphabet is ASCII lowercase all the characters should be lowercase shouldn't they mistaken can I print something actually yeah so it's all lowercase why doesn't this work then it also seems to convert it properly so probably the mistake is here yeah okay so the mistake is here oh yeah of course it's here of course it's here because we're actually applying this to the results there you go the test is passed now we just have to reverse this great uh what's this Buck here doesn't seem to show the code editor properly there you go okay so again as I said this is a stupid solution but I didn't want to change my list comprehension here because I want to move on with a 4K Challenge and we're already doing 37 minutes so let's move on to the D moving shift all we have to do here is we have to join all of this together this should not be too difficult so actually we should be able to just join the lists right so we just have to say empty string join and we join the content of s and that would be our new s and this should actually be it right or am I naive again test let's attempt oh one thing didn't pass which one's that moving shift slice indices must be integers or none or have an index method and that's not the case for me or what which line is that this is line 13 okay it seems to problem seems to be here um do I have to say integer okay um the backwards conversion doesn't work okay yeah of course doesn't work because we don't do it the same way that we do it here so here we need to again do the whole thing with the uppercase so we need to say actually that and then we need to say that and here basically you need to say this like that okay still not everything working here um let me just copy this code here so that I can run it locally I need to play around with it so what I want to do is I want to say print moving shift and I want to do one and I want to do hello world okay first of all oh no this actually works okay now I want to get this result here or let's just call it rest equals that and then we want to say print D moving shift press one okay yeah of course it doesn't work I mean it adds to things should do with the opposite of course doesn't work now it works okay we solved this one I'm sure there are better Solutions but we're going to move on now so let's see what the best Solutions here are okay actually they're not that short the other Solutions seem to be quite long as well so there there isn't a one-liner or something like that what does he do ABC what is ABC okay ASCII lowercase is ABC ABC here uppercase okay uh-huh yeah okay seems fine seems like our solution was not the worst possible solution so let's close this one I'm gonna try for q1 even though we're quite long we're doing this quite long already let me just check one thing real quick um okay uh just need to write a message here real quick all right um maybe I have to to make a cut here in a couple of minutes but we're going to look for a 4K challenge here so we're going to go and say difficulty for Q and this should hopefully give us something interesting so the smallest number with a given number of divisors no I don't want to do mathematics now multiply weighted graph okay all of this is math now tracking Pawns alphabet Wars simple memory manager let's see what this does brain [ __ ] translator okay unflatten a list harder than easy you get an array of integers and you have to unflatten it by these rules okay there are quite a lot of rules The observed pin number of integer partitions implementing sha-1 does it also describe how it works I don't want to do that multiplying polynomials block sequence I'm looking for an easy one maybe we can I think there's a filter actually or something that we can sort progress status sort by there you go most completed human readable duration format snail why is this checked have I done this already I don't know range extraction strip comments so many permutations okay uh let's get rid of this one let's get rid of this one okay your task in order to complete this car is to write a function which formats a duration given as a number of seconds in a human friendly way the function must accept a non-negative integer if it's zero it just returns now otherwise the duration is expressed as a combination of years days hours minutes and seconds it's much easier to understand with an example for seconds equals 62 your function should return one minute two seconds for seconds whatever it should return that okay so we always get just seconds is this correct hmm let's try this one it doesn't seem too difficult okay so we only get seconds this is good we only get seconds this shouldn't really be too difficult because the idea is you get seconds and we could Define certain thresholds so I think what's the maximum thing that we can have the maximum thing that we can have is a year right so we have basically if I write this as comment here 60 Seconds is going to be equal to one minute then we have uh 60 times 60. what is that three thousand 600 seconds gonna be equal to one hour then foreign this times 24 is going to be equal 86 400 seconds is going to be equal to one day then what did we have here okay we didn't have weeks and we didn't have months so we just need years and then we have 365 days um so that times 365. which is this number here so that many seconds those are the thresholds so that is one year which basically means okay sorry for actually trying um this basically means we can do this as a sort of modulo operation so year is not relevant if we have less than that amount of seconds which means that I can do let's just start with some basic code here if I say [Music] um if I say that this number here if I say that the seconds are less than this number then here is irrelevant then I can focus on day if I also say that seconds is below that number here then I can focus on hours if I say seconds is below this number here then I'm only focusing on minutes and if seconds is less than even 60 then we're focusing only on seconds because then we don't even have minutes so if that is the case we're going to just return a formatted string what was the format exactly we wanted to have uh let me just see here in the instructions just number seconds right so that would be just whatever the seconds are seconds seconds do we need to have a DOT at the end no okay so that is that now if we have more than 60 seconds we're going to have minutes so otherwise we're going to return here formatted string we're going to say seconds modulo no actually not module divided by so floor divided by 60 those are going to be the minutes and then we're going to say seconds modulo 60. seconds I think this makes sense here I'm gonna say actually we need to say uh the thing is now of course if we have exactly 60 seconds it should be one minute and not one minutes and zero seconds but we're gonna take care of that later on um here we're gonna say now if it is above 60 minutes we're going to do the same thing with hour so we're going to say here seconds floor divided by and then three thousand six hundred hours and and here we're gonna say now seconds now the question is how do we do that if I have let me just uh do an example here in the command line if I say Python 3 and I say now I have I don't know 3677 seconds if I say now 4 divided by three thousand six hundred that would be one hour okay so if I say now for the minutes we have 36 1077 modulo 3600 those are the seconds remaining here yeah okay then we basically just do this we basically just say modulo 3600 and that again four divided by sixty and at the end we have seconds and then we need to basically take this come on okay I'm gonna write myself seconds modulo 3600 modulo 60. and that again is going to be seconds and I think this pattern will basically continue here now because what we're going to do next is we're going to say okay for the days we're gonna do seconds divided by and then we have uh 86 400. days and for the hours now we're going to do here parentheses seconds then modulo uh 86 400 divided by 3600 then we're gonna copy this I'm gonna say ant I'm going to say here this but instead of divided here again modulo all this and divided by 60 floor divided by 60 and then again we're going to copy this here and we're gonna say and I'm gonna paste this here this again changes to modulo and this is seconds and finally we can basically copy this here but now we're gonna change these numbers here to uh actually can I not use can I use a Vim let me just see if I can use a Vim substitution here if I say substitute seconds with seconds or maybe I should use parentheses seconds modulo and whatever the number is here this one G did that working here of course I would have to yeah this actually works doesn't it I think this actually works because now we have seconds and of course I need to put this here in front so seconds floor division by that number that would be years and okay now the only difficulty here is what if I have a zero so this should work now for basically everything so I'm going to copy this I'm gonna Define this here does this work format duration yeah but the problem is now I think okay I can I can make this even larger what is that now uh ah of course it's substituted to last seconds as well um so let me copy this again okay there you go but the problem is now if I enter something like 3600 I'm gonna get one hours and zero and zero seconds this is the problem I guess this is where the solution won't be correct because I think now also one seconds should equal one second one minutes and two seconds should equal one minute and two seconds two minutes and zero seconds should equal two minutes so you can see basically it works but we don't have all of this handled so I'm gonna try to do this um in a sort of unusual way I'm gonna drink some water here what I'm gonna try to do is I'm gonna try to just keep it like that but what I'm gonna do is I'm gonna use a regex to remove the s's if the second is one or if the value is 1 from the string so I'm going to look for the pattern that I have some digit followed by a space and a word and then if the word or if the digit is one I'm gonna remove the s from the word and if the digit is not one I'm gonna have the S I'm going to keep the S there and then if the digit is zero I'm going to remove basically ah this is too complicated to come on um how do we do that how do we do that in an efficient way because doing it in general is not hard I could also just hard code it I could go through all the use cases or all the all the cases and then just filter out these cases but how can I do this intelligently how can I remove all the zeros and how can I remove all the uh s is from the once I don't want to have separate branches for the exact value so I would like to manipulate the strings in some way because also we cannot just remove everything after that if I have for example 3601 it would be one hour in one second so I cannot did I remove the minutes here oh yeah we don't have all the words we need to also add here that this is hours this is minutes and this is seconds this is days this is hours this is minutes and then we have here Years days hours and minutes and also I think that the instructions said yeah there should be a comma not an end so this is kind of yeah we need to put more thought into that but I would I would really like to have this as a um as a regular expression this would be awesome but the thing is I'm not allowed to Google the Exact Solutions um so I cannot just ask Chachi but Detroit a uh a regex for me but maybe I can I can Google for a regex uh to say remove s from hours in minutes for example since this is not the exact solution to the task I would say I'm allowed to use it um okay now this is actually but we already have this so this is not a big deal I mean yeah that would also be an idea instead of directly creating the strings we could store the values so we could say [Music] um how would we do that we would say you could do it like that we could say here in this case or actually let me just copy these here [Applause] instead of returning what we could do is we could say this just returns or this just saves a list of zero zero zero so Year Day hour minute seconds so like this let's call this Theta and here we would say data equals you would say zero zero zero then this then this and then like that and here again same thing would have data equals list like that and we would have two zeros in here and now we would have one zero and then that should be it and finally in the last row we would have data equals and we'd have to do all this again so we have a comma here we would have comma here we have a comma here and finally would have a comma here that would be it that and now we have the list of and we need to put a opening bracket here but now we have these year days or Years days hours minutes seconds values here now with those values we can format the strings so we can say how do we do that if hmm if data how would we do that now we need to do that intelligently because we need to I don't want to go through all the permutations obviously but I need to somehow figure out a way to to also remove values in between when there's zero and to to keep the values that are not zero so what we have to do is we have to Express this in a more generalized way but I don't have an idea right now how to do that so let's say I have here let's go with examples examples are always good let's say I have let's just comment this out just as possible let's say I have the following list I have like one year or maybe let's go zero years four days zero hours 54 minutes and 12 seconds how would I format this I would say years is zero so we can skip that so we say okay the first one is year zero if no we say whatever the value is years okay but now we don't have it so it's zero then we would say okay the next one is the next one zero now it's four okay so four what is it uh days and because it's not one it's days actually this is not too complicated I think we can do that quite easily can we not just say uh final string equals empty string and then we can say is there some problem with the parentheses here I think so because I'm constantly jumping to the wrong wrong line so final string is going to be an empty string and we're going to say that for elements or let's say 4 i x in enumerate then we're going to enumerate data we're going to say if or maybe we can also do here uh positions we can say here day hour minute second and we can say now if positions no not if position if data I actually we can do this in a different way we can save just I in range length data if the data here is equal to zero we just uh we just skip it does this make sense can we just say continue alif the data is equal to one then we append to the final string so we say final string plus equals and then we append to this a white space and then the value so data at that position and since it's just one we also get the position so positions I all right sorry had to go AFK for a second uh so yeah we have the data I we have the position size so basically in this case it would be one year or one day because we already have singular here so it's not plural now in all other cases we would want to say so basically just else in those cases the final string would be data I so still the amount then position I but with an s so that's it Years days hours minutes seconds the only thing that we now need to also take care of is the ends so we need to say um so if it's zero we don't really care we just continue but if we write something like one year one day whatever we need to say how many values do we have afterwards that are not zero so non-zero successors we can call it is going to be how many values do we have after this index that are not zero so I would have to say uh let me just take this list here as an example let's do it here so we have this list here this is my list and I want to say my list at position one for example is four okay so to get all the values after this would be four plus one or one plus one in this case so the index plus one until the end that would be it and I would just have to say is it any no it's not any how do I count non-zero value sufficiently what's not any right um can I count non-zero values in any way python count non-zero values and list okay numpy I don't want to use numpy for this okay no I can do it as list comprehension doesn't matter so I can just say basically take that and then go and say 1 4 x in this if X is not equal to zero and else zero there you go okay and then we can just sum it up so you get this sure okay so we copy this we can say paste and if instead of having one here we just say I provided of course it were not already at the last position but we can take care of this so the idea is if non-zero successors actually we should say if let's handle this right away if I is already the last position so if I is not the last position let's put it that way if I is not equal to length my list um minus one actually right because the last I is going to be in this case 0 1 2 3 4 but the list is five so if I is 4 so length minus one then I don't want to do this so I want to say okay if I is not that we're going to calculate how many non-zero successors we have and if this number is equal to 2. we're going to add to the string final string we're going to add to this a comma elif when this number is if this number is one we're going to add an and [Applause] no it's not if it's two if it's two or above so if it's greater than or equal to two and in all other cases we're going to just add nothing so basically hmm basically return is this correct 10 seconds does this make sense let me copy this and try it locally before I try here so let me just zoom out a little bit and then let's call this on format duration on I don't know something like this my list is not defined uh sure because our list is data still my list is not defined where am I using my list data not great why is that uh first of all we shouldn't return seconds we should return the final string of course but then again we also no uh this is this is stupid we just have to say if it is the last character we're gonna return shouldn't this work one hour ants okay we're missing some white spaces here and we have a white space in the beginning which is not good so if it's zero we shouldn't have a white space oh yeah I know why because we have a white space here every single time this should not be the case so we should have the white space actually here and here one hour and so now we're missing a white space after the one hour because here of course we should also have a white space one hour and 43 seconds okay seems to work let's go with something like this one hour and one second okay seems very promising let's just export this I'm gonna copy this here and we're gonna test this and we're gonna see if it works let's see I'm gonna run this okay two minutes didn't work and one hour didn't work so actually what didn't work is if I have exactly this value what do I get here none okay interesting why is that because I don't return anything final string is an empty string let me just see here uh print data okay we have one one in there that's good so at some point we should get to that and it should be here so it should actually append this to the string oh I get I get why it doesn't work uh it should not continue it should just pass I think oh no this messes up a lot hmm okay so what's the idea we go through the thing if it's zero we skip it but we should actually say if it's zero and if it's not the last thing so we should maybe say if uh maybe this one hour great let's see if the rest still works okay seems like it works let's try again to copy the output where's my cat command there it is copy then remove all of this here paste this and let's see there you go all the tests have passed let's attempt okay there you go oh yeah the only thing that I didn't take care of is the now so if we don't have anything it should be now before I uh therefore I don't return anything so if zero now basically that's the only thing I'm gonna do this right away here so if seconds equals zero return now attempt oh attempt there you go we solved this submits let's see what the other people have done here okay this looks quite concise yeah he does basically the same thing he has your tuples with the thresholds and he also does a pending here with comma and and this is too broad what the hell is that this is the most inefficient thing that you can do this is really not good okay no this this is garbage don't do it like this this looks okay this looks okay yeah seems like our solution wasn't too bad 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 other than that thank you much for watching see you next video and bye

Original Description

In this video we attempt a 4 Kyu challenge on Codewars for the first time. Codewars: https://www.codewars.com/ ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 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:24) Special Caesar Cipher (5 Kyu) (44:12) Duration Formatting (4 Kyu) (1:19:34) 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 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches how to approach and solve a 4 Kyu challenge on Codewars, specifically the Duration Formatting challenge, using Python programming skills and algorithmic thinking. The challenge requires formatting a duration in a human-readable format. The video provides a step-by-step solution to the challenge.

Key Takeaways
  1. Attempt a 4 Kyu challenge on Codewars
  2. Read and understand the challenge description
  3. Break down the problem into smaller sub-problems
  4. Use algorithmic thinking to solve the sub-problems
  5. Write Python code to implement the solution
  6. Test and refine the solution
💡 The key to solving coding challenges is to break down the problem into smaller sub-problems and use algorithmic thinking to solve them.

Related Reads

📰
I Built an AI-Powered LeetCode Auto Solver Chrome Extension (Using JavaScript + LLM APIs)
Build a Chrome extension that uses LLM APIs to automatically solve LeetCode problems, and learn how to integrate AI-powered solutions into your coding workflow
Medium · AI
📰
I Built an AI-Powered LeetCode Auto Solver Chrome Extension (Using JavaScript + LLM APIs)
Build a Chrome extension that uses AI to solve LeetCode problems automatically, leveraging JavaScript and LLM APIs
Medium · JavaScript
📰
Creative Technologist
Learn how to build an interactive AI academy platform to enhance learning experiences
Medium · AI
📰
Tokenmaxxing And The Future Of AI Inference: The New Cost Curve
Optimize AI inference costs by understanding the new cost curve and tokenmaxxing to improve reliability and reduce latency
Forbes Innovation
Up next
You've Been Using Claude Code Wrong (Do This Instead)
SCALER
Watch →