Skip to content

Commit

Permalink
Format files
Browse files Browse the repository at this point in the history
  • Loading branch information
meatball133 committed Jan 2, 2025
1 parent 43d8ff9 commit 93a1df0
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,58 @@ import Numerics

struct ComplexNumbers: Equatable {

var real: Double
var imaginary: Double

init(realComponent: Double, imaginaryComponent: Double? = 0) {
real = realComponent
imaginary = imaginaryComponent ?? 0
}

func add(complexNumber: ComplexNumbers) -> ComplexNumbers {
ComplexNumbers(realComponent: real + complexNumber.real, imaginaryComponent: imaginary + complexNumber.imaginary)
}


func sub(complexNumber: ComplexNumbers) -> ComplexNumbers {
ComplexNumbers(realComponent: real - complexNumber.real, imaginaryComponent: imaginary - complexNumber.imaginary)
}

func mul(complexNumber: ComplexNumbers) -> ComplexNumbers {
ComplexNumbers(realComponent: real * complexNumber.real - imaginary * complexNumber.imaginary, imaginaryComponent: real * complexNumber.imaginary + imaginary * complexNumber.real)
}

func div(complexNumber: ComplexNumbers) -> ComplexNumbers {
let denominator = complexNumber.real * complexNumber.real + complexNumber.imaginary * complexNumber.imaginary
let realComponent = (real * complexNumber.real + imaginary * complexNumber.imaginary) / denominator
let imaginaryComponent = (imaginary * complexNumber.real - real * complexNumber.imaginary) / denominator
return ComplexNumbers(realComponent: realComponent, imaginaryComponent: imaginaryComponent)
}

func absolute() -> Double {
sqrt(Double(real * real + imaginary * imaginary))
}

func conjugate() -> ComplexNumbers {
ComplexNumbers(realComponent: real, imaginaryComponent: -imaginary)
}

func exponent() -> ComplexNumbers {
let expReal = exp(Double(real)) * cos(Double(imaginary))
let expImaginary = exp(Double(real)) * sin(Double(imaginary))
return ComplexNumbers(realComponent: expReal, imaginaryComponent: expImaginary)
}

static func == (lhs: ComplexNumbers, rhs: ComplexNumbers) -> Bool {
lhs.real.isApproximatelyEqual(to: rhs.real, absoluteTolerance: 0.0001) && lhs.imaginary.isApproximatelyEqual(to: rhs.imaginary, absoluteTolerance: 0.0001)
}
}
var real: Double
var imaginary: Double

init(realComponent: Double, imaginaryComponent: Double? = 0) {
real = realComponent
imaginary = imaginaryComponent ?? 0
}

func add(complexNumber: ComplexNumbers) -> ComplexNumbers {
ComplexNumbers(
realComponent: real + complexNumber.real,
imaginaryComponent: imaginary + complexNumber.imaginary)
}

func sub(complexNumber: ComplexNumbers) -> ComplexNumbers {
ComplexNumbers(
realComponent: real - complexNumber.real,
imaginaryComponent: imaginary - complexNumber.imaginary)
}

func mul(complexNumber: ComplexNumbers) -> ComplexNumbers {
ComplexNumbers(
realComponent: real * complexNumber.real - imaginary * complexNumber.imaginary,
imaginaryComponent: real * complexNumber.imaginary + imaginary * complexNumber.real)
}

func div(complexNumber: ComplexNumbers) -> ComplexNumbers {
let denominator =
complexNumber.real * complexNumber.real + complexNumber.imaginary * complexNumber.imaginary
let realComponent =
(real * complexNumber.real + imaginary * complexNumber.imaginary) / denominator
let imaginaryComponent =
(imaginary * complexNumber.real - real * complexNumber.imaginary) / denominator
return ComplexNumbers(realComponent: realComponent, imaginaryComponent: imaginaryComponent)
}

func absolute() -> Double {
sqrt(Double(real * real + imaginary * imaginary))
}

func conjugate() -> ComplexNumbers {
ComplexNumbers(realComponent: real, imaginaryComponent: -imaginary)
}

func exponent() -> ComplexNumbers {
let expReal = exp(Double(real)) * cos(Double(imaginary))
let expImaginary = exp(Double(real)) * sin(Double(imaginary))
return ComplexNumbers(realComponent: expReal, imaginaryComponent: expImaginary)
}

static func == (lhs: ComplexNumbers, rhs: ComplexNumbers) -> Bool {
lhs.real.isApproximatelyEqual(to: rhs.real, absoluteTolerance: 0.0001)
&& lhs.imaginary.isApproximatelyEqual(to: rhs.imaginary, absoluteTolerance: 0.0001)
}
}
42 changes: 22 additions & 20 deletions exercises/practice/complex-numbers/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
import PackageDescription

