Master Lua Programming with Lua 5.3 | Abhishek Kumar | Skillshare

Master Lua Programming with Lua 5.3

Abhishek Kumar, Computer Scientist at Adobe

Master Lua Programming with Lua 5.3

Abhishek Kumar, Computer Scientist at Adobe

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
15 Lessons (2h 34m)
    • 1. Welcome to the Course

    • 2. What is Lua?

    • 3. Modules in Lua

    • 4. Modules as Tables

    • 5. require - load module

    • 6. Writing Modules in Lua

    • 7. Data Structures in Lua - Introduction

    • 8. Arrays

    • 9. Matrices and Multi-dimensional Arrays

    • 10. Linked Lists

    • 11. Queue and Deque concepts

    • 12. Deque implementation in Lua

    • 13. Implementing a Stack data structure

    • 14. N-Queens Assignment Solution

    • 15. Iterators and Closures

11 students are watching this class
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

Lua is a powerful, efficient, lightweight, embeddable scripting language. It supports procedural programming, object-oriented programming, functional programming, data-driven programming, and data description.

Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode with a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.

Topics covered are:

  • Introduction to Lua
  • IDE and installation
  • Basic Language Constructs
  • Numbers
  • Strings
  • Tables
  • Functions
  • Input/Output
  • Blocks and Loops
  • Closures
  • Pattern Matching
  • Date and Time
  • Bitwise Operations
  • Data Structures in Lua - Arrays, Matrices, Linked Lists, Queues
  • Modules and Packages
  • Iterators and generic for
  • Metatables and Metamethods
  • Object Oriented Programming
  • The Environment
  • Garbage
  • Coroutines
  • C API

So, let's learn some cool stuff and dive into the course.

Meet Your Teacher

Teacher Profile Image

Abhishek Kumar

Computer Scientist at Adobe


Computer Scientist @Adobe

See full profile

Class Ratings

Expectations Met?
  • Exceeded!
  • Yes
  • Somewhat
  • Not really
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Your creative journey starts here.

  • Unlimited access to every class
  • Supportive online creative community
  • Learn offline with Skillshare’s app

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.



