Lists

This page is under construction. Please come back later.
#!/usr/bin/env swift;
import Foundation
import Utils
func main() -> Void {
let NUM_SQUARES:Int = 5
var squares:[Int] = []
// Put the squares into the list
for i:Int in 0..<NUM_SQUARES {
squares.append(i * i)
}
// Print out the squares from the list
for i:Int in 0..<squares.count {
print(Utils.format("{0:d}^2 = {1:d}", i, squares[i]))
}
print(Utils.listToString(squares))
exit(EXIT_SUCCESS)
}
main()
Output
0^2 = 0
1^2 = 1
2^2 = 4
3^2 = 9
4^2 = 16
[0, 1, 4, 9, 16]
#!/usr/bin/env swift;
import Foundation
import Utils
func main() -> Void {
var squares:[Int] = [0, 1, 4, 9, 16, 25]
// Print out the squares from the list
for i:Int in 0..<squares.count {
print(Utils.format("{0:d}^2 = {1:d}", i, squares[i]))
}
exit(EXIT_SUCCESS)
}
main()
Output
Lists2.swift:6:6: warning: variable 'squares' was never mutated; consider changing to 'let' constant
4 |
5 | func main() -> Void {
6 | var squares:[Int] = [0, 1, 4, 9, 16, 25]
| `- warning: variable 'squares' was never mutated; consider changing to 'let' constant
7 |
8 | // Print out the squares from the list
0^2 = 0
1^2 = 1
2^2 = 4
3^2 = 9
4^2 = 16
5^2 = 25
#!/usr/bin/env swift;
import Foundation
func main() -> Void {
var names:[String] = ["Fred", "Wilma", "Barney", "Betty"]
for name in names {
print("Hello, " + name + "!")
}
names = ["Harry", "Ron", "Hermione"]
for name in names {
print("Hello, " + name + "!")
}
exit(EXIT_SUCCESS)
}
main()
Output
Hello, Fred!
Hello, Wilma!
Hello, Barney!
Hello, Betty!
Hello, Harry!
Hello, Ron!
Hello, Hermione!
#!/usr/bin/env swift;
import Foundation
import Utils
func main() -> Void {
// Print out the command line arguments
for i:Int in 1...(CommandLine.arguments.count - 1) {
print(String(i) + ":" + CommandLine.arguments[i])
}
exit(EXIT_SUCCESS)
}
main()
Output
1:Fred
2:Barney
3:Wilma
4:Betty
1:10
2:20
3:30
4:40
#!/usr/bin/env swift;
import Foundation
import Utils
func main() -> Void {
var names:[String] = ["Fred", "Wilma", "Barney", "Betty"]
// Print out the name based on command line argument 1-4
for i:Int in 1...(CommandLine.arguments.count - 1) {
let x:Int = Int(CommandLine.arguments[i]) ?? 0
print(String(i) + ":" + names[x - 1])
}
exit(EXIT_SUCCESS)
}
main()
Output
Lists5.swift:6:6: warning: variable 'names' was never mutated; consider changing to 'let' constant
4 |
5 | func main() -> Void {
6 | var names:[String] = ["Fred", "Wilma", "Barney", "Betty"]
| `- warning: variable 'names' was never mutated; consider changing to 'let' constant
7 |
8 | // Print out the name based on command line argument 1-4
1:Fred
2:Wilma
3:Barney
4:Betty
Lists5.swift:6:6: warning: variable 'names' was never mutated; consider changing to 'let' constant
4 |
5 | func main() -> Void {
6 | var names:[String] = ["Fred", "Wilma", "Barney", "Betty"]
| `- warning: variable 'names' was never mutated; consider changing to 'let' constant
7 |
8 | // Print out the name based on command line argument 1-4
1:Betty
2:Barney
3:Wilma
4:Fred
Lists5.swift:6:6: warning: variable 'names' was never mutated; consider changing to 'let' constant
4 |
5 | func main() -> Void {
6 | var names:[String] = ["Fred", "Wilma", "Barney", "Betty"]
| `- warning: variable 'names' was never mutated; consider changing to 'let' constant
7 |
8 | // Print out the name based on command line argument 1-4
1:Fred
2:Barney
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Array Last Element
- Central Limit Theorem
- Chromatic Scale Frequencies
- Income Tax (Revisited)
- Morse Code
- Renard Numbers (Preferred Numbers)
- Sample Mean and Standard Deviation
- Sieve of Eratosthenes
References
- [[Swift Community]]
- [[Swift Language Guide]]
- [[Swift Language Reference]]
- [[Swift Programming Language]], Apple Inc.
- [[Swift Doc]]
- [[We Heart Swift]]
- [[Swift Cookbook]]
- [[Swift Playground]]
- [[Swift at TutorialsPoint]]
- [[Hacking with Swift]]
Pure Programmer


