Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //: Playground - noun: a place where people can play
- func divisibile(n1:Int, n2:Int)->Bool?{
- if n1 % n2 == 0{
- return true
- }
- return nil
- }
- let num1 = 957
- let num2 = 3
- if let safeNum = divisibile(n1: num1, n2: num2){
- print("\(num1) is divisible by \(num2)")
- }else{
- print(String(num1)+" is not divisible by le palle "+String(num2))
- }
- func bisestile (n1: Int)->Bool?{
- if n1 % 400 == 0 || n1 % 4 == 0 && n1 % 100 != 0 {
- return true
- }
- return nil
- }
- let num3 = 9980
- if let safeNum = bisestile(n1: num3){
- print("\(num3) è bisestile")
- }else{
- print("\(num3) è non bisestile")
- }
- func ripetute (str: String)->Bool{
- var j:Int
- let charString = Array(str.characters)
- for i in 0...charString.count - 1 {
- j = i + 1
- while j < charString.count {
- if charString[i] == charString[j]{
- return true
- }
- j = j + 1
- }
- }
- return false
- }
- if ripetute(str: "precipitevolissivevolmente"){
- print("Ci sono lettere ripetute")
- }else{
- print("Non ci sono lettere ripetute")
- }
- struct Rectangle{
- var width: Int
- var height: Int
- var area: Int
- init(width: Int, height: Int){
- self.width = width
- self.height = height
- self.area = width * height
- }
- }
- extension Rectangle{
- func fitsInside(_ other : Rectangle)-> Bool{
- return (width < other.width) && (height < other.height)
- }
- func isGreaterThan(_ otherRect: Rectangle) -> Bool{
- return (self.area > otherRect.area)
- }
- }
- var rectangle1 = Rectangle( width: 2, height: 5)
- var rectangle2 = Rectangle( width: 30, height: 20)
- print("il rettangolo con altezza\(rectangle1.width),e larghezza \(rectangle1.height), ha l' area di \(rectangle1.area)")
- print("il rettangolo con altezza\(rectangle2.width),e larghezza \(rectangle2.height), ha l' area di \(rectangle2.area)")
- if !rectangle2.fitsInside(rectangle1){
- print("il secondo rettangolo non è contenuto nel primo")
- }else{
- print("il secondo rettangolo è contenuto nel primo")
- }
- if rectangle1.isGreaterThan(rectangle2) {
- print("L'area del primo rettangolo è maggiore di quella del secondo")
- } else {
- print("L'area del secondo rettangolo è maggiore di quella del primo")
- }
- class Fish{
- var name: String
- var type: String
- init(_ name: String){
- self.name = name
- self.type = "Fish"
- }
- func swim(){
- print("I'm " + name + ", a " + type + " who is swimming!")
- }
- }
- class FlyingFish: Fish{
- override init(_ name: String){//SOVRASCRIVO L'INIT DELLA CLASSE CREATA PRECEDENTEMENTE
- super.init(name)
- self.type = "Flying Fish"
- }
- func fly(){
- print("I'm " + name + ", a " + type + " who is flying!")
- }
- }
- var roger = Fish("Roger")
- var gaetano = FlyingFish("Gaetano")
- roger.swim()
- gaetano.fly()
- gaetano.swim()
- enum Alegnment{
- case left, right, up, down
- }
- let textAlignment = Alegnment.left
- switch textAlignment{
- case .left:
- print("sono a sinistra")
- case .right:
- print("sono a destra")
- case .up:
- print("sono sopra")
- case .down:
- print("sono sotto")
- //default:
- // print("non ci sono")
- }
- class Triangolo{
- enum TipoTriangolo{
- case isoscele, scaleno, equilatero, generico
- }
- var lato1 : Int
- var lato2 : Int
- var lato3 : Int
- var tipo: TipoTriangolo?
- init(lato1: Int, lato2: Int, lato3: Int){
- self.lato1 = lato1
- self.lato2 = lato2
- self.lato3 = lato3
- self.tipo = getTriangoloTipo(lato1, lato2, lato3)
- }
- func getTriangoloTipo(_ lato1: Int, _ lato2: Int, _ lat3: Int)-> TipoTriangolo{
- if (lato1 == lato2 && lato1 != lato3 && lato2 != lato3) || (lato1 != lato2 && lato1 == lato3 && lato2 != lato3) || (lato1 != lato2 && lato1 != lato3 && lato2 == lato3){
- return TipoTriangolo.isoscele
- }
- else if lato1 != lato2 && lato2 != lato3 && lato1 != lato3{
- return TipoTriangolo.scaleno
- }
- else if lato1 == lato2 && lato2 == lato3{
- return TipoTriangolo.equilatero
- }
- return TipoTriangolo.generico
- }
- }
- var triangolo = Triangolo(lato1: 4, lato2: 4, lato3: 4)
- if let safeTr = triangolo.tipo{
- print(safeTr)
- }
Add Comment
Please, Sign In to add comment