Advertisement
Guest User

Codewars kata

a guest
Jul 17th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System;
  4. public class Kata
  5. {
  6.     public static List<string> GetPINs(string observed)
  7.     {
  8.         var result = new List<string>();
  9.       var max = Convert.ToInt32(new String('9', observed.Length));
  10.         var masks = Enumerable.Range(0, max + 1).Select(s => AddZeroes(s, observed.Length));
  11.         foreach (string mask in masks)
  12.         {
  13.             try
  14.             {
  15.                 var pin = "";
  16.                
  17.                 for (int i = 0; i < observed.Length; i++)
  18.                 {
  19.                     pin += GetAdjacent(observed[i])[mask[i] - '0'];
  20.                 }
  21.                 result.Add(pin);
  22.             }
  23.             catch
  24.             {
  25.                 continue;
  26.             }
  27.         }
  28.         return result;
  29.     }
  30.    
  31.     private static string AddZeroes(int n, int length)
  32.     {
  33.         string result = n.ToString();
  34.         result = new String('0', length - result.Length) + result;
  35.         return result;
  36.     }
  37.    
  38.     private static List<char> GetAdjacent(char digit)
  39.     {
  40.       if (digit == '0') return new List<char>() {'0', '8'};
  41.       if (digit == '1') return new List<char>() {'1', '2', '4'};
  42.       if (digit == '2') return new List<char>() {'1', '2', '3', '5'};
  43.       if (digit == '3') return new List<char>() {'2', '3', '6'};
  44.       if (digit == '4') return new List<char>() {'1', '4', '5', '7'};
  45.       if (digit == '5') return new List<char>() {'2', '4', '5', '6', '8'};
  46.       if (digit == '6') return new List<char>() {'3', '5', '6', '9'};
  47.       if (digit == '7') return new List<char>() {'4', '7', '8'};
  48.       if (digit == '8') return new List<char>() {'5', '7', '8', '9', '0'};
  49.       if (digit == '9') return new List<char>() {'6', '8', '9'};
  50.       throw new ArgumentOutOfRangeException();
  51.     }
  52.    
  53.    
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement