-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunlock_pattern_visualization.html
149 lines (138 loc) · 4.38 KB
/
unlock_pattern_visualization.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Unlock Pattern Visualization</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.7.1/svg.min.js"></script>
<script src="unlock_pattern.js"></script>
<style>
body {
margin: 0;
}
#drawing {
width: 400px;
border: 1px solid black;
height: 400px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#form {
font-size: 30px;
}
#form button {
border: none;
display: inline-block;
padding: 8px 16px;
vertical-align: middle;
overflow: hidden;
text-decoration: none;
text-align: center;
cursor: pointer;
white-space: nowrap;
color: #fff !important;
background-color: #4caf50 !important;
}
#form input {
padding: 8px;
display: inline-block;
border-bottom: 1px solid #ccc;
border: 1px solid #ccc !important;
}
</style>
</head>
<body>
<p style="font-size: 20px;">
This problem was asked by Uber. One way to unlock an Android phone is
through a pattern of swipes across a 1-9 keypad. For a pattern to be
valid, it must satisfy the following: All of its keys must be distinct. It
must not connect two keys by jumping over a third key, unless that key has
already been used. For example, 4 - 2 - 1 - 7 is a valid pattern, whereas
2 - 1 - 7 is not. Find the total number of valid unlock patterns of length
N, where 1 <= N <= 9.
</p>
<div id="form">
<label>Length Of Each Pattern</label>
<input type="number" value="3" step="1" min="1" max="9" id="number" />
<label>Delay Each</label>
<input type="number" value="0" step="1" min="1" max="9" id="delay" />
<button onclick="makePatterns()">Make patterns</button>
</div>
<div id="drawing">
<div style="text-align: center;" id="list"></div>
</div>
<script>
class PatternBoard {
static boardImageURL =
"https://cdn3.iconfinder.com/data/icons/analytics-5/500/Analytics-45-512.png";
static pointsMap = {
1: [68, 60],
2: [199, 60],
3: [331, 60],
4: [68, 197],
5: [199, 197],
6: [331, 197],
7: [68, 332],
8: [199, 332],
9: [331, 332]
};
constructor(elem) {
this.draw = SVG(elem).size("100%", "100%");
this.image = this.draw
.image(this.constructor.boardImageURL)
.size("100%", "100%");
this.polyline = this.draw
.polyline([[0, 0]])
.fill("none")
.stroke({
color: "#f06",
width: 20,
linecap: "round",
linejoin: "round"
});
}
drawPattern(numbers) {
if (!Array.isArray(numbers)) {
throw new Error(
"drawPattern numbers must be array of numbers between 1-9"
);
}
let points = [];
if (numbers.length == 1) {
const p = numbers[0];
points.push(this.constructor.pointsMap[p]);
points.push(this.constructor.pointsMap[p]);
} else {
points = numbers.map(n => this.constructor.pointsMap[n]);
}
this.polyline.plot(points);
}
}
function wait(time) {
return new Promise(function(resolve, reject) {
setTimeout(() => {
resolve();
}, time);
});
}
const board = new PatternBoard("drawing");
const maker = new PatternMaker();
async function makePatterns() {
const number = parseInt(document.getElementById("number").value);
const delay = parseInt(document.getElementById("delay").value);
const pGenerator = maker.getGenerator(number);
const listElem = document.getElementById("list");
let count = 0;
for (let arr of pGenerator) {
listElem.innerText = "[ " + arr.join(", ") + " ]";
console.log("count", ++count, "arr", arr);
board.drawPattern(arr);
await wait(delay);
}
board.drawPattern([]);
}
</script>
</body>
</html>