Giter Club home page Giter Club logo

codility-lesson-swift's Introduction

Codility Lessons Solutions in Swift 3

Hey, just for fun. Some solutions for Codility lesson for my reference or may benefit to others

Lesson 3 - FrogJmp

public func solution(_ X : Int, _ Y : Int, _ D : Int) -> Int {
    
    if X == Y { return 0 }
    
    var count = 0
    
    if (Y - X) % D == 0 {
        count = (Y - X) / D
    }
    else {
        count = (Y - X) / D + 1
    }
    
    return count
}

Lesson 3 - PermMissingElem

public func solution(_ A : inout [Int]) -> Int {
    
    let n = A.count + 1 // total elements including missing number
    
    // https://math.stackexchange.com/questions/2260/proof-for-formula-for-sum-of-sequence-123-ldotsn
    let sumN = n * (n + 1) / 2 // well know formula to get sums of sequence number from 1 .. N
    
    let sumA = A.reduce(0, +)
    
    return sumN - sumA // subtract and the balance is missing number
}

Lesson 4 - FrogRiverOne

public func solution(_ X : Int, _ A : inout [Int]) -> Int {
    // write your code in Swift 3.0 (Linux)
    
    var arr = Array(repeating: 0, count: X)
    
    let expectedSum = X * (X + 1) / 2
    
    var sum = 0
    for (i, num) in A.enumerated() {

        if arr[num - 1] == 0 {
            arr[num - 1] = num
            sum += num
            if sum == expectedSum {
                return i
            }
        }
    }
 
    return -1
}

codility-lesson-swift's People

Contributors

xmhafiz avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.