Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.74 KB | None | 0 0
  1. /**
  2. * Fill the below function
  3. * You'll be scored on your function's character count of the line(s) between the brackets
  4. * Bragging rights for lowest run time of the line(s) between the brackets
  5. * You cannot use any existing library that solves the heart of the challenge
  6. * You cannot read from your own source code
  7. * All code must appear between the braces of your main function
  8. * Emitting errors/warnings not permitted
  9. * You cannot alter the function signature in any productive way
  10. * There may be hidden tests to be provided at scoring time, so check your assumptions
  11. * You may submit more than one function
  12. */
  13. /**
  14. * Meta-Primes
  15. *
  16. * While 25 is not prime, the sum of the digits of 25 is 7, which is prime. We'll call this "meta-prime".
  17. * Find the sum of meta-prime numbers less than N.
  18. *
  19. * The list of meta-primes under 30 is
  20. * 2,3,5,7,11,12,14,16,20,21,23,25,29
  21. * The sum of meta-primes below 30 is
  22. * 2+3+5+7+11+12+14+16+20+21+23+25+29 = 188
  23. *
  24. * Given N, return the sum of meta-primes less than N
  25. *
  26. * 1 is not considered a prime number
  27. *
  28. * @param int N
  29. * @return int[]
  30. */
  31. let Solutions = {
  32. codegolf103_fake: function f(N) {
  33. return expect;
  34. },
  35.  
  36. _codegolf103_tyler1a: function f(N) {
  37. // Write a prime checker
  38. P=n=>{if(n==1)return 0;for(i=2;i<n;i++)if(n%i<1)return 0;return n}
  39. // Sum the meta-primes
  40. for(r=0;--N;)r+=P([...''+N].reduce((a,b)=>+a+ +b,0))?N:0
  41. return r
  42. },
  43. // print(`${N} -> ${s}`)
  44. xcodegolf103_tyler1b: function f(N) {
  45. P=n=>{for(i=2;i<n;i++)if(n%i<1)return 0;return n}
  46. for(r=0;--N;)r+=P([...''+N].reduce((a,b)=>+a+ +b,0))>1?N:0
  47. return r
  48. },
  49.  
  50. xcodegolf103_tyler1c: function f(N) {
  51. P=n=>{for(i=2;i<n;i++)if(n%i<1)return 0;return n}
  52. for(r=0;--N;){for(s=0,n=N;n;n=Math.floor(n/10))s+=n%10;r+=P(s)>1?N:0}
  53. return r
  54. },
  55. xcodegolf103_tyler1c2: function f(N) {
  56. P=n=>[2,3,5,7,11,13,17,19,23,29,31,37,41,43].includes(n)?2:1
  57. for(r=0;--N;){for(s=0,n=N;n;n=Math.floor(n/10))s+=n%10;r+=P(s)>1?N:0}
  58. return r
  59. },
  60.  
  61. xcodegolf103_tyler1d: function f(N) {
  62. for(r=0;--N;r+=s>1?N:0)
  63. for(s=[...''+N].reduce((a,b)=>+a+ +b),i=1;++i<s;)if(s%i<1)s=1
  64. return r
  65. },
  66. codegolf103_tyler1e: function f(N) {
  67. for(r=0;--N;r+=s>1?N:0)for(s=[...''+N].reduce((a,b)=>+a+ +b),i=1;++i<s;)s=s%i?s:1
  68. return r
  69. },
  70.  
  71. codegolf103_tyler1g: function f(N) {
  72. for(r=0;--N;r+=s>1?N:0)for(s=[...''+N].reduce((a,b)=>a- -b),i=1;++i<s;)s=s%i?s:1
  73. return r
  74. },
  75. codegolf103_tyler1g_map_coop: function f(N) {
  76. for(r=0;--N;r+=s>1?N:0)for(s=0,[...''+N].map(b=>s-=-b),i=1;++i<s;)s=s%i?s:1
  77. return r
  78. },
  79. codegolf103_tyler1g_coop: function f(N) {
  80. for(r=0;--N;r+=s>1?N:0)for(s=eval([...N+''].join`+`),i=1;++i<s;)s=s%i?s:1
  81. return r
  82. },
  83.  
  84. codegolf103_tyler1f: function f(N) {
  85. for(s=[...''+--N].reduce((a,b)=>+a+ +b),i=1;++i<s;)s=s%i?s:1
  86. return(s>1?N:0)+(N?f(N):0)
  87. },
  88. codegolf103_tyler1f_coop: function f(N) {
  89. for(s=eval([...--N+''].join`+`),i=1;++i<s;)s=s%i?s:1
  90. return(s>1?N:0)+(N?f(N):0)
  91. },
  92. codegolf103_karl: function f(N) {
  93. for(t=0;N--;){r=[...N+''].reduce((a,b)=>+a+ +b)
  94. p=1
  95. for(x=2;x<r;)if(!(r%x++))p=0
  96. t+=p&r>1?N:0}return t;
  97. },
  98.  
  99.  
  100. codegolf103_andrew: function f(N) {
  101. P=(n,i=2)=>n==i?1:n%i&&n>1?P(n,i+1):0
  102. for(i=2,t=0;i<N;i++) {
  103. c=(i+'').split``.reduce((a,b)=>+a + +b)
  104. t+=v=P(c)?i:0
  105. }
  106.  
  107. return t;
  108. },
  109.  
  110. codegolf103_andrew2: function f(N) {
  111. P=(n,i=2)=>n==i?1:n%i&&n>1?P(n,i+1):0
  112. for(i=2,t=0;i<N;i++)
  113. t+=v=P(c=(i+'').split``.reduce((a,b)=>+a + +b))?i:0
  114. return t;
  115. },
  116.  
  117. codegolf103_andrew3: function f(N) {
  118. P=(n,i=2)=>n==i?1:n%i&&n>1?P(n,i+1):0
  119. for(i=2,t=0;i<N;i++)t+=P(c=(i+'').split``.reduce((a,b)=>+a+ +b))?i:0
  120. return t;
  121. },
  122.  
  123. codegolf103_andrew4: function f(N) {
  124. P=(n,i=2)=>n==i?1:n%i&&n>1?P(n,i+1):0
  125. for(t=0;N--;)
  126. t+=P(c=(N+'').split``.reduce((a,b)=>+a+ +b))?N:0
  127. return t
  128. },
  129.  
  130. codegolf103_andrew5: function f(N) {
  131. P=(n,i=2)=>n==i?1:n%i&&n>1?P(n,i+1):0
  132. for(t=0;N--;)
  133. t+=P(c=[...N+''].reduce((a,b)=>+a+ +b))?N:0
  134. return t
  135. },
  136.  
  137. codegolf103_andrew6: function f(N) {
  138. P=(n,i=2)=>n==i?1:n%i&&n>1?P(n,i+1):0
  139. for(t=0;N--;)t+=P(c=[...N+''].reduce((a,b)=>+a+ +b))?N:0
  140. return t
  141. },
  142.  
  143. codegolf103_andrew7: function f(N) {
  144. P=(n,i=2)=>n==i?1:n%i&&n>1?P(n,i+1):0
  145. for(t=0;N--;)t+=P(eval([...N+''].join`+`))?N:0
  146. return t
  147. },
  148.  
  149. codegolf103_andrew8: function f(N) {
  150. for(t=0;N--;t+=(P=(n,i=2)=>n==i?1:n%i&&n>1?P(n,i+1):0)(eval([...N+''].join`+`))?N:0);return t
  151. },
  152.  
  153. codegolf103_tw3: function f(N) {
  154. r=[]
  155. p=function(n){if(n==(0|1))return 0
  156. for(var i=2;i<n;i++){if(n%i==0)return 0}return 1}
  157. for(i=2;i<N;i++){v=eval(i.toString().replace(/(\d)(?=\d)/g,'$1+'))
  158. p(v)?r.push(i):0}
  159. return r.reduce((a,b)=>a+b,0)
  160. },
  161. codegolf103_tw4: function f(N) {
  162. r=[]
  163. l=[2,3,5,7,11,13,17,19,23,29,31,37,41,43]
  164. for(i=2;i<N;i++){v=eval(i.toString().replace(/(\d)(?=\d)/g,'$1+'))
  165. l.includes(v)?r.push(i):0}
  166. return r.reduce((a,b)=>a+b,0)
  167. },
  168. codegolf103_mark6: function f(N) {
  169. for(r=0;--N;r+=N*(p*s>1)){s=0;[...''+N].map(x=>s-=-x);for(p=x=s;--x>1;)p*=s%x}return r
  170. },
  171. codegolf103_mark6_coop: function f(N) {
  172. for(r=0;--N;r+=N*(p*s>1)){s=eval([...N+''].join`+`);for(p=x=s;--x>1;)p*=s%x}return r
  173. },
  174. codegolf103_mark7: function f(N) {
  175. for(r=0;--N;r+=p*(s>1)?N:0){s=0;[...''+N].map(x=>s-=-x);for(x=p=2;x<s;)p*=s%x++}return r
  176. },
  177.  
  178. codegolf103_cody: function f(N) {
  179. for(a=0,i=2;i<N;i++){c=0;s=i+'';for(j=0;j<s.length;j++)c+=+s[j];p=(c==1)?1:0;for(k=2;k<c;k++)if(c%k==0)p=1;if(!p)a+=i}return a
  180. },
  181. codegolf103_adam_golfed: function f(N) {
  182. r=0
  183. for(i=10;i<N;i++){m=0
  184. v=i
  185. while(v){m+=v%10
  186. v=Math.floor(v/10)}
  187. p=!(Array(m+1).join(1).match(/^1?$|^(11+?)\1+$/))
  188. if(p==true)r=r+i}return r+17
  189. },
  190. };
  191.  
  192. let tests = [
  193. {
  194. 'input': [10],
  195. 'expect': [17],
  196. },
  197. {
  198. 'input': [30],
  199. 'expect': [188],
  200. },
  201. {
  202. 'input': [100],
  203. 'expect': [1672],
  204. },
  205. {
  206. 'input': [1000],
  207. 'expect': [162948],
  208. },
  209. {
  210. 'input': [10000],
  211. 'expect': [14244131],
  212. },
  213. {
  214. 'input': [100000],
  215. 'expect': [1276409458],
  216. },
  217. ];
  218.  
  219. let timerCount = 1, results = [], bests = [], passed, input, expect, chars, time, actual, fails, error,
  220. prefix = 'codegolf103',
  221. colors = getColors(),
  222. handicaps = {};
  223. handicaps[prefix] = {
  224. tyler: 1 - 0.247,
  225. andrew: 1 - 0.017,
  226. tw: 1 + 0.308,
  227. karl: 1 + 0.270,
  228. mark: 1 - 0.293,
  229. adam: 1 + 0.201,
  230. john: 1 + 0.255,
  231. cody: 1 + 0.067,
  232. };
  233.  
  234.  
  235. for (let func in Solutions) {
  236. if (prefix !== func.substr(0, prefix.length)) {
  237. continue;
  238. }
  239. print(func + " - ");
  240.  
  241. // Test function against each provide data set
  242. fails = [];
  243. passed = true;
  244. time = +new Date();
  245. for (let test in tests) {
  246. // Delete lingering globals between tests
  247. for (vv in aa = [...'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$']) {
  248. eval('delete ' + aa[vv])
  249. eval('delete $' + aa[vv])
  250. }
  251. input = JSON.parse(JSON.stringify(tests[test].input));
  252. expect = JSON.parse(JSON.stringify(tests[test].expect));
  253. error = false;
  254. try {
  255. actual = Solutions[func](...input);
  256. } catch (e) {
  257. actual = null;
  258. error = e.message;
  259. }
  260.  
  261. if (actual != expect) {
  262. print("\nTest " + test + " Failed\nExpected: ");
  263. print(expect);
  264. print("Actual : ");
  265. print(actual);
  266. print("Input : ");
  267. print(input);
  268. print("Error : ");
  269. print((error ? colors.FgRed + error : colors.FgGreen + 'False') + colors.Reset);
  270. passed = false;
  271. fails.push(test)
  272. }
  273. }
  274. time = +new Date() - time;
  275. time = time / 1000;
  276.  
  277. print(func + " - " + (passed ? colors.FgGreen + 'Passed' : colors.FgRed + "Failed") + colors.Reset + "\n");
  278. chars = Solutions[func].toString().split('\n');
  279. chars = chars.slice(1, chars.length - 1).map(s => s.trim()).join().length;
  280.  
  281. // Time function
  282. if (1 < timerCount) {
  283. time = +new Date();
  284. let i;
  285. for (i = 0; i < timerCount; i++) {
  286. for (test in tests) {
  287. input = JSON.parse(JSON.stringify(tests[test].input));
  288. expect = tests[test].expect;
  289. try {
  290. actual = Solutions[func](...input);
  291. } catch (e) {
  292. }
  293. }
  294. if ((+new Date() - time) > 10000) {
  295. break;
  296. }
  297. }
  298. time = +new Date() - time;
  299. time = time / 1000 * (timerCount / i);
  300. }
  301.  
  302. let id = func.match(new RegExp(prefix + '_([a-z]+)'))
  303. let score = Math.round(chars / (handicaps[prefix][id[1]] || 1));
  304. let result = {};
  305. result['func'] = func;
  306. result['passed'] = (passed ? 'Yes' : 'no');
  307. result['chars'] = chars;
  308. result['score'] = passed ? score : '';
  309. result['time * ' + timerCount] = time;
  310. result['fails'] = fails;
  311. results.push(result);
  312.  
  313. // In support of multiple submissions, track everyone's best score
  314. if (passed
  315. && !func.match(/(fake|coop|post|fail)$/)
  316. && (!bests[id[1]] || bests[id[1]] > chars)
  317. ) {
  318. bests[id[1]] = chars;
  319. }
  320. }
  321.  
  322. results.sort((a, b) => a.passed != b.passed
  323. ? (a.passed > b.passed ? 1 : -1)
  324. : a.chars != b.chars
  325. ? (a.chars - b.chars)
  326. : a.time - b.time);
  327. bests = Object.values(bests);
  328. if (bests.length > 0) {
  329. let result = {};
  330. result['func'] = ' -- Average Best -- ';
  331. result['passed'] = 'Yes';
  332. result['chars'] = (bests.reduce((a, b) => a + b) / bests.length).toFixed(2);
  333. result['score'] = '';
  334. result['time * ' + timerCount] = ' - ';
  335. result['fails'] = '';
  336. results.push(result);
  337. }
  338. print(arrayToTextTable(results));
  339.  
  340. function print(s) {
  341. console.log(s);
  342. }
  343.  
  344. /**
  345. * Render an ASCII table from supplied 2d array
  346. *
  347. * @param input Data to render
  348. * @param keyAsCol1 Whether to include first dimension array key as column one
  349. *
  350. * @return string A basic ASCII table of the data
  351. */
  352. function arrayToTextTable(input, keyAsCol1) {
  353. let data = [];
  354. let widths = {};
  355.  
  356. // Determine column widths
  357. for (let key in input) {
  358. let values = input[key];
  359. if (keyAsCol1) {
  360. // Like array_unshift, but force empty string for key
  361. values._ = key;
  362. }
  363. // The actual column width determination
  364. for (let col in values) {
  365. let datum = values[col];
  366. widths[col] = Math.max(widths[col] || 0, String(datum).length);
  367. }
  368. data.push(values);
  369. }
  370. // The above may have added a column, get column widths for headers last
  371. let headers = JSON.parse(JSON.stringify(data[data.length - 1]));
  372. for (let col in headers) {
  373. widths[col] = Math.max(widths[col] || 0, col.length);
  374. }
  375.  
  376. // Draw horizontal bars for top and bottom
  377. let bar = '';
  378. for (let i in widths) {
  379. bar += '+'.padEnd(widths[i] + 3, '-');
  380. }
  381. bar += "+\n";
  382.  
  383. // Draw column headers
  384. for (let key in headers) {
  385. headers[key] = key.padEnd(widths[key], ' ');
  386. }
  387. result = bar + '| ' + Object.values(headers).join(' | ') + " |\n" + bar;
  388.  
  389. // Draw data
  390. for (let i in data) {
  391. let row = data[i];
  392. for (let col in row) {
  393. let datum = row[col];
  394. result += '| ' + ('' + datum).padEnd(widths[col] + 1, ' ');
  395. }
  396. result += "|\n";
  397. }
  398. result += bar;
  399.  
  400. return result;
  401. }
  402.  
  403. function getColors() {
  404. let colors = {}
  405.  
  406. colors.Reset = "\x1b[0m"
  407. colors.Bright = "\x1b[1m"
  408. colors.Dim = "\x1b[2m"
  409. colors.Underscore = "\x1b[4m"
  410. colors.Blink = "\x1b[5m"
  411. colors.Reverse = "\x1b[7m"
  412. colors.Hidden = "\x1b[8m"
  413.  
  414. colors.FgBlack = "\x1b[30m"
  415. colors.FgRed = "\x1b[31m"
  416. colors.FgGreen = "\x1b[32m"
  417. colors.FgYellow = "\x1b[33m"
  418. colors.FgBlue = "\x1b[34m"
  419. colors.FgMagenta = "\x1b[35m"
  420. colors.FgCyan = "\x1b[36m"
  421. colors.FgWhite = "\x1b[37m"
  422.  
  423. colors.BgBlack = "\x1b[40m"
  424. colors.BgRed = "\x1b[41m"
  425. colors.BgGreen = "\x1b[42m"
  426. colors.BgYellow = "\x1b[43m"
  427. colors.BgBlue = "\x1b[44m"
  428. colors.BgMagenta = "\x1b[45m"
  429. colors.BgCyan = "\x1b[46m"
  430. colors.BgWhite = "\x1b[47m"
  431.  
  432. return colors
  433. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement