Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Find Matches</title>
- <style type="text/css">
- table, td { border: 1px solid black; }
- td { padding: 2px 4px; text-align: center; }
- </style>
- <script type="text/javascript">
- // raw input arrays apply to both solutions
- var nums1 = [
- 170, 997, 678, 416, 260, 7, 658, 239, 63, 802,
- 419, 442, 879, 154, 281, 759, 439, 855, 338, 944,
- 656, 635, 846, 818, 985, 625, 615, 179, 892, 241,
- 8, 75, 196, 52, 318, 132, 466, 824, 923, 225, 22,
- 213, 469, 674, 354, 577, 223, 69, 423, 64, 241,
- 766, 948, 343, 369, 60, 762, 322, 148, 401, 19,
- 266, 993, 313, 139, 740, 388, 341, 802, 224, 270,
- 261, 114, 508, 840, 731, 336, 160, 718, 195, 201,
- 927, 501, 60, 494, 692, 127, 512, 705, 840, 588,
- 408, 703, 98, 823, 44, 346, 288, 482, 101
- ];
- var nums2 = [
- 509, 357, 679, 451, 372, 245, 747, 813, 75, 746,
- 341, 752, 796, 485, 390, 237, 995, 951, 685, 356,
- 891, 483, 906, 896, 196, 358, 27, 394, 104, 560,
- 987, 173, 779, 750, 752, 715, 895, 955, 241, 267,
- 504, 748, 241, 832, 105, 825, 590, 890, 232, 918,
- 677, 814, 383, 368, 907, 264, 779, 821, 378, 208,
- 566, 120, 416, 782, 207, 274, 530, 547, 927, 161,
- 214, 252, 35, 356, 164, 731, 173, 804, 173, 741,
- 942, 892, 680, 926, 850, 399, 403, 789, 178, 699,
- 566, 841, 136, 749, 546, 746, 237, 661, 182, 350
- ];
- // SOLUTION 1 ELEMENTS START HERE - - - -
- // utility
- function get( eid ) {return document.getElementById( eid ); };
- // get unique elements of array a
- function kernel( a ) {
- // Set hashtable style associative array working storage.
- var r = new Array();
- // For each element in a...
- for (var i = 0; i < a.length; i++) {
- // ...assign key/value pair - repeated values have same key and
- // replace one another, leaving only single instance of value.
- r[a[i].toString()] = a[i];
- }
- // Initialize return.
- var k = new Array();
- // For each key in r...
- for (var el in r) {
- // ...set value into index-oriented array.
- k.push(r[el]);
- }
- return k;
- };
- // find elements in a1 that are also in a2
- function findMatches( a1, a2 ) {
- // index counter for a1
- var i1 = 0;
- // array to collect matches
- var m = [];
- // while index for a1 is in bounds and there are any unprocessed
- // elements of a2 remaining
- while( i1 < a1.length && 0 < a2.length ) {
- // if current a1 matches first a2, add to collector and
- // shorten a2 by chopping its first element
- if ( a1[i1] == a2[0] ) {
- m.push( a1[i1] );
- a2.shift();
- i1++; // increment to next index for a1
- }
- // else, if current a1 greater than first a2, shorten a2 by
- // chopping its first element
- else if ( a1[i1] > a2[0] ) a2.shift();
- // NOTE:
- // whether match found or first a2 smaller than current a1,
- // first a2 needs never be considered again - could have left
- // it for case that it was matched without considering whether
- // smaller, but an extra iteration iteration of the {while}
- // would be required to remove the unneeded a2 element
- else i1++; // increment to next index for a1
- }
- return m;
- };
- // SOLUTION 1 ELEMENTS END HERE - - - -
- // once the page's HTML elements have resolved...
- window.onload = function() {
- // clean up the arrays by sorting them ascending and removing
- // any dulicate values
- var n1 = kernel( nums1.sort() );
- var n2 = kernel( nums2.sort() );
- /*
- // display the cleaned up versions of the input arrays (optional)
- get( 'n1' ).innerHTML = 'The ' + (nums1.length) + ' raw, unsorted ' +
- 'elements of array 1 give rise to the following ' + (n1.length) +
- ' unique, sorted elements :<br> ' +
- n1.join(', ');
- get( 'n2' ).innerHTML = 'The ' + (nums2.length) + ' raw, unsorted ' +
- 'elements of array 2 give rise to the following ' + (n2.length) +
- ' unique, sorted elements:<br> ' +
- n2.join(', ');
- */
- // get the matches and display in page
- get( 'out' ).innerHTML = 'Numbers in both: ' +
- findMatches( n1, n2 ).join(', ');
- displayTable(); // different visualization
- };
- // SOLUTION 2 ELEMENTS START HERE - - - -
- // offer a tabular display with a color key to indicate the source(s)
- // of the values
- function displayTable() {
- // get background color for table cell: green, if in first array
- // only - blue, if in second only - red, if in both
- function getBgColor( idx ) {
- var c = 'white';
- if (isIn( all[ idx ], n1)) {
- if (isIn( all[ idx ], n2)) c = '#FAA';
- else c = '#AFA';
- }
- else c = '#AAF';
- return c;
- };
- // return true if item is in set
- function isIn( item, set ) {
- var is = false;
- for (var i = 0; i < set.length; i++) {
- is = set[i] == item;
- if (is) break; // quit for first hit
- }
- return is;
- };
- var n1 = kernel( nums1.sort() );
- var n2 = kernel( nums2.sort() );
- var all = kernel( (n1.concat( n2 )).sort() );
- var last = all.length;
- var colCnt = 10; // arbitrary choice
- var lastRow = Math.ceil( last/colCnt );
- var tbl = '<table>';
- for (var rows = 0; rows < lastRow; rows++) {
- tbl += '<tr>';
- for (var cols = 0; cols < colCnt; cols++) {
- var idx = rows*colCnt + cols;
- if (idx < last) {
- var s = ' style="background-color:' +
- getBgColor( idx );
- tbl += '<td ' + s + '">' + all[ idx ] + '</td>';
- }
- else tbl += '<td >---</td>'; // pad end of table
- }
- tbl += '</tr>';
- }
- tbl += '</table>';
- get( 'tbl' ).innerHTML = tbl;
- };
- // SOLUTION 2 ELEMENTS END HERE - - - -
- </script>
- </head>
- <body>
- <p id="n1"></p>
- <p id="n2"></p>
- <p id="out"></p>
- <p id="tbl"></p>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement