Skip to content

Commit

Permalink
Add FibFrog task
Browse files Browse the repository at this point in the history
  • Loading branch information
omalovichko committed Jun 21, 2019
1 parent 90dd8ae commit 40cf324
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CodilityLessons.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
FA1F2A3C1E954154002437F3 /* Lesson6_NumberOfDiscIntersections.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA1F2A3B1E954154002437F3 /* Lesson6_NumberOfDiscIntersections.swift */; };
FA2999C8223AC6FE00F30C39 /* Lesson10_Flags.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA2999C7223AC6FE00F30C39 /* Lesson10_Flags.swift */; };
FA2A07E81EB3384700951C8A /* Lesson12_ChocolatesByNumbers.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA2A07E71EB3384700951C8A /* Lesson12_ChocolatesByNumbers.swift */; };
FA35586A22B579060096503A /* Lesson13_Ladder.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA35586922B579060096503A /* Lesson13_Ladder.swift */; };
FA35586C22B5792F0096503A /* Lesson13_FibFrog.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA35586B22B5792F0096503A /* Lesson13_FibFrog.swift */; };
FA3B90CE22B508F700EC941E /* Lesson11_CountSemiprimes.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA3B90CD22B508F700EC941E /* Lesson11_CountSemiprimes.swift */; };
FA3B90D022B53A3800EC941E /* Lesson11_CountNonDivisible.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA3B90CF22B53A3800EC941E /* Lesson11_CountNonDivisible.swift */; };
FA54888120ED6F4E00AB4CA5 /* Lesson17_MinAbsSum.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA54888020ED6F4E00AB4CA5 /* Lesson17_MinAbsSum.swift */; };
Expand Down Expand Up @@ -112,6 +114,8 @@
FA1F2A3B1E954154002437F3 /* Lesson6_NumberOfDiscIntersections.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Lesson6_NumberOfDiscIntersections.swift; sourceTree = "<group>"; };
FA2999C7223AC6FE00F30C39 /* Lesson10_Flags.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Lesson10_Flags.swift; sourceTree = "<group>"; };
FA2A07E71EB3384700951C8A /* Lesson12_ChocolatesByNumbers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Lesson12_ChocolatesByNumbers.swift; sourceTree = "<group>"; };
FA35586922B579060096503A /* Lesson13_Ladder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Lesson13_Ladder.swift; sourceTree = "<group>"; };
FA35586B22B5792F0096503A /* Lesson13_FibFrog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Lesson13_FibFrog.swift; sourceTree = "<group>"; };
FA3B90CD22B508F700EC941E /* Lesson11_CountSemiprimes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Lesson11_CountSemiprimes.swift; sourceTree = "<group>"; };
FA3B90CF22B53A3800EC941E /* Lesson11_CountNonDivisible.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Lesson11_CountNonDivisible.swift; sourceTree = "<group>"; };
FA54888020ED6F4E00AB4CA5 /* Lesson17_MinAbsSum.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Lesson17_MinAbsSum.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -342,6 +346,8 @@
FA3A3DB1222009E00064204D /* Lesson 13 Fibonacci numbers */ = {
isa = PBXGroup;
children = (
FA35586922B579060096503A /* Lesson13_Ladder.swift */,
FA35586B22B5792F0096503A /* Lesson13_FibFrog.swift */,
);
name = "Lesson 13 Fibonacci numbers";
sourceTree = "<group>";
Expand Down Expand Up @@ -537,6 +543,7 @@
FA1F2A321E953C66002437F3 /* Lesson4_MaxCounters.swift in Sources */,
FA1F2A361E953C66002437F3 /* Lesson5_GenomicRangeQuery.swift in Sources */,
FA1F2A3C1E954154002437F3 /* Lesson6_NumberOfDiscIntersections.swift in Sources */,
FA35586C22B5792F0096503A /* Lesson13_FibFrog.swift in Sources */,
FA8EF88A1EAF872F00B0523E /* Lesson15_CountDistinctSlices.swift in Sources */,
FA172A601EB22C7E00585F28 /* Lesson90_SlalomSkiing.swift in Sources */,
FA03AEB41EA60D5800663D2F /* Lesson99_TreeHeight.swift in Sources */,
Expand All @@ -548,6 +555,7 @@
FA6A4DB41E9E400D00C9BA31 /* Lesson7_StoneWall.swift in Sources */,
FA6A4DB21E9E3FF900C9BA31 /* Lesson7_Nesting.swift in Sources */,
FA6A4DB81E9E660000C9BA31 /* Lesson8_Dominator.swift in Sources */,
FA35586A22B579060096503A /* Lesson13_Ladder.swift in Sources */,
FA2A07E81EB3384700951C8A /* Lesson12_ChocolatesByNumbers.swift in Sources */,
FA1F2A2C1E953C66002437F3 /* Lesson3_FrogJmp.swift in Sources */,
FA8EF8881EAF7F9900B0523E /* Lesson15_AbsDistinct.swift in Sources */,
Expand Down
174 changes: 174 additions & 0 deletions CodilityLessonsTests/Lesson13_FibFrog.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
//
// Lesson13_FibFrog.swift
// CodilityLessonsTests
//
// Created by Oleksandr Malovichko on 15.06.2019.
//

import XCTest

/*
FibFrog
Count the minimum number of jumps required for a frog to get to the other side of a river.


The Fibonacci sequence is defined using the following recursive formula:

F(0) = 0
F(1) = 1
F(M) = F(M - 1) + F(M - 2) if M >= 2
A small frog wants to get to the other side of a river. The frog is initially located at one bank of the river (position −1) and wants to get to the other bank (position N). The frog can jump over any distance F(K), where F(K) is the K-th Fibonacci number. Luckily, there are many leaves on the river, and the frog can jump between the leaves, but only in the direction of the bank at position N.

The leaves on the river are represented in an array A consisting of N integers. Consecutive elements of array A represent consecutive positions from 0 to N − 1 on the river. Array A contains only 0s and/or 1s:

0 represents a position without a leaf;
1 represents a position containing a leaf.
The goal is to count the minimum number of jumps in which the frog can get to the other side of the river (from position −1 to position N). The frog can jump between positions −1 and N (the banks of the river) and every position containing a leaf.

For example, consider array A such that:

A[0] = 0
A[1] = 0
A[2] = 0
A[3] = 1
A[4] = 1
A[5] = 0
A[6] = 1
A[7] = 0
A[8] = 0
A[9] = 0
A[10] = 0
The frog can make three jumps of length F(5) = 5, F(3) = 2 and F(5) = 5.

Write a function:

public func solution(_ A : inout [Int]) -> Int
that, given an array A consisting of N integers, returns the minimum number of jumps by which the frog can get to the other side of the river. If the frog cannot reach the other side of the river, the function should return −1.

For example, given:

A[0] = 0
A[1] = 0
A[2] = 0
A[3] = 1
A[4] = 1
A[5] = 0
A[6] = 1
A[7] = 0
A[8] = 0
A[9] = 0
A[10] = 0
the function should return 3, as explained above.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [0..100,000];
each element of array A is an integer that can have one of the following values: 0, 1.
*/

class Lesson13_FibFrog: XCTestCase {

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

a = [0]
XCTAssertEqual(solution(&a), 1)

a = [1, 1, 0, 0, 0]
XCTAssertEqual(solution(&a), 2)

a = [0, 0, 0, 0, 1]
XCTAssertEqual(solution(&a), 2)

a = [0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0]
XCTAssertEqual(solution(&a), 3)
}

func testAllOnesPerformance() {
var a = [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,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1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1]

measure {
for _ in 0..<50 {
XCTAssertEqual(solution(&a), 3)
}
}
}

func testPerformance() {
var a = Array(repeating: 1, count: 5000)

measure {
_ = solution(&a)
}
}

public func solution(_ A : inout [Int]) -> Int {
// Find leafs
var leafPositions = Set<Int>()
for i in 0..<A.count {
if A[i] == 1 {
leafPositions.insert(i)
}
}
// Add other side of the river
let otherSideOfTheRiver = A.count
leafPositions.insert(otherSideOfTheRiver)

// https://www.wolframalpha.com/input/?i=fib%5B4%5D
// "Binet's formula"
let fibonacci: (_ n: Int) -> Int = { n in
let n = Double(n)
let dividend = pow(((1 + sqrt(5)) / 2), n) - pow(((1 - sqrt(5)) / 2), n)
let divisor = sqrt(5)
let quotient = dividend / divisor
return Int(quotient)
}

// Calculate possible jump distances
var fibNumbers = Set<Int>()
var currentFibNumber = 1
var n = 3
let maxFibNumber = otherSideOfTheRiver + 1
repeat {
fibNumbers.insert(currentFibNumber)
currentFibNumber = fibonacci(n)
n += 1
} while currentFibNumber <= maxFibNumber

// key: position, value: number of jumps
var paths = [Int: Int]()

// Add start position
paths[-1] = 0

repeat {
for path in paths {
let count = path.value
let position = path.key

for fibNumber in fibNumbers {
let newPosition = fibNumber + position
if leafPositions.contains(newPosition) {

let newCount = count + 1

if newPosition == otherSideOfTheRiver {
return newCount
}

if let existedCount = paths[newPosition] {
if newCount < existedCount {
paths[newPosition] = newCount
}
} else {
paths[newPosition] = newCount
}
}
}
paths[position] = nil
}
} while paths.count > 0

return -1
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ __Lesson 10 - Prime and composite numbers__
* Flags: Find the maximum number of flags that can be set on mountain peaks.

__Lesson 11 - Sieve of Eratosthenes__
* CountSemiprimes: Count the semiprime numbers in the given range [a..b]
* CountSemiprimes: Count the semiprime numbers in the given range [a..b].
* CountNonDivisible: Calculate the number of elements of an array that are not divisors of each element.

__Lesson 12 - Euclidean algorithm__
* ChocolatesByNumbers: There are N chocolates in a circle. Count the number of chocolates you will eat.
* CommonPrimeDivisors: Check whether two numbers have the same prime divisors.

__Lesson 13 - Fibonacci numbers__
* ~~FibFrog: Count the minimum number of jumps required for a frog to get to the other side of a river.~~
* ~~Ladder: Count the number of different ways of climbing to the top of a ladder.~~
* FibFrog: Count the minimum number of jumps required for a frog to get to the other side of a river.

__Lesson 14 - Binary search algorithm__
* ~~NailingPlanks: Count the minimum number of nails that allow a series of planks to be nailed.~~
Expand Down

0 comments on commit 40cf324

Please sign in to comment.