1. Welcome to the Course: welcome to the scores own lower programming language. First of all, who are you sure Lou is a powerful, efficient later it in veritable scripting language. It supports procedural programming, object oriented programming, functional programming, deter driven programming and deter description. It combines simple procedural Syntex with powerful data the scripts and constructs based on associative Berries and extends well, semantics. Louise dynamically typed runs by interpreting bite cold with register based were still machine and has automatic memory management with incremental garbage collection, making it ideal for configuration scripting and rapid prototyping. Hi, my name is over, say, Kumar and I work at a computer scientist at Adobe. And in this course I will teach you the nuts and bolts off lower programming language. This course is supposed to be the one stop solution for all of your lower knowledge. So let's briefly look at the course contents. So our discourse will start with a basic introduction to lure. Then we will do idea and an installation. Whatever idea we will use, will you Jiro Grain studio for this. Then we look at some of the basic language constructs. Then we will look at numbers string stables, functions in port or port blocks and loops in lower closures, pattern matching dirt and time, and we will also look at between your presence. Then we will look at Ah Abu or implement various data structures in lower using Ables. And in particular, we will look at a raise mattresses link list to use double 100 cues. Then we'll see how Boro create your own more guilt in lower and packages and how to use the existing models. Then we will also look at traitors and Gen req form a meta metered centimeter tables. They will look at Coteau du object oriented programming. Lure well, look at the environment, garbage collection, scorpions and CHP. There we will see how toe are right if functionality in C and expose that to Louis side and call it from lower, So let's dive into the course. 2. What is Lua?: Let's start program in lower, and in this course we will be starting Lua version 53 which is the latest, was an awful what that is available at the moment. So first, let's see who had the name lower. So it comes from a Portuguese word and it means whom. And if you see that, I kind of lure, you can see that this ICANN is inspired from the sleep off him. It looks like a moon, and it's revolving and are now let's see what is Lou and why do we study? Were wise. It's so popular. Eloisa powerful assistant lightweight, incredible scripting language. And it supports procedural programming. Object oriented programming. Functional programming data driven program ended her description. It combines simple procedural Syntex with powerful data description, constructs business with city Berries and extensible semantics. Lloyd is a dynamically typed language, and a transfer interpreting bite good with the Register based was to losing, so you don't need to satisfy the IBO variables here. These are interpreted. Automatic memory management is provided in lower with the help of incremental garbage collection. Now let's see, or what are the learning resources or this course in itself will be sufficient to get you started with Lou and have some good knowledge of lower. But if you want toe big, different to it on, want to learn the language in we really tell you can refer this book. This is the most popular book for Lower, and currently 4 37 is the littlest one 4 37 or it is lower 53 or their third. It isn't also which Fox is on Lower five to. There may not be too much difference, but if you have a choice, we'll go with the latest one. And then you can always move back to the Reference Manual of flu, which has Indust documentation so you can refer this also. 3. Modules in Lua: in the next few listens with a study. A what? Model? Sand packages in lower so many languages provide mechanisms through which we can organize the space off lower limbs and ah, these languages provide packages for young Berlin job and pearl or name spaces in sleeplessness. Eso, the main purpose off the packages is too are providing basic mechanism in order to avoid collision among names defined in different level res. So, for example, ins sleeplessness. You write name space earlier to see name Space one, and within that name space, you can define different functions F one F two and so on. Similarly, you can have another name. Space name is place to and you can have the same function name in another names face as well, and while calling, you have to specify lake and this one F one. So this will call F one belonging to name Space one. So it helps to avoid collision in the names. If you are defined everything but our names faced and there will be a collision between these two functions of missions. Similarly, in ah, Java, you have ah kee vertical package. So you write the package and then pack his name and then you defined class and other things . And then, in some other fail, you can used their off classes function using pack his door, a function name similarly in C plus. Plus, we have the cure name space. We also epic is important and other languages but in Louisville don't have an explicit mechanism for defining the packages. But we can define practices using tables, the mechanism off tables We have already seen what our tables in lower So we considered model is just like a library that can be loaded using required and as a single global name campaigning at people. And Morgan, seen Lou can be manipulated the same way as any other lower table. So we have seen that we can have a local variable which can hold a new number string. It can also hold a table. And we uh no. We have seen that more Dussel Abel's underneath. So a local very one week in a sane and Morgan. So we used a require keyword using this we load. Ah, moderate. So Mattie is a pretty fine model in lower. You can define your own model and I mean for the lessons, we will see how to load Ah and define our own more guilt in lower. No, I have been talking about models in lower but not packages. So because they're nothing but a complete tree off models, what is mean to a complete two years? Models are You can have multiple levels in a model so you can are define immortal. And within that you can have some organs and so on. And we have also seen that in overly listens that we can every table and the dinner table. We can have keys and of one of the keys may hold on other people and so on. So the same hierarchical concept is followed in, Model says Well, and the so the core. Also studying more deals and practices in Lieu are the models. Once you're 4. Modules as Tables: in our previous listen, we had seen that there is no explicit away or keyword in lower for defining model tour packages. But we have seen that we can define or lower models as a table. So in this listen, we will see how we can manipulate the models that we applauded as it normal lure table so it can be manipulated in the exact same way as any other simple lower table for young below , we assign a table to a local variable, and in this case, we use require keyword Lord a model. So Matt is immortal and we use require and this model is loaded in local. Very well. Um and this him is off type table so we can call the daughter greater on this local variable because him is a table, so we can call them door sign. And this will call sign function on this 3.14 which is very close toe by. So this would give some value very close to zero. This is just one way we can call it in other ways. For example, we can call require Matt, and we don't descend anything. Toe this library to a local variable, but but we just call requirement, and then we can call Matador Sign. So by default this well with a name in which it will be loaded. But we can assign it to any other very William as well. And another way would be to call require Matt and then a sane in the specific function. So this metal, every lab eso function lakes course saying and absolute value and any some other constantly power, So we just want to use a few functions. Then we can load specific functions to some local variables we can assign, especially function off this model to a local variable, for example, a sequel, Toe Matador, Tsien and then call Ah yes, off 3.14 So these are all different ways we cannot manipulate models in lower. So let's see some example in our I d. So we can. First call is a question Met aan den. We can sprint Math Nord sane, and we will call 3.14 which is very close toe by solar TSA. Run it and you can see the values very close to general and this is has expected and Noah, we will modify it. So let's store this met more duly into a local variable and this will be off the table. And no, we will call just in broad saying So Let's in digital here this sign of Jiri old Sergio. So you get exactly zero knowledge. So trade The third mentored, which is we call requirement. Then we store the same function into some local variable. No, it's a sign matador scene and no, we don't need to call it on model. We can just call Sign which so we have ah, stored the sine function which is defined inside this a model Matt into a local variable sign. And then we can use local this local variable as a proxy for this so we can call sane off you, which is zero. Let's go back to our by example 3.14159 and let's turn it so this is very close to zero. Distant dress stood power off minus six, which is approximately zero 5. require - load module: continuing our discussion on modules and packages in lower. Let's see what is required function we have already seen. Ah, a bit off record points in hourly listens. So let's a receipt in more detail. So first of all, or did require function eso It's a high level function and it is used to Lord and model. How do we load a modern? We just called required and it's a function. So we give a parent icis and them string literal and we can load Ah, this mortal to local variable And here I'm using Met. But by before to be some of the standard modules like Matt String are you is Table and other ah models are by the foretell ordered with the same name the modeling. But we can lord it different names and weakened in court endured devious or m door saying. But even if we don't, uh, import these kind off standard models, it will work. And when calling require, we can have ah, string literal, as is the case with this example. So the single argument can be either a string literal or can be very well. For example, here we're calling with a string literal, but we can ab in local Morgan and more the variable which horse? The name of the model. So this is string literal is held in this local variable. And now if we call the same thing local and make a little request moored. But they're different series that when it's a string literal, you can call this or this is exactly same as calling goulash Met So an equal to require Matt or chemical too require parentis. Ismet Otar accepted. But if this is not a string literal, as is the case here, we're calling with a variable which holds really of a string little This is not same as calling required and more. You will give a space here. So this will true in it or not Ah, it's not allowed. If it's not a string little, it will complain that expecting a string little what you're calling more so this you need to keep in mind so but heuristic with ah parentis if you're using a variable and it's also very common toe ignored this parenthesis and use the string little the actual name off the model so I can give a brief them off. This for that. You get my point, salute to a call local am equal to the question met and it will explain. Then I call this still it works plane? No, I store matting to this acceptable and you see, um, ex acquittal. So in this console, there is a problem that it does not store in local. So X is a big net. No, I call I would skip local Emmy Cool too. Request X. So this sort of work fine, because I have added the apprentices. But no, if we remove despair intereses complaints. Although exit is still holding this mess. So when you're not using string Little keep in mind that you give the parents. This is now let's move on. We need to know how the require function works. So it works with the help us able called Pakistan are loaded And what the stables worlds. Whenever we require any Morgan, it will be added to this table. So this back his doctor lorded Is it even and for example, in over kids it has a more detailed map which itself is your table, as we have seen in our previous listens or similarly, it will have other people's like string people all too. Then are you with and some other tables standard models. And if we define our own Morgan and we will see how to define our own models later, and ah, we then load this mortal using require then, uh, when you print because dark loaded, you will see entry for your own more. You'll also like mortal in equal toe able, and it will contain the functions that Europe defined inside your custom model. So whenever you call, require modeling it 1st 6 Whether this model is already present in Pakistan, loaded table or not. If it's present, it will simply return this value. Whatever is the model you're calling require bit, but if it's not present, it will move for their. So if the model is not a loaded here, then it searches for the world file with the same name as the modern name using the variable akkus door. But so when you print back is not part of this holds a list of parts inside it, where it will first look for this mortal. So if you call required ah more one, then it will look within the list of this parts Ah, for a file called Moored Wonder to lure. And if it finds a file it will loaded to using Lord file more than one daughter Lower Mr Resulted a function which record in order, Lord Stefansson that when called lords, the more do so in order to function. Ah, but it may not represent, uh, within this package or what? Ah, whatever is the part is stored in this So what it will do It will no look for in modern limit searches for c leathery been that name And it looks for because Lord see pot and ah, if it finds it a celebrity, then it will use back is dark lordly Here it will using Lord file here it's using Pack is not lordly looking for a function Corn Lou open under school Morgan him So this is the Lord er in this case in this case Lordy, Lord Foyle Hair. It's blew open morning. So either way, if it finds in the lower part or C library, it has in order. So no matter whether the model was found in a lawful order, delivery require now has a loader. And what is the function of Law Order Lord, is your function that when called Lords, the morgue finally required costal order with two arguments. So either way, it will have a loader. And what the way to call the lottery's toe past two arguments. When is the model name that was called with required? And there is the name of the file. Very it got the loader most more duties and just ignore these arguments. If the Lord returns any value, require returns this value and stores it's in the back. Historically loaded people are to return the same value in future as we have just seen to force require into loading same model choice. If you want that every time it called requires made require should do the entire work and load it again. Then once you're done, you can sit back is not lorded, Not more name equal to new. And this is the name of the custom model. So when next time it calls required, it will. Sick packages are loaded table and it will not find this morning, and it will re do the work salutes to see some examples to get over hands under this, uh, for example, back is not loaded or happens If I do this, it will. Our source used number off lorded models because a Z we have seen already it will contain Matt String Table are you was and many more standard models. So it will slurred this console with all of those So in order particular individual models we can print. So this will just print this more Do you see This morning starts with this braces So this morning itself a table and it has the list off various functions. So functions are first class surgeons in lieu and B you're assigned to a variable So obvious function is there are call sign is there Arc sine is there and all the similar Matt functions which you can think off like our referendum and alerts again called Pakis Door loaded north Ah, the string. So you see the different string functions are stored here Then similarly there are always Then they're more deals for you and you can try different things And you can also load your own mortal Onda. We will see our greater model in detail later work for time being, Let's create immortal my model equal toe empty table and simply return my model. So this did not have currently any function? No, What we will do here, we'll call my model equal to So first. Let's take that kids door Lord did not My model, it's it will printing. So my model is not yet ordered. No, I call Chemical too Is the question my Morgan? So in this case, it it will look for my model or glue in the Oh, this spot no designed it back is your pot and it will find it and it will load it so you can see that Mm is not new. It's an empty table as we have defined here. No, If I call like a stork loaded or my model, you see that it's not meal here. It was nil hair. It's not mill, but you. Concerted two nil and it will or reloaded on little to look at the contents off Pakis Dort part and back is nor see. But so I hope you got some good understanding off how what is required function and how it works in lieu, in order to Lord a model 6. Writing Modules in Lua: know that we have the basic understanding off more jewels, how model to work and how the require function works in lower. We are ready to create our one model, and we will see two methods off creating models in this listen. So in order to create a model, we will create a table we have already seen that mortals are nothing but able on. This table has a number of functions and their implementations. So first recreate an empty table, we can give it a name on. Do we put all the functions we want to export inside this table? And finally, we returned this table at the end off this file. So either we will return in the table name, which is the first metal, or we will return a list which will have a list of functions were just defined inside dart function. So let's look at the first method. I call it simple mortal mental. So first, uh, it's a file called a movie Dort lower or whatever is the name off your model and you create an empty table and then you create defined some functions and finally you put it inside this like more doubtful one Nicola this You could have also written like a local more equal toe. Instead of creating empty table, you can start the signing. We're loser like food one equal to function and the exact same function definition and football and other funds and definitions. And finally you return more. I have done it in exactly this way for stay clear to table Put the functions insider. So go Terry current and we will see that both are equivalent genderen exactly the same way . So first let's look at this material one. So yeah, I have created ah profile called to read or lower And here I will re creating our model which will handle different off presence on a rectory into the director. Can we end diamonds? No, but let's say we're considering it to. Time is no butter that is in plain X ray plane. So first we need to create even then, what are the presents that we want? Picks food? One method can be hard to creativity, given its two coordinates. So let's be flying this songs and so either way, like, ah function inimical to function or local function andare Grint list So it will take two arguments. Xingwei. Because in we denote victory EDS X then ah unit Victor, Let's say I this unit director along extraction plus way and you need to retire alone. Why direction? If we have more confidence, we would have more unit vectors. So we need to variables to values X and way to denote a veteran toady hand ribbons. So every victor will have ah, two features or two properties called X and Y coordinates. So x equal it works. You can give a different name also like I n g and their total to work and returned this winter and and and I forward to give it a name. Let's give it new. We go here we are creating a new rector. No, we want to urge to Victor's so it should take directors even anybody to. And it would return the result of that so we can return X equal to move even detects. So the victim medicine works like the yard. Ah, the confidence off two different directors along the same direction. They're different values along different conference. So all the values along ex confidence are added together and wait wither. So here we are just providing Wait where two victors and not move, so it will take exactly 22 Surgeon who's resultant X component result will also director who actually Oh, so in this case, we have I we will not. I So you have to maintain consistency. You need to keep in mind what are their two features here? And, jeez, be even Georgie. Plus we told ORT g similarly for subtract on and or let's say, we want to give a function for printing the victor bring to fix and it will take one victory in Onda and drink it. So let's say we want to print it in table form. I could too. So this would bring the victor on. Well, maybe now we These are just local from some. These are not part of this table. This more do that we want to export. So what we'll do to be dot whatever name you want to give you can give a different name, but I will keep the same name as the name of local variables. And once we're done with it, if you want to keep some ah functions private, it cannot be accessed by any other model, which is Oh, importing this more dependent you can you miss? Keep assigning it toe this table. So for jumble here, subtract is a local function or we will see the most it shortly solar. It's exported first. So no, we have cleared anymore. You define some functions for that model and then exported the model and here in the claim cooled we can have very well let's savic on this will call request Rick Duty And then let's say we cleared a few victors. Even Nicole toe ignored new 10 and 20 So 10 and 20. So you're so dizzy over X ray plane, then 10 and 20. This is a terrible reason somewhat like this resistant. This is 20 on This is we who won. We can write it as 10 20 or then I plus 20 years. Let's say we create another victor which is middle image of this that is minus 10 20. This is very to and is were these it will reeled in a victor like this whose ex competent will radio on why you will be 40. So this listen this So these directors every girl and you can droit here also this will be 90 degrees on this will be Ah, not 90 degrees. Sorry. This is our twist to one race you. So it's more than 45 degree so you can find out to what is There are the Greek word. You will see that it forms and I should sell a strangle. Well, this values him at this value because their magnitudes are same. So let's see their duty. Never. Korb. Let's create a victory to acquittal. That's not new. Minus 10. 20 on local equal to. So we have added undies and we will also call over rent function. So Victor drink tricks who we and then we will run the court regard some better. Ah, so we have missed some end somewhere. So in c placeless This is the problem when you're according in other languages. So here there is no turning off. The function is not like a delivery system, but my end saying Mister care new resign Bring this fine. So let's run it again. So the mistake it at 9 35 in two D. So it's not exported return. So is returning. Now Let's run it And it works as expected, sold The result ended Jiro, because this 10 in minus 10 can salute and then miss 10 20 plus twenties for P. Oh, let's also try earning it. And you see the differences 10 minus minus 10 is 20. On this way value Cancel orb. And no, let's say we want to make this subtraction private. So what we will do? We will not added to the exported from sense list. And no, if we call it, we get him because ah, this when we require all require this model is imported here as a table. The exact same table that we created here. What? This sub is not part of this table. This is a local funds into this morning. So Rick, nor some isn't it? So that is what it is. Complaining. So does he already went private functions? No. We'll see the second mentored offs creating models eso in a limited. We had created tables, then ended some function student and exported that table. So here we defined the functions the same way on. But while exporting, we don't add all of these late a model named orgy full one equal tofu when we don't need to assign all of these. Once we have defined some functions, all the function that you want to expose for treating this table. So you return a table and you return different key and the value are these function definitions. It's alerts mortify Evercore to work with. New Guimond isn't so. We don't need to do much. We will comment about this part of the court and is he also not used so it will not be required. So we will simply return if people there's new equal toe new and already court weird, you can give a different name here. So we returned this. Let's change some in blue and isn't so There were, ah, sealed art. It's the new rheumatism. So there are lots of yard, some extra characters in the print function. And no, we don't over Klein cord, and you see that we don't me to change it. This is so This is just another way off visiting our models and you see these extra characters here, so it's using our new implementation. So these are the two days off easier ways of creating models, and there were some older ways of creating more deals also, in a way, I'm not going to cover that here 7. Data Structures in Lua - Introduction: over the next few listens, we will see how the work with data structures in lower. So first of all, who are later structures or desire the main, the district system that comes to your mind. When you think of data structures, it can be a linear structures like area your link list or two dimensional or multidimensional arrays or mattresses. Then we abuse which followed first in first or principle and then via double ended twos. So, Accuser 100 that is You insert in there and and you removed from the beginning. Um, but double ended cues are you can remove or in searched from bow dance neither of record decks. Then we have suits of which have some properties like you store some there. Loser and duplicates are not alone. So these are has searched internally. Then we have has maps. So in many languages, like Java, are a C plus plus. Also, we have a standard template libraries and there are some very efficient implementation off all of these data structures there, and ah, in low. We don't have any of these, but we have the table. So able is not a religious structure in Lourdes but it's build a destructive that is, we will use or implement. All of these are data structures using table. So in some of the convents, known languages like Oh, well, see we don't have implement isn't for these but we implement all of these using ah a raise or lists. Similarly, we will implement all of these using tables. But I want to say that our tables are much more powerful than lists and a raise in C and ah Lord tables implement all of these structures much more efficiently and over the course off your lessons we will see how toe are used table to implement all of these structures. 8. Arrays: Let's start with the simplest infrastructure court marriage. This is a linear Detroit stricter Onda. We will see how we can use Able said There is in lower. So if you have some, If you have worked with raising some other languages you I know that, uh, there's a linear data structure and there are positions or in this is on there are actual elements which are sitting inside that data structure. So in some languages, like C Professor John Boy, it will start from zero want to And if there are in elements than it will corporal in minus one Oh, in lower there is no such restriction. You can, uh, in certain elements that any index even negative element needed to in distance. But there are some rules which is given mind if you have tow huge some of this tender oh facilities provided by lower celebrities and that says that usually start the indexing throng one. So first, let's see, let's forget toward indexing and let's see how we can or define marriage. So the implement areas by indexing tables within this is integers. So you create an empty table, then for 1 2000 you are treating your index I So first index movie you awful one on we are putting here. I This is one than a off to is do so So here it's one. It's too, and values also wanted to. And ah, you see that here It's not fixed, say, but has we insert more elements? One more index will be created and its size will increase by one. So it is. Do not have a fixed, say, but grew as needed and let Sylvia inserted cogent elements. And ah, the Indus is er from one protection because we have manually ardor Genesis one, 234 Uphill caution. So it's really tried to access. So if we have to access an element element, we will say print it and then it will print element that is stored it. And so it'll print them. But if we go outside this range index range, whatever we have is trained for jumble sales. Ah, 1000 Tim. So this is beyond one pageant and it will return a decision. Underwear off, creating Aires on Do you just provide an initial a journalist So you declare it and defend it in the same state in the same online. And why default? What will happen is that this will get index one. So if you try to print be one, it will print one. If you bring to me too, it will bring 10. So this is that index to this is three. So here you are not riding Index here we were providing in next. Ah, so it has started index from one. If you print be Jiro, it'll give nil. What? No other language like secret lists. If you have created an area like this and you give a square braces here in on the language , then this will be out in the zero. This will were index one. But that is not the case here. Although this lower standard libraries their toe this rule you are free to assign an index . And when you want to find this a definitive, for example in this case is this area. If you use this lent operator, it will print pageant. But I have added two question marks here because this will not always work. And when it will not work, we will certainly see example of this. So first let's create enter using the methods we have seen. So we created empty Devon then for equal to 1 200 What we knew. You put a equal to two times I just to make a change. And just to make a distinction between index and actually I multiplied right toe. No, what I do. Let's sprint. If you re loose from it, let's bring table. That's bring it in. Let's spring the last element at index 100 and let's sprint one index Curtis out of bombs when you do one, or it can be a nearly larger than 100. So you see Art index one. It's two times one Who is this to at in its 20 at 100 just 200 at 10 and you know it's new . And also, if you try to access a zero, it should be No, no. If you use this limp of return, then it should correctly print 100. But this limp of Richard will not always work. First, let's see the others method also, so this is using in its allies list again. If you bring to the limp or sage of this area, then it should carry equipment for so it could blueprints fourth and uh, if you access B zero, it's Irvine, Ill. Me. One be fourth me flavor. So let's first get some renters B zero for three nil because the index by default if we don't provide index you to start from one. So be generous and will be one is the first element system before his 50 be five does not exist. So nil so new 10. 59. Let's run it, Nilton, for signal, as expected. No, we were saying that this lentil return may not always work and when it will not work when we don't follow Ah, the method that it is recommended for in this is that is we are free to give any index for gamble in this case, same as the first example. But the thing is that instead of starting from one, we start from my Aniston and Gore built in, and we create a equal. So I so it creates in my intestine equal to minus 10 than a minus lane equal to minus name . Similarly Geo according to Geo, then on the positive side, you won equal to one, and finally it will corcula. It didn't riches then. So my intestine Chilton So here there are actually 21 elements going my intestine on the negative. Say then one is 0 11 and then then from 1 to 10. 21 elements are there. But it's you. I tried to find the sage minus 10 Hilton her and let me so you can bring to the region This is you can access using the same injustice that you assigned. So what we were in Dixie was saying that you can access later. So let's get some answers. We assign C minus 10 equal to minus 20 so it should print minus 20 then see Jiro Times zero is zero See, 10 will be two times 10 visits, 20 on this result off range which did not assign descendants index at any point of train. So it should be No. And as per our understanding, this will give you 21. But we will see that this is wrong. So you see the sage ist And so this is correct. Whatever we expected but decides is not correct. So it should have been 21. But it's giving then Why? Because the Indus is er from minus 10 all the way up to zero. Then followed way one all the way in. And we have seen that if we don't, uh, saying an index, you just starts from one. So 12 still four. And it is the recommended way that if he was resigning tease. You assign in this way starting from one because of lower level grease, the different implement isn't the function of these provide there to this A room. So here it will see that I must to find the side of this table So it will look for in this is 1234 So still where it was. So here it will ignore this part. It will only consider from one depend so it will print them. So unless necessary user to use in This is from one toe in 9. Matrices and Multi-dimensional Arrays: in our previous listen, we had seen how to represent areas using tables in war. In this listen, we will extend our knowledge for there, and we will represent mattresses or monetary damages militaries. So let's see. What are the different ways of representing mattresses? So I will be talking or different ways of representation off in metrics and the metrics, Is it dearie? We can huge similar concept to represent. Hired them insensible to like freedom. It's no Fordham. Is there a raise? So this eating metrics and it has four columns and flavor rules. So art is five, and C is for so one way of representing would be not oh, table inside table. So you create Oh, people. So let's say this metrics is denoted very variable, Matt. So you create an empty evil and what you do you create. Oh, they're five route for you Create five moral Ebel's. So this will be table one. This will be table toe full. You can run I equal to one. You are so this look will run are things. But in this case, flight teams And here you will create another table which will do not as a rule and this rule has seen more developments or four elements. So again you can run a little Z equal to want to see Go. And you're saying that year Ritalin J whatever you really want to still let sin you and then end and I for work to assign this rule. Matt. So after that your courage in the rule you can Assane Matt, I equals toe room. So let's see how this works. So initially this is empty table No, I e is one that is were the first true I use one Then we look for GE equal to 1 to 4 So this room is empty So Rule one will radio row, pool regional So are the end of this This several will be holding Jiro Mutual Jo Jo And here be resigning, Matt one equal to zero. So no, this match table has when he called to this dear Oh dear on your own you know, again I becomes too for become here Similarly, we create another stable off the rose and we assign it to Matt soon released and while printing also or accessing or doing any or prison on this metrics, we have to access it. Similarly, So it's the right met three I first met. You will look for key too, so it will reach here the second row and within that authority, so it will come toward column. So this is 23 so dizzy all weekend represent and metrics in lower. So let's write a court for this first. Then we will look at the second method. So let's define over rules and columns. Ruiz C is for metrics is empty for equal to want to you go let's store or different values just for fun. So we will store one here tow were 34 then save 67 it and so on. So you see you in the first rule. It's from one to see in the next roids C plus one C plus two until to see. So in first roids. One you'll see in second route C plus one C plus two. So we're just see to every value. So we were trade to represent in 30 years. When did you bliss? So if its first rule it will be J plus zero so we can subtract one from it. And in the second row, every values increased by sea in the told you everywhere we will increase very to see And this camp represented that first rule duly zero second road Will we see The constant third road will be to see three minutes when and then we ended on what we do or the a sane man I equals room and ended So we have created over man tricks. Now we want to drink our metrics. So let's killer difference in tow Drink metrics. And you took pick one metrics in a number of rules. Number of columns So print will print everything print and moved to new lines. So individual values we don't want toe. I moved to new line. We want to print all of these in the same line, so we will use my your art right here instead of print. Oh, you're not trade to then Ah Oh, you. So you see, how am accessing the value of thick metrics that are you through injured column and then and have characters after that? And once we have printed a rule, we can into a new line. No. Let's call this print Mert on this Matt metrics. Oh, and see so is either typically Prince the metrics. 12345678 1911 12 Till 20. So this was the first way off representing metrics. So we have an order table and inside that were in her tables which are rose. No, we can represent the same thing with single lemons. Alonso, Eso what we will do we have Don't be such values. 1234 5678 till 20 So we can have just one people or you can think off one lean years, Eddie. So it will have in this is 1234 So these will denote Rolen for a minister's 5678 Miss Willie Notaro and so on. So what we can do here we can again have and empty metrics and equal toe 12 rules and is equal to I want to call him. We can arm note mapped and or whatever logic we had tears. Oh, no tears This j plus I minus one c. So this is basically the index off. So we represented in linear personal. So these 1st 4 he's followed way. These four followed rate in these four. So how can we access any G? Let's say we want to access toward rule and second Key. So what we will do? Three bliss Ah J J. Is there, so it will be two plus three minus one times c is for so using the same logic, we can put their team logic here. Gee, Pless, are you minus one times? See? And we can assign some value. So let's implemented in and similarly for accessing, Let's say, want to exist toward ruin. Second column It will be somewhere here so we can use the same trick. Your toe access in there. So we will call it Matt toe on. We will not need over where we will be using just one table. So, Matt, then India's Ilta not required and we will need another drink function here. So this is in fact, a simplistic print. Folks in print Mert Hornby or this is just equal into printing an area. I will call it here. We can print Lee nearly also just in one lane, but let's say we want to print in well, this form it. Only then what we'll do. I want to work and then again from in certain the same thing. Let's an urban separate area. So you see these two or mattresses are same, but we have used two different represent is enough undies. What we can achieve achieve similar result. So here in this matter match who is a single one? The missing people on and we are not used an indestructible and we're storing just won t to it on here. We're using tables inside ultra table and we're we can achieve similar kind off Richard for both the cases. 10. Linked Lists: in this. Listen, we will study work link list destructive and in particular we will look at the case off single Xing Leland list. So let's understand what is linked list and how we can represented in lower using tables. So we represent each note with the table. So first, before coming to this late, let's understand what it just singly link list. So our link list is a list off north for we have in order. Andi Snort has to feels in the case of singly linguists, If we have the Brylin Christo, we will have one more field. So this field whole stuff value and this was the point off next Nord. So this is one Nordoff a linguist. Let's say toward civil you've been and currently it's next is null so you can write Neilia or eternally wouldn't bank. Then we have another Nordoff link list and this horse early off 20 This is Well, this is next. Similarly, we have another journal again. Each of them have a common structure. You can see when is well and when he's next. So if you have a table, you will have a field Caldwell really cool to some value, like 10 for the first case. And next it initially nil. So we can say this is and then, you know, nor pen so dizzy just very well in lure. So you see, using this stable structure were basically representing this north. No, the question is how to connect them. Because this itself, this itself is a link list with just one more. But if we make its next point to this by that we mean you see make. So this is over. And similarly, you can define the structure for in 20 and 30 with just value. Well, you're different. This is an 20 Mrs and 30 initially. Next off, all of these is nil. No, what I do. Four next off and then I make it. And 20 and in court, Away able rate and tender next equal to and 20. Or you can write in just one table also, and then next often don't be a Mick and 30 and it's next is still nil. So this really in order end off list to, um, we will be given the first in a lot of the link list. Middles. Ah, it's a singly linked list and we can traverse Onley Didrikson, There is no next from this going to any Nord previous off it. So there is one great reversal from left to right. So if we were given a pointer off this a reference to this first Lord, we can reach to lay another list. And this is a really full of storing data. Oh, in a link list, we store in a linear fashion. And how is it different from a reason some other languages? Because the area is contiguous. And if we have to insert anything Edward in between we have to sift everything. That is not the case in Ah Lord, that is the case. If you are maintaining a fixed index like 1 200 then if you want to or insert one element are 58 food is, um, some other element about occupying 58 47. Then you want to keep guard, Then you have to sift everything to the right. But that is not the case in link list. Let's say you want to insert something before 20 then you will create in, you know Oh, Viterelli off! Lots of 15 and you move this next from here to here, and it's next to here. So you will have a new list. All the ordering will be maintained and this will be done in constant time. Now we have some idea of link list. Let's see the court. So each node is represented with a table. Links are simply table feels that contender friends to other tables. So this is the way to define it. So you So you have list is new. So they're in? No, no. Then you create a list and make its next tickle toe. Whatever was a literalist so you could get in, Lord, and it's next is no pointing to the whatever early list Was there initially triangle and on dissipated. Able is the new list list is no. This when I insert again, I will create in north who's next? Billary the existing list. So this and we will insert value on this whole new tingle with a new list. So you see your this list very really already holding the first Ronald, we don't need anything else because we have the difference off all the other lords. So we have this note we can travels all the nodes so let's write code for this Bennett really more clear. So first of all, let's look the simple way. Or we will try to create three variables and 10 and 29 30 and then link them. And then we will also travels them so local and 10 equals do well equal to 10 next to court . Don't know. We don't need to write it better if we don't write this field in this table than decision. Implicitly eternal. But let's write it for clarity on Let's Air to nor Nerds call it and 20 value 20 30 and a straight away. We can put here next to equal to N 20 but let's do it in there. So at this point of time, we have three independent notes. They're not linked. We have to link them. So we will right in 10 nort next equal to in 20. So now the first connection has been established, but this link is still not there. Then we write and 20 note next week or two and 30 and we don't have any next off, and 30 and 30 is the last North, so we leave it as it is. No, let's very to function for traversing the list. So this will take the list on ways while list is there what we do? I want to print it in this person. So at this point of time, we have this or 10 tennis pointing to 2020 22 30 on 30 is pointing to new. So we will rent it in this personally so lister. Nor, Well, we'll print Lisnard well and then this aero again. We're not using print wiggle that will take us to the new line and we don't want that. And we also need to advance turtle List Mr North Next. So what we're doing initially reports and Tim So in 10 Israeli, it's not real. So we will print its value, which is 10. So we bring to 10 and then this arrow and then we make list ical to list next. So it's next year and 20 so no list becomes and 20. So we come here refusing 20 and in this era and now the new same variable becomes its own next. So it comes here so we print 30 and again we move next and now it's next isn't little. So we stopped or we print me earlier in the end and no weird new line. Let's call the function Traveler's list on and pin. So first, see if we call it on and 30 what will happen? So we're passing and 30 so it will print 30 and make moved to its next regional. It will stop so it will just print for 40 in this case. Ah, okay, So do is missing here. So you see just Prince 30 and because they had nowhere to go back. Worse, we can go only four words. If the right 20 years it will print 20 and 30 and if they didn't hear it, will bring the complete list. So while refering to list were always refering to the first inal, we also call it Heard Head an orb or or in some other terminology, we also call it to shoot. This is derived from the concept of trees. List is also a kind of tree, with just one taylor at each level, but a strange and all can have multiple tailed. So the leader called head or root the first note, and when we pass the first note traverso function, it will bring the complete list, and we can have some more stuff here. If we are designing some a p A or some more. Do, which is dealing with link list or presents than you will provide functions for inserting into the list. Let's keep a globalist that will make things even more simpler. User's pass the list to the function the same way we positive here but for list reason we can have in Global. So we have an empty list and we want to insert value. So what we're doing here, Oh, whatever value we want to insert, we will create it, Nor with that value and whatever was the distinguished. So the list had already three elements under oath, so it really uncertain or before the three elements. So in the beginning, and this polar list will be updated to hold this, will you? And then we called insert ah, flight. Then insert could be, then insert one. So it's inserting in the beginning. You understand why? Because you will be creating old. It's next is whatever was their literalist. So it's next. Is there little list and the list becomes off to the current. Nor so we just upended one node in the beginning, and now you drink it or travels the list. What is the head North? It's just in order to buy this list itself, but we're inserting in the beginning, so five is the last 50 inserted before that one before that. So 1 55 and let's run it so you see 1 55 new. So this is how you can define other functions like you want to do some, uh, reversing off the list. You can write a function from that. You can write the function, which takes two lists and returns and new list with the some of boat or more marching off. Coolest, you can think off. Ah, Lord number off prison that you can do on lists. This was just to get you started. How to create a list singly link list. Or, if you want the link list, you have to modify it slightly. So each nor Novella one will you feel lets it in next and devious. So it's next is this little of every nor will have three fields. No and its previous. So this really previous missile revalue This will read next for all of these. So it's rib useful repointing to this Nord, its previous pointing to this nor and its previous will be no so previous off here it is no mill. And in the case of lovely link list, we also keep their friends off tail. So I heard. Until so next off tail is nil and previous off heard is nil. So it would be a good exercise to implement in the blink. Liston, do our reversal and insert sanity. I hope you understood. Ah, this If not, try to watch it again and you should get the idea. 11. Queue and Deque concepts: in this lesson, we look at another very useful later structure called Q and A blended Q or Nick you. So first, let's understand that you So it follows seafood fee for strategy, which is first in first or so when you go for movie ticket and you're standing in a queue and this is the counter and the guy a year who's sitting is giving you movie tickets and people understanding they want the movie tickets. So this person came first and he will get out of this cute first. The next one will come and he will get our next Hilger. That ticket next and whenever in New Person comes, haven come here. Not in the beginning. So cue followed first in first or whoever comes first. No unusual. This is useful in some of the state doing tasks where server is there. He's getting a number of requests from the client, and he is just nor nor tea. But the server is just doing the request into a queue in order to process nudity. Christo one by one. So here we are mainly interested in Bush smack. So if this is a que we have, someone must in 20 30. Then we will all be insert in the and and we will always removed from the front. So you just exposes both smack. So this is pushed back and this is Bob Foreign. And then we learned there is no sage, so we will implement. Ah, all of this in the next. Listen first, let's see parties day queue, orderly under que So this is very similar to a Q. But it has some personal power. You can see pushback is still little up off, 20 still lives. I just deal. We have introduced do things Whose front? So we have again 10 2030 Then we can insert here. Begin also insert here and ah, we can removed from here and we can also remove from here. So what is our personal lives? Uh, discomfort toe you. This is a reasonable and this is at least least tour additional and we have accordingly those two MPs So in the next Listen, we'll just implement doubly ended cues where we will implement all of these five functions . And it's very simple toe update this to get a simpler version or that you can do that as an exercise. So see you in the next Listen 12. Deque implementation in Lua: in our previous listen. We just saw what are cues and water. Lovely 100 cues in Dublin, in queues, we can know, insert and remove both from the beginning of the Cuban end of the queue. So we will implement all of these five bps into our court and we will create in the form of more doing so. We will create our own model named ridicu and we will heard all of these functionality to that. And then we will create a chest cold or the client called Bear. We will import this model and right over claimed called. So let's begin. So I have created two fires, one big you where we will define our modern women. Functionalities and dick you test were real important this model So let's begin quickly. So first awful charity function to create in new who will never be our defining in the infrastructure. We need a mechanism toe, create a new instance under infrastructure. So our first let's understand how we will achieve that. Then we will go to implementation. So when the q is empty, um, it will it will not have any element. And at any point of time, we will be having doing this is because we can insert and remove from beginning and end as well. So we keep one index. So all of these values are being stored at some key. So we will keep track off this the first value, which is first, or it will. This first will dinner the first key and this will do not last UNIX or the last key so initially began initialized first to it. Zero on the last ret minus one. So whenever you see there to last his less than first, then the queues empty when we insert in the end. So initially, let's say first is zero last in minus one. Let's say the insert in the end, so last moved away. One first is not enough because we're inserting in there and so last become zero and we haven't element here. Let's it then, no boaters having value of zero if insert again in the end, we will implement this l 21 and it really repointing here. So whenever be in certain turned Oh, the increments the last value and put a value there when it will be removed from the last we make the last value nil. And then you're the criminal last, will you? And when we insert in the beginning, we will insert towards the left. So whatever the value, first it will increment. We won. So for Stanley inserting the beginning let's of 50 here then this s will become minus one. So whenever we're inserting the beginning, we're in agreement in the first And when we're inserting and then we're implementing the last And the reversal happened when we're removing or popping. So when report from last, we make it new and detriment This lost. And when we pour from beginning we make this new and incriminate the first and when last becomes less than first Bender Curious MP and this size it will return Last minutes First bliss one go In the base case, you see, last minus first is minus. One plus one is zero When we are both are pointing to the same index Lefty Jiro. Then we have inserted one element. So Jiro, my energy to plus one that this plus one. So I hope it's clear Snow, Let's begin. Our implement isn't so. We have to insist. So we need to return even consisting off first tickled zero last according to minus one. So most of the litter structure you will see that is simply an extension off able. We reading something started to it. No, let's first to write the signature off all the functions. Then we will fill them different on and then we need push back. Then we need or front and we don't need a value. We go do whatever reason of front. We will hope that then woke back again. We don't need a value here whatever reason in there and will report Oh then weird sage which will return the number of elements in the queue. Let's say recently put function for printing that you so that we can test in the it's called What is the CUC at the moment and in orderto export this morning what we will do even return all of these functions So news there whose friend them push back. You can give a different name if you like, but I'm keeping it Same then or friend open back. So no, we have exported all the functions. But we are not here different. You find him. We have just different you, Knoller to define the post front. So while pushing in the beginning, we don't need to Ah, check for whether the Q is empty or not. But while we're popping, we need to check whether the Q is empty or not, because you cannot fall from an empty queue. But in pushing that is not required. Whatever is the first element pointing to first index, we were discriminated and insert the value there. So what do we look? Was front? We will do self from north, first, equal to self absorbed post minus one and then sell. We put this value transforming to this key were the first disappointing, and post Mac will be very similar. Let's Koppett for instead of Khost, it will re last instead of minus it. Very plus no, we little forth from front. So we will make this self dark for stick or two nil because first is pointing to there this , uh, left most value. So we want to put in here when we were writing both pull front. So whatever is the value of this question? Next, make it Neil and move this first hair to 20. The objective version would be 2030 and first will point here and last here. So last remains where it was forced to move here. So for Stijnen committed and we make this Neil So we will exactly do that here. Oh, we're popping from front. So if self owned art first is more than self door last, remember, this is the case when Ah oh, the queues empty. So we cannot hope from an empty Gilbey Girls, there is nothing in it. Then level the turning it off. But if this is not MPRI, level it and we're them. We amid this equal to Milan in increments in the first, on a walk back we were losing blurting work again the same MP tick. Then we will make last equal to nil and last tickle toe lost minus one onda And there can be a variant of this pop functions Or in some cases, you may be asked that you not just remove it but also return the value than additionally, you'll first copy this Very whatever is here in the local very even for you number local well, equal toe this and finally after popping it, you can return well. But let's see, in our case, we're not returning. We're just hoping it were just removing it. But you can easily make their change similarly year come back. You may be asked that nor just for Pittman told to return the off the Value Onda Society is very simple. It should be returned. Self Lord lost minus self dork. First this one aan den bring to Kew so we can have we have to bring the all values starting from first still lost. So I really exist. Self Court first and Wales Idenix less than equal to sell food or lost. So this takes care of the empty case as well because rendered simply first is more than lost. So I really exist equal to first. Oh, so this condition really, really good in the beginning itself, so it will not bring anything. Let's give some space victory in the elements And in the end, we learned a new lane. Oh, I think we're done with this. Implement isn't on backs plays in Cuba, So we have added all the functionalities knowledge, right? A client called sorely cuticle toe. You'll call declare Did you and we will create a curier. Let's call it Cuban equal to Dick your canoe, so this will create a Q. Then alleged Awesome also presents so cuter with front putting toe Cuban worked in. Then looks do a few more also presents. And then let's insert 20 and then 30. And for a change, alerts insert in the back 40. And then we print de que dirt sage off Cuban. So first we will bring the size and then we will bring the cure itself. So what we will do? Bacuna alert Drink you Q. One salutes gets our enter what it should print. So we have a look. Use empty insert, then in the beginning, so we got them. Then we insert when in the beginning 20 is inserted in the beginning and 30 in the beginning again. 13 the beginning, then 40 in the end, so forth becomes here. So dizzy or a bogus? No, let's earn it. Oh, so there is some. So here there's something between our implement isn't perhaps we're not heralding the induces correctly, so let's stop it. So let's look back at our Korb. We have used todo No! Okay, so I understood of the eight year never implementing this RDX. So is its printing the first element of again and again because 30 is the first. And you see that idea axis pointing to first. And we're taking this so it will never reach the last. So this was a blunder. This loop Bolivar terminate. Now let's run it again. No, you see Prince correctly sizes for because we have inserted for elements and this is 30 2010 4 p, which exactly matches with our own estimate. No, let's do a few more or prisons here, let's do some for. So what will happen after this 30 will report, then do one more pop so that we just all or all off our functions. Then we pull from next 30 spot for his back popped severe left with 20 eaten. So let's do this exercise again size and renting it. And our guests here is 2010 and let's run it, and it correctly says that sizes too on, but also prints 20 and 10. So you see, ah, how easy it is to implement is such a powerful later structure called Lovely 100 Q I leave it as an exercise for you to implement to which will be a simpler version of that off this one where we will not who's in the front. So this function will not be there. Similarly, uh, Pop back will not be there. It only pushed back. You put in there and and all from front and side will be there. 13. Implementing a Stack data structure : in this would you rule Study War stacks? What artist? Tax new does structure andan. We will also see how to implement that in lieu The first let's understand stack and how is it different from Cuba? So if you remember our discussion on cue, you were late But you ve seen over real life We're standing in a queue. So the person that came first that entered the two first that will come out first and the person who comes last, will or get persist In the end, we also saw a doubly ended cure dick where we're allowed to add or remove from bottoms Stacks are somewhere different in that Ah, instead of FIFA would notice first in first, all stacks follow leaf notice last in first or so. Whatever you do for whatever is most first will opt last 10 Whatever is most politest the last element there was pushed when we pop notable development that will be removed So ah, let's see, we have stuck and it's growing from mortem to top such diva inserted Then I pushed me. Then I pushed see, so does it the state off stack and instead we have no sign off Don't. We cannot randomly access any element as is the case with Terry. It is We can only exist in the top element and we can only remove the top element if we won't remove, Be than first remove See than the moon be then pushed back. See? So only your prison celeb will be to get the value of stop to push a value Here you will pass the value and what it is now you can also have site. And so his new studio in orderto purport to work correctly or it can be turned away The level record as well. So dirty for peace Corland MP stuck. We don't do anything or it can be taken care by client. So there are molecules. You can implement it so they want to push a value to the steak. So what? Deliver the change. So here we want to push me. So this would be our any serious tech. When we pushed the it becomes this. So was he disbarred? These are untouched here in ears on Ladies Postaer On the other end. If you want toe pop something, then it will be always support from the and here. You don't need to assign passive value because it's not on. What do you want to pop? You can. Just for the top element. There is no choice here, but in case of person, you want to post any really whatever you want because you can push, but you don't have control over where you want to insert. It will have inserted in the top only and in the pulpit will be removed from there. Top only. So there's the basic concept off stuck. So putting it together, if this is the stack you pushed me, it gets posed on the top and on the same stage. If you do pop, it will be against the move from top. So how can be implemented? So let's sue first try to see how can we implement on the bushel prison. So we have a nice enough top, as I said earlier, So this is pointing toe up. So when the stock is empty, that is, it does not have any element. We can have topical toe geo, so we will do similar Do deck Implement isn't so. We have able. So this is tag really not able. When we call new and it will have dropped initialized to zero. So you never read? The stop is also denoting how many elements are there initially there are developments so top is zero. So when we do No, they want to Louis. Then all we will implement it. I'm not writing the code. We will shortly, right? They couldn't you also. But let's and stand here So first they will make topic or two. Oh, Bliss One crypt. So now first angry puts a value up will become one and then the will assigned the value of dis index. I don't. So the key is one and the values will be so the next time we do plus top will become too and value of key to equal toe. Let me will you? So this is only implement Bush? No. How can we implement Pop? So you doctor was having a really off three. So then report nor poppies pointing to second element Remember, for Stanley inserted, we implement a top to one. So the key was one. So in the table key one was holding the value off one. He too was holding the value off B and key tree was holding the value off. See? So we will leave it on Kate from one toe. So we're still one will be holding a Who will be holding be? We're called to remove it. Oh, first maker, Self topical to kneel. So what will happen by doing this? So our earlier in this table one was having he too was having me and three was having see so no, we make and top what you call 23 So we make self topical two nil. So this is replaced Vinyl So ah g equal to nil is a girl into this key not existing at all . So we have removed it from our table. And one thing they need to do more is topical. Do Nope. Minus one. So now top is equal to two. So whatever it was second last value that was inserted office for ending to that Because the last value has been popped. So you see Oh, it's very simple. Much more simple than our victim Fluent isn't. And for returning the sage well, we will return this stop so we can simplicity and self door top. So when we have 12 in topical to to when we have one topical to one When we pop this also, we will increment by one so terrible become zero and size will also be zero Ondo in pop you goner personally handle the case that you So what is less than or equal to zero Babel Aero not stack is already empty. So let's ah quickly implement this. If you understood this part, I think implementing it should be very, very, very easy. So I have treated to files one mistake brought lower and the richest tec pistol or lower. So you can see So let's implement the stack first. So we need a function for new Whenever we are implementing any destructor of Israel was provide a function floor new energy to good give it the name new itself Return a table which talk initialized to Jiro and and so news implemented And you remember Finally we also return a table new call to new for news implemented and also added in the export list No What is the function? Push local Because local Songshan in Commander top earned cert its value Corleto well, let's it no need for him to take it and look sedative You make a pickle too. No, and then metre minted. And there to do from there tired pop here next to weaken hard site stage on one thing we did in the case off. Oh, because to implement the print function. So this is just for our debugging purpose to see whether what is the current value off stuck? So we will I heard different function Also drink this tech so we can have for equal to want to go leisure The valid witnesses don't You're great self are you looks her the space between elements and Wyndham lets air the square record 100 pop in the beginning and end and the same thing in there And they will also in the new Lanier's and let's exposed this to the plant So I think we have invented it in any other function I don't see useful So the year things letter available in a general started a structure Let's disturber implement isn't with client kuhb And here we will stuck local Starkey cool too. Request on this Isn't capital letters stuck? Let's create a local stack stack nort you so a new stack is created. Then we will put some values to it. Stagner push often Next, A few more items. 2040 for p and now let's printed. Bring the size first. So what's so different? First, let's put our guests eso it should bring done. And this I couldn t 30 for Pete and top Is this 1 40? We will also print the top and then we painted the stack it since And then let's lose him or proper since once. So what should happen here? These 30 and 40 should go away because these were the last one that were inserted. So first for TV report, then 30 will report And when you call a function recursive lee or one function inside other solar So this is tech can be seen when you have a function is and this calls if one and this F one calls have to If they call SEFKO then you see all these functions are get and they're very well search for being the stack And first, if you will complete the function that started last will complete first. Then you go back toe the calling function than if one will complete and finally f will complete. So you see the stack Do trust bacteria. So after popping Z will again do the same thing Burnside, stop and also printing this tax. Let's run it. So the guard. So meters we generally good. So mirror and off First to go. Oh, our attempt to call nil value field. So it printed hillsides. SOS. Problem is, um print Prince Turk. Ah. Okay, so there it is there to were trying to call up and it's stacked. Just lay number 11 Lane number 11. Is this so we have not exposed the top. Ah, salutes exposed the top also, although this stop and sire turning same thing, let's make it explicit. And let's not only token and now it works. Ah, Okay, so first to be inserted these four elements So size is for on the top is for Okay, So I am gonna mistake years tops would return the top element and not the top. So we will return the value for that key so that you're not sending the tough, very, really same thing. But the both songs in the North end be generally ever truffles and in stuck of it is used to get the last value that was inserted. So now it should bring 40. So it brings 40 goes for peace there at the top. You remember this bacteria. So when we put its post in the top on top, it's pointing to this new. Really? So now the opposite E. So the top is 40 years and it correctly prints that sage is full. Then we bring the stack, which is 10 2030 40 Then we pop So tropical response. So for this part than 30 become stop and traditional support and 20 we can stop so say it is too. The office 20 as expected. And I did not under this the expected, really we just carpeted. So now it Princeton 20 viz is, as expected. 14. N-Queens Assignment Solution: in this Will you be able solve the famous in queen pageant? Uh, first, let's understand the puzzle And in order to make you understand it, I will use the value of and to be for because there would be easy to understand. And this can be generalized for it queen or higher order values also. So the problem is that you're given four queens or in general in Queens, and you have it in close and this world, in this case, for cross for to see the number off queens, it simmers the number of rules and columns. So this is a square grid and the current isn't is dirt or no queen scandal in the same room . So same rule is not Lord. So you know, only one queen can like. You can see these are the two possible solutions for for for queen puzzle similarly seem column Mortal world. You will see that each column has one queen and also no villain Morton load. So if you see this cell or credits daylan disease, it's stable on This is David. This is Donald. Anything along this So Donald for R c cell number RC In general, this is our sh are a true and see it column and we're talking war this. So what are dividends off Farsi? It's R minus one C minus one ar minus two C minus two Similarly decide R minus one C plus one ar minus to C plus two. So we have 1234 or two 34 to Lebanon for 33 year or ah three tour this 21 similarly 23 and 1/4. So we will solve it using backtracking and let's see the solution. The approach that we will follow first, let's see this these two configurations, you will see that they're all rallied. You see, another two queens are in the same room. None of them are in the same column. And also, if you look at this, none of them were longer. It's diagonals. Similarly, for this, none of them are a long diagonal. And for this all to and for this hold. So this is a valid solution and this is just me remainer of this human mirror. You see, uh, three distance away from meter three distance away from meter, then one distance close to close to mirror. Here also close to middle than four. Distance for distance two and two. So these two are valid solutions. There are no other valid solutions for this working people. So how we can solve this problem, you have to print all such Confucius is not just one configuration to let's see the approach we can have. Ah, separate. Ah, good. Same as this input for storing the So listen where we can mark. It's still as cross if it's not valued or two if it's valid. But we don't need that in cross in space to store or so listen, we can do it just in Oh, in sales only because we know that in one Drew one queen will be there and it has. None of the rules can be empty because there are inroads and there are in Queens. So we just keep track off all the queens from one to food, and this is over. So listen in the court that is provided, and we have represented in the court with a So is there, which provides the solution and the value. After one willed North in Rich column, the Queen one is placed so we just keep track off the columns for each queen devalued in Norton Queens for screening. So if you look at this configuration, this area will be, Ah, first green in second column. So it will be too. Second green and fourth column. So four third green and first column. So one and fourth queen in third column. So three. So this configuration is represented way using just and aerial say's for So this is over. So listen that we will feel ultimately. So what we do here? We start with this. First we frustrate to place the first queen, we find a safe place for it. So we, um we call in Queen on first. So if you see the course Nipper dirt is provided, These are some of the utility functions for taking efforts safe to place our queen in see it column. And this is already implemented for you. This is nothing. But when you're going to place our queen, you check from one to R minus one. You don't take greater than all because you are not placed. Not yet. So you look over in the rose. If there is any queen in the same column, if there is any queen in the left or right? Never. So this is just do safety condition. And then this is the for printing the solution. So, in order do print this area in this form, you check. You go for the first room and a chick for each row. You will look for all the columns and you see even column number becomes too. It matches with this. So you place Q dear other where you place some cross or some other characters that is up to you. And no, we look at the main part that is this in Quinn function that you need to complete. And you see, here we have put one because you will start by placing First Queen and this will again call ah on second Queen the same function on if it finds a valid confusing for second. It will call itself for third green and so on. And we will stop them in crosses. This Ah, this value in. Then we will print the solution and then continue. So let's understand the soldiers and first. So we tried to place it here for screen. So we call in Queen on wouldn't your tics if it's safe to place. You see if toe place first queen in first column If it's your safe, then we will do. Yeah. One. We could do one and then we will call in Queen four. Do so we please here. Then we go to next. True, we try to place here. It's not safe. So we could. This is the parent queen. Then we take next column again. North's if so, we take next column. You did say so. We place here and call in Green on 1/3 on 1/3 again we take from here. Is it safe to please? There's no Here's North Devon here. No, it's in the same room. Here's no, it's no one of So we did not find any. So listen, when we place the second queen in third column so even back trick. So we had already trade for this and this and this also we found her. There is no solution. So we will place it here and then call in Queen on three again. So we will retake. Throw me a Is it safe? No. Is your tooth? Ah, yes. Because it's Norden Dive in all of this and this So we please here and we call in Queen on Fourth Queen. So it starts from here. Is it safe? No. Here's no same column. Here's no same Deylam. Here's no, it's the same column. So we did not find a solution for this by placing the third green here. So we go back and we trade the next column. But here we cannot place since it's Dagenham here also, we cannot place So why placing this screen here also would not find solace in. And we have trade all the columns for second Queen when the first Queen is here. So this is not possible. So we have exhaustion. Disposability first going cannot replace years, if you please. Here we don't get any solution. So we tried the next value of see this is here and we try to please the 2nd 3rd and fourth queen. So for second Queen can replace here. No. Can you please? Here's blow. Here's no here. Yes, you say it's safe. Then we try to place The third Queen can replace here. Looks like yes, because no, nothing in Devon, Nothing in this rule. Nothing in this column. Now we try to place fourth queen can replace here? No, here's no same column. Here's yes, it's safe. So now we have placed Fourth Queen and we will try to place 15. But we have cross this four, so we will print this configuration. Whatever is the value, so does the loser accordingly objected. So here it's, too. Then we placed it in the fourth, then one, then three. So we will bring this whole listen. Once we print this terrorism, we take four next solution. So we have trade all the configurations then Q is here. Second Queen is here for Queen is in the first column. So we will go back and try for her queen by placing it here first. We will try for fourth going by here. It's not possible. So we tried all the possibilities this already printed. So we try next cell. But it's not valid, so we will not print it. So we have tried all possibilities of Fort Greene. Wintergreen is here full. They go back. Police. We cannot place here here also herald four year being adjusted. All the possibilities for first green being placed here. So we go back, move forward. We already printed this solution and we place queen here Then we take four second queen here. It's possible than 30 queen. It will be here on fourth win. It will be here. So we will again printed and next to we will place the first green here and we will not find the solution. So the two solutions air to full 13 on 31 food to salutes right The court for this. So if we tried to place Fifth Queen, we have released more than and so a printed We know that we have placed all the queens other ways. So not endear Martel's Yeah this award we were to do for C equal toe one toe end do if it's safe toe place in the Queen in Scotland Number C then e off in equal to see And Gordon, please the n plus one between there is it from the sea sense here for ends here on this condition is here So the suit work Let's try to run it Andi, you see it has printed the configuration the other two configurations. So if you don't understand our try toe easy do it yourself It's not to their easy problem backtracking. Ah, the concept seems very true. Will you try all the possibilities? If you try to understand this problem, let's say you are writing a maid's on and it has to Let's a go from here to here. So what did we do? It will come here. It has one. So listen So it will come here. Here it has to absence off his block to my left and right. So lets say, takes lift. It comes here, it reaches didn. So it goes back towards no state, then tries the other possibilities. So it comes here a straight street here it has two possibilities and it picks. This one comes here. In contrast, Durden so goes back and traced the off the ropes and and finally it will find the solution . So it's just like exhaustively searching for all the possibilities. So here also we tried to place the first Queen in first our top left corner and we tried for the steps ultimately released, injured in where we were not able to place the third Queen. So we know that this often was wrong. So we went back and changed. Ah tried all the possibilities for second queen. And if we did not get the solution, we went back and try to change the police and off first clean itself. And then is he do for 2nd 3rd and fourth? So regardless so listen, we printed it, then. Whatever was the remaining configure is, um we tried for their configuration so that we get all the solutions, but the cording seems to be just to line three lines. So, you know, generally it's difficult in the beginning, if you are according ah, backtracking thing. So I just tried to understand what this represents exactly the same thing. What we saw in the explanation, and after some practice, you will be able to get that. 15. Iterators and Closures: over the next few listens. Well, a studio traitors and Gen req for In and in this listen, I will give a brief introduction off by traitors in lower. So first of all, what is a traitor? A treasury, any construction that allows you So I treat over the elements off it. Hellickson in lure. It's represented way functions. Each time we call the function, it returns next, Children from the collection and we will shortly see who, Why, this is so and ah, and I treated needs to keep track off some state with between successive cause. For example, when you run, you know, using hype here or here it returns key value and your calling it Foursome Alex in. So in one you know, first addressing, let's it prints of first element of this collection. In the next call to it, it's somehow keeping track. Not I have. Don't feel first elements, or next time it call is made, it will try to access the second element and so on. So it needs to keep a state for keeping track off charities and how to proceed from there. Soap lawyers can be very useful for the scenario. Oh, because they provide an excellent mechanism. Oh, for this kind of ask. So let's see what is closer if you don't remember it. Closure is a function that access is one or more local variables from it, including function, including environment. So we have some variables here in this school. Then there is your function, and this excesses these variables. It needs thes variable in order for this function to work. So these eso access is one or more local wherever it's from. It's including environment, and these variables keep their values across successive calls. So the closure allowing the closure to remember verities a longer travel. So and this is where it will help in attracting making iitrader function and to create a nuclear, we must also create it's known local variables. Therefore, it lawyer construction typically involves two functions. Enclosure itself, a function and a factory, the function that creates the closure. Plus it's including variables. So let's look at an example of closures. So here we're creating a trillion, a simple iitrader for a list so you can think of this D as a list. And over here, unlike I peer, which returns key and value or the index and ah value in that index. It will simply return value, not index, that Sharia separates name values. Eso You see this there is a variable called I keeps track off the index that is the state who charities and it returns function. This is the close, your function or very treated, and he receipt increments I by one and then return the value it that index in the given list. So this is how we are financially. Oh, I traded for closure. And in this example, this is the factory function sort. Remember, we said that we will need to are two things. The closure itself, which is dysfunction because it's it's, er, doing the job of why treating it's incriminating index and returning the value of that index. So this is the actual closure function, and this is bill factory function that is creating it. So this closure keeps this. It's a state in its external variables. D and I d does the collection. I denotes the current index an east time we call a traitor. It returns in next value from the list year, and after the last element, it will return nil on. We will get him that we have reached the end. Call you to turning mill and we will stop. So for example, we can have a list. Oh, in 20 30 40 So let's satar just four elements this list Ben recreate and a treated and recall it this factory function will use and we provide the collection here D and no, we will not need to call values anymore. We will be calling writer So over began travels this list using while loop so well This this is Luke is true. We will keep calling Writer. So what decided Will do it will eso This is holding a decide traitor function whose task is to incriminate Index and returned the Valuer Dart index So when we call I, it will return in because I was in this illegitimate made it one and return or the value it in Knicks one. So I returned 10 and then we can have a chick that if this well is new than record off this loop So let's right. You two never called. So first, let's define our Oh, I tried to function. So this is the factory should know We have defined over included and earned. I treated, including the factory and Will Your functions aan den. We will create our list. It's if it's milled in Kubrick and no, let's run over Chord. So you cto since 10 2030 40 50 and then nil. And after that it is this condition and you'd came out of this loop some release. We want to return inaccessible so we can a I d. Invent and we can print idea in well, so it drinks Lynn Dix as a less value. So this is so you can right a custom I treated for yourself, but instead off calling a room knighted for Onda, calling this item multiple times and adding this chick when where is Daniel and then accessing the value, it would be easier to use genic for. After all, it's designed for this kind of high treason only. So let's use the same I traitor for our genetic for, uh so well, right for well in values era who do and let's modify this pullback to return, just values. Then let's run it. So the first part ill nil comes from over method of calling originator, and the 2nd 1 is using this general for which also calls over a traitor