import Foundation import Glibc class NamedShape { var name:String var sides:Int=0 init(name:String,sides:Int) { self.name=name self.sides=sides } func simpleDescription()-> String { return "\(name) has \(sides) sides " } } class Shape:NamedShape { var Area:Double=0.0 var len:Double=0 init(name:String,sides:Int,len:Double) { self.len=len; super.init(name:name,sides:sides) } func area(len:Double)->Double { Area=len*len return Area } func show()-> String { let a=area(len:len) return "\(name) has \(sides) sides with \(a) sq m " } } let shape=Shape(name:"square",sides:4,len:10.2) let h=shape.show() print(h)
import Foundation import Glibc func show (name:String, greet:String)->String{ return ("Hello \(name) Good \(greet)") } var H=show(name:"Aryan",greet:"Morning") print(H) func greet(_ person: String, _ day: String) -> String { return "Hello \(person), today is \(day)." } var H1=greet("Bob","Tuesday") print(H1)
let empty = "Hello" let start = empty.startIndex let end = empty.endIndex if start == end { print("Empty string.") } else { print("The string: \(empty) is not empty.") }
import Foundation // déclarer une variable var prenom = "Marion" var nom = "Hartmann" print(prenom + " "+nom) // déclarer une constante let constante = 22 print("age \(constante)") // associer un type à une variable var nombre1 : Int = 3 var prenom1 : String = "Marion"
import Foundation import Glibc var input = "the quick brown fox jumps over the lazy dog" let a = "a".characters let e = "e".characters let i = "i".characters let o = "o".characters let u = "u".characters let consonants = "bcdfghjklmnpqrstvwxyz".characters var aCount = 0 var eCount = 0 var iCount = 0 var oCount = 0 var uCount = 0 var consonantCount = 0 for letter in input.lowercased().characters { if consonants.contains(letter) { consonantCount += 1 } else { if a.contains(letter) { aCount += 1 } if e.contains(letter) { eCount += 1 } if i.contains(letter) { iCount += 1 } if o.contains(letter) { oCount += 1 } if u.contains(letter) { uCount += 1 } } } print("A =", aCount) print("E =",eCount) print("I =",iCount) print("O =",oCount) print("U =",uCount) print("Consonant =",consonantCount)
import Foundation import Glibc let player = ["rock", "paper", "scissors", "lizard", "spock"] srandom(UInt32(NSDate().timeIntervalSince1970)) for count in 1...3 { print(count) } print(player[random() % player.count]);
class Node<T: Equatable> { var value: T? = nil var next: Node? = nil } class LinkedList<T: Equatable> { var head = Node<T>() func insert(value: T) { //find to see if empty list if self.head.value == nil { self.head.value = value } else { //find the last node without a next value var lastNode = self.head while lastNode.next != nil { lastNode = lastNode.next! } //once found, create a new node and connect the linked list let newNode = Node<T>() newNode.value = value lastNode.next = newNode } } func reverse() { var currentNode: Node! = self.head //var currentNode = head; var previousNode = Node<T>() var nextNode = Node<T>() while currentNode.next != nil { print("ffff \(currentNode.value)") nextNode = currentNode.next! currentNode.next = previousNode previousNode = currentNode currentNode = nextNode } //self.head = currentNode.next! head = previousNode; if currentNode.next == nil { self.head = currentNode } } func remove(value: T) { //Check if the value is at the head if self.head.value == value { self.head = self.head.next! } //Traverse the linked list to see if node is in the linked list if self.head.value != nil { var node = self.head var previousNode = Node<T>() //If value found, exit the loop while node.value != value && node.next != nil { previousNode = node node = node.next! } //once found, connect the previous node to the current node's next if node.value == value { if node.next != nil { previousNode.next = node.next } else { //if at the end, the next is nil previousNode.next = nil } } } } func printAllKeys() { var current: Node! = self.head print("---------------") while current != nil && current.value != nil { print("The item is \(current.value!)") current = current.next } } } var myList = LinkedList<Int>() myList.insert(value:100) myList.insert(value:200) myList.insert(value:300) myList.insert(value:400) myList.insert(value:500) //myList.remove(value:100) myList.printAllKeys() myList.reverse() myList.printAllKeys()
import Foundation struct LinkedList<Value> { var head : Node<Value>? var tail : Node<Value>? init() {} func isEmpty() -> Bool { return head == nil } mutating func push(_ value: Value) { head = Node(value: value, next: head) if tail == nil { tail = head } } mutating func append(_ value : Value) { } } class Node<Value> { var value :Value var next : Node? init(value:Value, next:Node? = nil ) { self.value = value self.next = next } } extension LinkedList : CustomStringConvertible { var description : String { guard let head = head else { return " Empty List" } return String(describing: head) } } extension Node : CustomStringConvertible { var description : String { guard let next = next else { return "\(value)" } return "\(value) -> " + String(describing : next) + " " } } var list = LinkedList<Int>(); list.push(1) list.push(2) list.push(3) list.push(5) print(list)
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more