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
|
Task 1: "Read N-characters
You are given file $FILE.
Create subroutine readN($FILE, $number) returns the first n-characters
and moves the pointer to the (n+1)th character.
Example:
Input: Suppose the file (input.txt) contains "1234567890"
Output:
print readN("input.txt", 4); # returns "1234"
print readN("input.txt", 4); # returns "5678"
print readN("input.txt", 4); # returns "90"
"
My notes: weird question, hiding IO handles, so presumably a hash of filenames
to IO handles is needed?
Task 2: "Search Insert Position
You are given a sorted array of distinct integers @N and a target $N.
Write a script to return the index of the given target if found otherwise
place the target in the sorted array and return the index.
Example 1:
Input: @N = (1, 2, 3, 4) and $N = 3
Output: 2 since the target 3 is in the array at the index 2.
Example 2:
Input: @N = (1, 3, 5, 7) and $N = 6
Output: 3 since the target 6 is missing and
should be placed at the index 3.
Example 3:
Input: @N = (12, 14, 16, 18) and $N = 10
Output: 0 since the target 10 is missing and
should be placed at the index 0.
Example 4:
Input: @N = (11, 13, 15, 17) and $N = 19
Output: 4 since the target 19 is missing and
should be placed at the index 4.
"
My notes: nice question. Clearly defined for once:-)
Note that inserting the element in the list only matters if
we print the list out, so let's do that.
Also added decent amount of input checking (is the list sorted etc)
Also added test suite [invoke with --test]
|