blob: f173adcc400400e1e6b69dadcb2fea1bf1de8502 (
plain)
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
|
Task 1: "Fibonacci Sum
You are given a positive integer $N.
Write a script to find out all possible combination of Fibonacci Numbers
required to get $N on addition.
You are NOT allowed to repeat a number. Print 0 if none found.
Example 1:
Input: $N = 6
Output:
1 + 2 + 3 = 6
1 + 5 = 6
Example 2:
Input: $N = 9
Output:
1 + 8 = 9
1 + 3 + 5 = 9
"
My notes: ok. pretty straightforward, especially after last weeks' first task.
Not quite so trivial to do efficiently, my solution generates a lot of
duplicate solutions (hence the dedup() function), and is very slow for large N.
(See also ch-1a.pl for a tabulation of number of Fibonacci sums for i=1..N)
Task 2: "Lonely X
You are given m x n character matrix consists of O and X only.
Write a script to count the total number of X surrounded by O only. Print
0 if none found.
Example 1:
Input: [ O O X ]
[ X O O ]
[ X O O ]
Output: 1 as there is only one X at the first row last column surrounded
by only O.
Example 2:
Input: [ O O X O ]
[ X O O O ]
[ X O O X ]
[ O X O O ]
Output: 2
a) First X found at Row 1 Col 3.
b) Second X found at Row 3 Col 4.
"
My notes: interesting question, sounds simple but perhaps not quite
as simple as it sounds. Especially (obviously) "surrounded by only O"..
Note that I counted rows and columns from 0, not 1. So the output I
generate for the second grid (file grid2) is:
"2 lonely Xs in grid: [0, 2],[2, 3]"
|