Compare commits

...

1 Commits

Author SHA1 Message Date
ostrichb d404d455a6
added answers
3 years ago

@ -0,0 +1,30 @@
import Foundation
func printValue(_ n: Int) {
print("The value of n is \(String(n))")
}
func countLength(_ n: String) {
print("The length of n is \(n.count)")
}
func varProcessor(_ n: Any) {
let nInt = n as? Int ?? 0 // nnIntn
let nString = n as? String ?? "" // nnStringn
switch n{
case is Int:
// n
printValue(nInt)
case is String:
// n
countLength(nString)
default:
print("An error occurred.")
}
}
let test: [Any] = [0, 2, 0.2, "Hello"]
for item in test {
varProcessor(item)
}

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' buildActiveScheme='true' importAppTypes='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:">
</FileRef>
</Workspace>

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task1.playground#CharacterRangeLen=608&amp;CharacterRangeLoc=18&amp;EndingColumnNumber=0&amp;EndingLineNumber=30&amp;StartingColumnNumber=0&amp;StartingLineNumber=1&amp;Timestamp=689874488.556267"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>Task1 (Playground).xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

@ -0,0 +1,22 @@
import Foundation
func varProcessor(_ n: Any, intProcess: (Int) -> Void, strProcess: (String) -> Void) {
let nInt = n as? Int ?? 0 // nnIntn
let nString = n as? String ?? "" // nnStringn
switch n{
case is Int:
// n
intProcess(nInt)
case is String:
// n
strProcess(nString)
default:
print("An error occurred.")
}
}
let test: [Any] = [0, 2, 0.2, "Hello"]
for item in test {
varProcessor(item, intProcess: {print("The value of n is \(String($0))")}, strProcess: {print("The length of n is \($0.count)")})
}

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task2.playground#CharacterRangeLen=622&amp;CharacterRangeLoc=18&amp;EndingColumnNumber=0&amp;EndingLineNumber=22&amp;StartingColumnNumber=0&amp;StartingLineNumber=1&amp;Timestamp=689907958.99949"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' buildActiveScheme='true' importAppTypes='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:">
</FileRef>
</Workspace>

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>Task2 (Playground).xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

@ -0,0 +1,21 @@
import Foundation
struct Entity {
let ID, name: String
func verify() -> Bool {
// ID 13
let reFormula = try! NSRegularExpression(pattern: "^\\d{13}$", options: [NSRegularExpression.Options.anchorsMatchLines])
let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
let matchList = reFormula.matches(in: ID, range: matchRange)
if matchList.count != 1 {
return false
}
return true
}
}
var fly = Entity(ID: "0000000000987", name: "蒼蠅")
fly.verify()
var xiaoming = Entity(ID: "20211145145141919810", name: "可利斯")
xiaoming.verify()

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' buildActiveScheme='true' importAppTypes='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:">
</FileRef>
</Workspace>

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task3.playground#CharacterRangeLen=146&amp;CharacterRangeLoc=504&amp;EndingColumnNumber=0&amp;EndingLineNumber=21&amp;StartingColumnNumber=0&amp;StartingLineNumber=15&amp;Timestamp=689911058.503721"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task3.playground#CharacterRangeLen=146&amp;CharacterRangeLoc=504&amp;EndingColumnNumber=0&amp;EndingLineNumber=21&amp;StartingColumnNumber=0&amp;StartingLineNumber=15&amp;Timestamp=689911058.5037709"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task3.playground#CharacterRangeLen=146&amp;CharacterRangeLoc=504&amp;EndingColumnNumber=0&amp;EndingLineNumber=21&amp;StartingColumnNumber=0&amp;StartingLineNumber=15&amp;Timestamp=689911058.503805"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task3.playground#CharacterRangeLen=146&amp;CharacterRangeLoc=504&amp;EndingColumnNumber=0&amp;EndingLineNumber=21&amp;StartingColumnNumber=0&amp;StartingLineNumber=15&amp;Timestamp=689911058.503834"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task3.playground#CharacterRangeLen=146&amp;CharacterRangeLoc=504&amp;EndingColumnNumber=0&amp;EndingLineNumber=21&amp;StartingColumnNumber=0&amp;StartingLineNumber=15&amp;Timestamp=689911058.50386"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>

