Coder Social home page Coder Social logo

swiftcombination's Introduction

SwiftCombination

Ruby's "combination", "permutation" in Swift.

let combos = combination([0, 1, 2], length: 2)
print(combos) // => [[0, 1], [0, 2], [1, 2]]

let rCombos = repeatedCombination([0, 1, 2], length: 2)
print(rCombos) // => [[0, 0], [0, 1], [0, 2], [1, 1], [1, 2], [2, 2]]



let a = [1, 2, 3, 4]

print(combination(a, length: 1)) // => [[1], [2], [3], [4]]
print(combination(a, length: 2)) // => [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
print(combination(a, length: 3)) // => [[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
print(combination(a, length: 4)) // => [[1, 2, 3, 4]]
print(combination(a, length: 0)) // => [[]]
print(combination(a, length: 5)) // => []



// Array method

print(a.combination(3))
// => [[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]



// with closure

a.combination(2){ print($0) }
// =>
// [1, 2]
// [1, 3]
// [1, 4]
// [2, 3]
// [2, 4]
// [3, 4]



let chrs = ["h", "e", "l", "o", "!"]
chrs.repeatedCombination(6){ combo in
    if combo.joined(separator: "") == "hello!" { print(combo) }
}
// => [h, e, l, l, o, !]



// => permutation

var chars = ["a", "c", "t"]

//chars.permutation(3){ println($0 }
// => shorthand
chars.permutation{ print($0) }
// =>
// [a, c, t]
// [a, t, c]
// [c, a, t]
// [c, t, a]
// [t, a, c]
// [t, c, a]



// repeated permutation

[0, 1].repeatedPermutation(4){ print($0) }
// =>
// [0, 0, 0, 0]
// [0, 0, 0, 1]
// [0, 0, 1, 0]
// [0, 0, 1, 1]
// [0, 1, 0, 0]
// [0, 1, 0, 1]
// [0, 1, 1, 0]
// [0, 1, 1, 1]
// [1, 0, 0, 0]
// [1, 0, 0, 1]
// [1, 0, 1, 0]
// [1, 0, 1, 1]
// [1, 1, 0, 0]
// [1, 1, 0, 1]
// [1, 1, 1, 0]
// [1, 1, 1, 1]

Installation

Swift Package Manager

Create a Package.swift file.

import PackageDescription

let package = Package(
    name: "FooProject",
    targets: [],
    dependencies: [
        .Package(url: "https://github.com/all-user/SwiftCombination.git", versions: "0.2.0"..<"0.3.0")
    ]
)

And, Use as follows.

import SwiftCombination

swiftcombination's People

Contributors

all-user avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

swiftcombination's Issues

permutation encountered argument type error when given a closure

class FooBar {
    static func baz() {
        permutation([0, 1]) { print($0) } // Cannot convert value of type '(Any) -> ()' to expected argument type 'Int?'
    }
}
class FooBar {
    static func baz() {
        permutation([0, 1], length: 2) { print($0) } // Extra argument 'length' in call
    }
}

repeatedPermutation encountered argument type error when given a closure

class FooBar {
    static func baz() {
        repeatedPermutation([0, 1]) { print($0) } // Cannot convert value of type '(Any) -> ()' to expected argument type 'Int?'
    }
}
class FooBar {
    static func baz() {
        repeatedPermutation([0, 1], length: 2) { print($0) } // Extra argument 'length' in call
    }
}

combination, repeatedCombination, repeatedPermutation should not be zero length arguments

Theses methods in Ruby are not allow zero length arguments.

arr = [0,1]
arr.combination
# => ArgumentError: wrong number of arguments (given 0, expected 1)
arr.repeated_combination
# => ArgumentError: wrong number of arguments (given 0, expected 1)
arr.repeated_permutation
# => ArgumentError: wrong number of arguments (given 0, expected 1)
arr.permutaion
# => [[0, 1], [1, 0]]
# permutation allow zero length argument.

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.