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
|
Task 1: Minimum Index Sum
You are given two arrays of strings. Write a script to find out all
common strings in the given two arrays with minimum index sum. If no
common strings are found returns an empty list.
Example 1
Input: @list1 = ("Perl", "Raku", "Love")
@list2 = ("Raku", "Perl", "Hate")
Output: ("Perl", "Raku")
There are two common strings "Perl" and "Raku".
Index sum of "Perl": 0 + 1 = 1
Index sum of "Raku": 1 + 0 = 1
Example 2
Input: @list1 = ("A", "B", "C")
@list2 = ("D", "E", "F")
Output: ()
No common string found, so no result.
Example 3
Input: @list1 = ("A", "B", "C")
@list2 = ("C", "A", "B")
Output: ("A")
There are three common strings "A", "B" and "C".
Index sum of "A": 0 + 1 = 1
Index sum of "B": 1 + 2 = 3
Index sum of "C": 2 + 0 = 2
MY NOTES: very easy. Identify whether any common strings exist: set
intersection. Then calculate index sum of all common strings and choose
the minimum ones. Trickiest thing to work out is how to input two lists
of strings - let's choose an arbitrary separator ':'..
GUEST LANGUAGE: As a bonus, I also had a go at translating ch-1.pl into C
(look in the C directory for that).
Task 2: Duplicate and Missing
You are given an array of integers in sequence with one missing and one
duplicate. Write a script to find the duplicate and missing integer in
the given array. Return -1 if none found. For the sake of this task,
let us assume the array contains no more than one duplicate and missing.
Example 1:
Input: @nums = (1,2,2,4)
Output: (2,3)
Duplicate is 2 and Missing is 3.
Example 2:
Input: @nums = 1,2,3,4
Output: -1
No duplicate and missing found.
Example 3:
Input: @nums = (1,2,3,3)
Output: (3,4)
Duplicate is 3 and Missing is 4.
MY NOTES: also pretty easy - especially if the list of integers should (if it
were not for the one missing and one duplicated) form the sequence 1..N.
if so: the duplicate is the element where el[i] != i.
The missing is the sole member of set {1..N} - all el[i]
Let's generalise it slightly to B, B+1, .. B+N-1. Then: find the element
where el[i] != el[0]+i, that is the duplicate, and the missing is the sole
member of {B..N+N-1} - all el[i]
GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl into C
(look in the C directory for that)
|