-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.swift
More file actions
40 lines (33 loc) · 847 Bytes
/
Solution.swift
File metadata and controls
40 lines (33 loc) · 847 Bytes
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
/*
XML Encoding
Since XML is very verbose, you are given a way of encoding it where each tag gets mapped to a pre-defined integer value.
*/
class Element {
var name = ""
var value: String? = ""
var attributes: Array<Attribute> = []
var children: Array<Element> = []
}
class Attribute {
var name = ""
}
func encode(_ element: Element) -> String {
var result = ""
result += encodeString(element.name)
for attr in element.attributes {
result += encodeString(attr.name)
}
result += encodeString("0")
if let val = element.value {
result += encodeString(val)
} else {
for el in element.children {
result += encode(el)
}
}
result += encodeString("0")
return result
}
func encodeString(_ str: String) -> String {
return ""
}