The observed PIN - my solution

The observed PIN - my solution

月光魔力鸭

2018-10-25 08:37 阅读 2685 喜欢 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


感谢支持!

赞赏支持
提交评论
评论信息 (请文明评论)
暂无评论,快来快来写想法...
推荐
今天刷codewars的题目的时候碰到一个通过js来实现字符串转base64的题目,base64虽然在js或nodejs中经常用,但是我还真没有仔细去看过原理以及如何实现,这回绕不过去了,赶紧找了找资料看了下。
业务中有一段涉及到处理canvas的图片然后将内容进行上传,后测试发现在IE中不好使哎...
做作业的时候,需要在手机上预览下,但是发现如果想在移动端上展示A4样子的作业还是挺麻烦的,最后还是准备通过图片来展示,然后移动端缩放呗。。
需求如下:有一张大图,需要显示大图中的一小部分,目前能知道的时候小图的宽高和坐标,同时大图有一个旋转角度可以知道,目标就是把小图正确的显示出来。
通过修改数据库编码处理存储emoj表情导致的报错问题。
当一些业务必须通过横屏来实现,但是又没有原生来做,只能通过h5的时候怎么办?
之前的时候都是在各大主机厂商手动进行申请免费的,直到阿里的免费期限变更为3个月.. 我就开始觉的有些麻烦了,还不如使用这个let's encrypt进行部署呢。
突然来了一个调研任务,想要实现一个类似3D虚拟展厅类似的需求,主要就是放一些学生的作品,然后预览啥的,效果还是要全景的效果。 经过一番调查,最终锁定了以前从未接触过的krpano。