进入https://attachments.me/hirehack/public/computer.html这个页面(需翻墙),在绿色的耀眼的命令提示符下,你仿佛到了linux下编程,好吧,打个help开始javascript-hacker编程之旅。
vi 进入第一个程序factorial.js
读了几行注释发现此乃阶乘也。罢了。菜鸟做法阶乘无非一个for加*= 或是 一个递归可以解决。至于大数阶乘,咱们就先不考虑了。
我提交的是以下此段代码,貌似代码质量上还有扣分,得,没法像topcoder一样查看其他高手的源码,自认能力不足吧。
1 function factorial(input){ 2 return input === 0 ? 1 :input * factorial(input - 1 ); 3 }
顺便提提for代码和自己在java下写的BigInteger代码
for->*=
1 function factorial(input) { 2 // fill me in. 3 var sum = 1 ; 4 for ( var i = 2 ;i <= input;i ++ ){ 5 sum = sum * i; 6 } 7 return sum; 8 }
java->BigInteger
1 import java.math.BigInteger; 2 3 public class JieCheng { 4 public static void main(String[] args) { 5 long s = System.currentTimeMillis(); 6 System.out.println(fac( 200 )); 7 System.out.println( " 递归耗时: " + (System.currentTimeMillis() - s) + " 毫秒 " ); 8 s = System.currentTimeMillis(); 9 System.out.println(fac2( 200 )); 10 System.out.println( " for语句耗时: " + (System.currentTimeMillis() - s) + " 毫秒 " ); 11 12 String str = fac( 200 ).toString(); 13 System.out.println( " 位数: " + str.length()); 14 } 15 16 private static BigInteger fac( int n){ 17 if (n == 0 ) 18 return new BigInteger( " 1 " ); 19 BigInteger b = new BigInteger(String.valueOf(n)); 20 return b.multiply(fac(n - 1 )); 21 } 22 23 private static BigInteger fac2( int n){ 24 BigInteger s = new BigInteger( " 1 " ); 25 for ( int i = 1 ;i <= n;i ++ ){ 26 BigInteger b = new BigInteger(String.valueOf(i)); 27 s = s.multiply(b); 28 } 29 return s; 30 } 31 }
LOL 下个挑战—reverse.js即数组反转
记得javascript有专门的API让你反转数组 -- 此题so easy
1 function reverse(input){ 2 return input.reverse(); 3 }
下个挑战--strstr.js
注释中有写着wiki网址(http://clc-wiki.net/wiki/strstr),进去look一下,几秒后大脑反应出,原来如此呀。好吧。开始编写吧。
1 function strstr(needle, haystack) { 2 // Fill me in. 3 var i = haystack.indexOf(needle); 4 return i; 5 }
下个挑战—findPalindromesIn.js
是个回文,貌似在c中写过很多次,顺手。(这里说明下,其实可以优化的,不需要遍历length遍 二分之一遍足以,哎,算了,已提交了)
1 function findPalindromesIn(input) { 2 // Fill this in. 3 var output = new Array(); 4 var t,r = '' ; 5 for ( var i = 0 ;i < input.length;i ++ ){ 6 t = input[i]; 7 for ( var j = 0 ;j < input[i].length;j ++ ){ 8 r += t.substring(input[i].length - 1 - j,input[i].length - j); 9 } 10 if (t == r) 11 output.push(t); 12 r = '' ; 13 } 14 return output; 15 }
下个挑战—findPrimes.js
寻找质数,质数即只有1和本身两个因子也。好吧,一个理念是啥,就是判断x是否为质数就去循环除以2到根号x次,如果能除尽则不是质数,else就是咯。
1 function findPrimes(input) { 2 // Fill me in. 3 var primes = []; 4 for ( var i = 0 ;i < input.length;i ++ ){ 5 if (isPrime(input[i])) 6 primes.push(input[i]); 7 } 8 return primes; 9 } 10 11 function isPrime(input){ 12 if (input == 1 ) 13 return false ; 14 if (input == 2 || input == 3 ) 15 return true ; 16 for ( var i = 2 ;i <= Math.sqrt(input);i ++ ){ 17 if (input % i === 0 ) 18 return false ; 19 } 20 return true ; 21 }
下个挑战—recursiveMax.js
寻找最大数,难点哪?额,不就吓唬你说数组中含有数组,且递归的含有数组。差不多意思懂了就行了。
1 var m = 0 ; 2 function recursiveMax(input){ 3 for ( var i = 0 ;i < input.length;i ++ ){ 4 if ( typeof (input[i]) === " number " ){ 5 if (m < input[i]) 6 m = input[i]; 7 } else { // typeof(input[i])==="object" input[i] instanceof Array 8 recursiveMax(input[i]); 9 } 10 11 } 12 return m; 13 }
下个挑战—levenshteinDistance.js
不懂啊。。看注释中的wikipedia呗--http://en.wikipedia.org/wiki/Levenshtein_distance 就是从一个字符串变到另一个字符串需要用的步骤数
最精髓的就是那个俄罗斯科学家,啥啥啥来着的提出的思想,就是wiki中的那个表格。。好吧,看懂表格后,编码。
1 function levenshteinDistance(f, t){ 2 var d = new Array(t.length + 1 ); 3 for ( var z = 0 ; z < t.length + 1 ; z ++ ) { 4 d[z] = new Array(f.length + 1 ); 5 } 6 for ( var p = 0 ;p < t.length + 1 ;p ++ ) 7 d[p][ 0 ] = p; 8 for ( var k = 0 ;k < f.length + 1 ;k ++ ) 9 d[ 0 ][k] = k; 10 for ( var i = 1 ;i < t.length + 1 ;i ++ ){ 11 for ( var j = 1 ;j < f.length + 1 ;j ++ ){ 12 if (t[i - 1 ] === f[j - 1 ]) 13 d[i][j] = d[i - 1 ][j - 1 ]; 14 else { 15 var a = parseInt(d[i - 1 ][j], 10 ) + parseInt( 1 , 10 ); 16 var b = parseInt(d[i][j - 1 ], 10 ) + parseInt( 1 , 10 ); 17 var c = parseInt(d[i - 1 ][j - 1 ], 10 ) + parseInt( 1 , 10 ); 18 d[i][j] = getMin(a,b,c); 19 } 20 } 21 } 22 var h = '' ; 23 for ( var zz = 0 ;zz < t.length + 1 ;zz ++ ){ 24 for ( var pp = 0 ;pp < f.length + 1 ;pp ++ ){ 25 h = h + d[zz][pp]; 26 } 27 console.log(h); 28 h = '' ; 29 } 30 return d[t.length][f.length]; 31 } 32 33 function getMin(a,b,c){ 34 var min = a > b ? b:a; 35 min = min > c ? c:min; 36 return min; 37 }
下个挑战—filter.js
看个英文意思就是过滤啥啥啥,看完注释其实就是一个MD5码用js编写的难点。。说实话,本人就这题编不出来。所以请教高手去吧。---10分钟后->搞定了MD5编码
接下来要做什么。。。看注释,做个ACM解题咯。i believe you can.
1 function hex_md5(s) { return rstr2hex(rstr_md5(str2rstr_utf8(s))); } 2 3 function str2rstr_utf8(input) 4 { 5 var output = "" ; 6 var i = - 1 ; 7 var x, y; 8 9 while ( ++ i < input.length) 10 { 11 /* Decode utf-16 surrogate pairs */ 12 x = input.charCodeAt(i); 13 y = i + 1 < input.length ? input.charCodeAt(i + 1 ) : 0 ; 14 if ( 0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF ) 15 { 16 x = 0x10000 + ((x & 0x03FF ) << 10 ) + (y & 0x03FF ); 17 i ++ ; 18 } 19 20 /* Encode output as utf-8 */ 21 if (x <= 0x7F ) 22 output += String.fromCharCode(x); 23 else if (x <= 0x7FF ) 24 output += String.fromCharCode( 0xC0 | ((x >>> 6 ) & 0x1F ), 25 0x80 | ( x & 0x3F )); 26 else if (x <= 0xFFFF ) 27 output += String.fromCharCode( 0xE0 | ((x >>> 12 ) & 0x0F ), 28 0x80 | ((x >>> 6 ) & 0x3F ), 29 0x80 | ( x & 0x3F )); 30 else if (x <= 0x1FFFFF ) 31 output += String.fromCharCode( 0xF0 | ((x >>> 18 ) & 0x07 ), 32 0x80 | ((x >>> 12 ) & 0x3F ), 33 0x80 | ((x >>> 6 ) & 0x3F ), 34 0x80 | ( x & 0x3F )); 35 } 36 return output; 37 } 38 39 function rstr_md5(s) 40 { 41 return binl2rstr(binl_md5(rstr2binl(s), s.length * 8 )); 42 } 43 44 function rstr2hex(input) 45 { 46 try { hexcase } catch (e) { hexcase = 0 ; } 47 var hex_tab = hexcase ? " 0123456789ABCDEF " : " 0123456789abcdef " ; 48 var output = "" ; 49 var x; 50 for ( var i = 0 ; i < input.length; i ++ ) 51 { 52 x = input.charCodeAt(i); 53 output += hex_tab.charAt((x >>> 4 ) & 0x0F ) + hex_tab.charAt( x & 0x0F ); 54 } 55 return output; 56 } 57 58 function binl_md5(x, len) 59 { 60 /* append padding */ 61 x[len >> 5 ] |= 0x80 << ((len) % 32 ); 62 x[(((len + 64 ) >>> 9 ) << 4 ) + 14 ] = len; 63 64 var a = 1732584193 ; 65 var b = - 271733879 ; 66 var c = - 1732584194 ; 67 var d = 271733878 ; 68 69 for ( var i = 0 ; i < x.length; i += 16 ) 70 { 71 var olda = a; 72 var oldb = b; 73 var oldc = c; 74 var oldd = d; 75 76 a = md5_ff(a, b, c, d, x[i + 0 ], 7 , - 680876936 ); 77 d = md5_ff(d, a, b, c, x[i + 1 ], 12 , - 389564586 ); 78 c = md5_ff(c, d, a, b, x[i + 2 ], 17 , 606105819 ); 79 b = md5_ff(b, c, d, a, x[i + 3 ], 22 , - 1044525330 ); 80 a = md5_ff(a, b, c, d, x[i + 4 ], 7 , - 176418897 ); 81 d = md5_ff(d, a, b, c, x[i + 5 ], 12 , 1200080426 ); 82 c = md5_ff(c, d, a, b, x[i + 6 ], 17 , - 1473231341 ); 83 b = md5_ff(b, c, d, a, x[i + 7 ], 22 , - 45705983 ); 84 a = md5_ff(a, b, c, d, x[i + 8 ], 7 , 1770035416 ); 85 d = md5_ff(d, a, b, c, x[i + 9 ], 12 , - 1958414417 ); 86 c = md5_ff(c, d, a, b, x[i + 10 ], 17 , - 42063 ); 87 b = md5_ff(b, c, d, a, x[i + 11 ], 22 , - 1990404162 ); 88 a = md5_ff(a, b, c, d, x[i + 12 ], 7 , 1804603682 ); 89 d = md5_ff(d, a, b, c, x[i + 13 ], 12 , - 40341101 ); 90 c = md5_ff(c, d, a, b, x[i + 14 ], 17 , - 1502002290 ); 91 b = md5_ff(b, c, d, a, x[i + 15 ], 22 , 1236535329 ); 92 93 a = md5_gg(a, b, c, d, x[i + 1 ], 5 , - 165796510 ); 94 d = md5_gg(d, a, b, c, x[i + 6 ], 9 , - 1069501632 ); 95 c = md5_gg(c, d, a, b, x[i + 11 ], 14 , 643717713 ); 96 b = md5_gg(b, c, d, a, x[i + 0 ], 20 , - 373897302 ); 97 a = md5_gg(a, b, c, d, x[i + 5 ], 5 , - 701558691 ); 98 d = md5_gg(d, a, b, c, x[i + 10 ], 9 , 38016083 ); 99 c = md5_gg(c, d, a, b, x[i + 15 ], 14 , - 660478335 ); 100 b = md5_gg(b, c, d, a, x[i + 4 ], 20 , - 405537848 ); 101 a = md5_gg(a, b, c, d, x[i + 9 ], 5 , 568446438 ); 102 d = md5_gg(d, a, b, c, x[i + 14 ], 9 , - 1019803690 ); 103 c = md5_gg(c, d, a, b, x[i + 3 ], 14 , - 187363961 ); 104 b = md5_gg(b, c, d, a, x[i + 8 ], 20 , 1163531501 ); 105 a = md5_gg(a, b, c, d, x[i + 13 ], 5 , - 1444681467 ); 106 d = md5_gg(d, a, b, c, x[i + 2 ], 9 , - 51403784 ); 107 c = md5_gg(c, d, a, b, x[i + 7 ], 14 , 1735328473 ); 108 b = md5_gg(b, c, d, a, x[i + 12 ], 20 , - 1926607734 ); 109 110 a = md5_hh(a, b, c, d, x[i + 5 ], 4 , - 378558 ); 111 d = md5_hh(d, a, b, c, x[i + 8 ], 11 , - 2022574463 ); 112 c = md5_hh(c, d, a, b, x[i + 11 ], 16 , 1839030562 ); 113 b = md5_hh(b, c, d, a, x[i + 14 ], 23 , - 35309556 ); 114 a = md5_hh(a, b, c, d, x[i + 1 ], 4 , - 1530992060 ); 115 d = md5_hh(d, a, b, c, x[i + 4 ], 11 , 1272893353 ); 116 c = md5_hh(c, d, a, b, x[i + 7 ], 16 , - 155497632 ); 117 b = md5_hh(b, c, d, a, x[i + 10 ], 23 , - 1094730640 ); 118 a = md5_hh(a, b, c, d, x[i + 13 ], 4 , 681279174 ); 119 d = md5_hh(d, a, b, c, x[i + 0 ], 11 , - 358537222 ); 120 c = md5_hh(c, d, a, b, x[i + 3 ], 16 , - 722521979 ); 121 b = md5_hh(b, c, d, a, x[i + 6 ], 23 , 76029189 ); 122 a = md5_hh(a, b, c, d, x[i + 9 ], 4 , - 640364487 ); 123 d = md5_hh(d, a, b, c, x[i + 12 ], 11 , - 421815835 ); 124 c = md5_hh(c, d, a, b, x[i + 15 ], 16 , 530742520 ); 125 b = md5_hh(b, c, d, a, x[i + 2 ], 23 , - 995338651 ); 126 127 a = md5_ii(a, b, c, d, x[i + 0 ], 6 , - 198630844 ); 128 d = md5_ii(d, a, b, c, x[i + 7 ], 10 , 1126891415 ); 129 c = md5_ii(c, d, a, b, x[i + 14 ], 15 , - 1416354905 ); 130 b = md5_ii(b, c, d, a, x[i + 5 ], 21 , - 57434055 ); 131 a = md5_ii(a, b, c, d, x[i + 12 ], 6 , 1700485571 ); 132 d = md5_ii(d, a, b, c, x[i + 3 ], 10 , - 1894986606 ); 133 c = md5_ii(c, d, a, b, x[i + 10 ], 15 , - 1051523 ); 134 b = md5_ii(b, c, d, a, x[i + 1 ], 21 , - 2054922799 ); 135 a = md5_ii(a, b, c, d, x[i + 8 ], 6 , 1873313359 ); 136 d = md5_ii(d, a, b, c, x[i + 15 ], 10 , - 30611744 ); 137 c = md5_ii(c, d, a, b, x[i + 6 ], 15 , - 1560198380 ); 138 b = md5_ii(b, c, d, a, x[i + 13 ], 21 , 1309151649 ); 139 a = md5_ii(a, b, c, d, x[i + 4 ], 6 , - 145523070 ); 140 d = md5_ii(d, a, b, c, x[i + 11 ], 10 , - 1120210379 ); 141 c = md5_ii(c, d, a, b, x[i + 2 ], 15 , 718787259 ); 142 b = md5_ii(b, c, d, a, x[i + 9 ], 21 , - 343485551 ); 143 144 a = safe_add(a, olda); 145 b = safe_add(b, oldb); 146 c = safe_add(c, oldc); 147 d = safe_add(d, oldd); 148 } 149 return Array(a, b, c, d); 150 } 151 152 function binl2rstr(input) 153 { 154 var output = "" ; 155 for ( var i = 0 ; i < input.length * 32 ; i += 8 ) 156 output += String.fromCharCode((input[i >> 5 ] >>> (i % 32 )) & 0xFF ); 157 return output; 158 } 159 160 function rstr2binl(input) 161 { 162 var output = Array(input.length >> 2 ); 163 for ( var i = 0 ; i < output.length; i ++ ) 164 output[i] = 0 ; 165 for ( var i = 0 ; i < input.length * 8 ; i += 8 ) 166 output[i >> 5 ] |= (input.charCodeAt(i / 8 ) & 0xFF ) << (i % 32 ); 167 return output; 168 } 169 function md5_ff(a, b, c, d, x, s, t) 170 { 171 return md5_cmn((b & c) | (( ~ b) & d), a, b, x, s, t); 172 } 173 function md5_cmn(q, a, b, x, s, t) 174 { 175 return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b); 176 } 177 function safe_add(x, y) 178 { 179 var lsw = (x & 0xFFFF ) + (y & 0xFFFF ); 180 var msw = (x >> 16 ) + (y >> 16 ) + (lsw >> 16 ); 181 return (msw << 16 ) | (lsw & 0xFFFF ); 182 } 183 function bit_rol(num, cnt) 184 { 185 return (num << cnt) | (num >>> ( 32 - cnt)); 186 } 187 function md5_gg(a, b, c, d, x, s, t) 188 { 189 return md5_cmn((b & d) | (c & ( ~ d)), a, b, x, s, t); 190 } 191 function md5_hh(a, b, c, d, x, s, t) 192 { 193 return md5_cmn(b ^ c ^ d, a, b, x, s, t); 194 } 195 function md5_ii(a, b, c, d, x, s, t) 196 { 197 return md5_cmn(c ^ (b | ( ~ d)), a, b, x, s, t); 198 } 199 200 function filter(t,m){ 201 var tm = []; 202 for ( var i = 0 ;i < t.length;i ++ ){ 203 tm.push(hex_md5(t[i])); 204 } 205 var result = new Array(); 206 for ( var j = 0 ;j < m.length;j ++ ){ 207 for ( var k = 0 ;k < t.length;k ++ ){ 208 if (m[j] === tm[k]){ 209 result.push(t[k]); 210 break ; 211 } 212 } 213 } 214 return result; 215 }
自此全部挑战都已完成。 好吧。。我的ID是ChinaLOL(网站需要auth登录github账户)
好久没DOTA了。一直忙着编程,不过有时编程是比dota要有吸引力。废话不多说了。ZZZZzzz去了。