let package = Package(
name: "ComplexNumbers",
products: [
.library(
name: "ComplexNumbers",
targets: ["ComplexNumbers"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-numerics", from: "1.0.2"),
],
targets: [
.target(
name: "ComplexNumbers",
dependencies: [
.product(name: "Numerics", package: "swift-numerics"),
]),
.testTarget(
name: "ComplexNumbersTests",
dependencies: ["ComplexNumbers",
.product(name: "Numerics", package: "swift-numerics"),]),
]
name: "ComplexNumbers",
products: [
.library(
name: "ComplexNumbers",
targets: ["ComplexNumbers"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-numerics", from: "1.0.2")
],
targets: [
.target(
name: "ComplexNumbers",
dependencies: [
.product(name: "Numerics", package: "swift-numerics")
]),
.testTarget(
name: "ComplexNumbersTests",
dependencies: [
"ComplexNumbers",
.product(name: "Numerics", package: "swift-numerics"),
]),
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Poker {
if rank == .highCard || rank == .straightFlush || rank == .straight || rank == .flush {
return hands.max { hand1, hand2 in
let isFlush = rank == .straight || rank == .straightFlush

func highestCard(_ hand: [String]) -> String {
return hand.max { card1, card2 in
convertValue(card1, isFlush) < convertValue(card2, isFlush)
Expand All @@ -137,7 +137,7 @@ class Poker {
var hand2High = highestCard(hand2)

var hand1 = hand1
var hand2 = hand2
var hand2 = hand2

while convertValue(hand1High) == convertValue(hand2High) {
hand1 = hand1.filter { $0 != hand1High }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
struct Position : Equatable {
let row: Int
let column: Int
struct Position: Equatable {
let row: Int
let column: Int

static func ==(lhs: Position, rhs: Position) -> Bool {
return lhs.row == rhs.row && lhs.column == rhs.column
}
static func == (lhs: Position, rhs: Position) -> Bool {
return lhs.row == rhs.row && lhs.column == rhs.column
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
struct SaddlePoints {
static func saddlePoints(_ matrix: [[Int]]) -> [Position] {
var saddlePoints = [Position]()
for (rowIndex, row) in matrix.enumerated() {
for (columnIndex, element) in row.enumerated() {
if isSaddlePoint(matrix, rowIndex, columnIndex) {
saddlePoints.append(Position(row: rowIndex + 1, column: columnIndex + 1))
}
}
static func saddlePoints(_ matrix: [[Int]]) -> [Position] {
var saddlePoints = [Position]()
for (rowIndex, row) in matrix.enumerated() {
for (columnIndex, element) in row.enumerated() {
if isSaddlePoint(matrix, rowIndex, columnIndex) {
saddlePoints.append(Position(row: rowIndex + 1, column: columnIndex + 1))
}
return saddlePoints
}
}
return saddlePoints
}

static private func isSaddlePoint(_ matrix: [[Int]], _ rowIndex: Int, _ columnIndex: Int) -> Bool {
let element = matrix[rowIndex][columnIndex]
let row = matrix[rowIndex]
let column = matrix.map { $0[columnIndex] }
return row.allSatisfy { $0 <= element } && column.allSatisfy { $0 >= element }
}
static private func isSaddlePoint(_ matrix: [[Int]], _ rowIndex: Int, _ columnIndex: Int) -> Bool
{
let element = matrix[rowIndex][columnIndex]
let row = matrix[rowIndex]
let column = matrix.map { $0[columnIndex] }
return row.allSatisfy { $0 <= element } && column.allSatisfy { $0 >= element }
}
}
30 changes: 15 additions & 15 deletions exercises/practice/saddle-points/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
import PackageDescription

let package = Package(
name: "SaddlePoints",
products: [
.library(
name: "SaddlePoints",
targets: ["SaddlePoints"]),
],
dependencies: [],
targets: [
.target(
name: "SaddlePoints",
dependencies: []),
.testTarget(
name: "SaddlePointsTests",
dependencies: ["SaddlePoints"]),
]
name: "SaddlePoints",
products: [
.library(
name: "SaddlePoints",
targets: ["SaddlePoints"])
],
dependencies: [],
targets: [
.target(
name: "SaddlePoints",
dependencies: []),
.testTarget(
name: "SaddlePointsTests",
dependencies: ["SaddlePoints"]),
]
)

0 comments on commit 93a1df0

Please sign in to comment.