/
Correction_Swift.swift
233 lines (193 loc) · 6.1 KB
/
Correction_Swift.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// SwiftCorrection.swift
// HowToPracticeALanguage
//
// Created by P1kachu on 14/09/14.
//
// Reviewed by:
// - Imbavirus
// - (null)
// import Foundation
// Part One:
// Declare two variables: an integer named "age", and a string named "name" with corresponding values (your name and age)
var age:Int = 19
var name:String = "Stan"
// Print the following sentence in the console "You are NAME and you are AGE years old !". Don't forget to add a newline at the end
print("You are \(name) and you are \(age) years old ! \n")
// Create a new string variable called "hello" which value is "Hello ". Add "name" at the end of "hello" (Concatenation) then print it
var hello:String = "Hello "
hello += name
print(hello)
// create a new string array called "shoppingList", with three elements of your choice. Create an int variable containing the number of
// elements in "shoppingList" (using a function of the array/using the array)
var shoppingList:[String] = ["some milk", "a Chevy Camaro", "a life"]
var nbOfElemts:Int = shoppingList.count
// Create a simple for-loop for an integer "i" going from 1 to 10 that print the value of "i"
for i in 1...10
{
print(i)
}
// Create a for loop that iterate through "shoppingList" and prints each element with "You have to buy (elemt)".
for j in 0..<nbOfElemts
{
print("You have to buy \(shoppingList[j])")
}
// Do the same with a foreach-loop.
for elemt in shoppingList // In Swift, foreach-loops ar for[...]in loops.
{
print("You have to buy \(elemt)")
}
// Modify the first for-loop (with i from 1 to 10) such that it prints "(value of i) is even" when "i" is divisible
// by 2 (You may want to learn more about "modulo" (%)). Else, print "(value of i) is odd".
for i in 1...10
{
if i%2 == 0
{
print("\(i) is even")
}
else
{
print("\(i) is odd")
}
}
// Create a string variable called "element" with the value of your choice. Then create a for-loop/foreach-loop that checks if "shoppingList"
// contains "element". If yes, print "You have to buy (value of element) !", and stop the loop (search how to stop a loop).
// If not, print "Nope, you don't need (value of "element")".
let element:String = "a Chevy Camaro"; //Declaring a constant
for elemt in shoppingList
{
if elemt == element
{
print("You have to buy \(elemt)!")
break
}
else
{
print("Nope, you don't need \(elemt)")
}
}
//FIXME: "contains" function missing (could be in included file but couldn't test that on SwiftStub
//You can also do it without looping with the "contains" function:
/*if (contains(shoppingList,element))
{
print("You have to buy \(element)!")
}
else
{
print("Nope, you don't need \(element)")
}*/
// Part Two:
// Create a function that returns nothing and which doesn't takes any parameter. It should just be named "TriForce"
// and print the TriForce symbol (one triangle over two other ones, can be found on internet) with "TRIFORCE"
// Don't forget to call the function !
func TriForce() -> Void // When returning void, you can just write func TriForce(){ // Do stuff } without returning type
{
print(" /\\ ")
print(" /__\\ ")
print(" /\\ /\\ ")
print("/__\\/__\\ ")
print("TRIFORCE")
}
TriForce() //Calling the function
// Create a function that takes a string as parameter and returns "Hello (value of string) !"
func Hello(yourName:String) -> String
{
return "Hello \(yourName)!"
}
print(Hello("Stan"))
// Create a function that takes two integers as parameters and returns the addition of these two.
// You can do the same with multiplication, subtraction and division.
func Addition(a: Int, b: Int) -> Int
{
return a + b
}
func Subtraction(a:Int, b:Int) -> Int
{
return a - b
}
func Multiplication(a:Int, b:Int) -> Int
{
return a * b
}
func Division(a:Int, b:Int) -> Int
{
return a / b
}
print(Addition(5, b: 12))
print(Subtraction(5, b: 12))
print(Multiplication(5, b: 12))
print(Division(5, b: 12)) // returns 0 because "a" and "b" are Integers
print(Division(10, b: 2))
// FIXME: User entry
// Create a string variable that takes what the user enter in the console as value. Then print "You entered (value of string)"
// At this point, the Swift programming language doessn't have any easy function to get the user input,
// so you may skip this one. Here is a way to do it (Works in XCode6b5):
/*func input() -> String
{
var keyboard = NSFileHandle.fileHandleWithStandardInput()
var inputData = keyboard.availableData
return NSString(data: inputData, encoding:NSUTF8StringEncoding)
}
var userInput:String = input()
print("Uou entered \(userInput)")
*/
// Create a while loop that takes a number and divides it by 2 until it is less than 3
var number = 57
while number > 3
{
number = number/2
// can be done with number /= 2
print("\(number) ")
}
//print()
// Do the same with a do-while loop
var number2 = 526
repeat{ //Do..while is repeat..while in swift
number2 /= 2
print("\(number2) ")
} while number2 > 3;
//print()
// FIXME: Random generator
// Create a function that returns a random number
func rdm() -> Int
{
return Int(arc4random())
}
// FIXME: Random generator with bounds
// Create another function that returns a random number between two bounds given as parameters.
func rdmBounds(a:UInt32, b:UInt32) -> Int
{
return Int(arc4random_uniform(b - a) + a)
}
for i in 0...10
{
print((rdmBounds(0, b: 10)))
}
//print()
// FIXME: Multi-dimensionnal array
// Create a two dimensionnal int array of 3 columns and 3 rows. Use 2 for-loops to add a random number
// between 1 and 9 in each of the 9 rooms.
// You may use one of the two previously created function.
// Then print them such that they appear like this (with [x1,x9] being the 9 random integers):
// {x1,x2,x3,}
// {x4,x5,x6,}
// {x7,x8,x9,}
var multiArray:[[Int]] = [[0,0,0],[0,0,0],[0,0,0]] //easy Initialization
for i in 0..<multiArray.count
{
for j in 0..<multiArray[i].count
{
multiArray[i][j] = rdmBounds(1, b: 10)
}
}
for elemnt in multiArray
{
print("{")
for numbers in elemnt
{
print("\(numbers),")
}
print("},")
}
// TODO: Switch
//