The Swift Programming Language Book Chapter Section 控制流实验 3

Posted

技术标签:

【中文标题】The Swift Programming Language Book Chapter Section 控制流实验 3【英文标题】:The Swift Programming Language Book Chapter Section on Control Flow Experiment 3 【发布时间】:2014-06-03 22:02:28 【问题描述】:

我正在阅读本书的第一章,但无法弄清楚实验:

添加另一个变量来跟踪哪个数字是 最大的,以及最大的数字是多少。

这是书中的代码:

let interstingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13, 17],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 9, 16, 25, 36],
]
var largest = 0
for (kind, numbers) in interstingNumbers 
    for number in numbers 
        if number > largest 
            largest = number
        
    

largest

【问题讨论】:

【参考方案1】:

像这样:

let interstingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13, 17],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 9, 16, 25, 36],
]
var largest = 0
var largestKind = ""
for (kind, numbers) in interstingNumbers 
    for number in numbers 
        if number > largest 
            largest = number
            largestKind = kind
        
    

largest
largestKind

【讨论】:

非常感谢 :) @user3704869 您还可以考虑使用元组将最大数字和种类存储在一种复合类型中。试试看吧。 非常感谢您的提示【参考方案2】:

所以基本上你想在将number 放入largest 的同时将kind 放入某个地方,例如到一个名为largestKind的变量中。

【讨论】:

【参考方案3】:

要求跟踪值和类型的问题不仅仅是显示最终答案的类型。 我认为这更接近:

let interestingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 9, 16, 25],
]

var largest = 0
var largestType = [String]()
var largestValues = [Int]()
for (kind, numbers) in interestingNumbers 
    for number in numbers 
        if number > largest 
            largest = number
            largestValues.append(largest)
            largestType.append(kind)
        
    


print(largest)
print(largestType)
print(largestValues)

现在有了元组:

let interestingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 9, 16, 25],
]

var largest = 0
var largestValues: [(type: String, value: Int)] = []
for (kind, numbers) in interestingNumbers 
    for number in numbers 
        if number > largest 
            largest = number
            largestValues.append(type: kind, value: number)
        
    


print(largest)
print(largestValues)

【讨论】:

元组变体是最干净的恕我直言。感谢这个例子!【参考方案4】:
let interestingNumbers = [ "Prime"     : [2,3,5,7,11,13],
                           "Fibonacci" : [1,1,2,3,5,8,13],
                           "Square"    : [1,4,9,16,25,36],
                         ]

var largest = 0
var largestKind = ""

for (kind, numbers) in interestingNumbers 
    for number in numbers 
        if number > largest 
            largest = number
            largestKind = kind
        
     


print(largest, largestKind)

outputs : 36 Sqaure

【讨论】:

【参考方案5】:
let interestingNumbers = [
      "Prime": [2, 3, 5, 7, 11, 13],
      "Fibonacci": [1, 1, 2, 3, 5, 8],
      "Square": [1, 4, 9, 16, 25],
]

 var largest = 0
 var largestNumbers = [
     "Prime":0,
     "Fibonacci":0,
     "Square":0,
]


for (kind, numbers) in interestingNumbers 

  for number in numbers 

    if number > largest 
        largest = number
    

    if(number > largestNumbers[kind]!)
        largestNumbers[kind] = number;
    



print(largest)
print(largestNumbers)


Result is:
25
["Fibonacci": 8, "Square": 25, "Prime": 13]

【讨论】:

以上是关于The Swift Programming Language Book Chapter Section 控制流实验 3的主要内容,如果未能解决你的问题,请参考以下文章

The Swift Programming Language 中文翻译版

Swift基础学习《The Swift Programming Language 中文版》前言部分代码示例

读书笔记The Swift Programming Language (Swift 4.0.3)

读书笔记The Swift Programming Language (Swift 4.0.3)

读书笔记The Swift Programming Language (Swift 4.0.3)

《The Swift Programming Language》的笔记-第28页