Logic for Programmers - Set Theory

Logic Programming and Set Theory: Understanding Ordered Pairs and High Score Lists

In programming, ordered pairs are used to pair elements from two different sets together. When we take an element from one set and match it with an element from another set, all possible ordered pairs are created. For example, if we have a set of values {a, b, c} and a set of values {x, y, z}, then the possible ordered pairs would be ax, ay, az, bx, by, bz, cx, cy, dx, dy, dz.

Most programming languages have predefined functions that help us find values within a set. These functions are essential in various applications, including data analysis and processing. For instance, if we want to find the biggest value in a set, we would look for the max function. Similarly, if we want to find the smallest value in a set, we would use the min function. Programming languages also have predefined functions that help us search for specific elements or values within a set.

For example, suppose we are making a game and when the game ends, we need to display the high score list. We can create a logic to check if the current player's total score is greater than the minimum score on the high score list. If it is, then we add that score to the high score list. After adding the score to the list, we display the top 10 scores for all players to see.

Logic Programming and Set Theory in PHP

----------------------------------------

In PHP, we can create a logic to check if the current player's total score is greater than the minimum score on the high score list. We start by defining our variables: $totalScore, $highScores, and $min. The $totalScore variable equals the function that calculates the current player's total score. The $highScores variable equals another function that fetches the 10 high scores from the database.

We use the min function in PHP to find the minimum value in the $highScores array. If the $totalScore is greater than the $min, then we execute the code to add the $totalScore to the $highScores array. No matter what, we display the high score list for all players to see.

Logic Programming and Set Theory in JavaScript

-----------------------------------------------

In JavaScript, we can create a similar logic to check if the current player's total score is greater than the minimum score on the high score list. We start by defining our variables: $totalScore, $highScores, and $min. The $totalScore variable equals the function that calculates the current player's total score. The $highScores variable equals another function that fetches the 10 high scores from the database.

We use a similar approach to PHP, but we use the min function in JavaScript instead of PHP's built-in min function. We also make sure to check if the $totalScore is greater than the $min before adding it to the $highScores array. No matter what, we display the high score list for all players to see.

Practice and Homework

-------------------

To improve our understanding of logic programming and set theory, we need to practice and apply these concepts. We can do this by working on word problems or exercises that involve ordered pairs, sets, and logic operations. The last video in the series had a little quiz with three word problems for homework.

If you have any questions or need help with the homework, please feel free to comment on this video. I will be checking the comments and trying to respond to as many questions as possible. If you complete the homework and want me to look it over, you can upload it to GitLab, GitHub, or wherever you prefer.

Please tweet the link to this video and mention me and Free Code Camp, so I can see that you watched the video and are learning from it. If you don't hear back from me within a couple of days after completing the homework, feel free to give me a little nudge in the next video of the intro to logic for computer programmers series.

Conclusion

----------

Logic programming and set theory are essential concepts in computer science that help us process and analyze data efficiently. By understanding ordered pairs, sets, and logic operations, we can create more efficient algorithms and programs that solve real-world problems. In this article, we explored how to use logical operations to check if a current player's total score is greater than the minimum score on the high score list in PHP and JavaScript.

By practicing and applying these concepts, we can improve our problem-solving skills and become better programmers. Thank you for watching my video, and I hope you found it helpful. Happy coding!

"WEBVTTKind: captionsLanguage: enwelcome to free code camp i'm commander candy of coding commanders and today we're going to learn all about set theory and how we can use it to write some really awesome code you're watching the second video in our intro to logic series if you haven't seen the first video yet intro to propositional logic you might want to check that out introduction to logic for programmers video 2 set theory you'll find homework and documentation at codingcommanders.com logic.php as programmers and aspiring programmers the first question you probably have is how is that theory going to help me with programming generally speaking web applications tend to include a database server code and client-side code to illustrate the language relationship i will use an analogy for my video on front-end versus backend web development in the very front you'll find html and css displaying data to the client aka the user html is kind of like display shelves at a shop and css is kind of like the person who designs the shelves in the very back we have a database this is where we store data kind of like the shop's warehouse popular database management systems include mysql mongodb and postgres database design is arguably the most important part of your application's design oftentimes it's the root of your application and poor database management or implementation can slow down the entire development process and it can even slow down your entire application standing right in front of our database we have alligator server-side code woman she's kind of like the store manager she can communicate with the warehouse workers also known as the database management system she can also perform calculations manipulations and has security clearance to deal with sensitive information she's often in charge of the application's logic and infrastructure however she isn't the best at dealing with customers so at the front of the store we have our customer service manager javascript javascript works on the client side it can interact with the html and css it can go in the front of the house and talk to the designers and the clients to make sure our display shelves are displayed in a user-friendly manner she can also perform calculations and manipulations on non-secure data that is passed to her from alligator server-side code woman set theory is the foundation of database design a big part of server-side code and application design is interacting with the database so if you become a logic rockstar it's going to be a whole lot easier for you to design solid applications as a matter of fact in computer programming we have data types called arrays and objects these data types are used for storing sets of data these data sets are often directly defined by our database tables so next you might be wondering what is a set a set is a collection of objects or elements say you're designing a login page you'll deal with sets of user data each user object may be a set of data including username email and password users might be a set of user objects in mathematics we say x is an object and s is a set if the above statement's true then x can either belong to s or x cannot belong to s if you remember back to my intro to propositional logic video a proposition is a statement that can either be true or false the statement x is an element of s can be true x or false it could be true or it could be false but it can't be both in mathematics if it's true we say x is an element of s and it's denoted just like you see and if x is not a member of s we denote it like this let's say set s is equal to all possible even numbers you tell me are the following statements true or false 2 is an element of f yes that is true 2 is an even number 3 is an element of s nope 3 is odd it's not even 7 is not an element of s that's true 7 is not an element of s because 7 is odd how do you define a set there's two ways to define a set the first way is by extension and we do that like so by putting values into curly brackets we have s equals 2 4 6 and then we have x equals an empty set there's no values in there and y equals basically all even numbers starting at two what that dot dot dot means is that the pattern's going to continue so y is going to equal 2 4 6 8 10 12 and so on the second way we can define a set is by intention that's definition by membership conditions so we could say x is a member if x is an even number or in the second example x is a member such that x is an even number and it's also greater than zero s is going to equal the set of all real counting numbers so tell me are these statements true or false one is an element of s true 1000 is an element of s 1000 is a real counting number that is true negative 1 000 is not an element of s that is true as well x such that x equals three x plus negative three equals zero true because three plus negative three equals zero x is less than zero er false if x equals three then x must be greater than zero so this is false x is greater than two true because three yes indeed is greater than two sets are equal if they contain the exact same elements and order doesn't matter so if s equals one two and three and t equals two three and one s and t are still equal sets and if sets are equal remember by conditionals from intro to propositional logic x is an element of s if and only if x is an element of t subsets if all of a's elements are contained in b then we say a is a subset of b if a is a subset of b and x is an element contained in a then x has to also be an element contained in b null set that means it's completely empty there's no values in it and we write it like that union okay look at that diagram with a and b a union b x such that x is an element of a or x is an element of b if you look in the diagram everything in a and everything in b is going to be included in a union b x belongs to a or it could belong to b either one intersection intersections going to be this area i tried to make it kind of purple combining the blue and the pink x such that x is an element of a and x is an element of b a minus b x such that x is an element of a and x is not an element of b okay so if you look here this white area that's not shaded that would be a minus b that's the elements contained in a that a and b cartesian products okay just relax for a second and sit back and just get the concept the cartesian product is the product of two sets say a is a set which contains the values a b and c and b is a set that contains the values x and y we would write the cartesian product as a times b and it's going to equal all possible ordered pairs ordered pairs are when we take an element in a and we match it with an element in b so all possible ordered pairs are going to be ax a y bx b y c x and c y most programming languages also have some predefined functions that are going to help us find values within a set because of that i'm going to spare you all the mathematical equations to calculate these things most of them i think we know what they mean like we might have a set of values and we want to find out what's the biggest value in there that would be finding the max value of the set most programming languages have a function for that if we want to find the smallest value in a set we'll look for the minimum most programming languages have a function for that for search we we could want to search for all kinds of things and programming languages do tend to have different predefined functions where you might be searching for a particular string um say you want to return any members of the set that contain img you're looking for image content or maybe you're looking for specific value maybe you are looking for the blog article that was posted on a specific date so you're searching through the dates for the one that matches most programming languages have predefined functions to handle all this kind of stuff so let's do an example okay you're making a game when the game ends the high score list is displayed there are 10 spots on the high score list write the code okay so first let's do our logic thing let's let t equal total score h equal high scores and s equals show high scores just in case the total score qualifies we're going to have a function written somewhere to add that score to the high score list we want the code add score to execute if and only if total score is an element on the high score list and total score is going to be included as a member on the high score list if and only if the total score is greater than the minimum score on the high score list the smallest value there and regardless we are going to display the high score list somewhere we're going to have a function with code to display the top 10 scores and if your total score qualifies you will see your name and score there so let's write it in php we have dollar sign total score equal to a function that we have written out elsewhere which calculates the current player's total score dollar sign high scores is going to equal another function that we have someplace else which fetches i probably have a query there to go ahead go to the database get those 10 high scores and we'll store it in dollar sign high scores dollar sign min in php we do have a predefined function to find the minimum value in a set so dollar sign min is going to equal the minimum of dollar sign high scores if total score is greater than the minimum then we're going to execute the code to add the total score to the high score list and no matter what in any case we will display the high score list for everybody to see if you made it as a member on the high score list you will see your name there if you did not you will still see the chosen ones who did make it on the high score list now let's check it out in javascript we have var total score equaling that function to calculate the current total score we have our high scores equaling that function to go the database and get the high scores we have var min equaling in javascript we also have a predefined function to find the minimum value in a set and if total score is greater than that minimum we're going to execute the code to add that total score to our high score list and no matter what in any case we're gonna go ahead and we're gonna display the high score list for the player to see thank you for watching my video don't forget to go to codingcommanders.comlogic.php with logic programming math the only way you get better is by practice you watch the video you learn the concept then you have to apply it so please check out the homework the last video i had a little quiz for homework this time i have three word problems if you have any questions please feel free comment on this video i will be checking the comments if you complete the homework and you feel like you want me to look it over you can go ahead upload it to gitlab github wherever you want tweet the link make sure to mention me and free code camp i will try to get to everybody if you do reach out and it's been like a couple days you haven't heard from me feel free to give me a little nudge in the next video of the intro to logic for computer programmer free code camp series i am going to apply all the set theory we learned today test some actual sequel to learn how to make awesome databases using set theory thank you again for watching my video and until next time happy codingwelcome to free code camp i'm commander candy of coding commanders and today we're going to learn all about set theory and how we can use it to write some really awesome code you're watching the second video in our intro to logic series if you haven't seen the first video yet intro to propositional logic you might want to check that out introduction to logic for programmers video 2 set theory you'll find homework and documentation at codingcommanders.com logic.php as programmers and aspiring programmers the first question you probably have is how is that theory going to help me with programming generally speaking web applications tend to include a database server code and client-side code to illustrate the language relationship i will use an analogy for my video on front-end versus backend web development in the very front you'll find html and css displaying data to the client aka the user html is kind of like display shelves at a shop and css is kind of like the person who designs the shelves in the very back we have a database this is where we store data kind of like the shop's warehouse popular database management systems include mysql mongodb and postgres database design is arguably the most important part of your application's design oftentimes it's the root of your application and poor database management or implementation can slow down the entire development process and it can even slow down your entire application standing right in front of our database we have alligator server-side code woman she's kind of like the store manager she can communicate with the warehouse workers also known as the database management system she can also perform calculations manipulations and has security clearance to deal with sensitive information she's often in charge of the application's logic and infrastructure however she isn't the best at dealing with customers so at the front of the store we have our customer service manager javascript javascript works on the client side it can interact with the html and css it can go in the front of the house and talk to the designers and the clients to make sure our display shelves are displayed in a user-friendly manner she can also perform calculations and manipulations on non-secure data that is passed to her from alligator server-side code woman set theory is the foundation of database design a big part of server-side code and application design is interacting with the database so if you become a logic rockstar it's going to be a whole lot easier for you to design solid applications as a matter of fact in computer programming we have data types called arrays and objects these data types are used for storing sets of data these data sets are often directly defined by our database tables so next you might be wondering what is a set a set is a collection of objects or elements say you're designing a login page you'll deal with sets of user data each user object may be a set of data including username email and password users might be a set of user objects in mathematics we say x is an object and s is a set if the above statement's true then x can either belong to s or x cannot belong to s if you remember back to my intro to propositional logic video a proposition is a statement that can either be true or false the statement x is an element of s can be true x or false it could be true or it could be false but it can't be both in mathematics if it's true we say x is an element of s and it's denoted just like you see and if x is not a member of s we denote it like this let's say set s is equal to all possible even numbers you tell me are the following statements true or false 2 is an element of f yes that is true 2 is an even number 3 is an element of s nope 3 is odd it's not even 7 is not an element of s that's true 7 is not an element of s because 7 is odd how do you define a set there's two ways to define a set the first way is by extension and we do that like so by putting values into curly brackets we have s equals 2 4 6 and then we have x equals an empty set there's no values in there and y equals basically all even numbers starting at two what that dot dot dot means is that the pattern's going to continue so y is going to equal 2 4 6 8 10 12 and so on the second way we can define a set is by intention that's definition by membership conditions so we could say x is a member if x is an even number or in the second example x is a member such that x is an even number and it's also greater than zero s is going to equal the set of all real counting numbers so tell me are these statements true or false one is an element of s true 1000 is an element of s 1000 is a real counting number that is true negative 1 000 is not an element of s that is true as well x such that x equals three x plus negative three equals zero true because three plus negative three equals zero x is less than zero er false if x equals three then x must be greater than zero so this is false x is greater than two true because three yes indeed is greater than two sets are equal if they contain the exact same elements and order doesn't matter so if s equals one two and three and t equals two three and one s and t are still equal sets and if sets are equal remember by conditionals from intro to propositional logic x is an element of s if and only if x is an element of t subsets if all of a's elements are contained in b then we say a is a subset of b if a is a subset of b and x is an element contained in a then x has to also be an element contained in b null set that means it's completely empty there's no values in it and we write it like that union okay look at that diagram with a and b a union b x such that x is an element of a or x is an element of b if you look in the diagram everything in a and everything in b is going to be included in a union b x belongs to a or it could belong to b either one intersection intersections going to be this area i tried to make it kind of purple combining the blue and the pink x such that x is an element of a and x is an element of b a minus b x such that x is an element of a and x is not an element of b okay so if you look here this white area that's not shaded that would be a minus b that's the elements contained in a that a and b cartesian products okay just relax for a second and sit back and just get the concept the cartesian product is the product of two sets say a is a set which contains the values a b and c and b is a set that contains the values x and y we would write the cartesian product as a times b and it's going to equal all possible ordered pairs ordered pairs are when we take an element in a and we match it with an element in b so all possible ordered pairs are going to be ax a y bx b y c x and c y most programming languages also have some predefined functions that are going to help us find values within a set because of that i'm going to spare you all the mathematical equations to calculate these things most of them i think we know what they mean like we might have a set of values and we want to find out what's the biggest value in there that would be finding the max value of the set most programming languages have a function for that if we want to find the smallest value in a set we'll look for the minimum most programming languages have a function for that for search we we could want to search for all kinds of things and programming languages do tend to have different predefined functions where you might be searching for a particular string um say you want to return any members of the set that contain img you're looking for image content or maybe you're looking for specific value maybe you are looking for the blog article that was posted on a specific date so you're searching through the dates for the one that matches most programming languages have predefined functions to handle all this kind of stuff so let's do an example okay you're making a game when the game ends the high score list is displayed there are 10 spots on the high score list write the code okay so first let's do our logic thing let's let t equal total score h equal high scores and s equals show high scores just in case the total score qualifies we're going to have a function written somewhere to add that score to the high score list we want the code add score to execute if and only if total score is an element on the high score list and total score is going to be included as a member on the high score list if and only if the total score is greater than the minimum score on the high score list the smallest value there and regardless we are going to display the high score list somewhere we're going to have a function with code to display the top 10 scores and if your total score qualifies you will see your name and score there so let's write it in php we have dollar sign total score equal to a function that we have written out elsewhere which calculates the current player's total score dollar sign high scores is going to equal another function that we have someplace else which fetches i probably have a query there to go ahead go to the database get those 10 high scores and we'll store it in dollar sign high scores dollar sign min in php we do have a predefined function to find the minimum value in a set so dollar sign min is going to equal the minimum of dollar sign high scores if total score is greater than the minimum then we're going to execute the code to add the total score to the high score list and no matter what in any case we will display the high score list for everybody to see if you made it as a member on the high score list you will see your name there if you did not you will still see the chosen ones who did make it on the high score list now let's check it out in javascript we have var total score equaling that function to calculate the current total score we have our high scores equaling that function to go the database and get the high scores we have var min equaling in javascript we also have a predefined function to find the minimum value in a set and if total score is greater than that minimum we're going to execute the code to add that total score to our high score list and no matter what in any case we're gonna go ahead and we're gonna display the high score list for the player to see thank you for watching my video don't forget to go to codingcommanders.comlogic.php with logic programming math the only way you get better is by practice you watch the video you learn the concept then you have to apply it so please check out the homework the last video i had a little quiz for homework this time i have three word problems if you have any questions please feel free comment on this video i will be checking the comments if you complete the homework and you feel like you want me to look it over you can go ahead upload it to gitlab github wherever you want tweet the link make sure to mention me and free code camp i will try to get to everybody if you do reach out and it's been like a couple days you haven't heard from me feel free to give me a little nudge in the next video of the intro to logic for computer programmer free code camp series i am going to apply all the set theory we learned today test some actual sequel to learn how to make awesome databases using set theory thank you again for watching my video and until next time happy coding\n"