Guest User

Untitled

a guest
Jan 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. /**
  2. The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then:
  3.  
  4. If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set , is the median.
  5. If your set contains an even number of elements, the median is the average of the two middle elements of the sorted sample. In the sorted set , is the median.
  6. Given an input stream of integers, you must perform the following task for each integer:
  7.  
  8. Add the integer to a running list of integers.
  9. Find the median of the updated list (i.e., for the first element through the element).
  10. Print the list's updated median on a new line. The printed value must be a double-precision number scaled to decimal place (i.e., format).
  11. Input Format
  12.  
  13. The first line contains a single integer, , denoting the number of integers in the data stream.
  14. Each line of the subsequent lines contains an integer, , to be added to your list.
  15.  
  16. Constraints
  17.  
  18. Output Format
  19.  
  20. After each new integer is added to the list, print the list's updated median on a new line as a single double-precision number scaled to decimal place (i.e., format).
  21.  
  22. Sample Input
  23.  
  24. 6
  25. 12
  26. 4
  27. 5
  28. 3
  29. 8
  30. 7
  31. Sample Output
  32.  
  33. 12.0
  34. 8.0
  35. 5.0
  36. 4.5
  37. 5.0
  38. 6.0
  39. **/
  40.  
  41. 'use strict';
  42.  
  43. const fs = require('fs');
  44.  
  45. process.stdin.resume();
  46. process.stdin.setEncoding('utf-8');
  47.  
  48. let inputString = '';
  49. let currentLine = 0;
  50.  
  51. process.stdin.on('data', inputStdin => {
  52. inputString += inputStdin;
  53. });
  54.  
  55. process.stdin.on('end', _ => {
  56. inputString = inputString.trim().split('\n').map(str => str.trim());
  57.  
  58. main();
  59. });
  60.  
  61. function readLine() {
  62. return inputString[currentLine++];
  63. }
  64.  
  65. /*
  66. * Complete the runningMedian function below.
  67. */
  68. function runningMedian(a) {
  69. let result = '';
  70. let arr = [];
  71.  
  72. for (let i = 0; i < a.length; i++) {
  73. // insert number
  74. arr.push(a[i]);
  75. // sort arr
  76. for (let j = arr.length; j > 1; j--) {
  77. // swap
  78. if (arr[j - 2] > arr[j - 1]) {
  79. let temp = arr[j - 2];
  80. arr[j - 2] = arr[j - 1];
  81. arr[j - 1] = temp;
  82. } else {
  83. break;
  84. }
  85. }
  86. // get median
  87. if (arr.length % 2 === 0) {
  88. // if even
  89. let index = arr.length / 2;
  90. let median = (arr[index] + arr[index - 1]) / 2
  91. result += median + '\n';
  92. } else {
  93. // if odd
  94. let index = Math.ceil(arr.length / 2);
  95. result += arr[index-1] + '\n';
  96. }
  97.  
  98. }
  99.  
  100. return result;
  101. }
  102.  
  103. function main() {
  104. const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
  105.  
  106. const aCount = parseInt(readLine(), 10);
  107.  
  108. let a = [];
  109.  
  110. for (let aItr = 0; aItr < aCount; aItr++) {
  111. const aItem = parseInt(readLine(), 10);
  112. a.push(aItem);
  113. }
  114.  
  115. let result = runningMedian(a);
  116.  
  117. ws.write(result);
  118.  
  119. ws.end();
  120. }
Add Comment
Please, Sign In to add comment