Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.61 KB | None | 0 0
  1. /**
  2. * Fill the below function
  3. * You'll be scored on character count of the line(s) between the brackets
  4. * Bragging rights for the faster function
  5. * You cannot use any existing library that solves the heart of the challenge
  6. * You cannot read your own source
  7. */
  8.  
  9. /**
  10. * Run length encoding is the basis of many methods of compression
  11. * We will do a ridiculous implementation of run length encoding
  12. *
  13. * Given a string, return the compressed string
  14. * You must compress runs of the same character >= 4 characters long
  15. * The compression of a run is a *, followed by the character, followed by the count
  16. * For example, AAAA will compress to *A4
  17. * For example, 111111111111 will compress to *112
  18. * Literal * in the string should be encoded as **
  19. *
  20. *
  21. * @param string s
  22. *
  23. * @return string
  24. */
  25. let Solutions = {
  26. codegolf76_mark: function(s) {
  27. return s.replace(/([^*])\1{3,}|\*|./g,p=>p[1]?'*'+p[0]+p.length:p=='*'?'**':p)
  28. },
  29. codegolf76_mark2: function(s) {
  30. return s.replace(/([^*])\1+|\*|./g,p=>(l=p.length)>3?'*'+p[0]+l:p=='*'?'**':p)
  31. },
  32. codegolf76_mark3: function(s) {
  33. return s.replace(/\*|(.)\1{3,}|./g,p=>p[1]?'*'+p[0]+p.length:p=='*'?'**':p)
  34. },
  35. codegolf76_mark3: function(s) {
  36. return s.replace(/\*|(.)\1*/g,p=>p[3]?'*'+p[0]+p.length:p=='*'?p+p:p)
  37. },
  38. codegolf76_rick3_golfed: function(s) {
  39. r="";for(x of s.match(/([ -~\s])\1*/g)||[]){n=x.length;m=''+n;if(n>=4&&!x.match(/\*/i)){r+="@"+x[0]+m}else r+=x}return r.replace(/\*/g,'**').replace(/@/g,'*')
  40. },
  41. codegolf76_andrew: function(s) {
  42. return s.replace(/(.)\1*/g,m=>(l=m.length)&&m[0]==(x='*')?x.repeat(2*l):l>3?x+m[0]+l:m)
  43. },
  44. codegolf76_karl: function(s) {
  45. for(d=[],r='',l='',c=0;c<=s.length;c++){e=s[c];if(e==l){d[e]+=1;}else if(e!=l){if(d[l]>=4){r+='*'+l+d[l];d[l]=0}else{r+=l.repeat(d[l])}d[e]=1;}if(e=='*')r+=e;l=e;}return r
  46. },
  47. codegolf76_tyler2: function(s) {
  48. return s.replace(/(.)\1*/g,v=>'*'==v[0]?v+v:3<(c=v.length)?'*'+v[0]+c:v)
  49. },
  50. };
  51.  
  52. let tests = [
  53. {
  54. 'input': ['*'],
  55. 'expect': '**',
  56. },
  57. {
  58. 'input': ['****'],
  59. 'expect': '********',
  60. },
  61. {
  62. 'input': ['$***'],
  63. 'expect': '$******',
  64. },
  65. {
  66. 'input': [')))))*+'],
  67. 'expect': '*)5**+',
  68. },
  69. {
  70. 'input': ['OoOoOo'],
  71. 'expect': 'OoOoOo',
  72. },
  73. {
  74. 'input': ['BBBB'],
  75. 'expect': '*B4',
  76. },
  77. {
  78. 'input': ['BBB*'],
  79. 'expect': 'BBB**',
  80. },
  81. {
  82. 'input': ['aaa33bbbbb'],
  83. 'expect': 'aaa33*b5',
  84. },
  85. {
  86. 'input': ['aaa33bbbbb'],
  87. 'expect': 'aaa33*b5',
  88. },
  89. {
  90. 'input': ['111111111111'],
  91. 'expect': '*112',
  92. },
  93. {
  94. 'input': ['aaaaaaaaaaaaabbbbbbbbbbbbb'],
  95. 'expect': '*a13*b13',
  96. },
  97. {
  98. 'input': ['999***999'],
  99. 'expect': '999******999',
  100. },
  101. {
  102. 'input': ['This cannot compress'],
  103. 'expect': 'This cannot compress',
  104. },
  105. {
  106. 'input': ['This can compress the 555555555555555'],
  107. 'expect': 'This can compress the *515',
  108. },
  109. {
  110. 'input': ['OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO'],
  111. 'expect': '*O100',
  112. },
  113. {
  114. 'input': ['OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO'],
  115. 'expect': '*O111',
  116. },
  117. ];
  118.  
  119.  
  120. let timerCount = 1, results = [], passed, input, expect, chars, time, actual, fails,
  121. prefix = 'codegolf76',
  122. handicaps = {};
  123. handicaps[prefix] = {
  124. tyler: 1 - 0.170,
  125. andrew: 1 - 0.080,
  126. tw: 1 + 0.196,
  127. karl: 1 + 0.182,
  128. rick: 1 + 0.173,
  129. mark: 1 - 0.187,
  130. adam: 1 + 0.230,
  131. };
  132.  
  133.  
  134. for (let func in Solutions) {
  135. if (prefix !== func.substr(0, 10)) {
  136. continue;
  137. }
  138. print(func + " - ");
  139.  
  140. // Test function against each provide data set
  141. fails = [];
  142. passed = true;
  143. time = +new Date();
  144. for (let test in tests) {
  145. for (vv in aa = [...'abcdefghijklmnopqrstuvwxyz_$']) eval('delete ' + aa[vv])
  146. input = JSON.parse(JSON.stringify(tests[test].input));
  147. expect = JSON.parse(JSON.stringify(tests[test].expect));
  148. actual = Solutions[func](...input);
  149.  
  150. if (actual !== expect) {
  151. print("\nTest " + test + " Failed\nExpected: ");
  152. print(expect);
  153. print("Actual : ");
  154. print(actual);
  155. print("Input : ");
  156. print(input);
  157. passed = false;
  158. fails.push(test)
  159. }
  160. }
  161. time = +new Date() - time;
  162. time = time / 1000;
  163.  
  164. print(func + " - " + (passed ? 'Passed' : "Failed") + "\n");
  165. chars = Solutions[func].toString().split('\n');
  166. chars = chars.slice(1, chars.length - 1).map(s => s.trim()).join().length;
  167.  
  168. // Time function
  169. if (1 < timerCount) {
  170. time = +new Date();
  171. let i;
  172. for (i = 0; i < timerCount; i++) {
  173. for (test in tests) {
  174. input = JSON.parse(JSON.stringify(tests[test].input));
  175. expect = tests[test].expect;
  176. actual = Solutions[func](...input);
  177. }
  178. if ((+new Date() - time) > 10000) {
  179. break;
  180. }
  181. }
  182. time = +new Date() - time;
  183. time = time / 1000 * (timerCount / i);
  184. }
  185.  
  186. let id = func.match(new RegExp(prefix + '_([a-z]+)'))
  187. let score = Math.round(chars / (handicaps[prefix][id[1]] || 1));
  188. let result = {};
  189. result['func'] = func;
  190. result['passed'] = (passed ? 'Yes' : 'no');
  191. result['chars'] = chars;
  192. result['score'] = passed ? score : '';
  193. result['time * ' + timerCount] = time;
  194. result['fails'] = fails;
  195. results.push(result);
  196. }
  197.  
  198. results.sort((a, b) =>
  199. a.passed != b.passed ? (a.passed > b.passed ? 1 : -1)
  200. : a.chars != b.chars ? (a.chars - b.chars)
  201. : a.time - b.time
  202. );
  203. print(arrayToTextTable(results));
  204.  
  205. function print(s) {
  206. console.log(s);
  207. }
  208.  
  209. /**
  210. * Render an ASCII table from supplied 2d array
  211. *
  212. * @param input Data to render
  213. * @param keyAsCol1 Whether to include first dimension array key as column one
  214. *
  215. * @return string A basic ASCII table of the data
  216. */
  217. function arrayToTextTable(input, keyAsCol1) {
  218. let data = [];
  219. let widths = {};
  220.  
  221. // Determine column widths
  222. for (let key in input) {
  223. let values = input[key];
  224. if (keyAsCol1) {
  225. // Like array_unshift, but force empty string for key
  226. values._ = key;
  227. }
  228. // The actual column width determination
  229. for (let col in values) {
  230. let datum = values[col];
  231. widths[col] = Math.max(widths[col] || 0, String(datum).length);
  232. }
  233. data.push(values);
  234. }
  235. // The above may have added a column, get column widths for headers last
  236. let headers = JSON.parse(JSON.stringify(data[data.length - 1]));
  237. for (let col in headers) {
  238. widths[col] = Math.max(widths[col] || 0, col.length);
  239. }
  240.  
  241. // Draw horizontal bars for top and bottom
  242. let bar = '';
  243. for (let i in widths) {
  244. bar += '+'.padEnd(widths[i] + 3, '-');
  245. }
  246. bar += "+\n";
  247.  
  248. // Draw column headers
  249. for (let key in headers) {
  250. headers[key] = key.padEnd(widths[key], ' ');
  251. }
  252. result = bar + '| ' + Object.values(headers).join(' | ') + " |\n" + bar;
  253.  
  254. // Draw data
  255. for (let i in data) {
  256. let row = data[i];
  257. for (let col in row) {
  258. let datum = row[col];
  259. result += '| ' + ('' + datum).padEnd(widths[col] + 1, ' ');
  260. }
  261. result += "|\n";
  262. }
  263. result += bar;
  264.  
  265. return result;
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement