美文网首页
面试阿里笔试题

面试阿里笔试题

作者: 扶搏森 | 来源:发表于2019-02-27 12:35 被阅读0次
    /**
     * 说明:实现一个方法,用于比较两个版本号(version1、version2)
     *     如果version1 > version2,返回1;如果version1 < version2,返回-1,其他情况返回0
     *     版本号规则`x.y.z`,xyz均为大于等于0的整数,至少有x位
     * 示例:
     * compareVersion('0.1', '1.1.1'); // 返回-1
     * compareVersion('13.37', '1.2 '); // 返回1
     * compareVersion('1.1', '1.1.0'); // 返回0
     */
    
    function compareVersion(version1, version2) {
      /* 功能实现 */
      let arr1 = version1.split('.')
      let arr2 = version2.split('.')
      let len1 = arr1.length;
      let len2 = arr2.length;
      let max = Math.max(len1, len2)
      for(let i = 0; i < max; i++) {
        arr1[i] = arr1[i] || 0
        arr2[i] = arr2[i] || 0
        if(arr1[i] > arr2[i]) {
          return 1
        } else if (arr1[i] < arr2[i]){
          return -1
        }
      }
      return 0
    }
    
    
    /**
    * 说明:简单实现一个事件订阅机制,具有监听on和触发emit方法
    * 示例:
    * const event = new EventEmitter();
    * event.on('someEvent', (...args) => { 
    *     console.log('some_event triggered', ...args);
    * }); 
    * event.emit('someEvent', 'abc', '123'); 
    */
    class EventEmitter {
      /* 功能实现 */
      constructor() {
        this._cache = {}
      }
      on(type, callback) {
        let fns = this._cache[type] = this._cache[type] || []
        if(fns.indexOf(callback) === -1) {
          fns.push(callback)
        }
        return this
      }
      emit(type, ...data) {
        let fns = this._cache[type]
        if (Array.isArray(fns)) {
          fns.forEach((fn) => {
            fn(...data)
          })
        }
        return this
      }
    }
    
    const event = new EventEmitter();
    event.on('someEvent', (...args) => {
      console.log('some_event triggered', ...args);
    }); 
    event.emit('someEvent', 'abc', '123');
    
    /**
    * 说明:给定一个编码字符,按编码规则进行解码,输出字符串
    *     编码规则是`count[letter]`,将letter的内容count次输出,count是0或正整数,letter是区分大小写的纯字母
    * 示例:
    * const s = '3[a]2[bc]'; decodeString(s); // 返回'aaabcbc'
    * const s = '3[a2[c]]'; decodeString(s); // 返回'accaccacc'
    * const s = '2[abc]3[cd]ef'; decodeString(s); // 返回'abcabccdcdcdef'
    */
    
    function decodeString(s) {
      /* 功能实现 */
      function handle(s) {
        return s.replace(/(\d+)\[[a-zA-Z]+\]/g, (x, i) => {
          let datas = x.split(/[\[\]]/);
          let count = datas[0];
          let str = datas[1];
          let res = ''
          while (count--) {
            res += str;
          }
          return res
        })
      }
    
      while (s.includes('[')) {
        s = handle(s)
      }
      return s;
    };
    const s = '3[a]2[bc]'
    console.log(decodeString(s))
    
    /**
    * 说明:将以下数据结构变形一下,生成二维表
    */
    
    // 现有数据结构
    const dataMap = {
    cols: [
      {
        key: 'a',
        label: '甲列'
      },
      {
        key: 'b',
        label: '乙列'
      }
    ],
    data: [
      {
        b: 21,
        a: 'hello'
      },
      {
        b: 22,
        a: 'hello'
      },
      {
        b: 23,
        a: 'world'
      },
      {
        b: 23,
        a: 'hello'
      },
      {
        b: 24,
        a: 'hello'
      },
    ]
    };
    
    
    // 目标数据
    const target = {
      head: ['甲列', '乙列'],
      rows: [
        ['hello', 21],
        ['hello', 22],
        ['world', 23],
        ['hello', 23],
        ['hello', 24]
      ]
    }
    
    
    const transform = () => {
      let res = {};
      let keys = dataMap.cols.map(x => x.key);
      res.head = dataMap.cols.map(x => x.label);
      res.rows = dataMap.data.map(x => keys.map(key => x[key]));
      return res;
    }
    
    // const target == transform(dataMap)
    transform(dataMap)
    
    
    /** 进阶 **/
    // 在以上基础上,将相邻行中相同的数据合并单元格
    // 原表格
    +-------+------+
    | 甲列  | 乙列 |
    +-------+------+
    | hello | 21   |
    +-------+------+
    | hello | 22   |
    +-------+------+
    | world | 23   |
    +-------+------+
    | hello | 23   |
    +-------+------+
    | hello | 24   |
    +-------+------+
    
    // 合并成
    +-------+------+
    | 甲列  | 乙列 |
    +-------+------+
    |       | 21   |
    | hello |------+
    |       | 22   |
    +-------+------+
    | world |      |
    +-------| 23   |
    |       |      |
    | hello |------+
    |       | 24   |
    +-------+------+
    
    
    ['hello', 21],
    ['hello', 22],
    ['world', 23],
    ['hello', 23],
    ['hello', 24]
    
    即将以上结构算成以下结构
    
    [{ v: 'hello', r: 2 }, { v: 21 }],
    [ null               , { v: 22 }],
    [{ v: 'world', r: 1 }, { v: 23, r: 2 }],
    [{ v: 'hello', r: 2 }, null ],
    [ null               , { v: 24 }]
    
    var arr = [
      ['hello', 21],
      ['hello', 22],
      ['world', 23],
      ['hello', 23],
      ['hello', 24]
    ]
    let arr1 = arr.map(data => data[0])
    let arr2 = arr.map(data => data[1])
    
    function handle(arr) {
      let res = []
      res = arr.reduce((accumulator, currentValue, currentIndex, array) => {
        if (currentIndex === 0 || (currentIndex !== 0 && currentValue !== array[currentIndex - 1])) {
          accumulator.push({
            v: currentValue, r: 1
          })
          return accumulator
        }
        while (currentIndex) {
          --currentIndex
          if(accumulator[currentIndex]) {
            accumulator[currentIndex].r = accumulator[currentIndex].r + 1
            break
          }
        }
        accumulator.push(null)
        return accumulator
      }, [])
      return res
    }
    let resArr1 = handle(arr1)
    let resArr2 = handle(arr2)
    function combine(arr1 = [], arr2 = []) {
      if(arr1.length !== arr2.length) return console.log('传入数组长度必须相等')
      let res = arr1.map((item, index) => {
        return [item, arr2[index]]
      })
      return res
    }
    console.log(combine(resArr1, resArr2))
    
    我经历的笔试题,只做笔记,不做评论。

    相关文章

      网友评论

          本文标题:面试阿里笔试题

          本文链接:https://www.haomeiwen.com/subject/xcgxuqtx.html