forked from keshavnandan/Topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuipu.txt
81 lines (52 loc) · 2.18 KB
/
Quipu.txt
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
PROBLEM STATEMENT
The Incas used a sophisticated system of record keeping consisting of bundles of knotted cords.
Such a bundle of cords is called a quipu. Each individual cord represents a single number.
Surprisingly, the Incas used a base-10 positional system, just like we do today. Each digit of a number
is represented by a cluster of adjacent knots, with spaces between neighboring clusters. The digit is
determined by the number of knots in the cluster.
For example, the number 243 would be represented by a cord with knots tied in the following pattern
-XX-XXXX-XXX-
where each uppercase 'X' represents a knot and each '-' represents an unknotted segment of cord (all quotes for clarity only).
Unlike many ancient civilizations, the Incas were aware of the concept of zero, and used it in their quipus.
A zero is represented by a cluster containing no knots.
For example, the number 204003 would be represented by a cord with knots tied in the following pattern
-XX--XXXX---XXX-
^^ ^^^
^^ ^^^
^^ two zeros between these three segments
^^
one zero between these two segments
Notice how adjacent dashes signal the presence of a zero.
Your task is to translate a single quipu cord into an integer. The cord will be given as a string knots
containing only the characters 'X' and '-'. There will be a single '-' between each cluster
of 'X's, as well as a leading '-' and a trailing '-'. The first cluster will not be empty.
DEFINITION
Class:Quipu
Method:readKnots
Parameters:string
Returns:int
Method signature:int readKnots(string knots)
CONSTRAINTS
-knots contains between 3 and 50 characters, inclusive.
-knots contains only the characters 'X' and '-'. Note that 'X' is uppercase.
-The first and last characters of knots are '-'s. The second character is 'X'.
-knots does not contain 10 consecutive 'X's.
-knots will represent a number between 1 and 1000000, inclusive.
EXAMPLES
0)
"-XX-XXXX-XXX-"
Returns: 243
The first example above.
1)
"-XX--XXXX---XXX-"
Returns: 204003
The second example above.
2)
"-X-"
Returns: 1
3)
"-X-------"
Returns: 1000000
4)
"-XXXXXXXXX--XXXXXXXXX-XXXXXXXXX-XXXXXXX-XXXXXXXXX-"
Returns: 909979