Giter Site home page Giter Site logo

codewarschallenges's Introduction

CodeWars Code Challenges

This page represents my daily code challanges which focuse on:

  • Swift programming language.
  • Dart programming language (occasionally).

My current rank:

rank


Organization:

  • There is one main playground which holds many sub folders.
  • Each supfolder represents a new challenge.
  • Originally they were numbered by days, then by dates, and now I just organize them by its title and difuculty raiting for clearer context.
  • Each file has a title and kata rating (kyu) (8 being easiet, 1 being the hardest).

Each page contains:

  • Brief description of a problem.
  • My solution to a challenge.
  • Some other clever solutions made by other users.
  • XCTests.

Playground page structure:

Section1: Title with link to the challenge, example, and my approach.

// Gravity flip - 8kyu
// Link: https://www.codewars.com/kata/5f70c883e10f9e0001c89673

// Objective: Given direction of ether Left (L) or Right (R) sort the array (a) in corresponding order.

// Example:
// 'R', [3, 2, 1, 2]      ->  [1, 2, 2, 3]
// 'L', [1, 4, 5, 3, 5 ]  ->  [5, 5, 4, 3, 1]

// My approach:
// 1. Switch on the direction
// 2. Base on the direction, use sorted(by:) function to sort element
// 3. Return those sorted elements or empty array if something goes wrong.

Section2: My solution.

// MARK: - My Solution

func flip(_ direction: String, _ a: [Int]) -> [Int] {
  switch direction {
    case "L":
    return a.sorted(by: >)
    case "R" :
    return a.sorted()
    
  default:
      return []
  }
}

Section3: Other Celever solutions.

// MARK: - Other Solutions:

 func flip2(_ direction: String, _ a: [Int]) -> [Int] {
    return a.sorted(by: direction == "L" ? (>) : (<))
 }
 
 func flip3(_ direction: String, _ a: [Int]) -> [Int] {
     return direction == "R" ? a.sorted() : a.sorted().reversed()
 }

Section4: Tests.

// MARK: - Tests

import XCTest

class GravityFlipTests: XCTestCase {
    
    func testRightFlip() {
        XCTAssertEqual(flip("R", [3, 2, 1, 2]), [1, 2, 2, 3])
        XCTAssertEqual(flip2("R", [3, 2, 1, 2]), [1, 2, 2, 3])
        XCTAssertEqual(flip3("R", [3, 2, 1, 2]), [1, 2, 2, 3])
    }
    
    func testLeftFlip() {
        XCTAssertEqual(flip("L", [1, 4, 5, 3, 5]), [5, 5, 4, 3, 1])
        XCTAssertEqual(flip2("L", [1, 4, 5, 3, 5]), [5, 5, 4, 3, 1])
        XCTAssertEqual(flip3("L", [1, 4, 5, 3, 5]), [5, 5, 4, 3, 1])
    }
    
    func testInvalid() {
        XCTAssertEqual(flip("X", []), [])
        XCTAssertEqual(flip2("X", []), [])
        XCTAssertEqual(flip3("X", []), [])
    }
    
}

GravityFlipTests.defaultTestSuite.run()

Checkout Code Wars for yourself.

codewarschallenges's People

Contributors

xwrobelekx avatar

Watchers

James Cloos 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.