• codewars--js--Reverse or rotate?----es6变量,箭头函数,正则取块


    问题描述:

    对输入的str按照sz个数进行分块,若一块内所有数字的立方和是偶数,则倒序;否则,向左移动一位。然后将修改过的块整合到一个字符串,作为输出。

    The input is a string str of digits. Cut the string into chunks (a chunk here is a substring of the initial string) of size sz (ignore the last chunk if its size is less than sz).

    If a chunk represents an integer such as the sum of the cubes of its digits is divisible by 2, reverse that chunk; otherwise rotate it to the left by one position. Put together these modified chunks and return the result as a string.

    If

    • sz is <= 0 or if str is empty return ""
    • sz is greater (>) than the length of str it is impossible to take a chunk of size sz hence return "".
    Examples:
    revrot("123456987654", 6) --> "234561876549"
    revrot("123456987653", 6) --> "234561356789"
    revrot("66443875", 4) --> "44668753"
    revrot("66443875", 8) --> "64438756"
    revrot("664438769", 8) --> "67834466"
    revrot("123456779", 8) --> "23456771"
    revrot("", 8) --> ""
    revrot("123456779", 0) --> "" 
    revrot("563000655734469485", 4) --> "0365065073456944"

    我的答案
     1 function revrot(str, sz) {
     2       if (sz<=0 || str==null || str.length<sz){
     3             return "";
     4         }else{
     5           var num=parseInt(str.length/sz);
     6             var a=[];b=[];            
     7             for(var i=0;i<num;i++){
     8               a[i]=str.substr(sz*i,sz);
     9               var sum=0;
    10               for(var j=0;j<sz;j++){
    11                 sum=sum+Math.pow(a[i][j],3);
    12               }
    13               a[i]=sum%2==0?reverse(a[i]):rotate(a[i]);
    14               var b=b.concat(a[i]);
    15           }
    16           return b.join("");
    17       }
    18     }
    19     function reverse(str){
    20       return str.split("").reverse().join("").toString();
    21     }
    22     function rotate(str){  
    23       return str.substr(1).concat(str.slice(0,1));
    24     }
    
    

    优秀答案:

     1 function revrot(str, sz) {
     2   if (sz < 1 || sz > str.length) 
     3     return '';
     4 
     5   let reverse = s => s.split('').reverse().join('');
     6   let rotate  = s => s.slice(1) + s.slice(0, 1);
     7   let sum_cubes = c => c.split('').reduce((a, b) => a + +b ** 3, 0); 
     8 
     9   return str
    10     .match(new RegExp('.{' + sz + '}', 'g'))
    11     .map(c => sum_cubes(c) % 2 ? rotate(c) : reverse(c))
    12     .join('');
    13 }

     优秀答案精彩之处在于使用正则取块。

    不过有一点小问题就是在b**2部分,chrome显示Uncaught SyntaxError: Unexpected token **。替换成Math.pow(b,3); 就可以啦。

    知识点:

    1,es6的定义变量

    es6之前用var定义变量,存在诸多问题:

    可重复定义变量;无块级作用域;甚至不需要声明就可以使用

    es6 之后

    let定义变量

    const定义常量

    2,箭头函数

    参考 https://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/001438565969057627e5435793645b7acaee3b6869d1374000

    1 function(x,y){
    2     return x*2;        
    3 }

    等同于 (x,y) => x*2;

     
  • 相关阅读:
    CSS
    Html5
    [LeetCode] 78. Subsets(子集)
    [LeetCode] 22. Generate Parentheses(括号生成器)
    [LeetCode] 406. Queue Reconstruction by Height(按身高重排队列)
    [LeetCode] 46. Permutations(全排列)
    [LeetCode] 94. Binary Tree Inorder Traversal(二叉树的中序遍历)
    [LeetCode] 338. Counting Bits(数比特位)
    [LeetCode] 763. Partition Labels(标签划分)
    [LeetCode] 20. Valid Parentheses(有效的括号)
  • 原文地址:https://www.cnblogs.com/hiluna/p/8722611.html
Copyright © 2020-2023  润新知