lala14 3
lala14 3
import Foundation
sum(1,2)
sum(1.2, 2.0)
let a: CGFloat = 12
let b: CGFloat = 2.3sf
sum(a,b)
fsfs
struct Stack<T> {
private var elements = [T]()
(element)
}
func printStack() {
for i in stride(from: elements.count - 1, through:
0, by: -1) {
print("----\n| \(elements[i]) |")
}
print("-----------")
}
}
var
struct PankCake{}
var stacker = Stack<PankCake>()
stacker.push(PankCake())
stacker.push(PankCake())
stacker.push(PankCake())
stacker.push(PankCake())
stacker.push(PankCake())
stacker.push(PankCake())
stacker.printStack()
extension Stack {
var topItem: T? {
return(elements.isEmpty ? nil :
elements[elements.count - 1])
}
}
}
var hashMap = HashMap<Int, String>()
hashMap.setValue("25", forKey: 1)
hashMap.setValue("30", forKey: 2)
hashMap.getValue(forKey: 1)
let score = 10
let reallyBig = 10__00__0_000
print(reallyBig)
var counter = 10
counter += 5
counter *= 2
counter *= 3
isAuthenticated = !isAuthenticated
print(isAuthenticated)
gameOver.toggle()
print(gameOver)
let firstPart = "Hello, "
let secondPart = "world!"
let numbers = 11
let missionMessage = "Appollo \( String(numbers))
landed on the moon."
// switch value.self {
// case one:
// return one.rawValue
// case two:
// return two.rawValue
// case three:
// return three.rawValue
// case four:
// return four.rawValue
//
// }
}
print(beatles[value.three.rawValue])
print(temperatures[2])
print(number12[1])
beatles.append("Utsav")
print(beatles)
beatles.remove(at: 3)
print(beatles)
beatles.append(contentsOf: ["one", "two"])
print(beatles)
extension Array {
mutating func appendGeneric<T>(_ value: T) {
if let castedValue = value as? Element {
self.append(castedValue)
} else {
print("Type mismatch : Cannot append \
(type(of: value))to array of \(Element.self)")
}
}
}
//extension Array {
// mutating func appendGeneric<T>(_ value: T) {
// // Check if the array type matches the type of
the value being appended
// if let castedValue = value as? Element {
// self.append(castedValue)
// } else {
// print("Type mismatch: Cannot append \
(type(of: value)) to array of \(Element.self)")
//
// }
// }
//}
intArray.appendGeneric(4)
print("After appending 4: \(intArray)")
temperatures.appendGeneric(34.0)
print("After appending 34.0 : \(temperatures)")
temperatures.append("Bonjour")
print(temperatures)
archEnemies["Batman"] = "Peter"
archEnemies
selected = "Tuesday"
selected = "January"
enum Weekday {
case monday
case tuesday
case wednesday
case thursday
case friday, saturday, sunday
}
print(log10(10.0))
print(log10(50.0))
for i in 1..<100 {
let counter = 0.0
dict24[i] = log10(Double(i))
print("The logarithmic table for number \(i) is \
(log10(Double(i) + counter))")
}
var myContent7 = [String]()
for i in 1...12 {
print("The \(i) times table")
for j in 1...12 {
print(" \(j) x \(i) is \(j * i)")
myContent7.append(" \(j) x \(i) is \(j * i)")
}
}
myContent7
do {
// write the contents to the file
try content.description.write(to: fileURL,
atomically: true, encoding: .utf8)
try multContent.description.write(to: fileURL7,
atomically: true, encoding: .utf8)
print("File written successfully at path: \
(fileURL.path)")
} catch {
// Handle error if something goes wrong
print("Failed to write to file : \
(error.localizedDescription)")
}
} else {
print("Could not write into the document directory")
}
enum UIStyle {
case light, dark, system
}
let scored = 85
if scored > 80 {
print("Great job")
let speed = 88
let percentage = 85
let aged = 18
if speed >= 88 {
print("Where we're going we don't need roads.")
}
if percentage < 85 {
print("Sorry, you failed the text")
}
if aged >= 18 {
print("You're eligible to vote")
}
number7.append(4)
if number7.count > 2 {
number7.remove(at: 0)
}
print(number7)
if usernamed.isEmpty {
usernamed = "Anonymous"
}
print("Welcome, \(usernamed)!")
let age4 = 16
if age4 >= 18 {
print("You can vote in the next election")
}
if age4 <= 18 {
print("Sorry , you are too young to vote")
}
if a11 {
print("Code to run if a is true")
} else if b11 {
print("Code to run if b is true and a is false ")
} else {
print("Code to run if a and b are both false")
}
let temp = 25
if temp > 20 && temp < 30{
print("It's a nice day.")
}
let userAge = 04
let hasParentalConsent = false
enum TransportOption {
case airplane, helicopter, bicycle, car
}
enum Weather {
case sun, rain, wind, snow, unknown
}
switch forecast {
case .snow:
print("It's a snowy day")
case .sun:
print("It's a sunny day ")
case .rain:
print("It's a rainy day ")
case .wind:
print("It's a windy day ")
case .unknown:
print("It is unknown")
switch place {
case "Metropolis":
print("you are a batman")
case "Mega-City One":
print("You are a judge")
default:
print("Who are you?")
}
let daylight = 3
print("My true love gave to me!")
switch daylight {
case 5:
print("5 golden rings")
fallthrough
case 4:
print("4 calling birds")
fallthrough
case 3:
print("3 french hens")
fallthrough
case 2:
print("2 turtle doves")
fallthrough
default:
print("A pear tree")
}
let age2 = 18
let canVote = age2 >= 18 ? "Yes" : "No"
print(canVote)
let hour = 23
print(hour < 12 ? "It's before noon" : "midnight")
enum Theme {
case light, dark
}
for os in platform {
print("Swift works great on \(os).")
}
for i in 1...5 {
print("Counting from 1 through 5: \(i)")
}
for i in 1..<5 {
print("Counting from 1 through 5: \(i)")
}
print(lyric)
var countdown = 10
while countdown > 0 {
print("\(countdown )...")
countdown -= 1
}
print("Blast off!")
let id = Int.random(in: 1...1000)
let amount = Double.random(in: 0...1)
var roll = 0
while roll != 20 {
roll = .random(in: 1...20)
print("I rolled a \(roll)")
}
print("Critical hit")
//print(multiples)
//let number1 = 3
//let number2 = 5
//
//for i in 1...100 {
// if i.isMultiple(of: 3) && i.isMultiple(of: 5) {
// print("The number \(i) is ------ fizzbuzz")
// }
// else if i.isMultiple(of: 5){
// print("The number \(i) is ------ buzz")
// }
// else if i.isMultiple(of: 3) {
// print("The number \(i) is ------ fizz")
// }
// else {
// print("The number \(i) is ------ \(i)")
// }
//}
doMath()
doMoreMath()
greeter(name: "Kumar")
greeter(name: "Taylor Swift")
func getUser8() -> (first: String , last: String) {
return (first: "Taylor", last: "Swift")
}
//open("photo.jpg", "recipes.txt")
}
payment34()
animate(duration: 3) {
print("Fade out the image")
}
changeSpeed(34)
return current
}
print(sum1)
print(Int.random(in: 1...10))
print(getRandomNumber())
}
return function
}
//print(makeRandomGenerator())
repeat {
newNumber = Int.random(in: 1...10)
} while newNumber == previousNumber
previousNumber = newNumber
return newNumber
}
}
let generator1 = makeRandomNumberGenerator()
for _ in 1...10 {
print(generator1())
}
struct Users5 {
var name: String
var age: Int
var city: String
}
print("string".count)
print("string".hasPrefix("Hello"))
print("string".uppercased())
print("string".sorted())
struct Abuser {
var name: String
var yearsActive = 0
}
struct Employer {
var name: String
var yearsActive = 0
extension Employer {
init() {
self.name = "Anonymous"
print("Creating an anonymous employee")
}
}
struct Student {
var name: String
var bestFriend: String
// lazy
struct Unwrap {
static let appURL = "https://itunes.apple.com"
}
//Unwrap.appURL
struct SomeStruct {
private static var entropy = Int.random(in: 1...10)
static func getEntropy() -> Int {
entropy += 1
return entropy
}
}
//print(SomeStruct.entropy)
print(SomeStruct.getEntropy())
// Day 12
protocol Purchaseable {
var name: String {get set }
// var price: Int { get set }
}
extension Purchaseable {
func buy(_ item: Purchaseable) {
print("I'm buying \(item.name)")
}
}
protocol Product {
var price: Double { get set }
var weight: Int { get set }
}
enum num {
case one
case two
case three
case four
case five
case six
case seven
case eight
case nine
case zero
}
myNums[num.two]