Advertisement
And1

Untitled

Dec 26th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. //словарь
  2.  
  3. 'use strict';
  4.  
  5. const fs = require('fs');
  6.  
  7.  
  8. const isPunctuator = (x) =>
  9. {
  10. return x === ' ' || x === '\n' || x === '\t' || x === '\r' || x === ','
  11. || x === '.' || x === '-' || x === ',' || x === '(' || x === ')'
  12. || x === ':' || x === ';' || x === '?' || x === '!' || x === '\''
  13. || x === '"';
  14. }
  15.  
  16. const lexer1 = (path) =>
  17. {
  18. const data = fs.readFileSync(path, 'utf8');
  19. const arr = [];
  20. let wrd = false, lw;
  21.  
  22. for (let i = 0; i < data.length; i++)
  23. {
  24. if (isPunctuator(data[i]))
  25. {
  26. if (wrd)
  27. {
  28. wrd = false;
  29. arr[lw] = true;
  30. }
  31. }
  32. else
  33. {
  34. if (wrd)
  35. lw += data[i];
  36. else
  37. {
  38. wrd = true;
  39. lw = data[i];
  40. }
  41. }
  42. }
  43.  
  44. if (wrd)
  45. {
  46. wrd = false;
  47. arr[lw] = true;
  48. }
  49.  
  50. return arr;
  51. }
  52.  
  53. const lexer2 = (path, arr) =>
  54. {
  55. const data = fs.readFileSync(path, 'utf8');
  56. let wrd = false, lw, x = 1, y = 1;;
  57.  
  58. for (let i = 0; i < data.length; i++)
  59. {
  60. if (data[i] === '\n')
  61. {
  62. y++;
  63. x = 1;
  64. }
  65. else
  66. x++;
  67.  
  68.  
  69. if (isPunctuator(data[i]))
  70. {
  71. if (wrd)
  72. {
  73. wrd = false;
  74. if (!arr[lw])
  75. console.log(y + ',\t' + x + '\t' + lw);
  76. }
  77.  
  78. } else
  79. {
  80. if (wrd)
  81. lw += data[i];
  82. else
  83. {
  84. wrd = true;
  85. lw = data[i];
  86. }
  87. }
  88. }
  89.  
  90. if (wrd) {
  91. wrd = false;
  92. if (!arr[lw])
  93. console.log(y + ',\t' + x + '\t' + lw);
  94. }
  95.  
  96. return arr;
  97. }
  98.  
  99. lexer2(process.argv[3], lexer1(process.argv[2]));
  100.  
  101. //мемоизация + тесты
  102.  
  103. 'use strict'
  104.  
  105. const memFunc = (fun) => {
  106. let cache = {};
  107. return (...args) => {
  108. let n = args[0];
  109. if (n in cache){
  110. return cache[n];
  111. }
  112. else {
  113. let result = fun(n);
  114. cache[n] = result;
  115. return result;
  116. }
  117. }
  118. }
  119.  
  120. const fib = memFunc((n) => {
  121. let num;
  122. if (n >= 2){
  123. num = fib(n - 1) + fib (n - 2);
  124. }else num = n;
  125.  
  126. return num;
  127. });
  128. //console.log(fib(40))
  129.  
  130. const trib = memFunc((n) => {
  131. let num;
  132. if (n < 2){
  133. return 0;
  134. }else if (n === 2)
  135. return 1;
  136. else num = trib(n - 1) + trib(n - 2) + trib(n - 3);
  137.  
  138. return num;
  139. });
  140. console.log(trib(35));
  141.  
  142.  
  143. const factorial = memFunc((x) => {
  144. if (x == 0) {
  145. return 1;
  146. }
  147. else {
  148. return x * factorial(x - 1);
  149. }
  150. });
  151. //console.log(factorial(100));
  152.  
  153. //тесты без мем.
  154.  
  155. 'use strict'
  156.  
  157. const fib = (n) => {
  158. let num;
  159.  
  160. if (n >= 2) {
  161. num = fib(n - 1) + fib (n - 2);
  162. } else {
  163. num = n
  164. }
  165.  
  166. return num;
  167. }
  168. //console.log(fib(40));
  169.  
  170. const trib = (n) => {
  171. let num;
  172. if (n < 2){
  173. return 0;
  174. }else if (n === 2)
  175. return 1;
  176. else num = trib(n - 1) + trib(n - 2) + trib(n - 3);
  177.  
  178. return num;
  179. };
  180. //console.log(trib(35));
  181.  
  182. const factorial = (n) =>{
  183. if (n == 0) {
  184. return 1;
  185. }
  186. else {
  187. return n * factorial(n - 1);
  188. }
  189. };
  190. console.log(factorial(100))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement