The observed PIN - my solution

The observed PIN - my solution

月光魔力鸭

2018-10-25 08:37 阅读 2932 喜欢 1 codewar 刷题 PIN组合 codewars

Alright, detective, one of our colleagues successfully observed our target person, Robby the robber. We followed him to a secret warehouse, where we assume to find all the stolen stuff. The door to this warehouse is secured by an electronic combination lock. Unfortunately our spy isn't sure about the PIN he saw, when Robby entered it.

The keypad has the following layout:

┌───┬───┬───┐
│ 1 │ 2 │ 3 │
├───┼───┼───┤
│ 4 │ 5 │ 6 │
├───┼───┼───┤
│ 7 │ 8 │ 9 │
└───┼───┼───┘
    │ 0 │
    └───┘

He noted the PIN 1357, but he also said, it is possible that each of the digits he saw could actually be another adjacent digit (horizontally or vertically, but not diagonally). E.g. instead of the 1 it could also be the 2 or 4. And instead of the 5 it could also be the 2, 4, 6 or 8.

He also mentioned, he knows this kind of locks. You can enter an unlimited amount of wrong PINs, they never finally lock the system or sound the alarm. That's why we can try out all possible (*) variations.

Can you help us to find all those variations? It would be nice to have a function, that returns an array (or a list in Java and C#) of all variations for an observed PIN with a length of 1 to 8 digits. We could name the function getPINs (get_pins in python, GetPINs in C#). But please note that all PINs, the observed one and also the results, must be strings, because of potentially leading '0's. We already prepared some test cases for you.

Detective, we count on you!


There's my solution below.

idea

1 .we got a keypad map in question 2. then got a array with map and parameters 3. and we could get max-length of result 4. rise the index ,get the results

code

function getPINs(observed) {
  // TODO: This is your job, detective!
  var map = {
    1 : [1,2,4],
    2 : [1,2,3,5],
    3 : [3,2,6],
    4 : [1,4,5,7],
    5 : [2,4,5,6,8],
    6 : [3,5,6,9],
    7 : [4,7,8],
    8 : [5,7,8,9,0],
    9 : [6,8,9],
    0 : [8,0]
  }
  function getNum(arr,index) {
    return arr.map( (item,i)=>{
      return item[index[i].start];
    }).join('');
  }
  function riseNum(index){
    let isUp = false;
    return index.reverse().map( (item,i)=> {
      let start = item.start,max = item.max;
      if(i ===0 ||isUp === true)start++;
      if(start === max){
        isUp = true;
        start = 0;
      }else{
        isUp = false;
      }
      return {start : start,max : max};
    }).reverse();
  }
  var arr = observed.split('').map( item => {
    return map[item];
  });
  var index = arr.map( item=> {
    return {start : 0,max : item.length}
  });
  var max = arr.reduce( (total,item)=> {
    return total * item.length;
  },1);
  var res = [];
  for(let i=0;i<max;i++){
    res.push(getNum(arr,index));
    index = riseNum(index);
  }
  return res;
}

clever

function getPINs(observed) {
  var adjacent = [
    /* 0 */ [0, 8],
    /* 1 */ [1, 2, 4],
    /* 2 */ [1, 2, 3, 5],
    /* 3 */ [2, 3, 6],
    /* 4 */ [1, 4, 5, 7],
    /* 5 */ [2, 4, 5, 6, 8],
    /* 6 */ [3, 5, 6, 9],
    /* 7 */ [4, 7, 8],
    /* 8 */ [5, 7, 8, 9, 0],
    /* 9 */ [6, 8, 9]
  ];
  
  return observed
    .split('')
    .map(function(d) { return adjacent[d|0]; })
    .reduce(function(pa, da) {
      return da.reduce(function(pv, d) {
        return pv.concat(pa.map(function(p) {
          return '' + p + d;
        }));
      }, []);
    }, ['']);
}

Question from https://www.codewars.com/kata/the-observed-pin/solutions/javascript/me/best_practice

转载请注明出处: https://chrunlee.cn/article/js-code-war-pin.html


感谢支持!

赞赏支持
提交评论
评论信息 (请文明评论)
暂无评论,快来快来写想法...
推荐
java 对象中有很多引用,甚至会出现循环引用,比如 user 对象中有 school 对象,school 对象中又有 user 对象,这样在对 user 对象序列化的时候,就会出现死循环,导致内存溢出。通过一定的方式,将每个对象增加ID 和 REF 引用标识最终可以解决这个问题
web网站上总会有在文本域中提交代码的操作,那么如何处理呢?
在今天之前,我对canvas中rotate其实是一脸蒙逼的... 虽然之前有做过图片旋转,但那是在他人的基础上直接修改的,至于为啥会这样..讲真,还真没注意过,但是今天又需要用到这块了,实在搞不定了,找了各种资料,终于明白了.. 坐标系的问题。
问题是由一个BUG引起的,功能中有使用国际化组件,用的是jquery.i18n,在chrome上、ff上都没有什么问题,问题出在了IE上。万恶的IE..
nvm install 16.15.0 : The process cannot access the file because it is being used by another process
整理下关于axios的使用,一些常用的调用、处理以及其他。
最大公因数,也称最大公约数、最大公因子,指两个或多个整数共有约数中最大的一个。a,b的最大公约数记为(a,b),同样的,a,b,c的最大公约数记为(a,b,c),多个整数的最大公约数也有同样的记号。求最大公约数有多种方法,常见的有质因数分解法、短除法、辗转相除法、更相减损法。与最大公约数相对应的概念是最小公倍数,a,b的最小公倍数记为[a,b]。
关于web打印,需要对页面内容进行页面样式设置,呈现出分页的样子,同时对于题目中的图片或表格尽量不分到两个页面中,因此实现了一个jquery的web打印插件,当然,这个插件目前只适用于部分情况,仅供借鉴