2.1.1、数据类型练习

//: Playground - noun: a place where people can play

importUIKit

//1、var string="my name is Tina"将string按字母顺序排序

varstring="my name is Tina"

//let stringArray = string.characters.sorted {$0 < $1}

//print(stringArray)

//var stringRange = ""

//for character in stringArray

//{

//stringRange.append(character)

//}

//print(stringRange)

//print(string)

vararrayStrs:Array = []

for(index,char)instring.characters.enumerated() {

arrayStrs.append(String(char))

}

print(arrayStrs)

letcmp:(_a:String,_b:String) ->Int= {

(a,b)in

letx = a.compare(b)

ifx == .orderedAscending{//升序

return-1

}elseifx == .orderedSame{//相同

return0

}else{

return1

}

}

cmp("b","a")

foriin1..

forjin0..

ifcmp(arrayStrs[j],arrayStrs[j+1]) ==1{

lettemp =arrayStrs[j]

arrayStrs[j] =arrayStrs[j+1]

arrayStrs[j+1] = temp

}

}

}

varnewResult:String=""

foriinarrayStrs{

newResult+=i

}

print(arrayStrs,newResult)

//2、var bigArray=[4,6,1,3,2,5]进行排序,用三种方法

varresultArray = [String]()

varbigArray=[4,6,1,3,2,5]

//bigArray.sort()

foriin1..

{

forjin0..< (bigArray.count- i)

{

ifbigArray[j] >bigArray[j +1]

{

swap(&bigArray[j], &bigArray[j +1])

//let temp = bigArray[j]

//bigArray[j] = bigArray[j + 1]

//bigArray[j + 1] = temp

}

}

}

print(bigArray)

//3、写一个九九乘法表

/*

1 * 1 = 1

1 * 2 = 22 * 2 = 4

1 * 3 = 32 * 3 = 63 * 3 = 9

1 * 4 = 42 * 4 = 83 * 4 = 124 * 4 = 16

1 * 5 = 52 * 5 = 103 * 5 = 154 * 5 = 205 * 5 = 25

1 * 6 = 62 * 6 = 123 * 6 = 184 * 6 = 245 * 6 = 306 * 6 = 36

1 * 7 = 72 * 7 = 143 * 7 = 214 * 7 = 285 * 7 = 356 * 7 = 427 * 7 = 49

1 * 8 = 82 * 8 = 163 * 8 = 244 * 8 = 325 * 8 = 406 * 8 = 487 * 8 = 568 * 8 = 64

1 * 9 = 92 * 9 = 183 * 9 = 274 * 9 = 365 * 9 = 456 * 9 = 547 * 9 = 638 * 9 = 729 * 9 = 81

*/

foriin1...9{

forjin1...9{//j代表列,i代表行

//print("\(i)*\(j)=\(i*j) ",terminator:"")

ifi>=j {

print("\(j)*\(i)=\(i*j) ",terminator:"")

}

ifj ==9{

print("\n")

}

}

}

//var num = 1

////result为结果

//var result = ""

////外循环循环9次row为行数

//for row in 1...9{

////tmp临时存放结果,外循环执行一次重置一次tmp

//var tmp = ""

////内循环循环num次

//for row in 1..

//tmp = tmp + "\(row)*\(num)=\(row*num)\t"

//

//}

////拼接结果

//result =tmp + "\(row)*\(num)=\(row*num)"

//print(result)

////外循环执行一次需要出现结果的个数+1

//num += 1

//}

//4、编写一个函数可以计算其参数的平均值

funcaverage(a:Double...) ->Any{

varsum:Double=0

foraina{

sum += a

}

returnsum /Double(a.count)

}

average(a:1,2,3,4,5,6)

//5、定义一个数组,写一个函数,传任意数组中的两个元素,可以实现交换数组中的两个元素

vararray = [5,4,3,2,1,0]

swap(&array[0], &array[4])

funcexchangeData(array:Array, a:Int, b:Int) ->Array{

vararray = array

letdata = array[a]

array[a] = array[b]

array[b] = data

returnarray

}

exchangeData(array:array, a:4, b:0)

varoldArray = [5,4,3,2,1,0]

funcjiaohuan (array:Array, a:Int, b:Int) -> (Array){

varneArray = array

swap(&neArray[a],&neArray[b])

returnneArray

}

varanswer:Array=jiaohuan(array:oldArray, a:0, b:5)

funcswapData(_nums:inout[Int],_p:Int,_q:Int) {

lettemp = nums[p]

nums[p] = nums[q]

nums[q] = temp

}

swapData(&oldArray,5,0)

funcswap2(_nums:inout[T],_p:Int,_q:Int) {

(nums[p], nums[q]) = (nums[q], nums[p])

}

swap2(&oldArray,1,3)

//6、将数组[“A”,”B”,”C”]转换为字符串A:B:C

lettimu = ["A","B","C"]

//let zhuanhuan = timu.joined(separator: ":")

vars =""

foriintimu{

ifi !="C"{

s+=String("\(i):")

}else{

s+=String("\(i)")

}

}

print(s)

//6、将课堂上讲的朋友圈的三个数组(用了字典、元组、数组),用所学到的知识,单独获取到“小程序”这个字符串值

//元组

letdataArray1 = [[(image:"0",name:"朋友圈")],[(image:"1",name:"扫一扫"),(image:"2",name:"摇一摇")],[(image:"3",name:"附近的人"),(image:"4",name:"漂流瓶")],[(image:"5",name:"购物"),(image:"6",name:"游戏")],[(image:"7",name:"小程序")]]

letoneSection1 =dataArray1[4]

letoneRow1 =oneSection1[0]

letstring1 =oneRow1.name

print(string1)

//数组

letdataArray2 = [[["0","朋友圈"]],[["1","扫一扫"],["2","摇一摇"]],[["3","附近的人"],["4","漂流瓶"]],[["5","购物"],["6","游戏"]],[["7","小程序"]]]

letoneSection2 =dataArray2[4]

letoneRow2 =oneSection2[0]

letstring2 =oneRow2[1]

print(string2)

//字典

letdataArray3 = [[["image":"0","name":"朋友圈"]],[["image":"1","name":"扫一扫"],["image":"2","name":"摇一摇"]],[["image":"3","name":"附近的人"],["image":"4","name":"漂流瓶"]],[["image":"5","name":"购物"],["image":"6","name":"游戏"]],[["image":"7","name":"小程序"]]]

letoneSection3 =dataArray3[4]

letoneRow3 =oneSection3[0]

letstring3 =oneRow3["name"]

print(string3??"name值不存在")

你可能感兴趣的:(2.1.1、数据类型练习)