開発環境
- OS X Mavericks - Apple(OS)
- Xcode 6.0 Beta 6
- Swift (プログラミング言語)
Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 9(Repeating Code Using Loops)、9.10(Exercises) 8-a, b, c.をSwiftで考えてみる。
9.10(Exercises) 8-a, b, c.
コード(Xcode)
main.swift
// // main.swift // sample8 // // Created by kamimura on 9/10/14. // Copyright (c) 2014 kamimura. All rights reserved. // import Foundation func makeRandArray() -> [UInt32] { var result:[UInt32] = [] for x in 0..<9 { result.append(arc4random() % 10 + 1) } return result } let rat_1 = makeRandArray() let rat_2 = makeRandArray() println("rat_1 = \(rat_1)") println("rat_2 = \(rat_2)") println("a.") if rat_1[0] > rat_2[0] { println("Rat 1 weighed more than Rat 2 on day 1.") } else { println("Rat 1 weighed less than Rat 2 on day 1.") } println("b.") if rat_1[0] > rat_2[0] && rat_1.indexAt(-1) > rat_2.indexAt(-1) { println("Rat 1 remained heavier than Rat 2.") } else { println("Rat 2 became heavier than Rat 1.") } println("c.") if rat_1[0] > rat_2[0] { if rat_1.indexAt(-1) > rat_2.indexAt(-1) { println("Rat 1 remained heavier than Rat 2.") } else { println("Rat 2 became heavier than Rat 1.") } } else { println("Rat 2 became heavier than Rat 1.") }
array.swift
// // array.swift // array // // Created by kamimura on 8/21/14. // Copyright (c) 2014 kamimura. All rights reserved. // import Foundation extension Array { func indexAt(i:Int) -> T { if i >= 0 { return self[i] } let new_index:Int = self.count + i return self[new_index] } func slice(start:Int = 0, end:Int? = nil) -> Array { var new_start = start >= 0 ? start : self.count + start var new_end:Int if end == nil { new_end = self.count } else if end! >= 0 { new_end = end! } else { new_end = self.count + end! } var result:Array = [] if new_start >= new_end { return [] } for i in new_start..<new_end { result.append(self[i]) } return result } func forEach(f:(T) -> ()) { for e in self { f(e) } } } func sum(nums:[Int]) -> Int { return nums.reduce(0, combine: {(x, y) in x + y}) }
入出力結果(Console Output)
$ ./sample8 rat_1 = [2, 7, 1, 1, 6, 2, 5, 6, 2] rat_2 = [7, 9, 5, 3, 7, 10, 6, 1, 3] a. Rat 1 weighed less than Rat 2 on day 1. b. Rat 2 became heavier than Rat 1. c. Rat 2 became heavier than Rat 1. $ ./sample8 rat_1 = [3, 6, 8, 8, 5, 9, 4, 4, 8] rat_2 = [9, 3, 2, 10, 10, 10, 3, 9, 3] a. Rat 1 weighed less than Rat 2 on day 1. b. Rat 2 became heavier than Rat 1. c. Rat 2 became heavier than Rat 1. $ ./sample8 rat_1 = [1, 9, 1, 3, 2, 8, 5, 2, 5] rat_2 = [3, 8, 8, 1, 2, 10, 7, 8, 5] a. Rat 1 weighed less than Rat 2 on day 1. b. Rat 2 became heavier than Rat 1. c. Rat 2 became heavier than Rat 1. $ ./sample8 rat_1 = [1, 7, 1, 6, 6, 4, 3, 9, 10] rat_2 = [2, 8, 6, 1, 10, 10, 4, 6, 4] a. Rat 1 weighed less than Rat 2 on day 1. b. Rat 2 became heavier than Rat 1. c. Rat 2 became heavier than Rat 1. $ ./sample8 rat_1 = [9, 9, 6, 2, 2, 4, 2, 9, 1] rat_2 = [7, 2, 8, 3, 9, 7, 9, 1, 2] a. Rat 1 weighed more than Rat 2 on day 1. b. Rat 2 became heavier than Rat 1. c. Rat 2 became heavier than Rat 1. $
0 コメント:
コメントを投稿