makifay / codility

My C++ solutions to the Lessons section of Codility

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Codility Solutions

Lessons

Here are my solutions to the codility lessons. You can find the lessons here.

Details Legend: D = Difficulty; TS = Task Score; C = Completeness; P = Performance; TC = Time Complexity

Task - Solution Details
Lesson 1 - Iterations
BinaryGap.cpp
Lesson 2 - Arrays
CyclicRotation.cpp
OddOccurencesInArray.cpp
Lesson 3 - Time Complexity
PermMissingElem.cpp
FrogJmp - FrogJmp.cpp
TapeEquilibrium.cpp
Lesson 4 - Counting Elements
PermCheck.cpp
FrogRiverOne.cpp
MaxCounters.cpp
MissingInteger.cpp
Lesson 5 - Prefix Sums
PassingCars.cpp
GenomicRangeQuery.cpp
MinAvgTwoSlice.cpp
CountDiv.cpp
Lesson 6 - Sorting
Distinct.cpp
Triangle.cpp
MaxProductOfThree.cpp
NumberOfDiscIntersections
Lesson 7 - Stacks and Queues
Brackets.cpp
Nesting.cpp
StoneWall.cpp
Fish.cpp
Lesson 8 - Leader
EquiLeader.cpp
Dominator.cpp
Lesson 9 - Maximum Slice Problem
MaxSliceSum.cpp
MaxProfit.cpp
MaxDoubleSliceSum
Lesson 10 - Prime and Composite Numbers
CountFactors.cpp
MinPerimeterRectangle.cpp
Peaks
Flags
Lesson 11 - Sieve Of Eratosthenes
CountNonDivisible.cpp
CountSemiprimes
Lesson 12 - Euclidean Algorithm
ChocolatesByNumbers.cpp
CommonPrimeDivisors
Lesson 13 - Fibonacci Numbers
FibFrog.cpp
Ladder
Lesson 14 - Binary Search Algorithm
MinMaxDivision.cpp
NailingPlanks.cpp
Lesson 15 - Caterpillar Method
AbsDistinct.cpp
CountDistinctSlices.cpp
CountTriangles.cpp
MinAbsSumOfTwo
Lesson 16 - Greedy Algorithms
MaxNonoverlappingSegments.cpp
TieRopes.cpp
Lesson 17 - Dynamic Programming
NumberSolitaire
MinAbsSum

Tests

Assuming you are using cmake, tests will be located in your $BUILD_DIR}/bin directory. All tests are in the same codility binary. Individual tests can be run per the googletest framework documentation. Here is a quick reference:

/path/to/build/bin> codility --help
/path/to/build/bin> codility --gtest_list_tests
/path/to/build/bin> codility --gtest_filter=TestName.Test
/path/to/build/bin> codility --seed 12345

Dependencies

Test are built using the following packages. Use CMake and the dependencies will be downloaded and built for you.

Building

You can build the tests for your favorite development environment using cmake. e.g.:

  • For the command line (I prefer to put all build artifacts in a separate directory):
mkdir test/build
cd test/build
cmake -S .. -B .
make
  • For XCode:
mkdir test/xcode
cd test/xcode
cmake -G Xcode ..
make

Tests will be created in the bin directory located wherever you ran cmake from (test\build\bin in the example above).

Contributing

You are welcome to contribute provided you accept the Contributor Covenant Code of Conduct. Also, please do the following:

  • Submit a pull request. In the PR, Tell me how you figured out what you figured out. Links and background are very much appreciated (e.g. I used the binomial distribution found here. This is commonly used to solve this class of problem because ...). Remember, part of this code base is to educate.
  • Sign your commits.
  • Comment your code!
  • Look at the other source files and follow the same general style and naming conventions. Most importantly, use meaningful variable names, e.g.
    • bad example: int a, aa, a1, a1a, aaaaa;
    • good example: int a, b, average, numDogs, temperature;
  • Learn and use my test methodology. For function.cpp, I create functionTest.cpp with a solution() and bruteForce() method. Please use this when it makes sense. Simple solutions probably don't need it. If you are optimizing one of my methods that I couldn't figure out, please use the test methodology.

License

This repository is licensed under The Unlicense.

Acknowledgements

  • Thank you to the gflags command line parsing library.
  • Thank you to the googletest C++ test framework.
  • Thank you to @arthurafarias for providing the optimized solution to MinAvgTwoSlice and NumberOfDiscIntersections
  • Thank you to @farbod1277 for improving the solutions to MaxProductOfThree and MinAvgTwoSlice

About

My C++ solutions to the Lessons section of Codility

License:The Unlicense


Languages

Language:C++ 96.7%Language:CMake 3.3%