port overlort python ->Swift assed edition!

fukurou

the supreme coder
ADMIN
Swift:
/// Provides tools for generating string permutations from multiple lists.
class CombinatoricalUtils {
    private(set) var result: [String] = []

    /// Internal recursive function to generate permutations.
    private func generatePermutationsHelper(_ lists: [[String]], _ result: inout [String], _ depth: Int, _ current: String) {
        if depth == lists.count {
            result.append(current)
            return
        }
        for item in lists[depth] {
            generatePermutationsHelper(lists, &result, depth + 1, current + item)
        }
    }

    /// Public entry to generate permutations between nested string lists
    func generatePermutations(_ lists: [[String]]) {
        result.removeAll()
        generatePermutationsHelper(lists, &result, 0, "")
    }

    /// Alternate version using variadic parameters (similar to Python's *args)
    func generatePermutationsV2(_ lists: [String]...) {
        generatePermutations(lists)
    }
}
 

fukurou

the supreme coder
ADMIN
Swift:
/// Simulates a "Night Rider" LED display that moves position up and down in a bounce pattern.
class AXNightRider {
    private var mode: Int = 0
    private var position: Int = 0
    private var lim: Int = 0
    private var direction: Int = 1

    init(limit: Int) {
        if limit > 0 {
            self.lim = limit
        }
    }

    /// Set the maximum number of LED positions
    func setLim(_ lim: Int) {
        self.lim = lim
    }

    /// Set animation mode (more modes can be added later)
    func setMode(_ mode: Int) {
        if mode >= 0 && mode < 10 {
            self.mode = mode
        }
    }

    /// Compute and return the current LED position
    func getPosition() -> Int {
        switch mode {
        case 0:
            mode0()
        default:
            break
        }
        return position
    }

    /// Classic "ping-pong" animation: 0 → n → 0
    private func mode0() {
        position += direction

        if direction < 1 {
            if position < 1 {
                position = 0
                direction = 1
            }
        } else {
            if position > lim - 1 {
                position = lim
                direction = -1
            }
        }
    }
}
 
Top