@ -0,0 +1,91 @@
import Foundation
class Entity {
var ID: String = ""
var name: String = ""
func verify() -> Bool {
// ID 13
let reFormula = try! NSRegularExpression(pattern: "^\\d{13}$", options: [NSRegularExpression.Options.anchorsMatchLines])
let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
let matchList = reFormula.matches(in: ID, range: matchRange)
if matchList.count != 1 {
return false
}
return true
}
}
class Student: Entity {
override func verify() -> Bool {
// ID 1320xx51-3
let reFormula = try! NSRegularExpression(pattern: "^20\\d{2}[1-3]\\d{8}$", options: [NSRegularExpression.Options.anchorsMatchLines])
let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
let matchList = reFormula.matches(in: ID, range: matchRange)
if matchList.count != 1 {
return false
}
return true
}
}
class Cat: Entity {
override func verify() -> Bool {
// ID 130000
let reFormula = try! NSRegularExpression(pattern: "^0000\\d{9}$", options: [NSRegularExpression.Options.anchorsMatchLines])
let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
let matchList = reFormula.matches(in: ID, range: matchRange)
if matchList.count != 1 {
return false
}
return true
}
}
// ID 1320xx51-3
// let reFormula = try! NSRegularExpression(pattern: "^20\\d{2}[1-3]\\d{8}$", options: [NSRegularExpression.Options.anchorsMatchLines])
// let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
// let matchList = reFormula.matches(in: ID, range: matchRange)
// if matchList.count != 1 {
// return false
// }
// return true
// ID 13
// let reFormula = try! NSRegularExpression(pattern: "^\\d{13}$", options: [NSRegularExpression.Options.anchorsMatchLines])
// let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
// let matchList = reFormula.matches(in: ID, range: matchRange)
// if matchList.count != 1 {
// return false
// }
// return true
// ID 130000
// let reFormula = try! NSRegularExpression(pattern: "^0000\\d{9}$", options: [NSRegularExpression.Options.anchorsMatchLines])
// let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
// let matchList = reFormula.matches(in: ID, range: matchRange)
// if matchList.count != 1 {
// return false
// }
// return true
//
var fly = Entity()
fly.ID = "0000000099003"
fly.name = "蒼蠅"
fly.verify()
var siuming = Student()
siuming.ID = "2021502233076"
siuming.name = "小明"
siuming.verify()
var siuhung = Student()
siuhung.ID = "2022302191000"
siuhung.name = "小紅"
siuhung.verify()
var policecat = Cat()
policecat.ID = "0000999988222"
policecat.name = "警長"
policecat.verify()

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' buildActiveScheme='true' importAppTypes='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:">
</FileRef>
</Workspace>

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task4.playground#CharacterRangeLen=1501&amp;CharacterRangeLoc=1519&amp;EndingColumnNumber=0&amp;EndingLineNumber=91&amp;StartingColumnNumber=0&amp;StartingLineNumber=42&amp;Timestamp=689914131.681554"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task4.playground#CharacterRangeLen=1501&amp;CharacterRangeLoc=1519&amp;EndingColumnNumber=0&amp;EndingLineNumber=91&amp;StartingColumnNumber=0&amp;StartingLineNumber=42&amp;Timestamp=689914131.68161"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task4.playground#CharacterRangeLen=1501&amp;CharacterRangeLoc=1519&amp;EndingColumnNumber=0&amp;EndingLineNumber=91&amp;StartingColumnNumber=0&amp;StartingLineNumber=42&amp;Timestamp=689914131.6816519"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task4.playground#CharacterRangeLen=1501&amp;CharacterRangeLoc=1519&amp;EndingColumnNumber=0&amp;EndingLineNumber=91&amp;StartingColumnNumber=0&amp;StartingLineNumber=42&amp;Timestamp=689914131.681689"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task4.playground#CharacterRangeLen=1501&amp;CharacterRangeLoc=1519&amp;EndingColumnNumber=0&amp;EndingLineNumber=91&amp;StartingColumnNumber=0&amp;StartingLineNumber=42&amp;Timestamp=689914131.681718"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>Task4 (Playground).xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

