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
|
Task 1: Monotonic Array
You are given an array of integers.
Write a script to find out if the given array is Monotonic. Print 1 if
it is otherwise 0.
An array is Monotonic if it is either monotone increasing or decreasing.
Monotone increasing: for i <= j , nums[i] <= nums[j]
Monotone decreasing: for i <= j , nums[i] >= nums[j]
Example 1
Input: @nums = (1,2,2,3)
Output: 1
Example 2
Input: @nums = (1,3,2)
Output: 0
Example 3
Input: @nums = (6,5,5,4)
Output: 1
MY NOTES: seems very easy. use reverse() in order not to detect
monotonically increasing and decreasing..
GUEST LANGUAGE: As a bonus, I also had a go at translating ch-1.pl
into C (look in the C directory for the translation)
Task 2: Reshape Matrix
You are given a matrix (m x n) and two integers (r) and (c).
Write a script to reshape the given matrix in form (r x c) with the
original value in the given matrix. If you can't reshape print 0.
Example 1
Input: $matrix = [ [ 1, 2 ], [ 3, 4 ] ]
$r = 1
$c = 4
Output: [ 1 2 3 4 ]
Example 2
Input: $matrix = [ [ 1, 2, 3 ] , [ 4, 5, 6 ] ]
$r = 3
$c = 2
Output: [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
Example 3
Input: $matrix = [ [ 1, 2 ] ]
$r = 3
$c = 2
Output: 0
MY NOTES: also quite easy, can be done if R1*C1==R2*C
(of course we have to discover R1 and C1, the dimensions o
the original matrix). Also have to read the matrix in..
GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl
into C (look in the C directory for the translation). Needed to
pass original sizes into C routine..
|