The observed PIN - my solution

简介: Question from codewar,about all of array combinations.

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.

  • possible in sense of: the observed PIN itself and all variations considering the adjacent digits

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

  1. then got a array with map and parameters
  2. and we could get max-length of result
  3. 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

相关文章
|
5月前
|
Linux
gpio_direction_output 和 gpio_set_value之间的关系
gpio_direction_output 和 gpio_set_value之间的关系
618 0
|
存储 安全
atomic_int
atomic_int
349 0
C4.
|
7月前
|
C++ 开发者
C++ struct与class
C++ struct与class
C4.
48 0
|
Java 调度 C++
VirtualThread Pin的处理总结
VirtualThread Pin的处理总结
78 0
|
安全 网络协议 Unix
C10M - The Kernel is the Problem, Not the Solution
C10K早已经不是问题,开始迎接C10M的挑战。 这篇文章列出了Linux 内核面对C10M的瓶颈:数据包的处理,CPU调度,内存管理。很有启发性。
#define a int[10]与 typedef int a[10]用法
// #define a int[10] #include #include #define a int[10] int main() { int *p=(int *)malloc(sizeof(a)); p[0]=1; printf("%d\n",p[0]);...
1756 0