├── .gitignore ├── Challenges ├── 2020-12-17_Silver_2020_Challenge_of_2020-10-17 │ └── main.py └── 2022-06-28_Demo_Test │ └── task1 │ ├── solution.py │ └── test-input.txt ├── Exercises ├── Exercise_1_2015_Contest │ ├── FloodDepth │ │ └── task1 │ │ │ ├── solution.py │ │ │ └── test-input.txt │ └── LongestPassword │ │ └── task1 │ │ ├── solution.py │ │ └── test-input.txt ├── Exercise_2_2016_Contest │ └── RectangleBuilderGreaterArea │ │ └── task1 │ │ └── solution.py ├── Exercise_3_2017_Contest │ └── TennisTournament │ │ └── task1 │ │ ├── solution.py │ │ └── test-input.txt ├── Exercise_4_Algorithmic_skills │ ├── FirstUnique │ │ └── task1 │ │ │ └── solution.py │ ├── StrSymmetryPoint │ │ └── task1 │ │ │ └── solution.py │ └── TreeHeight │ │ └── task1 │ │ └── solution.py ├── Exercise_5_Coding_skills │ ├── ParityDegree │ │ └── task1 │ │ │ └── solution.py │ └── ParkingBill │ │ └── task1 │ │ └── solution.py ├── Exercise_6_SQL │ └── SqlSum │ │ └── task1 │ │ ├── solution.sql │ │ └── test-input-01.sql ├── Exercise_7_Data_Structures │ └── ArrListLen │ │ └── task1 │ │ └── solution.py ├── Exercise_8_Frontend │ ├── AngularLikeButton │ │ └── task1 │ │ │ └── solution.ts │ └── ReactLikeButton │ │ └── task1 │ │ └── solution.js └── Exercise_9_Bitwise_Operations │ └── BinaryGap │ └── task1 │ └── solution.py ├── Lessons ├── Lesson_01_Iterations │ ├── BinaryGap │ │ └── task1 │ │ │ └── solution.py │ └── Iterations.pdf ├── Lesson_02_Arrays │ ├── CyclicRotation │ │ └── task1 │ │ │ └── solution.py │ ├── 0-Arrays.pdf │ └── OddOccurrencesInArray │ │ └── task1 │ │ └── solution.js ├── Lesson_03_Time_Complexity │ ├── 1-TimeComplexity.pdf │ ├── FrogJmp │ │ └── task1 │ │ │ └── solution.py │ ├── PermMissingElem │ │ └── task1 │ │ │ └── solution.py │ └── TapeEquilibrium │ │ └── task1 │ │ └── solution.py ├── Lesson_04_Counting_Elements │ ├── 2-CountingElements.pdf │ ├── FrogRiverOne │ │ └── task1 │ │ │ └── solution.py │ └── PermCheck │ │ └── task1 │ │ └── solution.py ├── Lesson_05_Prefix_Sums │ ├── 3-PrefixSums.pdf │ └── PassingCars │ │ └── task1 │ │ └── solution.py ├── Lesson_06_Sorting │ ├── 4-Sorting.pdf │ ├── Distinct │ │ └── task1 │ │ │ └── solution.py │ ├── MaxProductOfThree │ │ └── task1 │ │ │ └── solution.py │ └── Triangle │ │ └── task1 │ │ └── solution.py ├── Lesson_07_Stacks_and_Queues │ ├── 5-Stacks.pdf │ ├── Brackets │ │ └── task1 │ │ │ └── solution.py │ ├── Fish │ │ └── task1 │ │ │ └── solution.py │ ├── Nesting │ │ └── task1 │ │ │ └── solution.py │ └── StoneWall │ │ └── task1 │ │ └── solution.py ├── Lesson_08_Leader │ ├── 6-Leader.pdf │ ├── Dominator │ │ └── task1 │ │ │ └── solution.py │ └── EquiLeader │ │ └── task1 │ │ └── solution.py ├── Lesson_09_Maximum_slice_problem │ ├── 7-MaxSlice.pdf │ ├── MaxProfit │ │ └── task1 │ │ │ └── solution.py │ └── MaxSliceSum │ │ └── task1 │ │ └── solution.py ├── Lesson_10_Prime_and_composite_numbers │ ├── 8-PrimeNumbers.pdf │ ├── CountFactors │ │ └── task1 │ │ │ └── solution.py │ └── MinPerimeterRectangle │ │ └── task1 │ │ └── solution.py ├── Lesson_11_Sieve_of_Eratosthenes │ └── 9-Sieve.pdf ├── Lesson_12_Euclidean_algorithm │ ├── 10-Gcd.pdf │ └── ChocolatesByNumbers │ │ └── task1 │ │ └── solution.py ├── Lesson_13_Fibonacci_numbers │ └── 11-Fibonacci.pdf ├── Lesson_14_Binary_search_algorithm │ └── 12-BinarySearch.pdf ├── Lesson_15_Caterpillar_method │ ├── 13-CaterpillarMethod.pdf │ ├── AbsDistinct │ │ └── task1 │ │ │ └── solution.py │ ├── CountDistinctSlices │ │ └── task1 │ │ │ └── solution.py │ └── CountTriangles │ │ └── task1 │ │ └── solution.py ├── Lesson_16_Greedy_algorithms │ ├── 14-GreedyAlgorithms.pdf │ ├── MaxNonoverlappingSegments │ │ └── task1 │ │ │ └── solution.py │ └── TieRopes │ │ └── task1 │ │ └── solution.py └── Lesson_17_Dynamic_programming │ └── 15-DynamicProgramming.pdf └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.toptal.com/developers/gitignore/api/python,pycharm,eclipse,java,intellij+all,c++ 3 | # Edit at https://www.toptal.com/developers/gitignore?templates=python,pycharm,eclipse,java,intellij+all,c++ 4 | 5 | ### C++ ### 6 | # Prerequisites 7 | *.d 8 | 9 | # Compiled Object files 10 | *.slo 11 | *.lo 12 | *.o 13 | *.obj 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Compiled Dynamic libraries 20 | *.so 21 | *.dylib 22 | *.dll 23 | 24 | # Fortran module files 25 | *.mod 26 | *.smod 27 | 28 | # Compiled Static libraries 29 | *.lai 30 | *.la 31 | *.a 32 | *.lib 33 | 34 | # Executables 35 | *.exe 36 | *.out 37 | *.app 38 | 39 | ### Eclipse ### 40 | .metadata 41 | bin/ 42 | tmp/ 43 | *.tmp 44 | *.bak 45 | *.swp 46 | *~.nib 47 | local.properties 48 | .settings/ 49 | .loadpath 50 | .recommenders 51 | 52 | # External tool builders 53 | .externalToolBuilders/ 54 | 55 | # Locally stored "Eclipse launch configurations" 56 | *.launch 57 | 58 | # PyDev specific (Python IDE for Eclipse) 59 | *.pydevproject 60 | 61 | # CDT-specific (C/C++ Development Tooling) 62 | .cproject 63 | 64 | # CDT- autotools 65 | .autotools 66 | 67 | # Java annotation processor (APT) 68 | .factorypath 69 | 70 | # PDT-specific (PHP Development Tools) 71 | .buildpath 72 | 73 | # sbteclipse plugin 74 | .target 75 | 76 | # Tern plugin 77 | .tern-project 78 | 79 | # TeXlipse plugin 80 | .texlipse 81 | 82 | # STS (Spring Tool Suite) 83 | .springBeans 84 | 85 | # Code Recommenders 86 | .recommenders/ 87 | 88 | # Annotation Processing 89 | .apt_generated/ 90 | .apt_generated_test/ 91 | 92 | # Scala IDE specific (Scala & Java development for Eclipse) 93 | .cache-main 94 | .scala_dependencies 95 | .worksheet 96 | 97 | # Uncomment this line if you wish to ignore the project description file. 98 | # Typically, this file would be tracked if it contains build/dependency configurations: 99 | #.project 100 | 101 | ### Eclipse Patch ### 102 | # Spring Boot Tooling 103 | .sts4-cache/ 104 | 105 | ### Intellij+all ### 106 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 107 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 108 | 109 | # User-specific stuff 110 | .idea/**/workspace.xml 111 | .idea/**/tasks.xml 112 | .idea/**/usage.statistics.xml 113 | .idea/**/dictionaries 114 | .idea/**/shelf 115 | 116 | # AWS User-specific 117 | .idea/**/aws.xml 118 | 119 | # Generated files 120 | .idea/**/contentModel.xml 121 | 122 | # Sensitive or high-churn files 123 | .idea/**/dataSources/ 124 | .idea/**/dataSources.ids 125 | .idea/**/dataSources.local.xml 126 | .idea/**/sqlDataSources.xml 127 | .idea/**/dynamic.xml 128 | .idea/**/uiDesigner.xml 129 | .idea/**/dbnavigator.xml 130 | 131 | # Gradle 132 | .idea/**/gradle.xml 133 | .idea/**/libraries 134 | 135 | # Gradle and Maven with auto-import 136 | # When using Gradle or Maven with auto-import, you should exclude module files, 137 | # since they will be recreated, and may cause churn. Uncomment if using 138 | # auto-import. 139 | # .idea/artifacts 140 | # .idea/compiler.xml 141 | # .idea/jarRepositories.xml 142 | # .idea/modules.xml 143 | # .idea/*.iml 144 | # .idea/modules 145 | # *.iml 146 | # *.ipr 147 | 148 | # CMake 149 | cmake-build-*/ 150 | 151 | # Mongo Explorer plugin 152 | .idea/**/mongoSettings.xml 153 | 154 | # File-based project format 155 | *.iws 156 | 157 | # IntelliJ 158 | out/ 159 | 160 | # mpeltonen/sbt-idea plugin 161 | .idea_modules/ 162 | 163 | # JIRA plugin 164 | atlassian-ide-plugin.xml 165 | 166 | # Cursive Clojure plugin 167 | .idea/replstate.xml 168 | 169 | # Crashlytics plugin (for Android Studio and IntelliJ) 170 | com_crashlytics_export_strings.xml 171 | crashlytics.properties 172 | crashlytics-build.properties 173 | fabric.properties 174 | 175 | # Editor-based Rest Client 176 | .idea/httpRequests 177 | 178 | # Android studio 3.1+ serialized cache file 179 | .idea/caches/build_file_checksums.ser 180 | 181 | ### Intellij+all Patch ### 182 | # Ignores the whole .idea folder and all .iml files 183 | # See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360 184 | 185 | .idea/ 186 | 187 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 188 | 189 | *.iml 190 | modules.xml 191 | .idea/misc.xml 192 | *.ipr 193 | 194 | # Sonarlint plugin 195 | .idea/sonarlint 196 | 197 | ### Java ### 198 | # Compiled class file 199 | *.class 200 | 201 | # Log file 202 | *.log 203 | 204 | # BlueJ files 205 | *.ctxt 206 | 207 | # Mobile Tools for Java (J2ME) 208 | .mtj.tmp/ 209 | 210 | # Package Files # 211 | *.jar 212 | *.war 213 | *.nar 214 | *.ear 215 | *.zip 216 | *.tar.gz 217 | *.rar 218 | 219 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 220 | hs_err_pid* 221 | 222 | ### PyCharm ### 223 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 224 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 225 | 226 | # User-specific stuff 227 | 228 | # AWS User-specific 229 | 230 | # Generated files 231 | 232 | # Sensitive or high-churn files 233 | 234 | # Gradle 235 | 236 | # Gradle and Maven with auto-import 237 | # When using Gradle or Maven with auto-import, you should exclude module files, 238 | # since they will be recreated, and may cause churn. Uncomment if using 239 | # auto-import. 240 | # .idea/artifacts 241 | # .idea/compiler.xml 242 | # .idea/jarRepositories.xml 243 | # .idea/modules.xml 244 | # .idea/*.iml 245 | # .idea/modules 246 | # *.iml 247 | # *.ipr 248 | 249 | # CMake 250 | 251 | # Mongo Explorer plugin 252 | 253 | # File-based project format 254 | 255 | # IntelliJ 256 | 257 | # mpeltonen/sbt-idea plugin 258 | 259 | # JIRA plugin 260 | 261 | # Cursive Clojure plugin 262 | 263 | # Crashlytics plugin (for Android Studio and IntelliJ) 264 | 265 | # Editor-based Rest Client 266 | 267 | # Android studio 3.1+ serialized cache file 268 | 269 | ### PyCharm Patch ### 270 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 271 | 272 | # *.iml 273 | # modules.xml 274 | # .idea/misc.xml 275 | # *.ipr 276 | 277 | # Sonarlint plugin 278 | # https://plugins.jetbrains.com/plugin/7973-sonarlint 279 | .idea/**/sonarlint/ 280 | 281 | # SonarQube Plugin 282 | # https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin 283 | .idea/**/sonarIssues.xml 284 | 285 | # Markdown Navigator plugin 286 | # https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced 287 | .idea/**/markdown-navigator.xml 288 | .idea/**/markdown-navigator-enh.xml 289 | .idea/**/markdown-navigator/ 290 | 291 | # Cache file creation bug 292 | # See https://youtrack.jetbrains.com/issue/JBR-2257 293 | .idea/$CACHE_FILE$ 294 | 295 | # CodeStream plugin 296 | # https://plugins.jetbrains.com/plugin/12206-codestream 297 | .idea/codestream.xml 298 | 299 | ### Python ### 300 | # Byte-compiled / optimized / DLL files 301 | __pycache__/ 302 | *.py[cod] 303 | *$py.class 304 | 305 | # C extensions 306 | 307 | # Distribution / packaging 308 | .Python 309 | build/ 310 | develop-eggs/ 311 | dist/ 312 | downloads/ 313 | eggs/ 314 | .eggs/ 315 | lib/ 316 | lib64/ 317 | parts/ 318 | sdist/ 319 | var/ 320 | wheels/ 321 | share/python-wheels/ 322 | *.egg-info/ 323 | .installed.cfg 324 | *.egg 325 | MANIFEST 326 | 327 | # PyInstaller 328 | # Usually these files are written by a python script from a template 329 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 330 | *.manifest 331 | *.spec 332 | 333 | # Installer logs 334 | pip-log.txt 335 | pip-delete-this-directory.txt 336 | 337 | # Unit test / coverage reports 338 | htmlcov/ 339 | .tox/ 340 | .nox/ 341 | .coverage 342 | .coverage.* 343 | .cache 344 | nosetests.xml 345 | coverage.xml 346 | *.cover 347 | *.py,cover 348 | .hypothesis/ 349 | .pytest_cache/ 350 | cover/ 351 | 352 | # Translations 353 | *.mo 354 | *.pot 355 | 356 | # Django stuff: 357 | local_settings.py 358 | db.sqlite3 359 | db.sqlite3-journal 360 | 361 | # Flask stuff: 362 | instance/ 363 | .webassets-cache 364 | 365 | # Scrapy stuff: 366 | .scrapy 367 | 368 | # Sphinx documentation 369 | docs/_build/ 370 | 371 | # PyBuilder 372 | .pybuilder/ 373 | target/ 374 | 375 | # Jupyter Notebook 376 | .ipynb_checkpoints 377 | 378 | # IPython 379 | profile_default/ 380 | ipython_config.py 381 | 382 | # pyenv 383 | # For a library or package, you might want to ignore these files since the code is 384 | # intended to run in multiple environments; otherwise, check them in: 385 | # .python-version 386 | 387 | # pipenv 388 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 389 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 390 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 391 | # install all needed dependencies. 392 | #Pipfile.lock 393 | 394 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 395 | __pypackages__/ 396 | 397 | # Celery stuff 398 | celerybeat-schedule 399 | celerybeat.pid 400 | 401 | # SageMath parsed files 402 | *.sage.py 403 | 404 | # Environments 405 | .env 406 | .venv 407 | env/ 408 | venv/ 409 | ENV/ 410 | env.bak/ 411 | venv.bak/ 412 | 413 | # Spyder project settings 414 | .spyderproject 415 | .spyproject 416 | 417 | # Rope project settings 418 | .ropeproject 419 | 420 | # mkdocs documentation 421 | /site 422 | 423 | # mypy 424 | .mypy_cache/ 425 | .dmypy.json 426 | dmypy.json 427 | 428 | # Pyre type checker 429 | .pyre/ 430 | 431 | # pytype static type analyzer 432 | .pytype/ 433 | 434 | # Cython debug symbols 435 | cython_debug/ 436 | 437 | # End of https://www.toptal.com/developers/gitignore/api/python,pycharm,eclipse,java,intellij+all,c++ 438 | -------------------------------------------------------------------------------- /Challenges/2020-12-17_Silver_2020_Challenge_of_2020-10-17/main.py: -------------------------------------------------------------------------------- 1 | def solution(A, B): 2 | sides = set(A).union(set(B)) 3 | side_counts_dict = {el: 0 for el in sides} 4 | N_dim = len(A) 5 | for i in range(N_dim): 6 | if A[i] == B[i]: 7 | side_counts_dict[A[i]] = side_counts_dict[A[i]] + 1 8 | else: 9 | side_counts_dict[A[i]] = side_counts_dict[A[i]] + 1 10 | side_counts_dict[B[i]] = side_counts_dict[B[i]] + 1 11 | return max(side_counts_dict.values()) 12 | 13 | 14 | N = int(input()) 15 | A_array = list(map(int, input().split())) 16 | B_array = list(map(int, input().split())) 17 | print(solution(A_array, B_array)) -------------------------------------------------------------------------------- /Challenges/2022-06-28_Demo_Test/task1/solution.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | 7 | # Solution 1: 8 | # res = 1 9 | # A = list(set(A)) 10 | # A.sort() 11 | # for i in A: 12 | # if i == res: 13 | # res += 1 14 | # else: 15 | # break 16 | # return res 17 | 18 | # Solution 2: 19 | res = 1 20 | A_set = set(A) 21 | for _ in range(len(A_set)): 22 | if res not in A_set: 23 | break 24 | res += 1 25 | return res 26 | -------------------------------------------------------------------------------- /Challenges/2022-06-28_Demo_Test/task1/test-input.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Challenges/2022-06-28_Demo_Test/task1/test-input.txt -------------------------------------------------------------------------------- /Exercises/Exercise_1_2015_Contest/FloodDepth/task1/solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | You are helping a geologist friend investigate an area with mountain lakes. 3 | A recent heavy rainfall has flooded these lakes 4 | and their water levels have reached the highest possible point. 5 | Your friend is interested to know the maximum depth in the deepest part of these lakes. 6 | 7 | We simplify the problem in 2-D dimensions. 8 | The whole landscape can be divided into small blocks 9 | and described by an array A of length N. 10 | Each element of A is the altitude of the rock floor of a block 11 | (i.e. the height of this block when there is no water at all). 12 | After the rainfall, all the low-lying areas 13 | (i.e. blocks that have higher blocks on both sides) 14 | are holding as much water as possible. 15 | You would like to know the maximum depth of water after this entire area is flooded. 16 | You can assume that the altitude outside this area is zero and 17 | the outside area can accommodate infinite amount of water. 18 | 19 | For example, consider array A such that: 20 | 21 | A[0] = 1 22 | A[1] = 3 23 | A[2] = 2 24 | A[3] = 1 25 | A[4] = 2 26 | A[5] = 1 27 | A[6] = 5 28 | A[7] = 3 29 | A[8] = 3 30 | A[9] = 4 31 | A[10] = 2 32 | 33 | The following picture illustrates the landscape after it has flooded: 34 | 35 | The gray area is the rock floor described by the array A above 36 | and the blue area with dashed lines represents the water filling the low-lying areas 37 | with maximum possible volume. 38 | Thus, blocks 3 and 5 have a water depth of 2 39 | while blocks 2, 4, 7 and 8 have a water depth of 1. 40 | Therefore, the maximum water depth of this area is 2. 41 | 42 | Write a function: 43 | 44 | def solution(A) 45 | 46 | that, given a non-empty array A consisting of N integers, 47 | returns the maximum depth of water. 48 | 49 | Given array A shown above, the function should return 2, as explained above. 50 | 51 | For the following array: 52 | 53 | A[0] = 5 54 | A[1] = 8 55 | 56 | the function should return 0, because this landscape cannot hold any water. 57 | 58 | Write an efficient algorithm for the following assumptions: 59 | - N is an integer within the range [1..100,000]; 60 | - each element of array A is an integer within the range [1..100,000,000]. 61 | """ 62 | 63 | # you can write to stdout for debugging purposes, e.g. 64 | # print("this is a debug message") 65 | 66 | def solution(A): 67 | # write your code in Python 3.6 68 | 69 | res = 0 70 | depths = {} 71 | wall = A[0] 72 | for i in range(1, len(A)-1): 73 | if A[i] > wall: 74 | wall = A[i] 75 | else: 76 | depths[i] = wall - A[i] 77 | 78 | wall = A[len(A)-1] 79 | for i in range(len(A)-2, 0, -1): 80 | if A[i] > wall: 81 | wall = A[i] 82 | elif i in depths.keys(): 83 | res = max(res, min(wall - A[i], depths[i])) 84 | return res 85 | 86 | 87 | if __name__ == "__main__": 88 | A = [1, 3, 2, 1, 2, 1, 5, 3, 3, 4, 2] 89 | print(solution(A)) 90 | -------------------------------------------------------------------------------- /Exercises/Exercise_1_2015_Contest/FloodDepth/task1/test-input.txt: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | def solution(A): 5 | # write your code in Python 3.6 6 | pass 7 | -------------------------------------------------------------------------------- /Exercises/Exercise_1_2015_Contest/LongestPassword/task1/solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | You would like to set a password for a bank account. 3 | However, there are three restrictions on the format of the password: 4 | 5 | - it has to contain only alphanumerical characters (a-z, A-Z, 0-9); 6 | - there should be an even number of letters; 7 | - there should be an odd number of digits. 8 | 9 | You are given a string S consisting of N characters. 10 | String S can be divided into words by splitting it at, and removing, the spaces. 11 | The goal is to choose the longest word that is a valid password. 12 | You can assume that if there are K spaces in 13 | string S then there are exactly K + 1 words. 14 | 15 | For example, given "test 5 a0A pass007 ?xy1", 16 | there are five words and three of them are valid passwords: 17 | "5", "a0A" and "pass007". 18 | Thus the longest password is "pass007" and its length is 7. 19 | Note that neither "test" nor "?xy1" is a valid password, because "?" 20 | is not an alphanumerical character and "test" contains an even number of digits (zero). 21 | 22 | Write a function: 23 | 24 | def solution(S) 25 | 26 | that, given a non-empty string S consisting of N characters, 27 | returns the length of the longest word from the string that is a valid password. 28 | If there is no such word, your function should return -1. 29 | 30 | For example, given S = "test 5 a0A pass007 ?xy1", 31 | your function should return 7, as explained above. 32 | 33 | Assume that: 34 | - N is an integer within the range [1..200]; 35 | - string S consists only of printable ASCII characters and spaces. 36 | 37 | In your solution, focus on correctness. 38 | The performance of your solution will not be the focus of the assessment. 39 | """ 40 | 41 | # you can write to stdout for debugging purposes, e.g. 42 | # print("this is a debug message") 43 | 44 | def solution(S): 45 | # write your code in Python 3.6 46 | possible_passwords = S.split(' ') 47 | max_length = -1 48 | for password in possible_passwords: 49 | if len(password) > max_length and is_valid_password(password): 50 | max_length = len(password) 51 | return max_length 52 | 53 | def is_valid_password(password): 54 | """ 55 | it has to contain only alphanumerical characters (a-z, A-Z, 0-9); 56 | there should be an even number of letters; 57 | there should be an odd number of digits. 58 | """ 59 | if len(password) % 2 == 0: 60 | return False 61 | letters = 0 62 | digits = 0 63 | for c in password: 64 | if not c.isalnum(): 65 | return False 66 | if c.isalpha(): 67 | letters += 1 68 | elif c.isdigit(): 69 | digits += 1 70 | return letters % 2 == 0 and digits % 2 == 1 71 | -------------------------------------------------------------------------------- /Exercises/Exercise_1_2015_Contest/LongestPassword/task1/test-input.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Exercises/Exercise_1_2015_Contest/LongestPassword/task1/test-input.txt -------------------------------------------------------------------------------- /Exercises/Exercise_2_2016_Contest/RectangleBuilderGreaterArea/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Exercises/Exercise_2_2016_Contest/RectangleBuilderGreaterArea/task1/solution.py -------------------------------------------------------------------------------- /Exercises/Exercise_3_2017_Contest/TennisTournament/task1/solution.py: -------------------------------------------------------------------------------- 1 | # you can write to stdout for debugging purposes, e.g. 2 | # print("this is a debug message") 3 | 4 | """ 5 | You are hosting a tennis tournament. P players, who will take part in the first round of this tournament, 6 | are already registered and you have reserved C tennis courts for the matches. 7 | Exactly two players play in each game and only one game can be played on each court at any given time. 8 | You want to host the maximum possible number of games starting at the same time 9 | (in order to finish the first round quickly). 10 | 11 | How many games can be hosted in parallel simultaneously? 12 | 13 | Write a function: 14 | 15 | def solution(P, C) 16 | 17 | that, given the number of players P and the number of reserved courts C, 18 | returns the maximum number of games that can be played in parallel. 19 | 20 | Examples: 21 | 22 | 1. Given P = 5 players and C = 3 available courts, the function should return 2. 23 | Two games can be played simultaneously (for instance, 24 | the first and second players can play on the first court, 25 | and the third and fourth players on the second court, 26 | and the third court will be empty because the fifth player does not have a partner to play with). 27 | 28 | 2. Given P = 10 players and C = 3 courts, the function should return 3. 29 | At most three games can be hosted in parallel. 30 | 31 | Assume that: 32 | 33 | P and C are integers within the range [1..30,000]. 34 | In your solution, focus on correctness. 35 | The performance of your solution will not be the focus of the assessment. 36 | """ 37 | 38 | def solution(P, C): 39 | # write your code in Python 3.6 40 | # if P < 2 41 | if P < 2: 42 | return 0 43 | 44 | # P > 2 45 | 46 | # if P >= C * 2 47 | if P >= C * 2: 48 | return C 49 | 50 | # 2 < P < C * 2 51 | 52 | # if P % 2 == 0 53 | if P % 2 == 0: 54 | return P // 2 55 | else: 56 | return (P - 1) // 2 57 | -------------------------------------------------------------------------------- /Exercises/Exercise_3_2017_Contest/TennisTournament/task1/test-input.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Exercises/Exercise_3_2017_Contest/TennisTournament/task1/test-input.txt -------------------------------------------------------------------------------- /Exercises/Exercise_4_Algorithmic_skills/FirstUnique/task1/solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | A non-empty array A consisting of N integers is given. 3 | The unique number is the number that occurs exactly once in array A. 4 | 5 | For example, the following array A: 6 | 7 | A[0] = 4 8 | A[1] = 10 9 | A[2] = 5 10 | A[3] = 4 11 | A[4] = 2 12 | A[5] = 10 13 | 14 | contains two unique numbers (5 and 2). 15 | 16 | You should find the first unique number in A. In other words, 17 | find the unique number with the lowest position in A. 18 | 19 | For above example, 5 is in second position (because A[2] = 5) 20 | and 2 is in fourth position (because A[4] = 2). 21 | So, the first unique number is 5. 22 | 23 | Write a function: 24 | 25 | def solution(A) 26 | 27 | that, given a non-empty array A of N integers, 28 | returns the first unique number in A. 29 | The function should return -1 if there are no unique numbers in A. 30 | 31 | For example, given: 32 | 33 | A[0] = 1 34 | A[1] = 4 35 | A[2] = 3 36 | A[3] = 3 37 | A[4] = 1 38 | A[5] = 2 39 | 40 | the function should return 4. 41 | There are two unique numbers (4 and 2 occur exactly once). 42 | The first one is 4 in position 1 and the second one is 2 in position 5. 43 | The function should return 4 bacause it is unique number with the lowest position. 44 | 45 | Given array A such that: 46 | 47 | A[0] = 6 48 | A[1] = 4 49 | A[2] = 4 50 | A[3] = 6 51 | 52 | the function should return -1. 53 | There is no unique number in A (4 and 6 occur more than once). 54 | 55 | Write an efficient algorithm for the following assumptions: 56 | - N is an integer within the range [1..100,000]; 57 | - each element of array A is an integer within the range [0..1,000,000,000]. 58 | """ 59 | -------------------------------------------------------------------------------- /Exercises/Exercise_4_Algorithmic_skills/StrSymmetryPoint/task1/solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | Write a function: 3 | 4 | def solution(S) 5 | 6 | that, given a string S, returns the index (counting from 0) of a character 7 | such that the part of the string to the left of that character 8 | is a reversal of the part of the string to its right. 9 | The function should return -1 if no such index exists. 10 | 11 | Note: reversing an empty string 12 | (i.e. a string whose length is zero) gives an empty string. 13 | 14 | For example, given a string: 15 | 16 | "racecar" 17 | 18 | the function should return 3, 19 | because the substring to the left of the character "e" at index 3 is "rac", 20 | and the one to the right is "car". 21 | 22 | Given a string: 23 | 24 | "x" 25 | 26 | the function should return 0, because both substrings are empty. 27 | 28 | Write an efficient algorithm for the following assumptions: 29 | - the length of string S is within the range [0..2,000,000]. 30 | """ 31 | -------------------------------------------------------------------------------- /Exercises/Exercise_4_Algorithmic_skills/TreeHeight/task1/solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | In this problem we consider binary trees, 3 | represented by pointer data structures. 4 | 5 | A binary tree is either an empty tree or a node (called the root) 6 | consisting of a single integer value and two further binary trees, 7 | called the left subtree and the right subtree. 8 | 9 | For example, the figure below shows a binary tree consisting of six nodes. 10 | Its root contains the value 5, and the roots of its 11 | left and right subtrees have the values 3 and 10, respectively. 12 | The right subtree of the node containing the value 10, 13 | as well as the left and right subtrees of the nodes 14 | containing the values 20, 21 and 1, are empty trees. 15 | 16 | A path in a binary tree is a non-empty sequence of nodes 17 | that one can traverse by following the pointers. 18 | The length of a path is the number of pointers it traverses. 19 | More formally, a path of length K is a sequence of nodes P[0], P[1], ..., P[K], 20 | such that node P[I + 1] is the root of the left or right subtree of P[I], 21 | for 0 ≤ I < K. 22 | For example, the sequence of nodes with values 5, 3, 21 23 | is a path of length 2 in the tree from the above figure. 24 | The sequence of nodes with values 10, 1 is a path of length 1. 25 | The sequence of nodes with values 21, 3, 20 is not a valid path. 26 | 27 | The height of a binary tree is defined 28 | as the length of the longest possible path in the tree. 29 | In particular, a tree consisting of only one node has height 0 and, 30 | conventionally, an empty tree has height -1. For example, 31 | the tree shown in the above figure is of height 2. 32 | 33 | Problem 34 | Write a function: 35 | 36 | def solution(T) 37 | 38 | that, given a non-empty binary tree T consisting of N nodes, returns its height. 39 | For example, given tree T shown in the figure above, 40 | the function should return 2, as explained above. 41 | Note that the values contained in the nodes are not relevant in this task. 42 | 43 | Technical details 44 | A binary tree can be given using a pointer data structure. 45 | Assume that the following declarations are given: 46 | 47 | class Tree(object): 48 | x = 0 49 | l = None 50 | r = None 51 | 52 | An empty tree is represented by an empty pointer (denoted by null). 53 | A non-empty tree is represented by a pointer to an object representing its root. 54 | The attribute x holds the integer contained in the root, 55 | whereas attributes l and r hold the left and 56 | right subtrees of the binary tree, respectively. 57 | 58 | For the purpose of entering your own test cases, 59 | you can denote a tree recursively in the following way. 60 | An empty binary tree is denoted by None. 61 | A non-empty tree is denoted as (X, L, R), 62 | where X is the value contained in the root 63 | and L and R denote the left and right subtrees, respectively. 64 | The tree from the above figure can be denoted as: 65 | 66 | (5, (3, (20, None, None), (21, None, None)), (10, (1, None, None), None)) 67 | 68 | Assumptions 69 | Write an efficient algorithm for the following assumptions: 70 | - N is an integer within the range [1..1,000]; 71 | - the height of tree T (number of edges on the longest path 72 | from root to leaf) is within the range [0..500]. 73 | """ 74 | -------------------------------------------------------------------------------- /Exercises/Exercise_5_Coding_skills/ParityDegree/task1/solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | A positive integer N is given. The goal is to find the highest power of 2 that divides N. 3 | In other words, we have to find the maximum K for which N modulo 2^K is 0. 4 | 5 | For example, given integer N = 24 the answer is 3, 6 | because 2^3 = 8 is the highest power of 2 that divides N. 7 | 8 | Write a function: 9 | 10 | def solution(N) 11 | 12 | that, given a positive integer N, returns the highest power of 2 that divides N. 13 | 14 | For example, given integer N = 24, the function should return 3, as explained above. 15 | 16 | Assume that: 17 | - N is an integer within the range [1..1,000,000,000]. 18 | 19 | In your solution, focus on correctness. 20 | The performance of your solution will not be the focus of the assessment. 21 | """ 22 | -------------------------------------------------------------------------------- /Exercises/Exercise_5_Coding_skills/ParkingBill/task1/solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | You parked your car in a parking lot and 3 | want to compute the total cost of the ticket. 4 | The billing rules are as follows: 5 | - The entrance fee of the car parking lot is 2; 6 | - The first full or partial hour costs 3; 7 | - Each successive full or partial hour (after the first) costs 4. 8 | 9 | You entered the car parking lot at time E and left at time L. In this task, 10 | times are represented as strings in the format "HH:MM" (where "HH" is a 11 | two-digit number between 0 and 23, which stands for hours, 12 | and "MM" is a two-digit number between 0 and 59, which stands for minutes). 13 | 14 | Write a function: 15 | 16 | def solution(E, L) 17 | 18 | that, given strings E and L specifying points in time in the format "HH:MM", 19 | returns the total cost of the parking bill from your entry at time E 20 | to your exit at time L. 21 | You can assume that E describes a time before L on the same day. 22 | 23 | For example, given "10:00" and "13:21" your function should return 17, 24 | because the entrance fee equals 2, the first hour costs 3 and there are 25 | two more full hours and part of a further hour, so the total cost is 26 | 2 + 3 + (3 * 4) = 17. Given "09:42" and "11:42" your function should return 9, 27 | because the entrance fee equals 2, the first hour costs 3 and 28 | the second hour costs 4, so the total cost is 2 + 3 + 4 = 9. 29 | 30 | Assume that: 31 | - strings E and L follow the format "HH:MM" strictly; 32 | - string E describes a time before L on the same day. 33 | 34 | In your solution, focus on correctness. 35 | The performance of your solution will not be the focus of the assessment. 36 | """ 37 | -------------------------------------------------------------------------------- /Exercises/Exercise_6_SQL/SqlSum/task1/solution.sql: -------------------------------------------------------------------------------- 1 | -- write your code in PostgreSQL 9.4 2 | -- Given a table elements with the following structure: 3 | 4 | -- create table elements ( 5 | -- v integer not null 6 | -- ); 7 | -- write an SQL query that returns the sum of the numbers in column v. 8 | 9 | -- For example, given: 10 | 11 | -- v 12 | -- --- 13 | -- 2 14 | -- 10 15 | -- 20 16 | -- 10 17 | -- your query should return 42. 18 | 19 | SELECT sum(v) FROM elements; 20 | -------------------------------------------------------------------------------- /Exercises/Exercise_6_SQL/SqlSum/task1/test-input-01.sql: -------------------------------------------------------------------------------- 1 | insert into elements values(2); 2 | insert into elements values(10); 3 | insert into elements values(20); 4 | insert into elements values(10); 5 | -------------------------------------------------------------------------------- /Exercises/Exercise_7_Data_Structures/ArrListLen/task1/solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | A non-empty array A consisting of N integers is given. 3 | 4 | Array A represents a linked list. 5 | A list is constructed from this array as follows: 6 | - the first node (the head) is located at index 0; 7 | - the value of a node located at index K is A[K]; 8 | - if the value of a node is -1 then it is the last node of the list; 9 | - otherwise, the successor of a node located at index K is located at index A[K] 10 | (you can assume that A[K] is a valid index, that is 0 ≤ A[K] < N). 11 | 12 | For example, for array A such that: 13 | 14 | A[0] = 1 15 | A[1] = 4 16 | A[2] = -1 17 | A[3] = 3 18 | A[4] = 2 19 | 20 | the following list is constructed: 21 | - the first node (the head) is located at index 0 and has a value of 1; 22 | - the second node is located at index 1 and has a value of 4; 23 | - the third node is located at index 4 and has a value of 2; 24 | - the fourth node is located at index 2 and has a value of -1. 25 | 26 | Write a function: 27 | 28 | def solution(A) 29 | 30 | that, given a non-empty array A consisting of N integers, 31 | returns the length of the list constructed from A in the above manner. 32 | 33 | For example, given array A such that: 34 | 35 | A[0] = 1 36 | A[1] = 4 37 | A[2] = -1 38 | A[3] = 3 39 | A[4] = 2 40 | 41 | the function should return 4, as explained in the example above. 42 | 43 | Assume that: 44 | - N is an integer within the range [1..200,000]; 45 | - each element of array A is an integer within the range [-1..N-1]; 46 | - it will always be possible to construct the list and its length will be finite. 47 | 48 | In your solution, focus on correctness. 49 | The performance of your solution will not be the focus of the assessment. 50 | """ 51 | -------------------------------------------------------------------------------- /Exercises/Exercise_8_Frontend/AngularLikeButton/task1/solution.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Build a like button component using Angular(v4). 3 | Export the like button component as "LikeButtonComponent" 4 | (export class LikeButtonComponent). 5 | 6 | Requirements: 7 | 1. There should be a like button: 8 | - The content of the like button should be in the following format: 9 | "Like | 100", where 100 is the total number of likes. 10 | - It should have a "like-button" class. 11 | - Wrap the number of likes in a span with a "likes-counter" class. 12 | - The initial number of likes in the counter should be 100. 13 | 14 | 2. Users can add a like.By clicking the button: 15 | - The number of likes should increase by one. 16 | - Like button should have "liked" class in addition to the "like-button" class. 17 | 18 | 3. Users can undo their like by clicking again on the button: 19 | - The counter should decrease by one. 20 | - "liked" class should be removed. 21 | 22 | Assessment / Tools: 23 | - Use the animation below as a reference for your solution. 24 | - Design / styling is not assessed and will not affect the score. 25 | You should focus only on implementing the requirements. 26 | - The "Preview" tab will display your component. 27 | You can use it for testing purposes. 28 | */ 29 | -------------------------------------------------------------------------------- /Exercises/Exercise_8_Frontend/ReactLikeButton/task1/solution.js: -------------------------------------------------------------------------------- 1 | /** 2 | Build a "like button" component using React 16. 3 | The component should be the default export (use export default). 4 | 5 | Requirements: 6 | 1. There should be a like button: 7 | - The content of the like button should be in the following format: 8 | "Like | 100", where 100 is the total number of likes. 9 | - It should have a "like-button" class. 10 | - Wrap the number of likes in a span with a "likes-counter" class. 11 | - The initial number of likes in the counter should be 100. 12 | 13 | 2. Users can add a like. By clicking the button: 14 | - The number of likes should increase by one. 15 | - Like button should have "liked" class in addition to the "like-button" 16 | class (you can use the classnames tool for that). 17 | 18 | 3. Users can undo their like by clicking again on the button: 19 | - The counter should decrease by one. 20 | - "liked" class should be removed. 21 | 22 | Assessment/Tools: 23 | - Only two imports are allowed: react (v16.8.6) and classnames (v2.2.5). 24 | Both are at the top of the starting code. 25 | - Use the animation below as a reference for your solution. 26 | - Design/styling is not assessed and will not affect the score. 27 | You should focus only on implementing the requirements. 28 | - The "Preview" tab will display your component. 29 | You can use it for testing purposes. 30 | */ 31 | -------------------------------------------------------------------------------- /Exercises/Exercise_9_Bitwise_Operations/BinaryGap/task1/solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | A binary gap within a positive integer N is any maximal sequence of 3 | consecutive zeros that is surrounded by ones at both ends in the binary representation of N. 4 | 5 | For example, number 9 has binary representation 1001 and 6 | contains a binary gap of length 2. 7 | The number 529 has binary representation 1000010001 8 | and contains two binary gaps: one of length 4 and one of length 3. 9 | The number 20 has binary representation 10100 and 10 | contains one binary gap of length 1. 11 | The number 15 has binary representation 1111 and has no binary gaps. 12 | The number 32 has binary representation 100000 and has no binary gaps. 13 | 14 | Write a function: 15 | 16 | def solution(N) 17 | 18 | that, given a positive integer N, returns the length of its longest binary gap. 19 | The function should return 0 if N doesn't contain a binary gap. 20 | 21 | For example, given N = 1041 the function should return 5, 22 | because N has binary representation 10000010001 and 23 | so its longest binary gap is of length 5. 24 | Given N = 32 the function should return 0, 25 | because N has binary representation '100000' and thus no binary gaps. 26 | 27 | Write an efficient algorithm for the following assumptions: 28 | - N is an integer within the range [1..2,147,483,647]. 29 | """ 30 | 31 | # you can write to stdout for debugging purposes, e.g. 32 | # print("this is a debug message") 33 | 34 | def solution(N): 35 | """ 36 | :param N: int 37 | :return: int 38 | """ 39 | # write your code in Python 3.6 40 | binary_str = bin(N)[2:] 41 | 42 | sequences = [] 43 | 44 | for i in range(len(binary_str)): 45 | if binary_str[i] == '1': 46 | if len(sequences) == 0: 47 | sequences.append({"start": i, "end": None}) 48 | elif sequences[-1]["start"] == i - 1: 49 | sequences[-1]["start"] = i 50 | elif sequences[-1]["end"] is None: 51 | sequences[-1]["end"] = i 52 | sequences.append({"start": i, "end": None}) 53 | else: 54 | sequences.append({"start": i, "end": None}) 55 | valid_sequences = [seq for seq in sequences if seq["end"] is not None] 56 | if len(valid_sequences) == 0: 57 | return 0 58 | longest_sequence = max(valid_sequences, key=lambda x: x["end"] - x["start"]) 59 | return longest_sequence["end"] - longest_sequence["start"] - 1 60 | 61 | 62 | N_s = [1041, 32, 15, 529, 9, 20, 10, 0, 1, 3, 5] 63 | 64 | for N in N_s: 65 | print(f"N = {N}, binary N = {bin(N)[2:]}, longest binary gap = {solution(N)}") 66 | -------------------------------------------------------------------------------- /Lessons/Lesson_01_Iterations/BinaryGap/task1/solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | A binary gap within a positive integer N is any maximal sequence of 3 | consecutive zeros that is surrounded by ones at both ends in the binary representation of N. 4 | 5 | For example, number 9 has binary representation 1001 and 6 | contains a binary gap of length 2. 7 | The number 529 has binary representation 1000010001 8 | and contains two binary gaps: one of length 4 and one of length 3. 9 | The number 20 has binary representation 10100 and 10 | contains one binary gap of length 1. 11 | The number 15 has binary representation 1111 and has no binary gaps. 12 | The number 32 has binary representation 100000 and has no binary gaps. 13 | 14 | Write a function: 15 | 16 | def solution(N) 17 | 18 | that, given a positive integer N, returns the length of its longest binary gap. 19 | The function should return 0 if N doesn't contain a binary gap. 20 | 21 | For example, given N = 1041 the function should return 5, 22 | because N has binary representation 10000010001 and 23 | so its longest binary gap is of length 5. 24 | Given N = 32 the function should return 0, 25 | because N has binary representation '100000' and thus no binary gaps. 26 | 27 | Write an efficient algorithm for the following assumptions: 28 | 29 | N is an integer within the range [1..2,147,483,647]. 30 | """ 31 | 32 | # you can write to stdout for debugging purposes, e.g. 33 | # print("this is a debug message") 34 | 35 | def solution(N): 36 | """ 37 | :param N: int 38 | :return: int 39 | """ 40 | # write your code in Python 3.6 41 | binary_str = bin(N)[2:] 42 | 43 | sequences = [] 44 | 45 | for i in range(len(binary_str)): 46 | if binary_str[i] == '1': 47 | if len(sequences) == 0: 48 | sequences.append({"start": i, "end": None}) 49 | elif sequences[-1]["start"] == i - 1: 50 | sequences[-1]["start"] = i 51 | elif sequences[-1]["end"] is None: 52 | sequences[-1]["end"] = i 53 | sequences.append({"start": i, "end": None}) 54 | else: 55 | sequences.append({"start": i, "end": None}) 56 | valid_sequences = [seq for seq in sequences if seq["end"] is not None] 57 | if len(valid_sequences) == 0: 58 | return 0 59 | longest_sequence = max(valid_sequences, key=lambda x: x["end"] - x["start"]) 60 | return longest_sequence["end"] - longest_sequence["start"] - 1 61 | 62 | 63 | N_s = [1041, 32, 15, 529, 9, 20, 10, 0, 1, 3, 5] 64 | 65 | for N in N_s: 66 | print(f"N = {N}, binary N = {bin(N)[2:]}, longest binary gap = {solution(N)}") 67 | -------------------------------------------------------------------------------- /Lessons/Lesson_01_Iterations/Iterations.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_01_Iterations/Iterations.pdf -------------------------------------------------------------------------------- /Lessons/Lesson_02_Arrays/ CyclicRotation/task1/solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place). 3 | 4 | The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times. 5 | 6 | Write a function: 7 | 8 | def solution(A, K) 9 | 10 | that, given an array A consisting of N integers and an integer K, returns the array A rotated K times. 11 | 12 | For example, given 13 | 14 | A = [3, 8, 9, 7, 6] 15 | K = 3 16 | the function should return [9, 7, 6, 3, 8]. Three rotations were made: 17 | 18 | [3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7] 19 | [6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9] 20 | [7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8] 21 | For another example, given 22 | 23 | A = [0, 0, 0] 24 | K = 1 25 | the function should return [0, 0, 0] 26 | 27 | Given 28 | 29 | A = [1, 2, 3, 4] 30 | K = 4 31 | the function should return [1, 2, 3, 4] 32 | 33 | Assume that: 34 | - N and K are integers within the range [0..100]; 35 | - each element of array A is an integer within the range [-1,000..1,000]. 36 | 37 | In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment. 38 | """ 39 | 40 | # you can write to stdout for debugging purposes, e.g. 41 | # print("this is a debug message") 42 | 43 | def solution(A, K): 44 | if len(A) == 0: 45 | return A 46 | K = K % len(A) 47 | return A if K == 0 else A[-K:] + A[:-K] 48 | -------------------------------------------------------------------------------- /Lessons/Lesson_02_Arrays/0-Arrays.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_02_Arrays/0-Arrays.pdf -------------------------------------------------------------------------------- /Lessons/Lesson_02_Arrays/OddOccurrencesInArray/task1/solution.js: -------------------------------------------------------------------------------- 1 | /** 2 | A non-empty array A consisting of N integers is given. 3 | The array contains an odd number of elements, 4 | and each element of the array can be paired with another element 5 | that has the same value, except for one element that is left unpaired. 6 | 7 | For example, in array A such that: 8 | 9 | A[0] = 9 A[1] = 3 A[2] = 9 10 | A[3] = 3 A[4] = 9 A[5] = 7 11 | A[6] = 9 12 | 13 | - the elements at indexes 0 and 2 have value 9, 14 | - the elements at indexes 1 and 3 have value 3, 15 | - the elements at indexes 4 and 6 have value 9, 16 | - the element at index 5 has value 7 and is unpaired. 17 | 18 | Write a function: 19 | 20 | function solution(A); 21 | 22 | that, given an array A consisting of N integers fulfilling the above 23 | conditions, returns the value of the unpaired element. 24 | 25 | For example, given array A such that: 26 | 27 | A[0] = 9 A[1] = 3 A[2] = 9 28 | A[3] = 3 A[4] = 9 A[5] = 7 29 | A[6] = 9 30 | the function should return 7, as explained in the example above. 31 | 32 | Write an efficient algorithm for the following assumptions: 33 | - N is an odd integer within the range [1..1,000,000]; 34 | - each element of array A is an integer within the range [1..1,000,000,000]; 35 | - all but one of the values in A occur an even number of times. 36 | */ 37 | 38 | // you can write to stdout for debugging purposes, e.g. 39 | // console.log('this is a debug message'); 40 | 41 | function solution(A) { 42 | let result = 0; 43 | let map = {}; 44 | for (let i = 0; i < A.length; i++) { 45 | if (map[A[i]]) { 46 | delete map[A[i]]; 47 | } else { 48 | map[A[i]] = 1; 49 | } 50 | } 51 | for (let key in map) { 52 | result = key; 53 | } 54 | return parseInt(result); 55 | } 56 | 57 | console.log(solution([9, 3, 9, 3, 9, 7, 9])); 58 | -------------------------------------------------------------------------------- /Lessons/Lesson_03_Time_Complexity/1-TimeComplexity.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_03_Time_Complexity/1-TimeComplexity.pdf -------------------------------------------------------------------------------- /Lessons/Lesson_03_Time_Complexity/FrogJmp/task1/solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | A small frog wants to get to the other side of the road. 3 | The frog is currently located at position X and 4 | wants to get to a position greater than or equal to Y. 5 | The small frog always jumps a fixed distance, D. 6 | 7 | Count the minimal number of jumps that 8 | the small frog must perform to reach its target. 9 | 10 | Write a function: 11 | 12 | def solution(X, Y, D) 13 | 14 | that, given three integers X, Y and D, 15 | returns the minimal number of jumps from 16 | position X to a position equal to or greater than Y. 17 | 18 | For example, given: 19 | 20 | X = 10 21 | Y = 85 22 | D = 30 23 | 24 | the function should return 3, because the frog will be positioned as follows: 25 | - after the first jump, at position 10 + 30 = 40 26 | - after the second jump, at position 10 + 30 + 30 = 70 27 | - after the third jump, at position 10 + 30 + 30 + 30 = 100 28 | 29 | Write an efficient algorithm for the following assumptions: 30 | - X, Y and D are integers within the range [1..1,000,000,000]; 31 | - X ≤ Y. 32 | """ 33 | 34 | # you can write to stdout for debugging purposes, e.g. 35 | # print("this is a debug message") 36 | 37 | def solution(X, Y, D): 38 | return (Y - X) // D + int((Y - X) % D != 0) 39 | -------------------------------------------------------------------------------- /Lessons/Lesson_03_Time_Complexity/PermMissingElem/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_03_Time_Complexity/PermMissingElem/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_03_Time_Complexity/TapeEquilibrium/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_03_Time_Complexity/TapeEquilibrium/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_04_Counting_Elements/2-CountingElements.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_04_Counting_Elements/2-CountingElements.pdf -------------------------------------------------------------------------------- /Lessons/Lesson_04_Counting_Elements/FrogRiverOne/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_04_Counting_Elements/FrogRiverOne/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_04_Counting_Elements/PermCheck/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_04_Counting_Elements/PermCheck/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_05_Prefix_Sums/3-PrefixSums.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_05_Prefix_Sums/3-PrefixSums.pdf -------------------------------------------------------------------------------- /Lessons/Lesson_05_Prefix_Sums/PassingCars/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_05_Prefix_Sums/PassingCars/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_06_Sorting/4-Sorting.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_06_Sorting/4-Sorting.pdf -------------------------------------------------------------------------------- /Lessons/Lesson_06_Sorting/Distinct/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_06_Sorting/Distinct/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_06_Sorting/MaxProductOfThree/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_06_Sorting/MaxProductOfThree/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_06_Sorting/Triangle/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_06_Sorting/Triangle/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_07_Stacks_and_Queues/5-Stacks.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_07_Stacks_and_Queues/5-Stacks.pdf -------------------------------------------------------------------------------- /Lessons/Lesson_07_Stacks_and_Queues/Brackets/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_07_Stacks_and_Queues/Brackets/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_07_Stacks_and_Queues/Fish/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_07_Stacks_and_Queues/Fish/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_07_Stacks_and_Queues/Nesting/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_07_Stacks_and_Queues/Nesting/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_07_Stacks_and_Queues/StoneWall/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_07_Stacks_and_Queues/StoneWall/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_08_Leader/6-Leader.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_08_Leader/6-Leader.pdf -------------------------------------------------------------------------------- /Lessons/Lesson_08_Leader/Dominator/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_08_Leader/Dominator/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_08_Leader/EquiLeader/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_08_Leader/EquiLeader/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_09_Maximum_slice_problem/7-MaxSlice.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_09_Maximum_slice_problem/7-MaxSlice.pdf -------------------------------------------------------------------------------- /Lessons/Lesson_09_Maximum_slice_problem/MaxProfit/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_09_Maximum_slice_problem/MaxProfit/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_09_Maximum_slice_problem/MaxSliceSum/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_09_Maximum_slice_problem/MaxSliceSum/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_10_Prime_and_composite_numbers/8-PrimeNumbers.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_10_Prime_and_composite_numbers/8-PrimeNumbers.pdf -------------------------------------------------------------------------------- /Lessons/Lesson_10_Prime_and_composite_numbers/CountFactors/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_10_Prime_and_composite_numbers/CountFactors/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_10_Prime_and_composite_numbers/MinPerimeterRectangle/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_10_Prime_and_composite_numbers/MinPerimeterRectangle/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_11_Sieve_of_Eratosthenes/9-Sieve.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_11_Sieve_of_Eratosthenes/9-Sieve.pdf -------------------------------------------------------------------------------- /Lessons/Lesson_12_Euclidean_algorithm/10-Gcd.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_12_Euclidean_algorithm/10-Gcd.pdf -------------------------------------------------------------------------------- /Lessons/Lesson_12_Euclidean_algorithm/ChocolatesByNumbers/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_12_Euclidean_algorithm/ChocolatesByNumbers/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_13_Fibonacci_numbers/11-Fibonacci.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_13_Fibonacci_numbers/11-Fibonacci.pdf -------------------------------------------------------------------------------- /Lessons/Lesson_14_Binary_search_algorithm/12-BinarySearch.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_14_Binary_search_algorithm/12-BinarySearch.pdf -------------------------------------------------------------------------------- /Lessons/Lesson_15_Caterpillar_method/13-CaterpillarMethod.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_15_Caterpillar_method/13-CaterpillarMethod.pdf -------------------------------------------------------------------------------- /Lessons/Lesson_15_Caterpillar_method/AbsDistinct/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_15_Caterpillar_method/AbsDistinct/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_15_Caterpillar_method/CountDistinctSlices/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_15_Caterpillar_method/CountDistinctSlices/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_15_Caterpillar_method/CountTriangles/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_15_Caterpillar_method/CountTriangles/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_16_Greedy_algorithms/14-GreedyAlgorithms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_16_Greedy_algorithms/14-GreedyAlgorithms.pdf -------------------------------------------------------------------------------- /Lessons/Lesson_16_Greedy_algorithms/MaxNonoverlappingSegments/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_16_Greedy_algorithms/MaxNonoverlappingSegments/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_16_Greedy_algorithms/TieRopes/task1/solution.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_16_Greedy_algorithms/TieRopes/task1/solution.py -------------------------------------------------------------------------------- /Lessons/Lesson_17_Dynamic_programming/15-DynamicProgramming.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/formidablae/Codility/abdbe9f74718733595a3d7d7a1b641cd963bbeea/Lessons/Lesson_17_Dynamic_programming/15-DynamicProgramming.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Codility 2 | 3 | Code written while solving exercises and challenges on Codility [codility.com](https://www.codility.com/) . 4 | 5 | Codility is a technical recruitment platform for teams to test the coding skills of developers. 6 | --------------------------------------------------------------------------------