Skip to content

Commit a018479

Browse files
committed
Print all possible variations of a given pin number
1 parent 96ddcef commit a018479

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* [Print all possible words from phone digits](./Recursion/mobileKeypad.cpp)
3333
* [Given an odd number, print diamond pattern of stars recursively](./Recursion/diamond.cpp)
3434
* [Print all subsets of a given set](./Recursion/powerset.rb)
35+
* [Print all possible variations of a given pin number](./Recursion/connected_digits.rb)
3536

3637
### Sorting
3738
* [Heap Sort](./Sorting/heapSort.c)

Recursion/connected_digits.rb

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# ┌───┬───┬───┐
2+
# │ 1 │ 2 │ 3 │
3+
# ├───┼───┼───┤
4+
# │ 4 │ 5 │ 6 │
5+
# ├───┼───┼───┤
6+
# │ 7 │ 8 │ 9 │
7+
# └───┼───┼───┘
8+
# │ 0 │
9+
# └───┘
10+
# each of the digits is connected to its adjacent digit (horizontally or vertically, but not diagonally).
11+
# digit 5 is connected to [2,4,6,8]
12+
# any two connected digits can be replaced with each other.
13+
14+
#print all possible pin numbers
15+
16+
$all_pins=[]
17+
def get_pins(observed)#returns an array of all variations
18+
key_pad = {}
19+
key_pad[1]=[1,2,4]
20+
key_pad[2]=[2,1,5,3]
21+
key_pad[3]=[3,2,6]
22+
key_pad[4]=[4,1,5,7]
23+
key_pad[5]=[5,2,4,6,8]
24+
key_pad[6]=[6,3,5,9]
25+
key_pad[7]=[7,4,8]
26+
key_pad[8]=[8,5,7,9,0]
27+
key_pad[9]=[9,8,6]
28+
key_pad[0]=[0,8]
29+
$all_pins=[]
30+
ans = []
31+
pins(observed.to_s,0,observed.to_s.length, key_pad, ans)
32+
# print $all_pins.sort
33+
$all_pins.sort
34+
end
35+
36+
def pins(observed,i,len, key_pad, ans)
37+
if i==len
38+
$all_pins << ans.join
39+
return
40+
end
41+
key_pad[observed[i].to_i].each do |j|
42+
ans.push(j)
43+
pins(observed,i+1,len, key_pad, ans)
44+
ans.pop
45+
end
46+
end
47+
48+
#Test cases:
49+
print get_pins('8')
50+
# ["0", "5", "7", "8", "9"]
51+
puts ""
52+
53+
print get_pins('369')
54+
#["236", "238", "239", "256", "258", "259", "266", "268", "269", "296", "298", "299", "336", "338", "339", "356", "358", "359", "366", "368", "369", "396", "398", "399", "636", "638", "639", "656", "658", "659", "666", "668", "669", "696", "698", "699"]
55+
56+
57+
puts ""
58+
print get_pins('12')
59+
#["11", "12", "13", "15", "21", "22", "23", "25", "41", "42", "43", "45"]
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# ┌───┬───┬───┐
2+
# │ 1 │ 2 │ 3 │
3+
# ├───┼───┼───┤
4+
# │ 4 │ 5 │ 6 │
5+
# ├───┼───┼───┤
6+
# │ 7 │ 8 │ 9 │
7+
# └───┼───┼───┘
8+
# │ 0 │
9+
# └───┘
10+
# each of the digits is connected to its adjacent digit (horizontally or vertically, but not diagonally).
11+
# digit 5 is connected to [2,4,6,8]
12+
# any two connected digits can be replaced with each other.
13+
14+
#print all possible permutation
15+
16+
$all_pins=[]
17+
def get_pins(observed)#returns an array of all variations
18+
key_pad = {}
19+
key_pad[1]=[1,2,4]
20+
key_pad[2]=[2,1,5,3]
21+
key_pad[3]=[3,2,6]
22+
key_pad[4]=[4,1,5,7]
23+
key_pad[5]=[5,2,4,6,8]
24+
key_pad[6]=[6,3,5,9]
25+
key_pad[7]=[7,4,8]
26+
key_pad[8]=[8,5,7,9,0]
27+
key_pad[9]=[9,8,6]
28+
key_pad[0]=[0,8]
29+
$all_pins=[]
30+
ans = []
31+
pins(observed.to_s,0,observed.to_s.length, key_pad, ans)
32+
# print $all_pins.sort
33+
$all_pins.sort
34+
end
35+
36+
def pins(observed,i,len, key_pad, ans)
37+
if i==len
38+
$all_pins << ans.join
39+
return
40+
end
41+
key_pad[observed[i].to_i].each do |j|
42+
ans.push(j)
43+
pins(observed,i+1,len, key_pad, ans)
44+
ans.pop
45+
end
46+
end
47+
print get_pins('369')
48+
#["236", "238", "239", "256", "258", "259", "266", "268", "269", "296", "298", "299", "336", "338", "339", "356", "358", "359", "366", "368", "369", "396", "398", "399", "636", "638", "639", "656", "658", "659", "666", "668", "669", "696", "698", "699"]
49+
50+
51+
puts ""
52+
print get_pins('12')
53+
#["11", "12", "13", "15", "21", "22", "23", "25", "41", "42", "43", "45"]

0 commit comments

Comments
 (0)