├── .gitignore ├── LICENSE ├── README.md ├── basic-data-types ├── FindTheRunnerUpScore.py ├── ListComprehension.py ├── Lists.py ├── finding-the-percentage.py ├── nested-lists.py └── tuples.py ├── built-ins ├── any-or-all.py ├── athlete-sort.py ├── ginortS.py ├── input.py ├── python-evaluation.py └── zipped.py ├── classes ├── class-2-find-the-torsional-angle.py └── dealing-with-complex-numbers.py ├── closures-and-decorators ├── decorator-2-name-directory.py └── standardize-mobile-numbers-using-decorator.py ├── collections ├── collections-counter.py ├── collections-dequeue.py ├── collections-named-tuple.py ├── collections-ordered-dict.py ├── company-logo.py ├── default-dict-tutorial.py ├── piling-up.py └── word-order.py ├── date-and-time ├── calendar-module.py └── time-delta.py ├── debugging ├── default-arguments.py └── word-score.py ├── errors-and-exceptions ├── exceptions.py └── incorrect-regex.py ├── introduction ├── ArithmeticOperator.py ├── Division.py ├── Loops.py ├── PrintFunction.py ├── PythonIfElse.py ├── SayHelloWorldWithPython.py └── WriteAFunction.py ├── itertools ├── compress-the-string.py ├── iterables-and-iterators.py ├── itertools-combinations-with-replacements.py ├── itertools-combinations.py ├── itertools-permutations.py ├── itertools-product.py └── maximize-it.py ├── math ├── find-angle-mbc.py ├── integers-come-in-all-sizes.py ├── mod-divmod.py ├── polar-coordinates.py ├── power-mod-power.py ├── triangle-quest-2.py └── triangle-quest.py ├── numpy-python ├── array-mathematics.py ├── arrays.py ├── concatenate.py ├── dot-cross.py ├── eye-and-identity.py ├── flatten-and-transpose.py ├── floor-ciel-and-rint.py ├── inner-outer.py ├── linear-algebra.py ├── mean-var-std.py ├── min-max.py ├── polynomials.py ├── shape-and-reshape.py ├── sum-and-prod.py └── zeroes-and-ones.py ├── python-functions ├── map-and-lambda-functions.py ├── reduce-function.py └── validating-email-addresses.py ├── regex-and-parsing ├── detect-floating-point-numbers.py ├── detect-html-tags-attributes.py ├── group-groups-groupdict.py ├── hex-color-code.py ├── html-parser-part-1.py ├── html-parser-part-2.py ├── matrix-script.py ├── re-findall-and-finditer.py ├── re-split.py ├── re-start-end.py ├── regex-substitution.py ├── validating-credit-card-numbers.py ├── validating-parsing-email-address.py ├── validating-phone-numbers.py ├── validating-postal-codes.py ├── validating-roman-numerals.py └── validating-uid.py ├── sets ├── check-subset.py ├── introduction-to-sets.py ├── is-strict-superset.py ├── no-idea.py ├── set-add.py ├── set-difference.py ├── set-disacard-remove-pop.py ├── set-intersection.py ├── set-mutations.py ├── set-symmetric-difference.py ├── set-union.py ├── symmetric-difference.py └── the-captains-room.py ├── strings ├── alphabet-rangoli.py ├── capitalize!.py ├── designer-door-mat.py ├── find-a-string.py ├── merge-the-tools.py ├── mutations.py ├── string-formatting.py ├── string-split-and-join.py ├── string-validators.py ├── swap-case.py ├── text-alignment.py ├── text-wrap.py ├── the-minion-game.py └── whats-your-name.py ├── test.py └── xml ├── xml-1-find-the-score.py └── xml-2-find-max-depth.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | envs/ 3 | venv 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2020 anishLearnsToCode 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 4 | documentation files (the "Software"), to deal in the Software without restriction, including without limitation the 5 | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit 6 | persons to whom the Software is furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the 9 | Software. 10 | 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 12 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 13 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 14 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HackerRank Python 🐍 Domain Solutions 2 | 3 | ![problems-solved](https://img.shields.io/badge/Problems%20Solved-115/115-1abc9c.svg) 4 | [![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming) 5 | 6 | HackerRank is an online platform that offers programmers the ability to test their 7 | sills, practice and learn something new. The problem sets on HackerRank are divided in Domains & Tutorials, 8 | and the following are the domains & tutorials on HackerRank: 9 | 10 |
11 | Domains 12 | 28 |
29 | 30 |
31 | Tutorials 32 | 38 |
39 | 40 | This repository contains solutions to the [Python domain](https://www.hackerrank.com/domains/python) 41 | part of HackerRank. My HackerRank profile can be viewed [here](https://www.hackerrank.com/anishviewer). 42 | 43 | I have also solved other domains on HackerRank that can be viewed at: 44 | 45 | | Domain | Solutions Repository Link | 46 | |--------|---------------------------| 47 | | [Algorithms](https://www.hackerrank.com/domains/algorithms) | [Solutions Repository](https://github.com/anishLearnsToCode/hackerrank-algorithms) | 48 | | [Data Structures](https://www.hackerrank.com/domains/data-structures) | [Solutions Repository](https://github.com/anishLearnsToCode/hackerrank-data-structures) | 49 | | [Java](https://www.hackerrank.com/domains/java) | [Solutions Repository](https://github.com/anishLearnsToCode/hackerrank-java) | 50 | | [Interview Preparation Kit](https://www.hackerrank.com/interview) | [Solutions Repository](https://github.com/anishLearnsToCode/hackerrank-interview-preparation-kit) | 51 | 52 | The Python Domain is further Divided into the following sub-domains. 53 | 54 | ## Sub Domains & Problems (+Solutions) in the Python Domain 55 | Quick Links ⚡ : 56 | [Introduction](#introduction-) | 57 | [Basic Data Types](#basic-data-types-) | 58 | [Strings](#strings-) | 59 | [Sets](#sets-) | 60 | [Math](#math-) | 61 | [Itertools](#itertools-) | 62 | [Collections](#collections-) | 63 | [Data and Time](#date-and-time-) | 64 | [Errors and Exceptions](#errors-and-exceptions-) | 65 | [Classes](#classes-) | 66 | [Built-Ins](#built-ins-) | 67 | [Python Functionals](#python-functionals-) | 68 | [Regex and Parsing](#regex-and-parsing-) | 69 | [XML](#xml-) | 70 | [Closures and Decorators](#closures-and-decorators-) | 71 | [NumPy](#numpy-) | 72 | [Debugging](#debugging-) | 73 | 74 | ### Introduction [👆](#sub-domains--problems-solutions-in-the-python-domain) 75 | | Problem | Difficulty | Solution Link | 76 | |---------|------------|---------------| 77 | | [Say "Hello World" With Python](https://www.hackerrank.com/challenges/py-hello-world) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/introduction/SayHelloWorldWithPython.py) | 78 | | [Python If-Else](https://www.hackerrank.com/challenges/py-if-else) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/introduction/PythonIfElse.py) | 79 | | [Arithmetic Operators](https://www.hackerrank.com/challenges/python-arithmetic-operators) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/introduction/ArithmeticOperator.py) | 80 | | [Python Division](https://www.hackerrank.com/challenges/python-division) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/introduction/Division.py) | 81 | | [Loops](https://www.hackerrank.com/challenges/python-loops) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/introduction/Loops.py)| 82 | | [Write a Function](https://www.hackerrank.com/challenges/write-a-function) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/introduction/WriteAFunction.py) | 83 | | [Print Function](https://www.hackerrank.com/challenges/python-print) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/introduction/PrintFunction.py) | 84 | 85 | 86 | ### Basic Data Types [👆](#sub-domains--problems-solutions-in-the-python-domain) 87 | | Problem | Difficulty | Solution Link | 88 | |---------|------------|---------------| 89 | | [List Comprehensions](https://www.hackerrank.com/challenges/list-comprehensions) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/basic-data-types/ListComprehension.py) | 90 | | [Find the Runner-Up Score](https://www.hackerrank.com/challenges/find-second-maximum-number-in-a-list) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/basic-data-types/FindTheRunnerUpScore.py) | 91 | | [Nested Lists](https://www.hackerrank.com/challenges/nested-list) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/basic-data-types/nested-lists.py) | 92 | | [Finding The Percentage](https://www.hackerrank.com/challenges/finding-the-percentage) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/basic-data-types/finding-the-percentage.py) | 93 | | [Lists](https://www.hackerrank.com/challenges/python-lists) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/basic-data-types/Lists.py) | 94 | | [Tuples](https://www.hackerrank.com/challenges/python-tuples) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/basic-data-types/tuples.py) | 95 | 96 | 97 | ### Strings [👆](#sub-domains--problems-solutions-in-the-python-domain) 98 | | Problem | Difficulty | Solution Link | 99 | |---------|------------|---------------| 100 | | [sWAP cASE](https://www.hackerrank.com/challenges/swap-case) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/strings/swap-case.py) | 101 | | [String Split and Join](https://www.hackerrank.com/challenges/python-string-split-and-join) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/strings/string-split-and-join.py) | 102 | | [What's Your Name](https://www.hackerrank.com/challenges/whats-your-name) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/strings/whats-your-name.py) | 103 | | [Mutations](https://www.hackerrank.com/challenges/python-mutations) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/strings/mutations.py) | 104 | | [Find a String](https://www.hackerrank.com/challenges/find-a-string) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/strings/find-a-string.py) | 105 | | [String Validators](https://www.hackerrank.com/challenges/string-validators) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/strings/string-validators.py) | 106 | | [Text Alignment](https://www.hackerrank.com/challenges/text-alignment) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/strings/text-alignment.py) | 107 | | [Text Wrap](https://www.hackerrank.com/challenges/text-wrap) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/strings/text-wrap.py) | 108 | | [Designer Door Mat](https://www.hackerrank.com/challenges/designer-door-mat) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/strings/designer-door-mat.py) | 109 | | [String Formatting](https://www.hackerrank.com/challenges/python-string-formatting) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/strings/string-formatting.py) | 110 | | [Alphabet Rangoli](https://www.hackerrank.com/challenges/alphabet-rangoli) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/strings/alphabet-rangoli.py) | 111 | | [Capatalize!](https://www.hackerrank.com/challenges/capitalize) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/strings/capitalize!.py) | 112 | | [The Minion Game](https://www.hackerrank.com/challenges/the-minion-game) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/strings/the-minion-game.py) | 113 | | [Merge The Tools](https://www.hackerrank.com/challenges/merge-the-tools) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/strings/merge-the-tools.py) | 114 | 115 | 116 | ### Sets [👆](#sub-domains--problems-solutions-in-the-python-domain) 117 | | Problem | Difficulty | Solution Link | 118 | |---------|------------|---------------| 119 | | [Introduction to Sets](https://www.hackerrank.com/challenges/py-introduction-to-sets) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/sets/introduction-to-sets.py) | 120 | | [No Idea!](https://www.hackerrank.com/challenges/no-idea) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/sets/no-idea.py) | 121 | | [Symmetric Difference](https://www.hackerrank.com/challenges/symmetric-difference) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/sets/symmetric-difference.py) | 122 | | [Set .add()](https://www.hackerrank.com/challenges/py-set-add) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/sets/set-add.py) | 123 | | [Set .discard() .remove() & .pop()](https://www.hackerrank.com/challenges/py-set-discard-remove-pop) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/sets/set-disacard-remove-pop.py) | 124 | | [Set .union() Operation](https://www.hackerrank.com/challenges/py-set-union) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/sets/set-union.py) | 125 | | [Set .intersection() Operation](https://www.hackerrank.com/challenges/py-set-intersection-operation) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/sets/set-intersection.py) | 126 | | [Set .difference() Operation](https://www.hackerrank.com/challenges/py-set-difference-operation) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/sets/set-difference.py) | 127 | | [Set .symmetric_difference() Operation](https://www.hackerrank.com/challenges/py-set-symmetric-difference-operation) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/sets/set-symmetric-difference.py) | 128 | | [Set Mutations](https://www.hackerrank.com/challenges/py-set-mutations) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/sets/set-mutations.py) | 129 | | [Tha Captain's Room](https://www.hackerrank.com/challenges/py-the-captains-room) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/sets/the-captains-room.py) | 130 | | [Check Subset](https://www.hackerrank.com/challenges/py-check-subset) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/sets/check-subset.py) | 131 | | [Check Strict Superset](https://www.hackerrank.com/challenges/py-check-strict-superset) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/sets/is-strict-superset.py) | 132 | 133 | 134 | ### Math [👆](#sub-domains--problems-solutions-in-the-python-domain) 135 | | Problem | Difficulty | Solution Link | 136 | |---------|------------|---------------| 137 | | [Polar Coordinates](https://www.hackerrank.com/challenges/polar-coordinates) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/math/polar-coordinates.py) | 138 | | [Find Angle MBC](https://www.hackerrank.com/challenges/find-angle) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/math/find-angle-mbc.py) | 139 | | [Triangle Quest 2](https://www.hackerrank.com/challenges/triangle-quest-2) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/math/triangle-quest-2.py) | 140 | | [Mod Divmod](https://www.hackerrank.com/challenges/python-mod-divmod) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/math/mod-divmod.py) | 141 | | [Power - Mod Power](https://www.hackerrank.com/challenges/python-power-mod-power) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/math/power-mod-power.py) | 142 | | [Integers Come in all sizes](https://www.hackerrank.com/challenges/python-integers-come-in-all-sizes) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/math/integers-come-in-all-sizes.py) | 143 | | [Triangle Quest](https://www.hackerrank.com/challenges/python-quest-1) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/math/triangle-quest.py) | 144 | 145 | 146 | ### Itertools [👆](#sub-domains--problems-solutions-in-the-python-domain) 147 | | Problem | Difficulty | Solution Link | 148 | |---------|------------|---------------| 149 | | [itertools.product()](https://www.hackerrank.com/challenges/itertools-product) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/itertools/itertools-product.py) | 150 | | [itertools.permutations()](https://www.hackerrank.com/challenges/itertools-permutations) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/itertools/itertools-permutations.py) | 151 | | [itertools.combinations()](https://www.hackerrank.com/challenges/itertools-combinations) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/itertools/itertools-combinations.py) | 152 | | [itertools.combinations_with_replacement()](https://www.hackerrank.com/challenges/itertools-combinations-with-replacement) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/itertools/itertools-combinations-with-replacements.py) | 153 | | [Compress the String!](https://www.hackerrank.com/challenges/compress-the-string) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/itertools/compress-the-string.py) | 154 | | [Iterables & Iterators](https://www.hackerrank.com/challenges/iterables-and-iterators) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/itertools/iterables-and-iterators.py) | 155 | | [Maximize It!](https://www.hackerrank.com/challenges/maximize-it) | Hard | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/itertools/maximize-it.py) | 156 | 157 | 158 | ### Collections [👆](#sub-domains--problems-solutions-in-the-python-domain) 159 | | Problem | Difficulty | Solution Link | 160 | |---------|------------|---------------| 161 | | [collections.Counter()](https://www.hackerrank.com/challenges/collections-counter) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/collections/collections-counter.py) | 162 | | [DefaultDict Tutorial](https://www.hackerrank.com/challenges/defaultdict-tutorial) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/collections/default-dict-tutorial.py) | 163 | | [Collections.namedTuple()](https://www.hackerrank.com/challenges/py-collections-namedtuple) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/collections/collections-named-tuple.py) | 164 | | [Collections.orderedDict()](https://www.hackerrank.com/challenges/py-collections-ordereddict) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/collections/collections-ordered-dict.py) | 165 | | [Word Order](https://www.hackerrank.com/challenges/word-order) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/collections/word-order.py) | 166 | | [Collections.dequeue()](https://www.hackerrank.com/challenges/py-collections-deque) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/collections/collections-dequeue.py) | 167 | | [Company Logo](https://www.hackerrank.com/challenges/most-commons) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/collections/company-logo.py) | 168 | | [Piling Up!](https://www.hackerrank.com/challenges/piling-up) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/collections/piling-up.py) | 169 | 170 | 171 | ### Date and Time [👆](#sub-domains--problems-solutions-in-the-python-domain) 172 | | Problem | Difficulty | Solution Link | 173 | |---------|------------|---------------| 174 | | [Calender Module](https://www.hackerrank.com/challenges/calendar-module) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/date-and-time/calendar-module.py) | 175 | | [Time Delta](https://www.hackerrank.com/challenges/python-time-delta) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/date-and-time/time-delta.py) | 176 | 177 | 178 | ### Errors and Exceptions [👆](#sub-domains--problems-solutions-in-the-python-domain) 179 | | Problem | Difficulty | Solution Link | 180 | |---------|------------|---------------| 181 | | [Exceptions](https://www.hackerrank.com/challenges/exceptions) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/errors-and-exceptions/exceptions.py) | 182 | | [Incorrect Regex](https://www.hackerrank.com/challenges/incorrect-regex) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/errors-and-exceptions/incorrect-regex.py) | 183 | 184 | 185 | ### Classes [👆](#sub-domains--problems-solutions-in-the-python-domain) 186 | | Problem | Difficulty | Solution Link | 187 | |---------|------------|---------------| 188 | | [Class 2 - Find the Torsional Angle](https://www.hackerrank.com/challenges/class-2-find-the-torsional-angle) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/classes/class-2-find-the-torsional-angle.py) | 189 | | [Classes: Dealing with Complex Numbers](https://www.hackerrank.com/challenges/class-1-dealing-with-complex-numbers) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/classes/dealing-with-complex-numbers.py) | 190 | 191 | 192 | ### Built Ins [👆](#sub-domains--problems-solutions-in-the-python-domain) 193 | | Problem | Difficulty | Solution Link | 194 | |---------|------------|---------------| 195 | | [Zipped!](https://www.hackerrank.com/challenges/zipped) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/built-ins/zipped.py) | 196 | | [Input()](https://www.hackerrank.com/challenges/input) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/built-ins/input.py) | 197 | | [Python Evaluation](https://www.hackerrank.com/challenges/python-eval) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/built-ins/python-evaluation.py) | 198 | | [Athlete Sort](https://www.hackerrank.com/challenges/python-sort-sort) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/built-ins/athlete-sort.py) | 199 | | [Any or All](https://www.hackerrank.com/challenges/any-or-all) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/built-ins/any-or-all.py) | 200 | | [ginortS](https://www.hackerrank.com/challenges/ginorts) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/built-ins/ginortS.py) | 201 | 202 | 203 | ### Python Functionals [👆](#sub-domains--problems-solutions-in-the-python-domain) 204 | | Problem | Difficulty | Solution Link | 205 | |---------|------------|---------------| 206 | | [Map and Lambda Function](https://www.hackerrank.com/challenges/map-and-lambda-expression) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/python-functions/map-and-lambda-functions.py) | 207 | | [Validating Email Address with Filter](https://www.hackerrank.com/challenges/validate-list-of-email-address-with-filter) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/python-functions/validating-email-addresses.py) | 208 | | [Reduce Function](https://www.hackerrank.com/challenges/reduce-function) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/python-functions/reduce-function.py) | 209 | 210 | 211 | ### Regex and Parsing [👆](#sub-domains--problems-solutions-in-the-python-domain) 212 | | Problem | Difficulty | Solution Link | 213 | |---------|------------|---------------| 214 | | [Detect Floating Point Number](https://www.hackerrank.com/challenges/introduction-to-regex) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/regex-and-parsing/detect-floating-point-numbers.py) | 215 | | [Re.split()](https://www.hackerrank.com/challenges/re-split) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/regex-and-parsing/re-split.py) | 216 | | [Group(), Groups() and Groupdict()](https://www.hackerrank.com/challenges/re-group-groups) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/regex-and-parsing/group-groups-groupdict.py) | 217 | | [Re.findall() & Re.finditer()](https://www.hackerrank.com/challenges/re-findall-re-finditer) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/regex-and-parsing/re-findall-and-finditer.py) | 218 | | [Re.start() and Re.end()](https://www.hackerrank.com/challenges/re-start-re-end) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/regex-and-parsing/re-start-end.py) | 219 | | [Regex Substitution](https://www.hackerrank.com/challenges/re-sub-regex-substitution) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/regex-and-parsing/regex-substitution.py) | 220 | | [Validating Roman Numerals](https://www.hackerrank.com/challenges/validate-a-roman-number) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/regex-and-parsing/validating-roman-numerals.py) | 221 | | [Validating Phone Numbers](https://www.hackerrank.com/challenges/validating-the-phone-number) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/regex-and-parsing/validating-phone-numbers.py) | 222 | | [Validating and Parsing Email Addresses](https://www.hackerrank.com/challenges/validating-named-email-addresses) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/regex-and-parsing/validating-parsing-email-address.py) | 223 | | [Hex Color Code](https://www.hackerrank.com/challenges/hex-color-code) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/regex-and-parsing/hex-color-code.py) | 224 | | [HTML Parser - Part 1](https://www.hackerrank.com/challenges/html-parser-part-1) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/regex-and-parsing/html-parser-part-1.py) | 225 | | [HTML Parser - Part 2](https://www.hackerrank.com/challenges/html-parser-part-2) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/regex-and-parsing/html-parser-part-2.py) | 226 | | [Detect HTML Tags, Attributes and Attribute Values](https://www.hackerrank.com/challenges/detect-html-tags-attributes-and-attribute-values) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/regex-and-parsing/detect-html-tags-attributes.py) | 227 | | [Validating UID](https://www.hackerrank.com/challenges/validating-uid) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/regex-and-parsing/validating-uid.py) | 228 | | [Validating Credit Card Numbers](https://www.hackerrank.com/challenges/validating-credit-card-number) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/regex-and-parsing/validating-credit-card-numbers.py) | 229 | | [Validating Postal Codes](https://www.hackerrank.com/challenges/validating-postalcode) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/regex-and-parsing/validating-postal-codes.py) | 230 | | [Matrix Script](https://www.hackerrank.com/challenges/matrix-script) | Hard | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/regex-and-parsing/matrix-script.py) | 231 | 232 | 233 | ### XML [👆](#sub-domains--problems-solutions-in-the-python-domain) 234 | | Problem | Difficulty | Solution Link | 235 | |---------|------------|---------------| 236 | | [XML 1 - Find the Score](https://www.hackerrank.com/challenges/xml-1-find-the-score) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/xml/xml-1-find-the-score.py) | 237 | | [XML 2 - Find the Maximum Depth](https://www.hackerrank.com/challenges/xml2-find-the-maximum-depth) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/xml/xml-2-find-max-depth.py) | 238 | 239 | 240 | ### Closures and Decorators [👆](#sub-domains--problems-solutions-in-the-python-domain) 241 | | Problem | Difficulty | Solution Link | 242 | |---------|------------|---------------| 243 | | [Standardize Mobile Numbers Using Decorators](https://www.hackerrank.com/challenges/standardize-mobile-number-using-decorators) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/closures-and-decorators/standardize-mobile-numbers-using-decorator.py) | 244 | | [Decorators 2 - Name Directory](https://www.hackerrank.com/challenges/decorators-2-name-directory) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/closures-and-decorators/decorator-2-name-directory.py) | 245 | 246 | 247 | ### Numpy [👆](#sub-domains--problems-solutions-in-the-python-domain) 248 | | Problem | Difficulty | Solution Link | 249 | |---------|------------|---------------| 250 | | [Arrays](https://www.hackerrank.com/challenges/np-arrays) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/numpy-python/arrays.py) | 251 | | [Shape and Reshape](https://www.hackerrank.com/challenges/np-shape-reshape) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/numpy-python/shape-and-reshape.py) | 252 | | [Transpose and Flatten](https://www.hackerrank.com/challenges/np-transpose-and-flatten) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/numpy-python/flatten-and-transpose.py) | 253 | | [Concatenate](https://www.hackerrank.com/challenges/np-concatenate) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/numpy-python/concatenate.py) | 254 | | [Zeroes and Ones](https://www.hackerrank.com/challenges/np-zeros-and-ones) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/numpy-python/zeroes-and-ones.py) | 255 | | [Eye and Identity](https://www.hackerrank.com/challenges/np-eye-and-identity) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/numpy-python/eye-and-identity.py) | 256 | | [Array Mathematics](https://www.hackerrank.com/challenges/np-array-mathematics) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/numpy-python/array-mathematics.py) | 257 | | [Floor, Ciel and Rint](https://www.hackerrank.com/challenges/floor-ceil-and-rint) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/numpy-python/floor-ciel-and-rint.py) | 258 | | [Sum and Prod](https://www.hackerrank.com/challenges/np-sum-and-prod) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/numpy-python/sum-and-prod.py) | 259 | | [Min and Max](https://www.hackerrank.com/challenges/np-min-and-max) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/numpy-python/min-max.py) | 260 | | [Mean, var and Std](https://www.hackerrank.com/challenges/np-mean-var-and-std) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/numpy-python/mean-var-std.py) | 261 | | [Dot and Cross](https://www.hackerrank.com/challenges/np-dot-and-cross) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/numpy-python/dot-cross.py) | 262 | | [Inner and Outer](https://www.hackerrank.com/challenges/np-inner-and-outer) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/numpy-python/inner-outer.py) | 263 | | [Polynomials](https://www.hackerrank.com/challenges/np-polynomials) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/numpy-python/polynomials.py) | 264 | | [Linear Algebra](https://www.hackerrank.com/challenges/np-linear-algebra) | Easy | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/numpy-python/linear-algebra.py) | 265 | 266 | 267 | 268 | ### Debugging [👆](#sub-domains--problems-solutions-in-the-python-domain) 269 | | Problem | Difficulty | Solution Link | 270 | |---------|------------|---------------| 271 | | [Word Score](https://www.hackerrank.com/challenges/words-score) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/debugging/word-score.py) | 272 | | [Default Arguments](https://www.hackerrank.com/challenges/default-arguments) | Medium | [Solution](https://github.com/anishLearnsToCode/hackerrank-python/blob/master/debugging/default-arguments.py) | 273 | -------------------------------------------------------------------------------- /basic-data-types/FindTheRunnerUpScore.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/find-second-maximum-number-in-a-list/problem 2 | 3 | 4 | def second_highest(array): 5 | array.sort() 6 | maximum = array[len(array) - 1] 7 | for i in range(len(array)): 8 | if array[len(array) - 1 - i] < maximum: 9 | return array[len(array) - 1 - i] 10 | 11 | 12 | n = int(input()) 13 | array = list(map(int, input().split())) 14 | print(second_highest(array)) 15 | -------------------------------------------------------------------------------- /basic-data-types/ListComprehension.py: -------------------------------------------------------------------------------- 1 | x = int(input()) 2 | y = int(input()) 3 | z = int(input()) 4 | n = int(input()) 5 | 6 | print([[a, b, c] for a in range(x + 1) for b in range(y + 1) for c in range(z + 1) if a + b + c != n]) 7 | -------------------------------------------------------------------------------- /basic-data-types/Lists.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/python-lists/problem 2 | 3 | 4 | queries = int(input()) 5 | data = [] 6 | for _ in range(queries): 7 | command, *parameter = input().split() 8 | parameters = list(map(int, parameter)) 9 | 10 | if command == 'insert': 11 | data.insert(parameters[0], parameters[1]) 12 | elif command == 'print': 13 | print(data) 14 | elif command == 'remove': 15 | data.remove(parameters[0]) 16 | elif command == 'append': 17 | data.append(parameters[0]) 18 | elif command == 'sort': 19 | data.sort() 20 | elif command == 'pop': 21 | data.pop() 22 | elif command == 'reverse': 23 | data.reverse() -------------------------------------------------------------------------------- /basic-data-types/finding-the-percentage.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/finding-the-percentage/problem 2 | 3 | 4 | def average(marks): 5 | return "{0:.2f}".format(sum(marks) / 3) 6 | 7 | 8 | n = int(input()) 9 | student_marks = {} 10 | for _ in range(n): 11 | name, *line = input().split() 12 | scores = list(map(float, line)) 13 | student_marks[name] = scores 14 | 15 | 16 | query_name = input() 17 | print(average(student_marks[query_name])) 18 | -------------------------------------------------------------------------------- /basic-data-types/nested-lists.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/nested-list/problem 2 | 3 | def get_grade(element): 4 | return element[1] 5 | 6 | 7 | def get_second_min_grade(array, min_grade): 8 | for i in range(len(array)): 9 | if array[i][1] != min_grade: 10 | return array[i][1] 11 | 12 | 13 | def get_names_with_second_min_grade(array, second_min_grade): 14 | names = [] 15 | for i in range(len(array)): 16 | if array[i][1] == second_min_grade: 17 | names.append(array[i][0]) 18 | 19 | return names 20 | 21 | 22 | students = int(input()) 23 | data = [] 24 | 25 | for i in range(students): 26 | name = input() 27 | grade = float(input()) 28 | data.append([name, grade]) 29 | 30 | data.sort(key=get_grade) 31 | min_grade = data[0][1] 32 | print('min grade:', min_grade) 33 | 34 | second_min_grade = get_second_min_grade(data, min_grade) 35 | print('second min:', second_min_grade) 36 | 37 | names_with_second_min_grade = sorted(get_names_with_second_min_grade(data, second_min_grade)) 38 | 39 | for name in names_with_second_min_grade: 40 | print(name) 41 | -------------------------------------------------------------------------------- /basic-data-types/tuples.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/python-tuples/problem 2 | 3 | 4 | length = int(input()) 5 | tokens = input().split() 6 | data = list(map(int, tokens)) 7 | t = tuple(data) 8 | print(hash(t)) 9 | -------------------------------------------------------------------------------- /built-ins/any-or-all.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/any-or-all/problem 2 | 3 | 4 | def is_palindrome(number): 5 | return number == number[::-1] 6 | 7 | 8 | N = int(input()) 9 | array = list(input().split()) 10 | 11 | print( 12 | all(int(element) >= 0 for element in array) 13 | and any(is_palindrome(element) for element in array) 14 | ) 15 | -------------------------------------------------------------------------------- /built-ins/athlete-sort.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/python-sort-sort/problem 2 | 3 | N, M = map(int, input().split()) 4 | data = [] 5 | for _ in range(N): 6 | data.append(list(map(int, input().split()))) 7 | 8 | K = int(input()) 9 | data = sorted(data, key=lambda x: x[K]) 10 | 11 | for row in data: 12 | print(' '.join(map(str, row))) 13 | -------------------------------------------------------------------------------- /built-ins/ginortS.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/ginorts/problem 2 | 3 | line = sorted(input(), key=lambda c: (c.isdigit() - c.islower(), c in '02468', c)) 4 | print(''.join(line)) 5 | -------------------------------------------------------------------------------- /built-ins/input.py: -------------------------------------------------------------------------------- 1 | x, k = map(int, input().split()) 2 | polynomial = input() 3 | result = eval(polynomial) 4 | print(result == k) 5 | -------------------------------------------------------------------------------- /built-ins/python-evaluation.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/python-eval/problem 2 | 3 | line = input() 4 | eval(line) 5 | -------------------------------------------------------------------------------- /built-ins/zipped.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/zipped/problem 2 | 3 | N, X = map(int, input().split()) 4 | data = [] 5 | for _ in range(X): 6 | subject_marks = list(map(float, input().split())) 7 | data.append(subject_marks) 8 | 9 | tuples = zip(*data) 10 | for element in tuples: 11 | print(sum(element) / X) 12 | -------------------------------------------------------------------------------- /classes/class-2-find-the-torsional-angle.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | 4 | class Points(object): 5 | def __init__(self, x, y, z): 6 | self.x = x 7 | self.y = y 8 | self.z = z 9 | 10 | def __sub__(self, no): 11 | return Points((self.x - no.x), (self.y - no.y), (self.z - no.z)) 12 | 13 | def dot(self, no): 14 | return (self.x * no.x) + (self.y * no.y) + (self.z * no.z) 15 | 16 | def cross(self, no): 17 | return Points((self.y * no.z - self.z * no.y), (self.z * no.x - self.x * no.z), (self.x * no.y - self.y * no.x)) 18 | 19 | def absolute(self): 20 | return pow((self.x ** 2 + self.y ** 2 + self.z ** 2), 0.5) 21 | 22 | 23 | if __name__ == '__main__': 24 | points = list() 25 | for i in range(4): 26 | a = list(map(float, input().split())) 27 | points.append(a) 28 | 29 | a, b, c, d = Points(*points[0]), Points(*points[1]), Points(*points[2]), Points(*points[3]) 30 | x = (b - a).cross(c - b) 31 | y = (c - b).cross(d - c) 32 | angle = math.acos(x.dot(y) / (x.absolute() * y.absolute())) 33 | 34 | print("%.2f" % math.degrees(angle)) 35 | -------------------------------------------------------------------------------- /classes/dealing-with-complex-numbers.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | 4 | class Complex(object): 5 | def __init__(self, real, imaginary): 6 | self.real = real 7 | self.imaginary = imaginary 8 | 9 | def __add__(self, other): 10 | return Complex(self.real + other.real, self.imaginary + other.imaginary) 11 | 12 | def __sub__(self, other): 13 | return Complex(self.real - other.real, self.imaginary - other.imaginary) 14 | 15 | def __mul__(self, other): 16 | return Complex( 17 | self.real * other.real - self.imaginary * other.imaginary, 18 | self.real * other.imaginary + self.imaginary * other.real 19 | ) 20 | 21 | def __truediv__(self, other): 22 | other_mod_2 = other.mod_square() 23 | answer = Complex(self.real / other_mod_2, self.imaginary / other_mod_2) * other.conjugate() 24 | return Complex(round(answer.real, 2), round(answer.imaginary, 2)) 25 | 26 | def mod(self): 27 | return Complex(round(math.sqrt(self.mod_square()), 2), 0.00) 28 | 29 | def mod_square(self): 30 | return round(self.real * self.real + self.imaginary * self.imaginary, 2) 31 | 32 | def conjugate(self): 33 | return Complex(self.real, -self.imaginary) 34 | 35 | def __str__(self): 36 | if self.imaginary == 0: 37 | result = "%.2f+0.00i" % (self.real) 38 | elif self.real == 0: 39 | if self.imaginary >= 0: 40 | result = "0.00+%.2fi" % (self.imaginary) 41 | else: 42 | result = "0.00-%.2fi" % (abs(self.imaginary)) 43 | elif self.imaginary > 0: 44 | result = "%.2f+%.2fi" % (self.real, self.imaginary) 45 | else: 46 | result = "%.2f-%.2fi" % (self.real, abs(self.imaginary)) 47 | return result 48 | 49 | 50 | if __name__ == '__main__': 51 | c = map(float, input().split()) 52 | d = map(float, input().split()) 53 | x = Complex(*c) 54 | y = Complex(*d) 55 | print(*map(str, [x+y, x-y, x*y, x/y, x.mod(), y.mod()]), sep='\n') 56 | -------------------------------------------------------------------------------- /closures-and-decorators/decorator-2-name-directory.py: -------------------------------------------------------------------------------- 1 | def person_lister(f): 2 | def inner(people): 3 | return map(f, sorted(people, key=lambda x: int(x[2]))) 4 | 5 | return inner 6 | 7 | 8 | @person_lister 9 | def name_format(person): 10 | return ("Mr. " if person[3] == "M" else "Ms. ") + person[0] + " " + person[1] 11 | 12 | 13 | if __name__ == '__main__': 14 | people = [input().split() for i in range(int(input()))] 15 | print(*name_format(people), sep='\n') 16 | -------------------------------------------------------------------------------- /closures-and-decorators/standardize-mobile-numbers-using-decorator.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/standardize-mobile-number-using-decorators/problem 2 | 3 | 4 | def wrapper(f): 5 | def phone(l): 6 | f(["+91 "+c[-10:-5]+" "+c[-5:] for c in l]) 7 | return phone 8 | 9 | 10 | @wrapper 11 | def sort_phone(l): 12 | print(*sorted(l), sep='\n') 13 | 14 | 15 | if __name__ == '__main__': 16 | l = [input() for _ in range(int(input()))] 17 | sort_phone(l) 18 | -------------------------------------------------------------------------------- /collections/collections-counter.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/collections-counter/problem 2 | 3 | import collections 4 | 5 | number_of_shoes = int(input()) 6 | shoes = collections.Counter(list(map(int, input().split()))) 7 | queries = int(input()) 8 | 9 | profit = 0 10 | for _ in range(queries): 11 | size, price = map(int, input().split()) 12 | if size in shoes: 13 | profit += price 14 | if shoes[size] == 1: 15 | del shoes[size] 16 | else: 17 | shoes[size] -= 1 18 | 19 | print(profit) 20 | -------------------------------------------------------------------------------- /collections/collections-dequeue.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/py-collections-deque/problem 2 | 3 | import collections 4 | 5 | queue = collections.deque() 6 | 7 | queries = int(input()) 8 | 9 | for _ in range(queries): 10 | line = input() 11 | if line == 'pop': 12 | queue.pop() 13 | elif line == 'popleft': 14 | queue.popleft() 15 | else: 16 | line = line.split() 17 | command = line[0] 18 | element = int(line[1]) 19 | if command == 'append': 20 | queue.append(element) 21 | else: 22 | queue.appendleft(element) 23 | 24 | for element in queue: 25 | print(element, end=' ') 26 | -------------------------------------------------------------------------------- /collections/collections-named-tuple.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/py-collections-namedtuple/problem 2 | 3 | import collections 4 | 5 | n, columns = (int(input()), input().split()) 6 | Grade = collections.namedtuple('Grade', columns) 7 | marks = [int(Grade(*input().split()).MARKS) for _ in range(n)] 8 | print((sum(marks) / len(marks))) 9 | -------------------------------------------------------------------------------- /collections/collections-ordered-dict.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/py-collections-ordereddict/problem 2 | 3 | import collections 4 | 5 | 6 | n = int(input()) 7 | 8 | prices = collections.OrderedDict() 9 | 10 | for i in range(n): 11 | line = input().split() 12 | cost = int(line[len(line) - 1]) 13 | name = ' '.join(line[0:len(line) - 1]) 14 | if name in prices: 15 | prices[name] += cost 16 | else: 17 | prices[name] = cost 18 | 19 | 20 | for key in prices: 21 | print(key, prices[key]) 22 | -------------------------------------------------------------------------------- /collections/company-logo.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/most-commons/problem 2 | 3 | import collections 4 | 5 | name = sorted(input()) 6 | frequencies = collections.Counter(name) 7 | frequencies = sorted(frequencies.items(), key=lambda x: x[1], reverse=True) 8 | 9 | count = 0 10 | for element in frequencies: 11 | if count == 3: 12 | break 13 | print(element[0], element[1]) 14 | count += 1 15 | -------------------------------------------------------------------------------- /collections/default-dict-tutorial.py: -------------------------------------------------------------------------------- 1 | # https://github.com/anishLearnsToCode/hackerrank-python/blob/master/collections/collections-counter.py 2 | 3 | import collections 4 | 5 | 6 | n, m = map(int, input().split()) 7 | frequencies = collections.defaultdict(list) 8 | 9 | for i in range(n): 10 | word = input() 11 | frequencies[word].append(i + 1) 12 | 13 | for _ in range(m): 14 | word = input() 15 | if len(frequencies[word]) > 0: 16 | print(' '.join(map(str, frequencies[word]))) 17 | else: 18 | print(-1) 19 | -------------------------------------------------------------------------------- /collections/piling-up.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/piling-up/problem 2 | 3 | def get_list(): 4 | return list(map(int, input().split())) 5 | 6 | 7 | def can_clear_stack(stack, array): 8 | if len(stack) > 0: 9 | for index in range(0, len(stack)): 10 | reverse_index = len(stack) - 1 - index 11 | if stack[reverse_index] < array[len(array) - 1]: 12 | return False 13 | array.append(stack[reverse_index]) 14 | 15 | return True 16 | 17 | 18 | def is_stackable(array): 19 | min_index = array.index(min(array)) 20 | left_stack = array[0: min_index] 21 | middle_stack = [array[min_index]] 22 | right_stack = array[min_index + 1:][::-1] 23 | 24 | i = len(left_stack) - 1 25 | j = len(right_stack) - 1 26 | 27 | while i >= 0 and j >= 0: 28 | next = min(left_stack[i], right_stack[j]) 29 | if next >= middle_stack[len(middle_stack) - 1]: 30 | middle_stack.append(next) 31 | if next == left_stack[i]: 32 | left_stack.pop() 33 | i -= 1 34 | else: 35 | right_stack.pop() 36 | j -= 1 37 | else: 38 | return False 39 | 40 | return can_clear_stack(left_stack, middle_stack) and can_clear_stack(right_stack, middle_stack) 41 | 42 | 43 | queries = int(input()) 44 | 45 | for _ in range(queries): 46 | input() 47 | array = get_list() 48 | if is_stackable(array): 49 | print("Yes") 50 | else: 51 | print("No") 52 | -------------------------------------------------------------------------------- /collections/word-order.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/word-order/problem 2 | 3 | import collections 4 | 5 | frequencies = collections.OrderedDict() 6 | n = int(input()) 7 | for i in range(n): 8 | word = input() 9 | if word in frequencies: 10 | frequencies[word] += 1 11 | else: 12 | frequencies[word] = 1 13 | 14 | print(len(frequencies)) 15 | 16 | for word in frequencies: 17 | print(frequencies[word], end=' ') 18 | -------------------------------------------------------------------------------- /date-and-time/calendar-module.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/calendar-module/problem 2 | 3 | import datetime 4 | 5 | month, date, year = map(int, input().split()) 6 | date = datetime.datetime(year=year, month=month, day=date) 7 | print(date.strftime('%A').upper()) 8 | -------------------------------------------------------------------------------- /date-and-time/time-delta.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | 3 | date_format = '%a %d %b %Y %X %z' 4 | queries = int(input()) 5 | 6 | for _ in range(queries): 7 | date1 = datetime.datetime.strptime(input(), date_format) 8 | date2 = datetime.datetime.strptime(input(), date_format) 9 | diff = int(abs((date2 - date1).total_seconds())) 10 | print(diff) 11 | -------------------------------------------------------------------------------- /debugging/default-arguments.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/default-arguments/problem 2 | 3 | class EvenStream(object): 4 | def __init__(self): 5 | self.current = 0 6 | 7 | def get_next(self): 8 | to_return = self.current 9 | self.current += 2 10 | return to_return 11 | 12 | 13 | class OddStream(object): 14 | def __init__(self): 15 | self.current = 1 16 | 17 | def get_next(self): 18 | to_return = self.current 19 | self.current += 2 20 | return to_return 21 | 22 | 23 | def print_from_stream(n, stream=EvenStream()): 24 | stream.__init__() 25 | for _ in range(n): 26 | print(stream.get_next()) 27 | 28 | 29 | queries = int(input()) 30 | for _ in range(queries): 31 | stream_name, n = input().split() 32 | n = int(n) 33 | if stream_name == "even": 34 | print_from_stream(n) 35 | else: 36 | print_from_stream(n, OddStream()) 37 | -------------------------------------------------------------------------------- /debugging/word-score.py: -------------------------------------------------------------------------------- 1 | def is_vowel(letter): 2 | return letter in ['a', 'e', 'i', 'o', 'u', 'y'] 3 | 4 | 5 | def score_words(words): 6 | score = 0 7 | for word in words: 8 | num_vowels = 0 9 | for letter in word: 10 | if is_vowel(letter): 11 | num_vowels += 1 12 | 13 | if num_vowels % 2 == 0: 14 | score += 2 15 | else: 16 | score += 1 17 | 18 | return score 19 | 20 | 21 | n = int(input()) 22 | words = input().split() 23 | print(score_words(words)) 24 | -------------------------------------------------------------------------------- /errors-and-exceptions/exceptions.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/exceptions/problem 2 | 3 | 4 | def take_input(): 5 | try: 6 | a, b = list(map(int, input().split())) 7 | print(a // b) 8 | except ZeroDivisionError as error: 9 | print('Error Code:', error) 10 | except ValueError as error: 11 | print('Error Code:', error) 12 | 13 | 14 | n = int(input()) 15 | for _ in range(n): 16 | take_input() 17 | -------------------------------------------------------------------------------- /errors-and-exceptions/incorrect-regex.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | 4 | def is_valid_regex(regex): 5 | try: 6 | re.compile(regex) 7 | return True 8 | except re.error: 9 | return False 10 | 11 | 12 | queries = int(input()) 13 | for _ in range(queries): 14 | regex = input() 15 | print(is_valid_regex(regex)) 16 | -------------------------------------------------------------------------------- /introduction/ArithmeticOperator.py: -------------------------------------------------------------------------------- 1 | a = int(input()) 2 | b = int(input()) 3 | 4 | print(a + b) 5 | print(a - b) 6 | print(a * b) 7 | -------------------------------------------------------------------------------- /introduction/Division.py: -------------------------------------------------------------------------------- 1 | a = int(input()) 2 | b = int(input()) 3 | 4 | print(a // b) 5 | print(a / b) 6 | -------------------------------------------------------------------------------- /introduction/Loops.py: -------------------------------------------------------------------------------- 1 | number = int(input()) 2 | for x in range(number): 3 | print(x * x) 4 | -------------------------------------------------------------------------------- /introduction/PrintFunction.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/python-print/problem 2 | 3 | 4 | def print_numbers(number): 5 | string = '' 6 | for i in range(number): 7 | string += str(i + 1) 8 | print(string) 9 | 10 | 11 | number = int(input()) 12 | print_numbers(number) 13 | -------------------------------------------------------------------------------- /introduction/PythonIfElse.py: -------------------------------------------------------------------------------- 1 | number = int(input()) 2 | 3 | if number % 2 == 1: 4 | print('Weird') 5 | elif 2 <= number <= 5: 6 | print('Not Weird') 7 | elif 6 <= number <= 20: 8 | print('Weird') 9 | elif number > 20: 10 | print('Not Weird') 11 | -------------------------------------------------------------------------------- /introduction/SayHelloWorldWithPython.py: -------------------------------------------------------------------------------- 1 | print("Hello, World!") 2 | -------------------------------------------------------------------------------- /introduction/WriteAFunction.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/write-a-function/problem 2 | 3 | 4 | def is_leap(year): 5 | if (year % 4 == 0): 6 | if (year % 400 == 0): 7 | return True 8 | if (year % 100 == 0): 9 | return False 10 | return True 11 | return False 12 | 13 | year = int(input()) 14 | print(is_leap(year)) 15 | -------------------------------------------------------------------------------- /itertools/compress-the-string.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/compress-the-string/problem 2 | 3 | import itertools 4 | 5 | line = input() 6 | result = '' 7 | 8 | for entry in itertools.groupby(line): 9 | count = 0 10 | for element in entry[1]: 11 | count += 1 12 | result += str((count, int(entry[0]))) + ' ' 13 | 14 | print(result) 15 | -------------------------------------------------------------------------------- /itertools/iterables-and-iterators.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/iterables-and-iterators/problem 2 | 3 | 4 | def compute(value, count): 5 | result = 1 6 | for i in range(count): 7 | result *= value - i 8 | return result 9 | 10 | 11 | def number_of_a(array): 12 | count = 0 13 | for element in array: 14 | if element == 'a': 15 | count += 1 16 | return count 17 | 18 | 19 | N = int(input()) 20 | array = input().split() 21 | k = int(input()) 22 | n = number_of_a(array) 23 | 24 | numerator = compute(N - n, k) 25 | denominator = compute(N, k) 26 | probability = 1 - numerator / denominator 27 | print(probability) 28 | -------------------------------------------------------------------------------- /itertools/itertools-combinations-with-replacements.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/itertools-combinations-with-replacement/problem 2 | 3 | import itertools 4 | 5 | line = input().split() 6 | word = sorted(line[0]) 7 | k = int(line[1]) 8 | 9 | for entry in itertools.combinations_with_replacement(word, k): 10 | print(''.join(entry)) 11 | -------------------------------------------------------------------------------- /itertools/itertools-combinations.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/itertools-combinations/problem 2 | 3 | import itertools 4 | 5 | line = input().split() 6 | word = sorted(line[0]) 7 | k = int(line[1]) 8 | 9 | for i in range(1, k + 1): 10 | for j in itertools.combinations(word, i): 11 | print(''.join(j)) 12 | -------------------------------------------------------------------------------- /itertools/itertools-permutations.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/itertools-permutations/problem 2 | 3 | import itertools 4 | 5 | line = input().split() 6 | word = line[0] 7 | k = int(line[1]) 8 | 9 | permutations = list(itertools.permutations(word, k)) 10 | permutations.sort() 11 | 12 | for permutation in permutations: 13 | print(''.join(permutation)) 14 | -------------------------------------------------------------------------------- /itertools/itertools-product.py: -------------------------------------------------------------------------------- 1 | import itertools 2 | 3 | array1 = list(map(int, input().split())) 4 | array2 = list(map(int, input().split())) 5 | 6 | result = list(itertools.product(array1, array2)) 7 | 8 | for entry in result: 9 | print(str(entry) + ' ', end='') 10 | -------------------------------------------------------------------------------- /itertools/maximize-it.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/maximize-it/problem 2 | 3 | import itertools 4 | 5 | 6 | def get_list(): 7 | return list(map(int, input().split())) 8 | 9 | 10 | line = input().split() 11 | K = int(line[0]) 12 | M = int(line[1]) 13 | 14 | data = [] 15 | for i in range(K): 16 | data.append(get_list()[1:]) 17 | 18 | result = 0 19 | for entry in itertools.product(*data): 20 | result = max(result, sum(x*x for x in entry) % M) 21 | 22 | print(result) 23 | -------------------------------------------------------------------------------- /math/find-angle-mbc.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/find-angle/problem 2 | 3 | import math 4 | 5 | AB = int(input()) 6 | BC = int(input()) 7 | h = math.sqrt(pow(AB, 2) + pow(BC, 2)) 8 | print(str(round(math.degrees(math.acos(BC / h)))) + '°') 9 | -------------------------------------------------------------------------------- /math/integers-come-in-all-sizes.py: -------------------------------------------------------------------------------- 1 | a, b, c, d = int(input()), int(input()), int(input()), int(input()) 2 | print(a** b + c**d) 3 | -------------------------------------------------------------------------------- /math/mod-divmod.py: -------------------------------------------------------------------------------- 1 | a = int(input()) 2 | b = int(input()) 3 | 4 | print(a // b) 5 | print(a % b) 6 | print(divmod(a, b)) 7 | -------------------------------------------------------------------------------- /math/polar-coordinates.py: -------------------------------------------------------------------------------- 1 | import cmath 2 | 3 | number = complex(input()) 4 | 5 | print(abs(number)) 6 | print(cmath.phase(number)) 7 | -------------------------------------------------------------------------------- /math/power-mod-power.py: -------------------------------------------------------------------------------- 1 | a, b, m = int(input()), int(input()), int(input()) 2 | 3 | print(pow(a, b)) 4 | print(pow(a, b, m)) 5 | -------------------------------------------------------------------------------- /math/triangle-quest-2.py: -------------------------------------------------------------------------------- 1 | for i in range(1,int(input())+1): 2 | print([1, 121, 12321, 1234321, 123454321, 12345654321, 1234567654321, 3 | 123456787654321, 12345678987654321, 12345678910987654321][i - 1]) 4 | -------------------------------------------------------------------------------- /math/triangle-quest.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/python-quest-1/problem 2 | 3 | n = int(input()) 4 | 5 | for i in range(n): 6 | # I learn new things everyday 7 | print((10**(i)//9)*i) 8 | # print("{}".format(10)*5) 9 | -------------------------------------------------------------------------------- /numpy-python/array-mathematics.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/np-array-mathematics/problem 2 | 3 | import numpy 4 | 5 | N, M = map(int, input().split()) 6 | A = [] 7 | for _ in range(N): 8 | A.append(list(map(int, input().split()))) 9 | A = numpy.array(A) 10 | 11 | B = [] 12 | for _ in range(N): 13 | B.append(list(map(int, input().split()))) 14 | B = numpy.array(B) 15 | 16 | print(A + B) 17 | print(A - B) 18 | print(A * B) 19 | print(A // B) 20 | print(A % B) 21 | print(A ** B) 22 | -------------------------------------------------------------------------------- /numpy-python/arrays.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/np-arrays/problem 2 | 3 | import numpy 4 | 5 | 6 | def arrays(arr): 7 | return numpy.array(arr[::-1], float) 8 | 9 | 10 | arr = input().strip().split(' ') 11 | result = arrays(arr) 12 | print(result) 13 | -------------------------------------------------------------------------------- /numpy-python/concatenate.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/np-concatenate/problem 2 | 3 | import numpy 4 | 5 | N, M, P = map(int, input().split()) 6 | 7 | data1 = [] 8 | for _ in range(N): 9 | data1.append(list(map(int, input().split()))) 10 | 11 | data2 = [] 12 | for _ in range(M): 13 | data2.append(list(map(int, input().split()))) 14 | 15 | result = numpy.concatenate((data1, data2), axis=0) 16 | print(result) 17 | -------------------------------------------------------------------------------- /numpy-python/dot-cross.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/np-dot-and-cross/problem 2 | 3 | import numpy 4 | numpy.set_printoptions(legacy='1.13') 5 | 6 | 7 | def zero(size): 8 | return [0 for _ in range(size)] 9 | 10 | 11 | def get_matrix(size): 12 | matrix = [] 13 | for _ in range(size): 14 | matrix.append(list(map(int, input().split()))) 15 | return matrix 16 | 17 | 18 | N = int(input()) 19 | matrix1 = numpy.array(get_matrix(N)) 20 | matrix2 = numpy.array(get_matrix(N)).transpose() 21 | 22 | result = [] 23 | for row in range(N): 24 | result.append(zero(N)) 25 | for column in range(N): 26 | result[row][column] = int(numpy.dot(matrix1[row], matrix2[column])) 27 | 28 | print(numpy.array(result)) 29 | -------------------------------------------------------------------------------- /numpy-python/eye-and-identity.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/np-eye-and-identity/problem 2 | 3 | import numpy 4 | 5 | # there is an error on the code setters part and tehe below cde although absolutely correct doesn't pass the test cases 6 | M, N = map(int, input().split()) 7 | # print(numpy.eye(M, N)) 8 | 9 | # The problem setter's code prints 1's and 0's with an extra space and hence we will add an extra space in front of 10 | # 0 and 1 using replace() function 11 | 12 | print(str(numpy.eye(M, N)).replace('1',' 1').replace('0',' 0')) 13 | -------------------------------------------------------------------------------- /numpy-python/flatten-and-transpose.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/np-transpose-and-flatten/problem 2 | 3 | import numpy 4 | 5 | N, M = map(int, input().split()) 6 | data = [] 7 | for _ in range(N): 8 | data.append(list(map(int, input().split()))) 9 | 10 | data = numpy.array(data) 11 | print(data.transpose()) 12 | print(data.flatten()) 13 | -------------------------------------------------------------------------------- /numpy-python/floor-ciel-and-rint.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/floor-ceil-and-rint/problem 2 | 3 | import numpy 4 | 5 | numpy.set_printoptions(sign=' ') 6 | 7 | array = numpy.array(list(map(float, input().split())), dtype=float) 8 | print(numpy.floor(array)) 9 | print(numpy.ceil(array)) 10 | print(numpy.rint(array)) 11 | -------------------------------------------------------------------------------- /numpy-python/inner-outer.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/np-inner-and-outer/problem 2 | 3 | import numpy 4 | numpy.set_printoptions(legacy='1.13') 5 | 6 | A = numpy.array(list(map(int, input().split()))) 7 | B = numpy.array(list(map(int, input().split()))) 8 | 9 | print(numpy.inner(A, B)) 10 | print(numpy.outer(A, B)) 11 | -------------------------------------------------------------------------------- /numpy-python/linear-algebra.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/np-linear-algebra/problem 2 | 3 | import numpy 4 | 5 | N = int(input()) 6 | matrix = [] 7 | for _ in range(N): 8 | matrix.append(list(map(float, input().split()))) 9 | 10 | result = round(numpy.linalg.det(matrix), 2) 11 | print(result) 12 | -------------------------------------------------------------------------------- /numpy-python/mean-var-std.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/np-mean-var-and-std/problem 2 | 3 | import numpy 4 | 5 | # added for proper formatting in the output - complete stupidity by the problem setter 6 | numpy.set_printoptions(legacy='1.13') 7 | 8 | N, M = map(int, input().split()) 9 | data = [] 10 | for _ in range(N): 11 | data.append(list(map(int, input().split()))) 12 | 13 | data = numpy.array(data) 14 | print(data.mean(axis=1)) 15 | print(data.var(axis=0)) 16 | print(data.std()) 17 | -------------------------------------------------------------------------------- /numpy-python/min-max.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/np-min-and-max/problem 2 | 3 | import numpy 4 | 5 | N, M = map(int, input().split()) 6 | data = [] 7 | for _ in range(N): 8 | data.append(list(map(int, input().split()))) 9 | 10 | data = numpy.array(data) 11 | data = data.min(axis=1) 12 | result = data.max() 13 | print(result) 14 | -------------------------------------------------------------------------------- /numpy-python/polynomials.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/np-polynomials/problem 2 | 3 | import numpy 4 | numpy.set_printoptions(legacy='1.13') 5 | 6 | polynomial = list(map(float, input().split())) 7 | x = float(input()) 8 | value = numpy.polyval(polynomial, x) 9 | print(value) 10 | -------------------------------------------------------------------------------- /numpy-python/shape-and-reshape.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/np-shape-reshape/problem 2 | 3 | import numpy as np 4 | 5 | array = np.array(list(map(int, input().split()))) 6 | array.shape = (3, 3) 7 | print(array) 8 | -------------------------------------------------------------------------------- /numpy-python/sum-and-prod.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/np-sum-and-prod/problem 2 | 3 | import numpy 4 | 5 | N, M = map(int, input().split()) 6 | data = [] 7 | for _ in range(N): 8 | data.append(list(map(int, input().split()))) 9 | 10 | data = numpy.array(data) 11 | data = data.sum(axis=0) 12 | product = data.prod() 13 | print(product) 14 | -------------------------------------------------------------------------------- /numpy-python/zeroes-and-ones.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/np-zeros-and-ones/problem 2 | 3 | import numpy 4 | 5 | dimensions = tuple(map(int, input().split())) 6 | print(numpy.zeros(dimensions, dtype=numpy.int)) 7 | print(numpy.ones(dimensions, dtype=numpy.int)) 8 | -------------------------------------------------------------------------------- /python-functions/map-and-lambda-functions.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/map-and-lambda-expression/problem 2 | 3 | 4 | def get_fib(n): 5 | array = [] 6 | for i in range(n): 7 | if i == 0: 8 | array.append(0) 9 | continue 10 | if i == 1: 11 | array.append(1) 12 | continue 13 | 14 | array.append(array[i - 1] + array[i - 2]) 15 | return array 16 | 17 | 18 | N = int(input()) 19 | 20 | fibonacci = get_fib(N) 21 | fibonacci = list(map(lambda x: pow(x, 3), fibonacci)) 22 | print(fibonacci) 23 | -------------------------------------------------------------------------------- /python-functions/reduce-function.py: -------------------------------------------------------------------------------- 1 | from fractions import Fraction 2 | from functools import reduce 3 | 4 | 5 | def product(fracs): 6 | t = reduce(lambda x, y: x * y, fracs) 7 | return t.numerator, t.denominator 8 | 9 | 10 | if __name__ == '__main__': 11 | fracs = [] 12 | for _ in range(int(input())): 13 | fracs.append(Fraction(*map(int, input().split()))) 14 | result = product(fracs) 15 | print(*result) 16 | -------------------------------------------------------------------------------- /python-functions/validating-email-addresses.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | pattern = '([a-z]|[A-Z]|\d|_|-)+@([a-z]|[A-Z]|\d)*[.].{1,3}' 4 | 5 | 6 | def fun(email): 7 | match = re.fullmatch(pattern, email) 8 | if match is not None: 9 | return match.span() == (0, len(email)) 10 | 11 | 12 | def filter_mail(emails): 13 | return list(filter(fun, emails)) 14 | 15 | 16 | if __name__ == '__main__': 17 | n = int(input()) 18 | emails = [] 19 | for _ in range(n): 20 | emails.append(input()) 21 | 22 | filtered_emails = filter_mail(emails) 23 | filtered_emails.sort() 24 | print(filtered_emails) 25 | -------------------------------------------------------------------------------- /regex-and-parsing/detect-floating-point-numbers.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/introduction-to-regex/problem 2 | 3 | import re 4 | 5 | pattern = '([+|-]){0,1}(\d)*[.](\d)+' 6 | 7 | 8 | def is_float(number): 9 | try: 10 | float(number) 11 | info = re.match(pattern, number) 12 | return info is not None and info.span() == (0, len(number)) 13 | except ValueError as _: 14 | return False 15 | 16 | 17 | queries = int(input()) 18 | for _ in range(queries): 19 | number = input() 20 | print(is_float(number)) 21 | -------------------------------------------------------------------------------- /regex-and-parsing/detect-html-tags-attributes.py: -------------------------------------------------------------------------------- 1 | from html.parser import HTMLParser 2 | 3 | 4 | class MyHTMLParser(HTMLParser): 5 | def handle_starttag(self, tag, attrs): 6 | print(tag) 7 | for attribute in attrs: 8 | print('-> {} > {}'.format(*attribute)) 9 | 10 | 11 | html = '\n'.join([input() for _ in range(int(input()))]) 12 | parser = MyHTMLParser() 13 | parser.feed(html) 14 | parser.close() 15 | -------------------------------------------------------------------------------- /regex-and-parsing/group-groups-groupdict.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/re-group-groups/problem 2 | 3 | import re 4 | 5 | pattern = r'([a-zA-Z0-9])\1+' 6 | match = re.search(pattern, input().strip()) 7 | print(match.groups()[0] if match else -1) 8 | -------------------------------------------------------------------------------- /regex-and-parsing/hex-color-code.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/hex-color-code/problem 2 | 3 | import re 4 | 5 | hex_color = r'(?', attribute[0], '>', attribute[1]) 22 | 23 | 24 | parser = MyHTMLParser() 25 | 26 | n = int(input()) 27 | for _ in range(n): 28 | html = input() 29 | parser.feed(html) 30 | -------------------------------------------------------------------------------- /regex-and-parsing/html-parser-part-2.py: -------------------------------------------------------------------------------- 1 | from html.parser import HTMLParser 2 | 3 | from html.parser import HTMLParser 4 | 5 | 6 | class CustomHTMLParser(HTMLParser): 7 | def handle_comment(self, data): 8 | number_of_line = len(data.split('\n')) 9 | if number_of_line > 1: 10 | print('>>> Multi-line Comment') 11 | else: 12 | print('>>> Single-line Comment') 13 | if data.strip(): 14 | print(data) 15 | 16 | def handle_data(self, data): 17 | if data.strip(): 18 | print(">>> Data") 19 | print(data) 20 | 21 | 22 | parser = CustomHTMLParser() 23 | 24 | n = int(input()) 25 | html_string = '' 26 | for i in range(n): 27 | html_string += input().rstrip() + '\n' 28 | 29 | parser.feed(html_string) 30 | -------------------------------------------------------------------------------- /regex-and-parsing/matrix-script.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/matrix-script/problem 2 | 3 | import re 4 | 5 | n, m = map(int, input().split()) 6 | data = [] 7 | for _ in range(n): 8 | data.append(input()) 9 | 10 | string = '' 11 | for elem in zip(*data): 12 | string += ''.join(elem) 13 | 14 | print(re.sub(r'(?<=\w)([^\w]+)(?=\w)', ' ', string)) 15 | -------------------------------------------------------------------------------- /regex-and-parsing/re-findall-and-finditer.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/re-findall-re-finditer/problem 2 | 3 | import re 4 | 5 | 6 | consonants = '[qwrtypsdfghjklzxcvbnm]' 7 | a = re.findall("(?<=" + consonants + ")([aeiou]{2,})" + consonants, input(), re.I) 8 | print('\n'.join(a or ['-1'])) 9 | -------------------------------------------------------------------------------- /regex-and-parsing/re-split.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | regex_pattern = r"[.]|," # Do not delete 'r'. 4 | print("\n".join(re.split(regex_pattern, input()))) 5 | -------------------------------------------------------------------------------- /regex-and-parsing/re-start-end.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/re-start-re-end/problem 2 | 3 | import re 4 | 5 | 6 | line = input() 7 | substring = input() 8 | pattern = re.compile(substring) 9 | 10 | match = pattern.search(line) 11 | if match is None: 12 | print((-1, -1)) 13 | 14 | while match: 15 | print('({}, {})'.format(match.start(), match.end())) 16 | match = pattern.search(line, match.start() + 1) 17 | -------------------------------------------------------------------------------- /regex-and-parsing/regex-substitution.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/re-sub-regex-substitution/problem 2 | 3 | import re 4 | 5 | and_pattern = r' [&]{2}(?= )' 6 | and_replacement = ' and' 7 | or_pattern = r' [|]{2}(?= )' 8 | or_replacement = ' or' 9 | 10 | N = int(input()) 11 | for _ in range(N): 12 | string = input() 13 | string = re.sub(and_pattern, and_replacement, string) 14 | string = re.sub(or_pattern, or_replacement, string) 15 | print(string) 16 | -------------------------------------------------------------------------------- /regex-and-parsing/validating-credit-card-numbers.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/validating-credit-card-number/problem 2 | 3 | import re 4 | 5 | pattern_start = r'^' 6 | pattern_no_repetition = r'(?!.*(\d)(-?\1){3})' 7 | credit_card_pattern = r'[456]((\d){15}|(\d){3}(-[\d]{4}){3})' 8 | pattern_end = r'$' 9 | 10 | pattern = re.compile( 11 | pattern_start 12 | + pattern_no_repetition 13 | + credit_card_pattern 14 | + pattern_end 15 | ) 16 | 17 | for _ in range(int(input())): 18 | credit_card = input() 19 | print("Valid" if pattern.search(credit_card) else 'Invalid') 20 | -------------------------------------------------------------------------------- /regex-and-parsing/validating-parsing-email-address.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/validating-named-email-addresses/problem 2 | 3 | import email.utils 4 | import re 5 | 6 | 7 | email_pattern = r'([a-zA-Z](\w|\d|_|-|[.])*)@([a-zA-Z])*[.]([a-zA-Z]{1,3})' 8 | 9 | 10 | def is_valid_email_address(person): 11 | email = person[1] 12 | return re.fullmatch(email_pattern, email) is not None 13 | 14 | 15 | people = [] 16 | n = int(input()) 17 | for _ in range(n): 18 | line = input() 19 | people.append(email.utils.parseaddr(line)) 20 | 21 | for element in (filter(is_valid_email_address, people)): 22 | print(email.utils.formataddr(element)) 23 | -------------------------------------------------------------------------------- /regex-and-parsing/validating-phone-numbers.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/validating-the-phone-number/problem 2 | 3 | import re 4 | 5 | 6 | phone_number_pattern = r'^[789]\d{9}$' 7 | N = int(input()) 8 | for _ in range(N): 9 | line = input() 10 | match = re.fullmatch(phone_number_pattern, line) 11 | print('YES' if match is not None and match.span() == (0, len(line)) else 'NO') 12 | -------------------------------------------------------------------------------- /regex-and-parsing/validating-postal-codes.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/validating-postalcode/problem 2 | 3 | import re 4 | 5 | 6 | P = input() 7 | 8 | regex_integer_in_range = r"^[1-9]\d{5}$" 9 | regex_alternating_repetitive_digit_pair = r"(.)(?=.\1)" 10 | 11 | print (bool(re.match(regex_integer_in_range, P)) 12 | and len(re.findall(regex_alternating_repetitive_digit_pair, P)) < 2) 13 | -------------------------------------------------------------------------------- /regex-and-parsing/validating-roman-numerals.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/validate-a-roman-number/problem 2 | 3 | import re 4 | 5 | 6 | roman_numeral_strict = r'^(?=[MDCLXVI])M{0,3}(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$' 7 | number = input() 8 | match = re.fullmatch(roman_numeral_strict, number) 9 | print(match and match.span() == (0,len(number))) 10 | -------------------------------------------------------------------------------- /regex-and-parsing/validating-uid.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/validating-uid/problem 2 | 3 | import re 4 | 5 | pattern_2_upper_case = r'[A-Z]{2}' 6 | pattern_3_digits = r'(\d){3}' 7 | pattern_alphanum = r'[0-9a-zA-Z]{10}' 8 | pattern_has_repitions = r'^.*(.).*\1.*$' 9 | 10 | for _ in range(int(input())): 11 | uid = ''.join(sorted(input())) 12 | try: 13 | assert re.search(pattern_2_upper_case, uid) 14 | assert re.search(pattern_3_digits, uid) 15 | assert re.search(pattern_alphanum, uid) 16 | assert not re.search(pattern_has_repitions, uid) 17 | assert len(uid) == 10 18 | print("Valid") 19 | except: 20 | print('Invalid') 21 | -------------------------------------------------------------------------------- /sets/check-subset.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/py-check-subset/problem 2 | 3 | 4 | def get_set(): 5 | return set(map(int, input().split())) 6 | 7 | 8 | queries = int(input()) 9 | 10 | for _ in range(queries): 11 | input() 12 | A = get_set() 13 | input() 14 | B = get_set() 15 | print(A.issubset(B)) 16 | -------------------------------------------------------------------------------- /sets/introduction-to-sets.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | tokens = map(int , input().split()) 3 | plants = set(tokens) 4 | print(sum(plants) / len(plants)) 5 | -------------------------------------------------------------------------------- /sets/is-strict-superset.py: -------------------------------------------------------------------------------- 1 | def get_set(): 2 | return set(map(int, input().split())) 3 | 4 | 5 | def is_super_set(main, sets): 6 | for set in sets: 7 | if not main.issuperset(set): 8 | return False 9 | 10 | return True 11 | 12 | 13 | A = get_set() 14 | queries = int(input()) 15 | sets = [] 16 | 17 | for _ in range(queries): 18 | sets.append(get_set()) 19 | 20 | print(is_super_set(A, sets)) 21 | -------------------------------------------------------------------------------- /sets/no-idea.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | 3 | array = list(map(int, input().split())) 4 | A = set(map(int, input().split())) 5 | B = set(map(int, input().split())) 6 | 7 | happiness = 0 8 | for element in array: 9 | if element in A: 10 | happiness += 1 11 | elif element in B: 12 | happiness -= 1 13 | 14 | print(happiness) -------------------------------------------------------------------------------- /sets/set-add.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/py-set-add/problem 2 | 3 | n = int(input()) 4 | stamps = set() 5 | for i in range(n): 6 | country = input() 7 | stamps.add(country) 8 | 9 | print(len(stamps)) 10 | -------------------------------------------------------------------------------- /sets/set-difference.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/py-set-difference-operation/problem 2 | 3 | int(input()) 4 | english_subscriptions = set(map(int, input().split())) 5 | 6 | int(input()) 7 | french_subscriptions = set(map(int, input().split())) 8 | 9 | all_subscriptions = english_subscriptions.difference(french_subscriptions) 10 | print(len(all_subscriptions)) 11 | -------------------------------------------------------------------------------- /sets/set-disacard-remove-pop.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/py-set-discard-remove-pop/problem 2 | 3 | n = int(input()) 4 | numbers = set(map(int, input().split())) 5 | queries = int(input()) 6 | 7 | for i in range(queries): 8 | command = input() 9 | 10 | if command == 'pop': 11 | numbers.pop() 12 | else: 13 | line = command.split() 14 | command = line[0] 15 | element = int(line[1]) 16 | 17 | if command == 'remove': 18 | numbers.remove(element) 19 | else: 20 | numbers.discard(element) 21 | 22 | print(sum(numbers)) 23 | -------------------------------------------------------------------------------- /sets/set-intersection.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/py-set-intersection-operation/problem 2 | 3 | int(input()) 4 | english_subscriptions = set(map(int, input().split())) 5 | 6 | int(input()) 7 | french_subscriptions = set(map(int, input().split())) 8 | 9 | all_subscriptions = english_subscriptions.intersection(french_subscriptions) 10 | print(len(all_subscriptions)) 11 | -------------------------------------------------------------------------------- /sets/set-mutations.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/py-set-mutations/problem 2 | 3 | _ = input() 4 | A = set(map(int, input().split())) 5 | 6 | queries = int(input()) 7 | 8 | for i in range(queries): 9 | command = input().split()[0] 10 | temp = set(map(int, input().split())) 11 | 12 | if command == 'update': 13 | A.update(temp) 14 | elif command == 'intersection_update': 15 | A.intersection_update(temp) 16 | elif command == 'difference_update': 17 | A.difference_update(temp) 18 | else: 19 | A.symmetric_difference_update(temp) 20 | 21 | print(sum(A)) 22 | -------------------------------------------------------------------------------- /sets/set-symmetric-difference.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/py-set-symmetric-difference-operation/problem 2 | 3 | int(input()) 4 | english_subscriptions = set(map(int, input().split())) 5 | 6 | int(input()) 7 | french_subscriptions = set(map(int, input().split())) 8 | 9 | all_subscriptions = english_subscriptions.symmetric_difference(french_subscriptions) 10 | print(len(all_subscriptions)) 11 | -------------------------------------------------------------------------------- /sets/set-union.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/py-set-union/problem 2 | 3 | int(input()) 4 | english_subscriptions = set(map(int, input().split())) 5 | 6 | int(input()) 7 | french_subscriptions = set(map(int, input().split())) 8 | 9 | all_subscriptions = english_subscriptions.union(french_subscriptions) 10 | print(len(all_subscriptions)) 11 | -------------------------------------------------------------------------------- /sets/symmetric-difference.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/symmetric-difference/problem 2 | 3 | m = int(input()) 4 | M = set(map(int, input().split())) 5 | 6 | n = int(input()) 7 | N = set(map(int, input().split())) 8 | 9 | D = M.symmetric_difference(N) 10 | array = list(D) 11 | array.sort() 12 | 13 | for element in array: 14 | print(element) 15 | -------------------------------------------------------------------------------- /sets/the-captains-room.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/py-the-captains-room/problem 2 | 3 | k = int(input()) 4 | array = list(map(int, input().split())) 5 | 6 | frequencies = dict() 7 | 8 | for element in array: 9 | frequencies[element] = (frequencies[element] if element in frequencies else 0) + 1 10 | 11 | for key in frequencies: 12 | if frequencies[key] == 1: 13 | print(key) 14 | break 15 | -------------------------------------------------------------------------------- /strings/alphabet-rangoli.py: -------------------------------------------------------------------------------- 1 | import string 2 | 3 | rangoli = [] 4 | size = int(input()) 5 | for i in range(size): 6 | line = '-'.join(string.ascii_lowercase[size - i - 1:size]) 7 | decreasing = line[::-1] 8 | increasing = line[1:] 9 | pattern = (decreasing + increasing).center(4 * size - 3, '-') 10 | rangoli.append(pattern) 11 | 12 | inverted = rangoli[0:size - 1][::-1] 13 | # print(inverted) 14 | 15 | for i in range(len(rangoli)): 16 | print(rangoli[i]) 17 | 18 | for i in range(len(inverted)): 19 | print(inverted[i]) 20 | -------------------------------------------------------------------------------- /strings/capitalize!.py: -------------------------------------------------------------------------------- 1 | line = input() 2 | words = line.split() 3 | for word in words: 4 | line = line.replace(word, word.capitalize()) 5 | 6 | print(line) 7 | -------------------------------------------------------------------------------- /strings/designer-door-mat.py: -------------------------------------------------------------------------------- 1 | def printPattern(pattern, count): 2 | for j in range(count): 3 | print(pattern, end='') 4 | 5 | 6 | n, m = map(int, input().split()) 7 | 8 | # Upper part 9 | for i in range(n//2): 10 | printPattern('-', (m-3) // 2 - (3 * i)) 11 | printPattern('.|.', 2 * i + 1) 12 | printPattern('-', (m - 3) // 2 - (3 * i)) 13 | print() 14 | 15 | # Middle part 16 | printPattern('-', (m - 7) // 2) 17 | print('WELCOME', end='') 18 | printPattern('-', (m - 7) // 2) 19 | print() 20 | 21 | # Lower Part 22 | for i in range(n // 2): 23 | printPattern('-', 3 * (i + 1)) 24 | printPattern('.|.', n - 2 * (i + 1)) 25 | printPattern('-', 3 * (i + 1)) 26 | print() 27 | -------------------------------------------------------------------------------- /strings/find-a-string.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/find-a-string/problem 2 | 3 | 4 | def count_substring(string, sub_string): 5 | count = 0 6 | for i in range(len(string)): 7 | if string[i] == sub_string[0]: 8 | if string[i:(i + len(sub_string))] == sub_string: 9 | count += 1 10 | i += len(sub_string) - 1 11 | 12 | return count 13 | 14 | 15 | string = input() 16 | sub_string = input() 17 | print(count_substring(string,sub_string)) 18 | -------------------------------------------------------------------------------- /strings/merge-the-tools.py: -------------------------------------------------------------------------------- 1 | line, k = input(), int(input()) 2 | 3 | iterator = line.__iter__() 4 | iterators = zip(*([iterator] * k)) 5 | 6 | for word in iterators: 7 | d = dict() 8 | result = ''.join([d.setdefault(letter, letter) for letter in word if letter not in d]) 9 | print(result) 10 | -------------------------------------------------------------------------------- /strings/mutations.py: -------------------------------------------------------------------------------- 1 | def mutate_string(string, position, character): 2 | return string[:position] + character + string[(position + 1):] 3 | 4 | 5 | 6 | string = input() 7 | parameters = input().split() 8 | index = int(parameters[0]) 9 | character = parameters[1] 10 | print(mutate_string(string, index, character)) -------------------------------------------------------------------------------- /strings/string-formatting.py: -------------------------------------------------------------------------------- 1 | n: int = int(input()) 2 | width = len('{0:b}'.format(n)) 3 | 4 | for i in range(1, n + 1): 5 | print('{0:{width}d} {0:{width}o} {0:{width}X} {0:{width}b}'.format(i, width=width)) 6 | -------------------------------------------------------------------------------- /strings/string-split-and-join.py: -------------------------------------------------------------------------------- 1 | def split_and_join(string): 2 | tokens = string.split() 3 | return '-'.join(tokens) 4 | 5 | 6 | string = input() 7 | print(split_and_join(string)) -------------------------------------------------------------------------------- /strings/string-validators.py: -------------------------------------------------------------------------------- 1 | def hasalpha(string): 2 | for character in string: 3 | if character.isalpha(): 4 | return True 5 | 6 | return False 7 | 8 | 9 | def hasalnum(string): 10 | for character in string: 11 | if character.isalnum(): 12 | return True 13 | 14 | return False 15 | 16 | 17 | def hasdigit(string): 18 | for character in string: 19 | if character.isdigit(): 20 | return True 21 | 22 | return False 23 | 24 | 25 | def haslower(string): 26 | for character in string: 27 | if character.islower(): 28 | return True 29 | 30 | return False 31 | 32 | 33 | def hasupper(string): 34 | for character in string: 35 | if character.isupper(): 36 | return True 37 | 38 | return False 39 | 40 | 41 | string = input() 42 | 43 | print(any(c.isalnum() for c in string)) 44 | print(any(c.isalpha() for c in string)) 45 | print(any(c.isdigit() for c in string)) 46 | print(any(c.islower() for c in string)) 47 | print(any(c.isupper() for c in string)) 48 | -------------------------------------------------------------------------------- /strings/swap-case.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/swap-case/problem 2 | 3 | 4 | def swap_case(s): 5 | result = "" 6 | for let in s: 7 | if let.isupper(): 8 | result += let.lower() 9 | else: 10 | result += let.upper() 11 | return result 12 | 13 | 14 | string = input() 15 | print(swap_case(string)) 16 | -------------------------------------------------------------------------------- /strings/text-alignment.py: -------------------------------------------------------------------------------- 1 | # Replace all ______ with rjust, ljust or center. 2 | 3 | 4 | thickness = int(input()) # This must be an odd number 5 | c = 'H' 6 | 7 | # Top Cone 8 | for i in range(thickness): 9 | print((c * i).rjust(thickness - 1) + c + (c * i).ljust(thickness - 1)) 10 | 11 | # Top Pillars 12 | for i in range(thickness + 1): 13 | print((c * thickness).center(thickness * 2) + (c * thickness).center(thickness * 6)) 14 | 15 | # Middle Belt 16 | for i in range((thickness + 1) // 2): 17 | print((c * thickness * 5).center(thickness * 6)) 18 | 19 | # Bottom Pillars 20 | for i in range(thickness + 1): 21 | print((c * thickness).center(thickness * 2) + (c * thickness).center(thickness * 6)) 22 | 23 | # Bottom Cone 24 | for i in range(thickness): 25 | print( 26 | ((c * (thickness - i - 1)).rjust(thickness) + c + (c * (thickness - i - 1)).ljust(thickness)).rjust(thickness * 6) 27 | ) 28 | -------------------------------------------------------------------------------- /strings/text-wrap.py: -------------------------------------------------------------------------------- 1 | def wrap(string, max_width): 2 | paragraph = '' 3 | count = 0 4 | for i in range(len(string)): 5 | if i % max_width == 0 and i != 0: 6 | paragraph += '\n' 7 | paragraph += string[i] 8 | return paragraph 9 | 10 | 11 | string, max_width = input(), int(input()) 12 | result = wrap(string, max_width) 13 | print(result) 14 | -------------------------------------------------------------------------------- /strings/the-minion-game.py: -------------------------------------------------------------------------------- 1 | consonants = [ 2 | 'b', 'c', 'd', 'f', 'g', 3 | 'h', 'j', 'k', 'l', 'm', 4 | 'n', 'p', 'q', 'r', 's', 5 | 't', 'v', 'w', 'x', 'y', 6 | 'z' 7 | ] 8 | 9 | vowels = ['A', 'E', 'I', 'O', 'U'] 10 | 11 | 12 | def get_vowel_score(word): 13 | score = 0 14 | for i in range(len(word)): 15 | if word[i] in vowels: 16 | score += len(word) - i 17 | return score 18 | 19 | 20 | def get_consonant_score(word): 21 | score = 0 22 | for i in range(len(word)): 23 | if word[i] not in vowels: 24 | score += len(word) - i 25 | return score 26 | 27 | 28 | word = input() 29 | 30 | score_consonant = get_consonant_score(word) 31 | score_vowel = get_vowel_score(word) 32 | 33 | if score_consonant > score_vowel: 34 | print('Stuart ' + str(score_consonant)) 35 | elif score_consonant < score_vowel: 36 | print('kevin ' + str(score_vowel)) 37 | else: 38 | print('Draw') 39 | -------------------------------------------------------------------------------- /strings/whats-your-name.py: -------------------------------------------------------------------------------- 1 | def print_full_name(firstName, lastName): 2 | print('Hello ' + firstName + ' ' + lastName+ '! You just delved into python.') 3 | 4 | 5 | first = input() 6 | last = input() 7 | print_full_name(first, last) 8 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | 3 | date_format = '%a %d %b %Y %X %z' 4 | queries = int(input()) 5 | 6 | for _ in range(queries): 7 | date1 = datetime.datetime.strptime(input(), date_format) 8 | date2 = datetime.datetime.strptime(input(), date_format) 9 | diff = int(abs((date2 - date1).total_seconds())) 10 | print(diff) 11 | -------------------------------------------------------------------------------- /xml/xml-1-find-the-score.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/xml-1-find-the-score/problem 2 | 3 | import xml.etree.ElementTree as etree 4 | 5 | 6 | def get_attr_number(node): 7 | result = 0 8 | for child in node: 9 | result += get_attr_number(child) 10 | return result + len(node.attrib) 11 | 12 | 13 | if __name__ == '__main__': 14 | xml = '' 15 | for _ in range(int(input())): 16 | xml += input() 17 | tree = etree.ElementTree(etree.fromstring(xml)) 18 | root = tree.getroot() 19 | 20 | print(get_attr_number(root)) 21 | -------------------------------------------------------------------------------- /xml/xml-2-find-max-depth.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/xml2-find-the-maximum-depth/problem 2 | 3 | import xml.etree.ElementTree as etree 4 | 5 | maxdepth = 0 6 | def depth(elem, level): 7 | global maxdepth 8 | for child in elem: 9 | maxdepth = max(maxdepth, depth(child, level + 1)) 10 | 11 | maxdepth = max(maxdepth, level + 1) 12 | return maxdepth 13 | 14 | if __name__ == '__main__': 15 | n = int(input()) 16 | xml = "" 17 | for i in range(n): 18 | xml = xml + input() + "\n" 19 | tree = etree.ElementTree(etree.fromstring(xml)) 20 | depth(tree.getroot(), -1) 21 | print(maxdepth) --------------------------------------------------------------------------------