JavaScript/String/面试题

来自康健生活
Kangkk​(留言 | 贡献)2021年1月18日 (一) 13:13的版本 (→输出字符串)

输出字符串

之前微软小冰创始团队的一个大佬面试笔试,题目如下:

/**
Problem: Longest continous valid substring

Your task is to find the longest continous substrings
consists of only "valid" characters for each line and
output the substring as a separate line to output.
Here "valid" is defined as English letters (upper and
lower cases) and space.

Here is an example of two lines of input stream:
^%oA i923diuo
(989872347897*&&:":}{?>
And the ouput stream should also be two lines:
oA i


Please note that the output lines should be in the 
exact same order as their input lines and the number
of input lines and output lines should be the same.

InStream and OutStream are two already implemented 
interfaces for input and output respectively. You should use them for I/O and they have only the methods 
defined below. Don't read their implementations, which
are not relevant to this interview.

TWO TASKs:
1. add enough (according to you) test cases in line 52.
2. implement LongestValidSubstrings() of Processor class

ADDITIONAL REQUIREMENTS:
Do not use regular expression.
Use as little memory as you can.

You may search on the web freely when you code.

If any questions please contact me.
*/

/**
 * The interface for input
 * 
 * Only sequential access allowed. Cannot get length information.
 * 
 * Note: 
 *     Ask question if anything is not clear.
 *     The implementations are just mock. Your code should not depend on them.
 */
class InStream
{
  constructor() {
    // TAKS 1: PLEASE ADD UNITTEST CASES HERE.
    this.UnittestCases = "^%oA i923diuo\n(989872347897*&oA it&:":}{?>";

    this.ptr = 0;
  }

  readChar() {
    // 返回下标的单个字符串
    if (!this.hasEnded()) {
      return this.UnittestCases[this.ptr++];
    } else {
      return null;
    }
  }

  readLine() {
    if (!this.hasEnded()) {
      var start = this.ptr;
      var end = this.UnittestCases.indexOf('\n', this.ptr);
      // 检验 回车位置
      if (end === -1) {
        end = this.UnittestCases.length;
      }
      this.ptr = end+1;
      return this.UnittestCases.substring(start, end);
    } else {
      return null;
    }
  }

  readBuffer(size) {
    if (!this.hasEnded()) {
      var start = this.ptr;
      var end = Math.min(this.ptr + size, this.UnittestCases.length)
      this.ptr = end;
      return this.UnittestCases.substring(start, end);
    } else {
      return null;
    }
  }
  // 计数器判断
  hasEnded() {
    return this.UnittestCases.length <= this.ptr;
  }
}

/**
 * The interface for output
 * 
 * Note: 
 *     Ask question if anything is not clear.
 *     The implementations are just mock. Your code should not depend on them.
 */
class OutStream
{
  writeLine(line) {
    console.log(line);
  }

  writeBuffer(buff) {
    process.stdout.write(buff);
  }
  text(tex){
    console.log(tex)
  }
}

class Processor {
  constructor() {
    this.inStream = new InStream();
    this.outStream = new OutStream();
  }
  
  process() {
    // 断行
    while (!this.inStream.hasEnded()) {
      var line = this.inStream.readLine();
      this.longestValidSubstrings(line);
    }
  }
  isSpaceArray(arr){
    if(arr.length<1){
      return true
    }
    return false
  }
  isUpper(text){
    if(text<'A' || text>'Z')
      return false;
    return true;
  }
  isLower(text){
    if(text<'a' || text>'z')
      return false;
    return true;
  }
  isSpace(text){
    if(text !== ' ')
      return false;
    return true;
  }
  longestValidSubstrings(line) {
    var str = ''
    let arr = []
    var sign = {
      isSpace: false,
      isUpper: false,
      isLower: false
    }
    for(const index in line){
      if(this.isSpace(line[index])){
        str = str + line[index]
        sign.isSpace = true
      }else if(this.isUpper(line[index])){
        str = str + line[index]
        sign.isUpper = true
      }else if(this.isLower(line[index])){
        str = str + line[index]
        sign.isLower = true
      }else{
        if(sign.isLower&&sign.isUpper&&sign.isSpace){
          arr.push(str)
        }
        str = ''
        sign = {
          isSpace: false,
          isUpper: false,
          isLower: false
        }
     }
    }
    let resultArr = [...new Set(arr)].filter(function (s) {
       return s && s.trim();
    });
    if(!this.isSpaceArray(resultArr)){
      for(let i=0;i<resultArr.length;i++){
        this.outStream.writeBuffer(resultArr[i])
        // or
        // this.outStream.text(resultArr[i])
      }
    }
  }
}

let processor = new Processor();
processor.process();

但是不符合题目求,然后改正: 模板:Leftrightflex 然后笔试通过了,心满意足~