1+ // src/07-set/01-using-myset-class.js
2+
3+ const MySet = require ( './set' ) ;
4+
5+ const article = {
6+ title : 'The importance of data structures in programming' ,
7+ content : '...' ,
8+ tags : new MySet ( ) // using MySet to store tags
9+ } ;
10+
11+ // add tags
12+ article . tags . add ( 'programming' ) ;
13+ article . tags . add ( 'data structures' ) ;
14+ article . tags . add ( 'algorithms' ) ;
15+ article . tags . add ( 'programming' ) ;
16+
17+ console . log ( article . tags . size ) ; // 3
18+ console . log ( article . tags . has ( 'data structures' ) ) ; // true
19+ console . log ( article . tags . has ( 'algorithms' ) ) ; // true
20+ console . log ( article . tags . has ( 'programming' ) ) ; // true
21+ console . log ( article . tags . has ( 'javascript' ) ) ; // false
22+ console . log ( article . tags . values ( ) ) ; // ['programming', 'data structures', 'algorithms']
23+
24+ // remove tags
25+ article . tags . delete ( 'programming' ) ;
26+ article . tags . add ( 'JavaScript' ) ;
27+ console . log ( article . tags . values ( ) ) ; // ['data structures', 'algorithms', 'JavaScript']
28+
29+ // union example
30+ const interestsFromWebsites = new MySet ( ) ;
31+ interestsFromWebsites . addAll ( [ 'technology' , 'politics' , 'photography' ] ) ;
32+
33+ const interestsFromSocialMedia = new MySet ( ) ;
34+ interestsFromSocialMedia . addAll ( [ 'technology' , 'movies' , 'books' ] ) ;
35+
36+ const allInterests = interestsFromWebsites . union ( interestsFromSocialMedia ) ;
37+ console . log ( allInterests . values ( ) ) ; // ['technology', 'politics', 'photography', 'movies', 'books']
38+
39+ // intersection example
40+ const job1Skills = new MySet ( ) ;
41+ job1Skills . addAll ( [ 'JavaScript' , 'Angular' , 'Java' , 'SQL' ] ) ;
42+ const job2Skills = new MySet ( ) ;
43+ job2Skills . addAll ( [ 'Python' , 'Machine Learning' , 'SQL' , 'Statistics' ] ) ;
44+ const jobPostings =
45+ [ {
46+ title : 'Software Engineer' ,
47+ skills : job1Skills
48+ } ,
49+ {
50+ title : 'Data Scientist' ,
51+ skills : job2Skills
52+ } ] ;
53+
54+ const candidateSkills = new MySet ( ) ;
55+ candidateSkills . addAll ( [ 'JavaScript' , 'Angular' , 'TypeScript' , 'AWS' ] ) ;
56+ const candidate = {
57+ name : 'Loiane' ,
58+ skills : candidateSkills
59+ } ;
60+
61+ // Function to match candidate with jobs
62+ function matchCandidateWithJobs ( candidate , jobPostings ) {
63+ const matches = [ ] ;
64+ for ( const job of jobPostings ) {
65+ const matchingSkillsSet = candidate . skills . intersection ( job . skills ) ;
66+ if ( ! matchingSkillsSet . isEmpty ( ) ) {
67+ matches . push ( {
68+ title : job . title ,
69+ matchingSkills : matchingSkillsSet . values ( )
70+ } ) ;
71+ }
72+ }
73+ return matches ;
74+ }
75+
76+ // Find matching jobs for the candidate
77+ const matchingJobs = matchCandidateWithJobs ( candidate , jobPostings ) ;
78+ console . log ( matchingJobs ) ;
79+ // output: [{ title: 'Software Engineer', matchingSkills: [ 'JavaScript', 'Angular' ] }]
80+
81+ // difference example
82+ // Sets representing subscriber interests
83+ const allSubscribers = new MySet ( ) ;
84+ allSubscribers . addAll ( [ 'Aelin' , 'Rowan' , 'Xaden' , 'Poppy' , 'Violet' ] ) ;
85+ const booksInterested = new MySet ( ) ;
86+ booksInterested . addAll ( [ 'Aelin' , 'Poppy' , 'Violet' ] ) ;
87+ const alreadyPurchasedBooks = new MySet ( ) ;
88+ alreadyPurchasedBooks . addAll ( [ 'Poppy' ] ) ;
89+
90+ // Find subscribers interested in books but haven't purchased yet
91+ const targetSubscribers = booksInterested . difference ( alreadyPurchasedBooks ) ;
92+
93+ // Send targeted email
94+ targetSubscribers . values ( ) . forEach ( subscriber => {
95+ sendEmail ( subscriber , 'New books you will love!' ) ;
96+ } ) ;
97+
98+ function sendEmail ( subscriber , message ) {
99+ console . log ( `Sending email to ${ subscriber } : ${ message } ` ) ;
100+ }
101+ // Sending email to Aelin: New books you will love!
102+ // Sending email to Violet: New books you will love!
103+
104+ // subset example
105+ // Example Recipe Data
106+
107+ const chickenIngredients = new MySet ( )
108+ chickenIngredients . addAll ( [ 'chicken' , 'tomato' , 'onion' , 'garlic' , 'ginger' , 'spices' ] ) ;
109+
110+ const spaghettiIngredients = new MySet ( ) ;
111+ spaghettiIngredients . addAll ( [ 'spaghetti' , 'eggs' , 'bacon' , 'parmesan' , 'pepper' ] ) ;
112+
113+ const recipes =
114+ [ {
115+ name : 'Chicken Tikka Masala' ,
116+ ingredients : chickenIngredients
117+ } ,
118+ {
119+ name : 'Spaghetti Carbonara' ,
120+ ingredients : spaghettiIngredients
121+ } ] ;
122+
123+ // User's available ingredients
124+ const userIngredients = new MySet ( ) ;
125+ userIngredients . addAll ( [ 'chicken' , 'onion' , 'garlic' , 'ginger' ] ) ;
126+
127+ // Function to filter recipes
128+ function filterRecipes ( recipes , userIngredients ) {
129+ const filteredRecipes = [ ] ;
130+ for ( const recipe of recipes ) {
131+ if ( userIngredients . isSubsetOf ( recipe . ingredients ) ) {
132+ filteredRecipes . push ( { name : recipe . name } ) ;
133+ }
134+ }
135+ return filteredRecipes ;
136+ }
137+
138+ // Filter recipes based on user's ingredients
139+ const matchingRecipes = filterRecipes ( recipes , userIngredients ) ;
140+ console . log ( matchingRecipes ) ;
141+ // [ { name: 'Chicken Tikka Masala' } ]
142+
143+
144+
145+ // to see the output of this file use the command: node src/07-set/01-using-myset-class.js
0 commit comments