0% found this document useful (0 votes)
53 views7 pages

MDSD Assignment Swift Programming Arshia Thasnim S Cse-A 1517102016

The document contains 16 programs written in Swift. The programs include functions that perform calculations and logical operations on various data types like integers, strings, arrays. They also include functions that manipulate string and array values.

Uploaded by

Ezhil P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views7 pages

MDSD Assignment Swift Programming Arshia Thasnim S Cse-A 1517102016

The document contains 16 programs written in Swift. The programs include functions that perform calculations and logical operations on various data types like integers, strings, arrays. They also include functions that manipulate string and array values.

Uploaded by

Ezhil P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

MDSD Assignment

Swift Programming
Arshia Thasnim S

Cse-A

1517102016
Program 1 :

func sum (a:Int , b:Int) -> Int {


if a == b {
return 3*(a+b)
}else{
return a+b;
}
}

print(sum(a:1,b:2))

Program 2 :

func compute (n:Int) -> Int {


if n > 51 {
return (n-51)*2
}else{
return (51-n)
}
}
print(compute(n:60))

Program 3 :
func compute (a:Int , b:Int) -> Bool {
if (a > 0 && b > 0) {
return false
}
return true
}
print(compute(a:1,b:4))

Program 4 :

func compute (s : String) -> String {


if (s.starts(with:"IS ")) {
return s
}
return "IS " + s
}
print(compute(s:"IS "))
Program 5 :

func compute (st : String) -> String {


var s = st;
let first = s.removeFirst()
let last = s.removeLast()

s = String(last) + s
s.append(first)
return s
}

print(compute(st:"Swift"))

Program 6 :

func compute (a : Int) -> Bool {


if(a%3 == 0 || a%5 == 0){
return true
}
return false
}
print(compute(a:34))

Program : 7

func compute (a : Int , b : Int) -> Int {

let c = abs(a-10)
let d = abs(b-10)
if (c == d){
return 0
}
return c < d ? a : b
}
print(compute(a:9,b:12))

Program 8:
func compute (a : [Int]) -> Bool {

for (index,value) in a.enumerated() {


if(index > 3){
break
}
if value == 7 {
return true
}
}
return false
}

print(compute(a:[1,7,33,5,3]))

Program 9 :

func compute (a : [Int]) -> Bool {

if(a.count > 0){


return ( (a[0] == 5) && (a[a.count-1] == 5) )
}
return false
}
print(compute(a:[5,7,33,5]))

Program 10 :

func compute (a : [Int]) -> Int {

if a.count == 0 {
return 0
}else if a.count == 1{
return a[0]
}else{
return a[0] + a[1]
}

Program 11 :

func compute (a : [Int]) -> [Int] {

if a.count == 0 {
return []
} else{
return [a[0] , a[a.count-1]]
}
}

print(compute(a:[5,7,33]))

Program 12 :

func compute (a : [Int]) -> [Int] {


var result = [Int] (repeating: 0 , count:a.count*2)
result[result.count-1] = a[a.count-1]
return result
}

print(compute(a:[5,7,33]))

Program 13 :
func compute (a : [Int], b: [Int]) -> Int {

let sumOfA = a.reduce(0,+)


let sumOfB = b.reduce(0,+)

return sumOfB > sumOfA ? sumOfB : sumOfA;

print(compute(a:[5,5] ,b:[10,10] ))

Program 14 :

func compute (a : String) -> String {

if a.count < 2 {
return a
}else{
return String(a.prefix(2))
}
}

print(compute(a:"qwe"))
Program 15 :

func compute (a : String , b: String) -> String {

if(a.count > 0 || b.count > 0){


if(a.suffix(1) == b.prefix(1)){
var changedB = b
if(changedB.count > 0){
changedB.removeFirst()
}
return a + changedB
}
}
return a+b
}

print(compute(a:"q",b:"w"))
Program 16 :

func compute (a : String) -> String {

var changeableA = a
if(a.count > 0){
if(a.suffix(1) == "a"){
changeableA.removeLast()
}

if(changeableA.count > 1 && changeableA.prefix(1) == "a"){


changeableA.removeFirst()
}

return changeableA

print(compute(a:""))

Output Prediction

17 . Neck ties are cool


18. Howdy, Jayne
19. ["Sulaco", "Nostromo", "X-Wing", "TIE Fighter"]
20. 128
21. Error
22. Error
23. “55”
24 . Error
25. Error
26.
1
2
3
27. Double
28. Error
29. “Number was 3”
30. Error
31. 32- bit(4 Bytes)
32. Error
33. “Galatica”
34. 0
35. [5,7,9]
36. [1, 1, 2, 2, 3, 3]

37.
1
5
9
13

38. “Bones”
39. Optional(1701)
40. 16
41. “Message one”
42. “Hello, Dave!”
43. 30
44. “Kailee”
45. “Public Relations”
46. Optional(“Barbara”)
47.
0: y
2: i
3: s

48. The length is Still 5 , because the Arrays in Swift are Value Types .
49. The Error is at Line 4 , ‘+’ Operator only takes the Arguments with same type . so
It can be fixed by explicitly Typecasting the Variables op1 , op2 to Double
50. ["cat", "chicken", "dog", "fish"]

You might also like