Skip to content

Commit aa980ea

Browse files
committed
pb7 + readme
1 parent 64ec157 commit aa980ea

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,17 @@ Implement car and cdr.
8686

8787
[JS](pb5/answer.js) (Not timed)
8888

89+
## 6
90+
> This problem was asked by Google.
91+
92+
An XOR linked list is a more memory efficient doubly linked list. Instead of each node holding `next` and `prev` fields, it holds a field named both, which is an XOR of the next node and the previous node. Implement an XOR linked list; it has an `add(element)` which adds the element to the end, and a `get(index)` which returns the node at index.
93+
If using a language that has no pointers (such as Python), you can assume you have access to get_pointer and dereference_pointer functions that converts between nodes and memory addresses.
94+
95+
96+
### 7
97+
> This problem was asked by Facebook.
98+
99+
Given the mapping `a = 1, b = 2, ... z = 26`, and an encoded message, count the number of ways it can be decoded.
100+
For example, the message '111' would give 3, since it could be decoded as 'aaa', 'ka', and 'ak'.
101+
You can assume that the messages are decodable. For example, '001' is not allowed.
102+

pb7/Problem.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
> This problem was asked by Facebook.
2+
3+
Given the mapping `a = 1, b = 2, ... z = 26`, and an encoded message, count the number of ways it can be decoded.
4+
For example, the message '111' would give 3, since it could be decoded as 'aaa', 'ka', and 'ak'.
5+
You can assume that the messages are decodable. For example, '001' is not allowed.

pb7/answer.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const m1 = 111;
2+
const m2 = 12345;
3+
const m3 = 4;
4+
5+
// Less optimized, clearer version
6+
const decode = (message) => {
7+
message = message.toString();
8+
9+
const split1 = [];
10+
const split2 = [];
11+
//Split1, start at index 0
12+
for (var i = 0; i < message.length; i+=2) {
13+
// If we have enough letters left to make a par
14+
if(message.charAt(i+1)){
15+
split1.push(message.charAt(i) + message.charAt(i+1));
16+
} else {
17+
split1.push(message.charAt(i));
18+
}
19+
}
20+
21+
//Split1, start at index 1
22+
split2.push(message.charAt(0));
23+
for (var i = 1; i < message.length; i+=2) {
24+
// If we have enough letters left to make a par
25+
if(message.charAt(i+1)){
26+
split2.push(message.charAt(i) + message.charAt(i+1))
27+
} else {
28+
split2.push(message.charAt(i))
29+
}
30+
31+
}
32+
console.log(split1, split2)
33+
// base : one way to solve
34+
// Every time we encounter an index that is <= 26, that's another way
35+
const nbWays = 1 + split1.concat(split2).filter( x => x <= 26 && x >= 10).length ;
36+
return nbWays
37+
}
38+
39+
// Optimized, more cryptic version
40+
const decode2 = (message) => {
41+
message = message.toString();
42+
43+
const split1 = [];
44+
const split2 = [];
45+
46+
split2.push(message.charAt(0));
47+
for (var i = 0; i < message.length; i++) {
48+
if(i % 2 === 0) {
49+
if(message.charAt(i+1)){
50+
split1.push(message.charAt(i) + message.charAt(i+1));
51+
} else {
52+
split1.push(message.charAt(i));
53+
}
54+
} else {
55+
if(message.charAt(i+1)){
56+
split2.push(message.charAt(i) + message.charAt(i+1))
57+
} else {
58+
split2.push(message.charAt(i))
59+
}
60+
}
61+
}
62+
console.log(split1, split2)
63+
const nbWays = 1 + split1.concat(split2).filter( x => x <= 26 && x >= 10).length ;
64+
return nbWays
65+
}
66+
67+
/*
68+
console.log(decode(m2));
69+
console.log('---------')
70+
*/
71+
console.log(decode2(m2));
72+

0 commit comments

Comments
 (0)