· 4 years ago · Feb 12, 2021, 03:56 PM
1Please note:
2
3 Read the questions carefully. Every word is important.
4
5 Feel free to add comments in your answers and read the documentation as you see fit.
6
7 Don't forget to mention the language you are using.
8
9 Do test your code!
10
11 Between parenthesis is the weight of each question, the maximum score is 20
12
13
14Test 1: Any language (3/20)Write a program that outputs sequentially the integers from 1 to 99, but on some conditions prints a string instead:
15
16 when the integer is a multiple of 3 print “Open” instead of the number,
17
18 when it is a multiple of 7 print “Source” instead of the number,
19
20 when it is a multiple of both 3 and 7 print “OpenSource” instead of the number.
21
22
23Answer 1
24So data:
25 - integers from 1 to 99
26 -costrain: multiple of 3 printing string1
27 -costrain: multiple of 7 printing string2
28 -costrain: both 3 and 7 print string 3
29Language: Javascript
30Coding And Testing environment: https://jsfiddle.net/
31So i need a checker for multiples of a number
32we have the trick of if a n/divisor is float is not a multiple, if is an int is a multiple
33
34(function numberCounter()
35{
36 for (let i=0; i<99; i++)
37 {
38 let Number = i+1;
39 if (isInt( Number/3 ) && isInt( Number/7 ) ){
40 console.log("OpenSource"); continue;
41 }
42 if (isInt( Number/3 ) ){
43 console.log("Open"); continue;
44 }
45 if ( isInt( Number/7 ) ){
46 console.log("Source"); continue;
47 }
48 console.log(Number);
49 }
50})();
51
52function isInt(n){
53 // case n==9.3 false
54 // case n==9 true
55 return parseInt(n)===n;
56}
57
58LINK: https://jsfiddle.net/vfga1h8t/
59
60LBA: 3/3
61
62Test 2: Any language (3/20)
63Solve the game "Guess a number", find a secret integer between 1 and 1000000
64in less than 50 guesses. Write a function that solves the game without user input and returns the
65solution by using the function verify() which is defined with the following
66specification:
67function verify(guess: integer) -> integer
68Argument:
69 guess (integer) the number to verify
70Returns:
71 0 if the guess is the solution, your program won
72 -1 if the solution is smaller than the guess parameter
73 1 if the solution is bigger than the guess parameter
74
75Warning: You are not allowed to call verify() more that 50 times or you lose.
76
77
78Answer 2
79So data:
80 - a function for integer between 1 and 1000000
81 - less than 50 guesses ( AH! binary search Algoritm!!)
82 - function that solves the game
83 - function verify( secretNum , testNum ) { //return range of 0 -1 1 }
84
85Language: Javascript
86Coding And Testing environment: https://jsfiddle.net/
87
88(function gameSolver(){
89 let counter = 0;
90 let leftEdge= 1 ;
91 let rightEdge = 1000000;
92 let secretNumber = getRandomIntInclusive( leftEdge , rightEdge );
93 while( counter < 50 ){
94 console.log("Attempt n.",counter)
95 let testNumber = getRandomIntInclusive( leftEdge , rightEdge );
96 switch (verify( secretNumber , testNumber ) ){
97 case 0: console.log("you win! Number to Guess was ", secretNumber); counter=50; break;
98 case 1: console.log("test is Bigger!"); rightEdge = testNumber; break;
99 case -1: console.log("test is Smaller!"); leftEdge = testNumber; break;
100 }
101 counter++;
102 }
103})()
104
105function verify( secretNumber, testNumber ){
106 if( secretNumber==testNumber ) return 0;
107 if( secretNumber>testNumber ) return -1;
108 if( secretNumber<testNumber ) return 1;
109}
110
111function getRandomIntInclusive(min, max) {
112 min = Math.ceil(min);
113 max = Math.floor(max);
114 return Math.floor(Math.random() * (max - min + 1)) + min;
115}
116
117LINK: https://jsfiddle.net/d92u16mr/
118
119LBA: 0/3, taking the guess at random between the bounds doesn't guarentee that it will take less than 50 guesses. Your function verify returns 1 when it should return -1 and vice versa.
120
121Test 3: Any language (3/20)Write a function that takes a list of strings and returns the sum of the list items that represent an integer (skipping the other items)
122Answer 3
123So data:
124- Array mixed strings and numbers
125- sum the numbers and skipthe other
126
127Language: Javascript
128Coding And Testing environment: https://jsfiddle.net/
129
130let exerciseArray = ["2","apples","oranges","54","7","bananas","78",];
131
132(
133function sumOnlyNums(array){
134let numArray = array.filter(
135 value => { return Number.isInteger( parseInt( value ) )}
136);
137console.log( "sum is" ,
138 numArray.reduce( (accumulator, currentValue) =>
139 { return parseInt(accumulator) + parseInt(currentValue)}, 0)
140);
141}
142)(exerciseArray)
143
144LINK: https://jsfiddle.net/t8vzd02e/
145
146LBA: 0/3, it doesn't return anything and it adds the integer part of non-integer numbers.
147
148Test 4: Any language (1/20)Write a recursive version of the previous function (or an iterative version if you have already done a recursive version).
149recursive ? oh my ...
150OK
151Answer 4
152So data:
153 - same of 3 but recursive ()
154Language: Javascript
155Coding And Testing environment: https://jsfiddle.net/
156
157
158(function addOrPrint( sum, index, array ){
159
160//initialisation
161if (isNaN(sum) ){
162 index = 0;
163 array = ["2","apples","oranges","54","7","bananas","78",];
164 sum = 0;
165}
166//Stop
167if( index === array.length ) {
168console.log( "recursive sum is: ", sum);
169return sum }
170
171if ( Number.isInteger( parseInt( array[index] ) ) ){
172sum+= parseInt( array[index] );
173console.log("addOrPrint( sum, index + 1, array )",sum, index + 1, array)
174 addOrPrint( sum, index + 1, array );
175}
176else{
177 addOrPrint( sum, index + 1, array );
178}
179//dOrPrint
180
181
182})(NaN) ;
183
184LINK: https://jsfiddle.net/ne89dq0k/
185
186LBA: 0/1, it doesn't return anything.
187
188Test 5: SQL (3/20)
189
190 Write pseudo-SQL statements to create database tables to store the products of a basic webshop. Each product has a name, a price, a creation date and may belong to several categories. Categories have a name and a flag to indicate whether the category is private or public.
191
192 Write a SQL query to find the list of products that belong to more than 5 public categories.
193
194
195Answer 5
196So data:
197 - we need a table of name, a price, a creation date
198 and may belong to several categories so N to N ?
199 ( there are multiple product for categories and multiple categories for a product
200 - so we need a junction table
201 - Categories have a name and a flag to indicate whether the category is private or public.
202 - products join categories on ID where visibility public Group by categories ID having categories ID Are >5
203 - sub-query??
204Language: SQL
205Coding And Testing environment: N/A
206
207 CREATE TABLE products{
208 ID INT NOT NULL AUTO INCREMENT,
209 name VARCHAR(64),
210 price FLOAT(6,2),
211 creation date CURRENT_DATE,
212 PRIVATE KEY(ID)
213 }
214 CREATE TABLE categories{
215 ID INT NOT NULL AUTO INCREMENT,
216 name VARCHAR(64),
217 visibility ENUM("PRIVATE","PUBLIC"),
218 }
219 CREATE TABLE product_category{
220 ID INT NOT NULL AUTO INCREMENT,
221 product_ID INT NOT NULL,
222 category_ID INT NOT NULL,
223 FOREIGN KEY(fk_product)
224 REFERENCES products(ID),
225 FOREIGN KEY(fk_categories)
226 REFERENCES categories(ID)
227 }
228 SELECT name from products
229 JOIN product_category ON products.ID=product_category.product_ID
230 WHERE product_category.category_ID
231 IN (
232 SELECT ID FROM categories
233 WHERE categories.visibility LIKE "PUBLIC"
234 )
235 GROUP BY product_category.category_ID
236 HAVING product_category.category_ID > 5
237 ORDER BY products.creation date
238
239LBA: 1/3, some primary/foreign keys are missing. The "HAVING" clause of the query is incorrect, it should count the categories.
240
241Test 6: Any language (1/20)Write a program to download the contents of https://www.sap.com/belgique/index.html (the SAP homepage for Belgium), and then save the contents of the page to a new local file, with all occurrences of "SAP" replaced by "Odoo".
242Seems like a task for a pretty js jQuery bookmark
243
244Answer 6
245Language: Javascript
246Coding And Testing environment: browser console in the page on the given link
247Tutorial:
248 go to https://www.sap.com/belgique/index.html
249 use that in the url bar: ( made with https://medialab.github.io/artoo/generator/ ):
250
251 javascript: (function(e){var t={eval:'"!function(){var e=\\"object\\"==typeof window&&window.window===window?window:\\"object\\"==typeof self&&self.self===self?self:\\"object\\"==typeof global&&global.global===global?global:this;function t(e,t,n){var o=new XMLHttpRequest;o.open(\\"GET\\",e),o.responseType=\\"blob\\",o.onload=function(){i(o.response,t,n)},o.onerror=function(){console.error(\\"could not download file\\")},o.send()}function n(e){var t=new XMLHttpRequest;t.open(\\"HEAD\\",e,!1);try{t.send()}catch(e){}return t.status>=200&&t.status<=299}function o(e){try{e.dispatchEvent(new MouseEvent(\\"click\\"))}catch(n){var t=document.createEvent(\\"MouseEvents\\");t.initMouseEvent(\\"click\\",!0,!0,window,0,0,0,80,20,!1,!1,!1,!1,0,null),e.dispatchEvent(t)}}var a=e.navigator&&/Macintosh/.test(navigator.userAgent)&&/AppleWebKit/.test(navigator.userAgent)&&!/Safari/.test(navigator.userAgent),i=e.saveAs||(\\"object\\"!=typeof window||window!==e?function(){}:\\"download\\"in HTMLAnchorElement.prototype&&!a?function(a,i,r){var l=e.URL||e.webkitURL,s=document.createElement(\\"a\\");i=i||a.name||\\"download\\",s.download=i,s.rel=\\"noopener\\",\\"string\\"==typeof a?(s.href=a,s.origin!==location.origin?n(s.href)?t(a,i,r):o(s,s.target=\\"_blank\\"):o(s)):(s.href=l.createObjectURL(a),setTimeout((function(){l.revokeObjectURL(s.href)}),4e4),setTimeout((function(){o(s)}),0))}:\\"msSaveOrOpenBlob\\"in navigator?function(e,a,i){if(a=a||e.name||\\"download\\",\\"string\\"==typeof e)if(n(e))t(e,a,i);else{var r=document.createElement(\\"a\\");r.href=e,r.target=\\"_blank\\",setTimeout((function(){o(r)}))}else navigator.msSaveOrOpenBlob(function(e,t){return void 0===t?t={autoBom:!1}:\\"object\\"!=typeof t&&(console.warn(\\"Deprecated: Expected third argument to be a object\\"),t={autoBom:!t}),t.autoBom&&/^\\\\s*(?:text\\\\/\\\\S*|application\\\\/xml|\\\\S*\\\\/\\\\S*\\\\+xml)\\\\s*;.*charset\\\\s*=\\\\s*utf-8/i.test(e.type)?new Blob([String.fromCharCode(65279),e],{type:e.type}):e}
252(e,i),a)}:function(n,o,i,r){if((r=r||open(\\"\\",\\"_blank\\"))&&(r.document.title=r.document.body.innerText=\\"downloading...\\"),\\"string\\"==typeof n)return t(n,o,i);var l=\\"application/octet-stream\\"===n.type,s=/constructor/i.test(e.HTMLElement)||e.safari,c=/CriOS\\\\/[\\\\d]+/.test(navigator.userAgent);if((c||l&&s||a)&&\\"undefined\\"!=typeof FileReader){var u=new FileReader;u.onloadend=function(){var e=u.result;e=c?e:e.replace(/^data:[^;]*;/,\\"data:attachment/file;\\"),r?r.location.href=e:location=e,r=null},u.readAsDataURL(n)}else{var f=e.URL||e.webkitURL,d=f.createObjectURL(n);r?r.location=d:location.href=d,r=null,setTimeout((function(){f.revokeObjectURL(d)}),4e4)}});e.saveAs=i.saveAs=i,\\"undefined\\"!=typeof module&&(module.exports=i)}(),async function(e){await e(\\"*:contains(\'SAP\'):not(:has(*)):not(\'script\')\\").each((function(){let t=e(this).text().replace(\\"SAP\\",\\"Odoo\\");e(this).text(t)}));var t=new Blob([e(\\"html\\").html()],{type:\\"text/html;charset=utf-8\\"});await saveAs(t,\\"page.html\\")}(jQuery);"'},o=!0;if("object"==typeof this.artoo&&(artoo.settings.reload||(artoo.log.verbose("artoo already exists within this page. No need to inject him again."),artoo.loadSettings(t),artoo.exec(),o=!1)),o){var n=document.getElementsByTagName("body")[0];n||(n=document.createElement("body"),document.firstChild.appendChild(n));var a=document.createElement("script");console.log("artoo.js is loading..."),a.src="//medialab.github.io/artoo/public/dist/artoo-latest.min.js",a.type="text/javascript",a.id="artoo_injected_script",a.setAttribute("settings",JSON.stringify(t)),n.appendChild(a)}}).call(this);
253
254and enjoy your page HTML :)!
255
256
257Clean Code here:
258LINK: https://jsfiddle.net/h5od2vj1/
259
260LBA: 0/1, we are asking for a program to do it, why does it need to use an external browser ?
261
262Test 7: Any language (2/20)You have a huge file named "data.bin" that does not fit in memory; code a program that deletes every 7th byte of it. truncate can be used to change its size.
263Too much to Info Gather for do it right now
264
265Test 8: Regular Expression (2/20)Write a regular expression to match strings containing both "Odoo" and "#rules" in any order.
266Answer 8
267So data:
268 - regex must catch BOTH
269 - in any order ( RIP "OR" operator :'c )
270
271is that a Lookahead?? (?=.*?(Odoo))(?=.*?(#rules))
272
273LINK: https://regex101.com/r/5UzVAE/1
274
275LBA: 2/2
276
277Test 9: Javascript (1/20)Write a function that, when called, returns an array for which each element is a letter of the alphabet, from "A" to "Z" (exactly once, in order and upper case). Your code cannot contain the character ' (quote), " (double quote) or ` (back quote)
278
279that is conceptually simple, we can get chars by ASCII or another charcoding type and filter out with /[A-Z]/ regex hehehehhe
280
281Answer 9
282
283Language: Javascript
284Coding And Testing environment: jsfiddle
285
286//js encoding reference : UTF-16 code units.
287// The range is between 0 and 65535 .
288for (let i=0; i<65536;i++ ) {
289let char = String.fromCharCode(i).match(/[A-Z]/);
290char != null ? console.log( char[0] ) : null;
291}
292LINK: https://jsfiddle.net/2xmj4b31/
293
294LBA: 0/1, that's not a function.
295
296Test 10: Unix (1/20)Write a C program that roughly measures the overhead of a mode switch between the kernel and userspace on a UNIX/Linux system (without using the pthread API). Please indicate the assumptions your rough estimation is based on.
297
298NOPE out of my competence :'(