@ -0,0 +1,105 @@
import Foundation
enum InsufficientEnergy:Error {
case crazyThursdayVMe50
}
class Entity {
let ID, name: String
init?(id ID: String, name: String) {
self.ID = ID
self.name = name
let reFormula = try! NSRegularExpression(pattern: "^\\d{13}$", options: [NSRegularExpression.Options.anchorsMatchLines])
let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
let matchList = reFormula.matches(in: ID, range: matchRange)
if matchList.count != 1 {
return nil
}
}
convenience init(id ID: String) {
self.init(id: ID, name: "Unknown")!
}
}
class People: Entity {
func work() throws -> Void {
if Int.random(in: 1...7) == 7 {
throw InsufficientEnergy.crazyThursdayVMe50
}
}
}
class Student: People {
override init?(id ID: String, name: String){
super.init(id: ID, name: name)
let reFormula = try! NSRegularExpression(pattern: "^20\\d{2}[1-3]\\d{8}$", options: [NSRegularExpression.Options.anchorsMatchLines])
let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
let matchList = reFormula.matches(in: ID, range: matchRange)
if matchList.count != 1 {
return nil
}
}
}
class Cat: Entity {
override init?(id ID: String, name: String){
super.init(id: ID, name: name)
let reFormula = try! NSRegularExpression(pattern: "^20\\d{2}[1-3]\\d{8}$", options: [NSRegularExpression.Options.anchorsMatchLines])
let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
let matchList = reFormula.matches(in: ID, range: matchRange)
if matchList.count != 1 {
return nil
}
}
}
// ID 1320xx51-3
// let reFormula = try! NSRegularExpression(pattern: "^20\\d{2}[1-3]\\d{8}$", options: [NSRegularExpression.Options.anchorsMatchLines])
// let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
// let matchList = reFormula.matches(in: ID, range: matchRange)
// if matchList.count != 1 {
// return false
// }
// return true
// ID 13
// let reFormula = try! NSRegularExpression(pattern: "^\\d{13}$", options: [NSRegularExpression.Options.anchorsMatchLines])
// let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
// let matchList = reFormula.matches(in: ID, range: matchRange)
// if matchList.count != 1 {
// return false
// }
// return true
// ID 130000
// let reFormula = try! NSRegularExpression(pattern: "^0000\\d{9}$", options: [NSRegularExpression.Options.anchorsMatchLines])
// let matchRange = NSRange(ID.startIndex..<ID.endIndex, in: ID)
// let matchList = reFormula.matches(in: ID, range: matchRange)
// if matchList.count != 1 {
// return false
// }
// return true
var siuziu = Student(id: "2022202121000", name: "小趙")
var siuchow = Student(id: "2000503205613", name: "小周")
var studentList = [siuziu, siuchow]
for item in studentList {
guard item != nil else {
print("Invalid students")
continue
}
do {
var count = 0
while true{
count += 1
try item!.work()
print("Siu Ziu has worked for \(String(count)) \(count <= 1 ? "hour" : "hours").")
}
}
catch InsufficientEnergy.crazyThursdayVMe50 {
print("I'm hungry, please v me 50!")
}
}

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' buildActiveScheme='true' importAppTypes='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:">
</FileRef>
</Workspace>

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task5.playground#CharacterRangeLen=1714&amp;CharacterRangeLoc=1748&amp;EndingColumnNumber=0&amp;EndingLineNumber=105&amp;StartingColumnNumber=0&amp;StartingLineNumber=55&amp;Timestamp=689915757.102043"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task5.playground#CharacterRangeLen=1714&amp;CharacterRangeLoc=1748&amp;EndingColumnNumber=0&amp;EndingLineNumber=105&amp;StartingColumnNumber=0&amp;StartingLineNumber=55&amp;Timestamp=689915757.102255"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task5.playground#CharacterRangeLen=1714&amp;CharacterRangeLoc=1748&amp;EndingColumnNumber=0&amp;EndingLineNumber=105&amp;StartingColumnNumber=0&amp;StartingLineNumber=55&amp;Timestamp=689915757.102429"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task5.playground#CharacterRangeLen=1714&amp;CharacterRangeLoc=1748&amp;EndingColumnNumber=0&amp;EndingLineNumber=105&amp;StartingColumnNumber=0&amp;StartingLineNumber=55&amp;Timestamp=689915757.102581"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task5.playground#CharacterRangeLen=1714&amp;CharacterRangeLoc=1748&amp;EndingColumnNumber=0&amp;EndingLineNumber=105&amp;StartingColumnNumber=0&amp;StartingLineNumber=55&amp;Timestamp=689915757.10271"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/ostrichb/Downloads/Task5.playground#CharacterRangeLen=6&amp;CharacterRangeLoc=2891&amp;EndingColumnNumber=11&amp;EndingLineNumber=83&amp;StartingColumnNumber=5&amp;StartingLineNumber=83&amp;Timestamp=689915757.1028529"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
Loading…
Cancel
Save