Skip to content

Commit

Permalink
Minor changes. Add some test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
omalovichko committed Jul 10, 2021
1 parent 1d3ee1a commit 50b924a
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 206 deletions.
2 changes: 1 addition & 1 deletion CodilityLessons.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@
FA3A3DA62220090C0064204D /* In progress */ = {
isa = PBXGroup;
children = (
FA172A5F1EB22C7E00585F28 /* Lesson90_SlalomSkiing.swift */,
FAD7743124CCBF1F00185FBA /* Lesson91_TreeProduct.swift */,
FAD7743324CCBF2600185FBA /* Lesson91_HilbertMaze.swift */,
FA172A5F1EB22C7E00585F28 /* Lesson90_SlalomSkiing.swift */,
FADC82FC22E9F49E0072D77C /* Lesson92_ArrayRecovery.swift */,
);
name = "In progress";
Expand Down
2 changes: 1 addition & 1 deletion CodilityLessonsTests/Lesson11_CountNonDivisible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Lesson11_CountNonDivisible: XCTestCase {
func testPerformance() {
var array = [Int]()
let range = 1...(2 * 50_000)
for _ in 1...5000 {
for _ in 1...3000 {
array.append(Int.random(in: range))
}
measure {
Expand Down
2 changes: 1 addition & 1 deletion CodilityLessonsTests/Lesson12_CommonPrimeDivisors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Lesson12_CommonPrimeDivisors: XCTestCase {
b = [97, 2, 74, 16, 99, 2, 88, 69, 86, 100, 24, 34, 71, 29, 27, 23, 86, 88, 80, 23, 93, 52, 24, 46, 42, 8, 57, 36, 57, 94, 65, 41, 87, 100, 50, 95, 2, 15, 18, 94, 70, 72, 29, 50, 89, 10, 12, 5, 47, 85, 56, 41, 51, 18, 95, 36, 24, 32, 83, 32, 91, 34, 45, 8, 91, 80, 48, 12, 50, 38, 85, 93, 75, 29, 5, 90, 95, 16, 50, 49, 98, 5, 7, 85, 57, 47, 78, 60, 70, 26, 9, 1, 34, 65, 38, 35, 62, 71, 60, 32]

measure {
for _ in 0..<1000 {
for _ in 0..<100 {
XCTAssertEqual(solution(&a, &b), 3)
}
}
Expand Down
2 changes: 1 addition & 1 deletion CodilityLessonsTests/Lesson14_NailingPlanks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Lesson14_NailingPlanks: XCTestCase {

func testPerformance() {
var a = [Int]()
for i in 1...30_000 {
for i in 1...5_000 {
a.append(i)
}
var b = a
Expand Down
2 changes: 1 addition & 1 deletion CodilityLessonsTests/Lesson15_CountTriangles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Lesson15_CountTriangles: XCTestCase {

func testPerformance() {
var a = [Int]()
for i in 0..<1000 {
for i in 0..<200 {
a.append(i)
}
measure {
Expand Down
8 changes: 4 additions & 4 deletions CodilityLessonsTests/Lesson17_MinAbsSum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Lesson17_MinAbsSum: XCTestCase {

func testPerfomance2() {
var a = [Int]()
for _ in 0..<20_000 {
for _ in 0..<2_000 {
a.append(Int.random(in: -100...100))
}
measure {
Expand All @@ -86,16 +86,16 @@ class Lesson17_MinAbsSum: XCTestCase {
}

func testPerfomance3() {
var a = Array(repeatElement(5, count: 10_000))
a.append(contentsOf: Array(repeatElement(42, count: 10_000)))
var a = Array(repeatElement(5, count: 5_000))
a.append(contentsOf: Array(repeatElement(42, count: 5_000)))
a.shuffle()
measure {
_ = self.solution(&a)
}
}

func testPerfomance4() {
var a = Array(repeatElement(4, count: 19_999))
var a = Array(repeatElement(4, count: 9_999))
a.append(1)
measure {
XCTAssertEqual(self.solution(&a), 3)
Expand Down
3 changes: 3 additions & 0 deletions CodilityLessonsTests/Lesson6_MaxProductOfThree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class Lesson6Task2: XCTestCase {
func test() {
var arr = [-3, 1, 2, -2, 5, 6]
XCTAssertEqual(solution(&arr), 60)

arr = [-1, 10, 2, -6, -5, 5, 4]
XCTAssertEqual(solution(&arr), 300)
}

public func solution(_ A : inout [Int]) -> Int {
Expand Down
255 changes: 65 additions & 190 deletions CodilityLessonsTests/Lesson90_SlalomSkiing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,221 +14,96 @@ import XCTest


You are a skier participating in a giant slalom. The slalom track is located on a ski slope, goes downhill and is fenced by barriers on both sides. The barriers are perpendicular to the starting line located at the top of the slope. There are N slalom gates on the track. Each gate is placed at a distinct distance from the starting line and from the barrier on the right-hand side (looking downhill).

You start from any place on the starting line, ski down the track passing as many gates as possible, and finish the slalom at the bottom of the slope. Passing a gate means skiing through the position of the gate.

You can ski downhill in either of two directions: to the left or to the right. When you ski to the left, you pass gates of increasing distances from the right barrier, and when you ski to the right, you pass gates of decreasing distances from the right barrier. You want to ski to the left at the beginning.

Unfortunately, changing direction (left to right or vice versa) is exhausting, so you have decided to change direction at most two times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

The arrangement of the gates is given as an array A consisting of N integers, whose elements specify the positions of the gates: gate K (for 0 ≤ K < N) is at a distance of K+1 from the starting line, and at a distance of A[K] from the right barrier.

For example, consider array A such that:
A[0] = 15
A[1] = 13
A[2] = 5
A[3] = 7
A[4] = 4
A[5] = 10
A[6] = 12
A[7] = 8
A[8] = 2
A[9] = 11
A[10] = 6
A[11] = 9
A[12] = 3

A[0] = 15
A[1] = 13
A[2] = 5
A[3] = 7
A[4] = 4
A[5] = 10
A[6] = 12
A[7] = 8
A[8] = 2
A[9] = 11
A[10] = 6
A[11] = 9
A[12] = 3


The picture above illustrates the example track with N = 13 gates and a course that passes eight gates. After starting, you ski to the left (from your own perspective). You pass gates 2, 3, 5, 6 and then change direction to the right. After that you pass gates 7, 8 and then change direction to the left. Finally, you pass gates 10, 11 and finish the slalom. There is no possible way of passing more gates using at most two changes of direction.

Write a function:

public func solution(_ A : inout [Int]) -> Int
that, given an array A consisting of N integers, describing the positions of the gates on the track, returns the maximum number of gates that you can pass during one ski run.

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:
A[0] = 1
A[1] = 5

A[0] = 1
A[1] = 5
the function should return 2.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [1..100,000];
each element of array A is an integer within the range [1..1,000,000,000];
the elements of A are all distinct.
*/

class Lesson90_SlalomSkiing: XCTestCase {
/*

func test() {
var a = [Int]()

a = [15, 13, 5, 7, 4, 10, 12, 8, 2, 11, 6, 9, 3]
XCTAssertEqual(solution(&a), 8)

a = [1, 10, 4, 2, 7, 5, 9, 8, 6, 3]
XCTAssertEqual(solution(&a), 7)

a = [1, 5]
XCTAssertEqual(solution(&a), 2)
// var a = [Int]()
//
// a = [15, 13, 5, 7, 4, 10, 12, 8, 2, 11, 6, 9, 3]
// XCTAssertEqual(solution(&a), 8)
//
// a = [2, 3, 4, 1, 5, 6, 9, 8, 7, 10]
// XCTAssertEqual(solution(&a), 9)
//
// a = [1, 10, 4, 2, 7, 5, 9, 8, 6, 3]
// XCTAssertEqual(solution(&a), 7)
//
// a = [1, 5]
// XCTAssertEqual(solution(&a), 2)
//
// a = [1, 2, 3, 4, 5]
// XCTAssertEqual(solution(&a), 5)
//
// a = [5, 4, 3, 2, 1]
// XCTAssertEqual(solution(&a), 5)
}

func testMeasure() {
var arr = [Int]()
let count = 3_000
for i in 0..<count {
arr.append(i)
}
measure {
self.solution(&arr)
// XCTAssertEqual(self.solution(&arr), 3000)
}
}
// Time: 0.048 sec
// func testPerformance() {
// var arr = [Int]()
// let gatesCount = 2_000
// for i in 0..<gatesCount {
// arr.append(1_000_000_000 - i)
// arr.append(i)
// }
// measure {
// _ = self.solution(&arr)
// }
// }

public func solution(_ A : inout [Int]) -> Int {
if A.count <= 3 {
guard A.count > 3 else {
return A.count
}

// Left direction
var zeroTurns = [(gate: Int, previousGate: Int)]()
// Right direction
var oneTurn = [Int: (gate: Int, previousGate: Int)]()

var zeroTurnsLeftDirection = [Int: Int]()
var oneTurnRightDirection = [Int: Int]()
var twoTurnsLeftDirection = [Int: Int]()

zeroTurns.append((A[0], 0))

for i in 1..<A.count {
let gate = A[i]

// for route in oneTurn {
// let oneTurnGate = route.key
//
// // can be called once
// if oneTurnGate > gate {
// let index = route.key + 1
//
// if let value = oneTurn[index] {
// oneTurn[index] = max(gate, value)
// } else {
// oneTurn[index] = gate
// }
//
// } else if oneTurnGate < gate {
// let index = route.key
// oneTurn[index] = gate
//// if let value = oneTurn[index] {
//// oneTurn[index] = max(gate, value)
//// } else {
//// oneTurn[index] = gate
//// }
// }
// }

let zeroTurnsCount = zeroTurns.count
for i in 0..<zeroTurns.count {
let zeroTurnsGate = zeroTurns[i]

// can be called once
if zeroTurnsGate.gate < gate {
// Move left
let index = i + 1

// if let value = oneTurn[index], value.previousGate < zeroTurnsGate.gate {
// oneTurn[gate] = (index, gate)
// } else {
// oneTurn[gate] = (index, zeroTurnsGate.gate)
// }

if index == zeroTurnsCount {
zeroTurns.append((gate, zeroTurnsGate.gate))
}
} else if zeroTurnsGate.gate > gate && zeroTurnsGate.previousGate < gate {
// Change current gate
zeroTurns[i] = (gate, zeroTurnsGate.previousGate)
} else {
// Move right
let index = i + 1
let gatesCount = index + 1

if let value = oneTurn[gatesCount] {
if value.gate < gate {
// check if it is possible
oneTurn[gatesCount] = (gate, zeroTurnsGate.gate)
} else if value.gate == gate && value.previousGate < zeroTurnsGate.gate {
// check if it is possible
oneTurn[gatesCount] = (gate, zeroTurnsGate.gate)
}
} else {
oneTurn[gatesCount] = (gate, zeroTurnsGate.gate)
}
}
// else {
// break
// }
}



for route in twoTurnsLeftDirection {
if route.key < gate {
if twoTurnsLeftDirection[gate] == nil {
twoTurnsLeftDirection[gate] = route.value + 1
} else {
twoTurnsLeftDirection[gate] = max(twoTurnsLeftDirection[gate]!, route.value + 1)
}
}
}

for route in oneTurnRightDirection {
if route.key > gate {
if oneTurnRightDirection[gate] == nil {
oneTurnRightDirection[gate] = route.value + 1
} else {
oneTurnRightDirection[gate] = max(oneTurnRightDirection[gate]!, route.value + 1)
}
} else if route.key < gate {
if twoTurnsLeftDirection[gate] == nil {
twoTurnsLeftDirection[gate] = route.value + 1
} else {
twoTurnsLeftDirection[gate] = max(twoTurnsLeftDirection[gate]!, route.value + 1)
}
}
}

for route in zeroTurnsLeftDirection {
if route.key < gate {
if zeroTurnsLeftDirection[gate] == nil {
zeroTurnsLeftDirection[gate] = route.value + 1
} else {
zeroTurnsLeftDirection[gate] = max(zeroTurnsLeftDirection[gate]!, route.value + 1)
}
} else if route.key > gate {
if oneTurnRightDirection[gate] == nil {
oneTurnRightDirection[gate] = route.value + 1
} else {
oneTurnRightDirection[gate] = max(oneTurnRightDirection[gate]!, route.value + 1)
}
}
}

if zeroTurnsLeftDirection[gate] == nil {
zeroTurnsLeftDirection[gate] = 1
}
}

var maxNumberOfGates = 0
twoTurnsLeftDirection.forEach { (key: Int, value: Int) in
maxNumberOfGates = max(maxNumberOfGates, value)
}
return maxNumberOfGates
assertionFailure("Implement")
return 0
}
*/
}
Loading

0 comments on commit 50b924a

Please sign in to comment.