Advertisement
Queses

ITMO Algorithms Course: Week 3 Task 1 written on JavaScript

Mar 9th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { PassThrough } = require('stream')
  2. const fs = require('fs')
  3.  
  4. const NUMBER_BASE = 512
  5. const NUMBER_BASE_MAX = 511
  6. const NUMBER_BASE_POW = 9
  7. const MAX_NUM_LENGTH = 4
  8.  
  9. class Week3Task1 {
  10.     run () {
  11.         return EdxIo.create()
  12.             .then((io) => { this.io = io })
  13.             .then(this.buildArray.bind(this))
  14.             .then(this.digitSort.bind(this))
  15.             .then(this.writeOutput.bind(this))
  16.             .then(() => this.io.close())
  17.     }
  18.    
  19.     countSort (bufferArr, counters, digit) {
  20.         counters.fill(0)
  21.    
  22.         const itemShift = NUMBER_BASE_POW * digit
  23.         for (let i = 0; i < this.arr.length; i++) {
  24.             counters[(this.arr[i] >> itemShift) & NUMBER_BASE_MAX]++
  25.         }
  26.    
  27.         let prevCounters = counters[0]
  28.         for (let i = 1; i < NUMBER_BASE; i++) {
  29.             if (counters[i] > 0) {
  30.                 counters[i] += prevCounters
  31.                 prevCounters = counters[i]
  32.             }
  33.         }
  34.    
  35.         for (let i = this.arr.length - 1; i >= 0; i--) {
  36.             const value = this.arr[i]
  37.             bufferArr[--counters[(value >> itemShift) & NUMBER_BASE_MAX]] = value
  38.         }
  39.     }
  40.  
  41.     digitSort () {
  42.         let bufferArr = new Array(this.arr.length)
  43.         const counters = new Array(NUMBER_BASE)
  44.         for (let digit = 0; digit < MAX_NUM_LENGTH; digit++) {
  45.             this.countSort(bufferArr, counters, digit)
  46.             const arrTmp = this.arr
  47.             this.arr = bufferArr
  48.             bufferArr = arrTmp
  49.         }
  50.     }
  51.  
  52.     writeOutput () {
  53.         let tenthSum = 0
  54.         for (let i = 0; i < this.arr.length; i += 10) {
  55.             tenthSum += this.arr[i]
  56.         }
  57.  
  58.         this.io.write(tenthSum)
  59.     }
  60.    
  61.     buildArray () {
  62.         const firstLength = this.io.nextInt()
  63.         const secondLength = this.io.nextInt()
  64.  
  65.         const firstArr = Array(firstLength)
  66.         for (let i = 0; i < firstLength; i++) {
  67.             firstArr[i] = this.io.nextInt()
  68.         }
  69.  
  70.         this.arr = Array(firstLength * secondLength)
  71.         let offset = 0
  72.         let value
  73.         for (let j = 0; j < secondLength; j++) {
  74.             value = this.io.nextInt()
  75.             for (let i = 0; i < firstLength; i++) {
  76.                 this.arr[i + offset] = firstArr[i] * value
  77.             }
  78.             offset += firstLength
  79.         }
  80.     }
  81. }
  82.  
  83. // ---------------------------------- EdxIO ----------------------------------
  84.  
  85. class EdxIo {
  86.     constructor() {
  87.         this._writeStream = new PassThrough()
  88.         this._fsWriteStream = fs.createWriteStream('./output.txt')
  89.         this._writeStream.pipe(this._fsWriteStream)
  90.        
  91.         this._readBytes = Array(256)
  92.         this._readStream = fs.createReadStream('./input.txt')
  93.         this._isReadable = new Promise((resolve) => {
  94.             this._readStream.once('readable', () => resolve(this))
  95.         })
  96.  
  97.         this.BYTE_SPACE = 32
  98.         this.BYTE_RETURN = 13
  99.         this.BYTE_NEWLINE = 10
  100.     }
  101.  
  102.     write(value = '') {
  103.         this._writeStream.push(value.toString())
  104.     }
  105.    
  106.     writeLn(value = '') {
  107.         this._writeStream.push(value.toString() + '\n')
  108.     }
  109.  
  110.     nextInt() {
  111.         return parseInt(this.nextToken(), 10)
  112.     }
  113.  
  114.     nextFloat() {
  115.         return parseFloat(this.nextToken())
  116.     }
  117.  
  118.     nextToken() {
  119.         let byteBuffer = this._getNextByte()
  120.         let added = 0
  121.         while (byteBuffer !== null) {
  122.             switch (byteBuffer[0]) {
  123.                 case this.BYTE_NEWLINE:
  124.                 case this.BYTE_RETURN:
  125.                 case this.BYTE_SPACE:
  126.                     if (added > 0) {
  127.                         byteBuffer = null
  128.                         continue
  129.                     }
  130.                     break
  131.                 default:
  132.                     this._readBytes[added++] = byteBuffer[0]
  133.             }
  134.            
  135.             byteBuffer = this._getNextByte()
  136.         }
  137.  
  138.         return (added > 0)
  139.             ? String.fromCharCode.apply(String, this._readBytes.slice(0, added))
  140.             : undefined
  141.     }
  142.  
  143.     nextChar() {
  144.         let byteBuffer = this._getNextByte()
  145.         while (byteBuffer !== null) {
  146.             switch (byteBuffer[0]) {
  147.                 case this.BYTE_NEWLINE:
  148.                 case this.BYTE_RETURN:
  149.                 case this.BYTE_SPACE:
  150.                     break
  151.                 default:
  152.                     return String.fromCharCode(byteBuffer[0])
  153.             }
  154.            
  155.             byteBuffer = this._getNextByte()
  156.         }
  157.     }
  158.  
  159.     close() {
  160.         this._readStream.close()
  161.         this._writeStream.push(null)
  162.         this._fsWriteStream.close()
  163.  
  164.         return Promise.all([
  165.             new Promise(resolve => this._readStream.on('close', resolve)),
  166.             new Promise(resolve => this._fsWriteStream.on('close', resolve))
  167.         ])
  168.     }
  169.  
  170.     _getNextByte() {
  171.         return this._readStream.read(1)
  172.     }
  173. }
  174.  
  175. EdxIo.create = () => new EdxIo()._isReadable
  176.  
  177. // --------------------------------- / EdxIO ---------------------------------
  178.  
  179. new Week3Task1().run().catch(console.error)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement