├── assets └── next-button.png ├── Programming-Contest └── Programming-Contest.md ├── Conversions ├── Celsius-to-Fahrenheit.md ├── Miles-to-Kilometers.md ├── Decimal-to-binary-recursive.md └── Decimal-to-binary.md ├── Easy-ones ├── Random-Number.md ├── Math-Power.md ├── User-input-to-Number.md ├── Temporary-variable.md └── Floor-Division.md ├── Prime-number ├── Smallest-prime-factor.md ├── Prime-Factors.md ├── Prime-Numbers.md └── Check-Prime.md ├── Computations ├── Simple-Interest.md ├── Calculate-Grades.md ├── Gravitational-Force.md ├── Complex-Interest.md └── Triangle-Area.md ├── Harder ├── Generate-Sentences.md ├── Simple-Calculator.md ├── Password-generator.md ├── Password-with-requirements.md └── Permutations.md ├── Medium ├── Check-palindrome.md ├── Dictionary-of-cubes.md ├── Least-Common-Multiple.md ├── Armstrong-number.md └── Greatest-common-divisor.md ├── User-Submitted ├── Calculate-age.md ├── Birthday-remaining.md └── Simple-Clock.md ├── Loop-Related ├── Sum-of-elements.md ├── Largest-element-of-a-list.md ├── Remove-duplicate-Chars.md ├── Second-smallest.md ├── Second-Largest.md └── Sum-of-squares.md ├── Number-Related ├── Sum-of-digits.md ├── max-of-two.md ├── Max-of-three.md ├── Divisible-by-3-and-5.md └── Average-of-numbers.md ├── Solution-Strategy.md ├── Simple-Game ├── Cows-and-bulls(4digits).md ├── Word-hangman.md ├── Rock-paper-scissor.md ├── Guess-game.md ├── Cows-and-bulls.md └── Word-completion.md ├── Reverse ├── Reverse-String-(stack).md ├── Reverse-Number.md ├── Reverse-String.md ├── Reverse-String-(recursive).md └── Reverse-word.md └── README.md /assets/next-button.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ProgrammingHero1/100-plus-python-coding-problems-with-solutions/HEAD/assets/next-button.png -------------------------------------------------------------------------------- /Programming-Contest/Programming-Contest.md: -------------------------------------------------------------------------------- 1 | # 10. Programming Contest 2 | 3 | ## Programming Contest 4 | If you are a first-year Computer Science student in a university, we highly recommend you to participate in a programming contest. 5 | 6 | ## For Beginners 7 | There are online programming contests as well. Just Google, "programming contest for beginners" and you will get a lot of them. 8 | 9 | Also, there are many websites like HackerRank, LeetCode, etc. where you can practice problem-solving in your favorite programming language. 10 | 11 | ## Looking for a job 12 | However, if you are learning programming yourself or need to get a job quickly, you can skip programming contests. 13 | 14 | Just focus on learning and building a few software. 15 | 16 | ## Real-world problems 17 | The real-world problems that we are covering in Programming Hero will be enough for you to move forward. 18 | 19 | ## Keep going 20 | Keep going. Keep learning. 21 | Happy Journey. 22 | 23 |   24 | [![Next Page](../assets/next-button.png)](../Simple-Game/Guess-game.md) 25 |   -------------------------------------------------------------------------------- /Conversions/Celsius-to-Fahrenheit.md: -------------------------------------------------------------------------------- 1 | # 4.2 Celsius to Fahrenheit 2 | 3 | ## The problem 4 | Take the temperature in degrees Celsius and convert it to Fahrenheit. 5 | 6 | ## Hins: 7 | To convert degrees Celsius temperature to Fahrenheit, you have to multiply by 9 and divide by 5. 8 | 9 | And then, add 32. 10 | 11 | 12 | Think for a second...How will you multiply a variable by 9 and then divide by 5? and then add 32. Can you do it without looking at the solution? 13 | 14 | ## The solution 15 | ```python 16 | celsius = float(input("Enter temperature in degrees Celsius: ")) 17 | 18 | fahrenheit = celsius*9/5+32 19 | 20 | print("Temperature in Fahrenheit:", fahrenheit) 21 | ``` 22 | 23 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 24 | 25 |   26 | [![Next Page](../assets/next-button.png)](Decimal-to-binary.md) 27 |   28 | 29 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 30 | 31 | -------------------------------------------------------------------------------- /Easy-ones/Random-Number.md: -------------------------------------------------------------------------------- 1 | 2 | # Create a random number 3 | 4 | ## The problem 5 | Create a random number between 0 to 10 6 | 7 | ## Hints 8 | To create a random number, you have to import a built-in library named random. And then you can call the randint method on it 9 | 10 | `result = 4**3` 11 | 12 | ## Solution 13 | 14 | ```python 15 | import random 16 | 17 | random_num = random.randint(0,10) 18 | print(random_num) 19 | ``` 20 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 21 | 22 | ## Quiz 23 | How will you generate a random integer number? 24 | 25 | 1. math.random 26 | 2. random.randint 27 | 3. random.randomint 28 | 29 |
30 | Show Answer 31 |

The answer is : 2

32 |
33 | 34 | ## Take Away 35 | 36 | Use math.randomint to get a random integer. 37 | 38 |   39 | [![Next Page](../assets/next-button.png)](Floor-Division.md) 40 |   41 | 42 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Prime-number/Smallest-prime-factor.md: -------------------------------------------------------------------------------- 1 | # Smallest prime factor [premium] 2 | 3 | ## The problem 4 | Find the smallest prime factor for the given number. 5 | 6 | ## Solution Hint 7 | After finishing all the prime factors, this will be a piece of cake for you. 8 | 9 | ## Solution 10 | ```python 11 | def get_smallest_factor(num): 12 | factor = 2 13 | while num % factor != 0: 14 | factor += 1 15 | return factor 16 | 17 | num = int(input('Enter your number: ')) 18 | 19 | smallest_factor = get_smallest_factor(num) 20 | 21 | print('The smallest prime factor is: ', smallest_factor) 22 | ``` 23 | 24 | ## Explanation 25 | Since we need only one factor. We can do it more easily. 26 | 27 | Just run a while loop. The condition of the loop is checking the remainder. If there is no remainder, the number got divided and you got the prime factor. If the remainder is not 0, then increase the factor by one. 28 | 29 | 30 |   31 | [![Next Page](../assets/next-button.png)](../Reverse/Reverse-String.md) 32 |   33 | 34 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Computations/Simple-Interest.md: -------------------------------------------------------------------------------- 1 | # 6.1: Simple Interest 2 | 3 | --- 4 | 5 | ## The Problem 6 | You borrowed $5000 for 2 years with 2% interest per year. 7 | Calculate the simple interest to know how much you have to pay? 8 | 9 | ## Hint 10 | Just take amount, duration and interest rate. 11 | 12 | You have to multiply these three. And, don’t forget: you have to convert percent to a fraction by dividing it by 100. 13 | 14 | ## The Solution 15 | 16 | ```python 17 | principle = int(input("Money you borrowed: ")) 18 | interest_rate = float(input("Interest Rate: ")) 19 | time = float(input("Overall Duration: ")) 20 | 21 | # Calculates simple interest 22 | simple_interest = principle * (interest_rate/100) * time 23 | 24 | print("Simple interest is:", simple_interest) 25 | ``` 26 | 27 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 28 | 29 | ## Explanation 30 | Read the code. I think you don’t need any extra explanation here. 31 | 32 | 33 |   34 | [![Next Page](../assets/next-button.png)](Complex-Interest.md) 35 |   36 | 37 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 38 | -------------------------------------------------------------------------------- /Harder/Generate-Sentences.md: -------------------------------------------------------------------------------- 1 | # 12.5: Generate Sentences [Premium] 2 | 3 | ## The Problem 4 | You have three lists of words. Create all possible combinations of sentences by taking one element from each list. 5 | 6 | ## Hint 7 | For each list, run a for loop for its len. This would be nested three for loops. 8 | 9 | Under the last loop, access elements from each loop and show them as output. 10 | 11 | ## Solution 12 | ```python 13 | subjects=["I", "You"] 14 | verbs=["Play", "Love"] 15 | objects=["Hockey","Football"] 16 | for i in range(len(subjects)): 17 | for j in range(len(verbs)): 18 | for k in range(len(objects)): 19 | print (subjects[i], verbs[j], objects[k]) 20 | ``` 21 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 22 | 23 | ## Explanation 24 | It's simple...Just run three for loops for three lists you have, each one under another. And finally, under the last one, access all three. 25 | 26 | Damn easy, isn't it? 27 | 28 | 29 |   30 | [![Next Page](../assets/next-button.png)](../User-Submitted/Simple-Clock.md) 31 |   32 | 33 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Computations/Calculate-Grades.md: -------------------------------------------------------------------------------- 1 | # 6.3 Calculate grades 2 | 3 | ## The problem 4 | Calculate grade of five subjects. 5 | 6 | ## Hint 7 | So, you have to take five inputs. These will be the marks of five subjects. Then, create the average. 8 | 9 | Once you have the average. It just running an if-else. And decide the grade. 10 | 11 | ## The Solution 12 | ```python 13 | print('Enter your marks:') 14 | sub1=int(input("First subject: ")) 15 | sub2=int(input("Second subject: ")) 16 | sub3=int(input("Third subject: ")) 17 | sub4=int(input("Fourth subject: ")) 18 | sub5=int(input("Fifth subject: ")) 19 | 20 | avg=(sub1+sub2+sub3+sub4+sub5)/5 21 | 22 | if avg >= 90: 23 | print("Grade: A") 24 | elif avg >= 80: 25 | print("Grade: B") 26 | elif avg >= 70: 27 | print("Grade: C") 28 | elif avg >=60: 29 | print("Grade: D") 30 | else: 31 | print("Grade: F") 32 | ``` 33 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 34 | 35 | ## Explanation 36 | Calculation of average is easy. Add them all and then divide by the count. As we are taking numbers for 5 subjects we are dividing the total by 5. 37 | 38 | After that, we are running a simple if-else to determine the grade. 39 | 40 |   41 | [![Next Page](../assets/next-button.png)](Gravitational-Force.md) 42 |   43 | 44 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Computations/Gravitational-Force.md: -------------------------------------------------------------------------------- 1 | # 6.4 Gravitational Force 2 | 3 | ## The Problem 4 | Compute gravitational force between two objects. 5 | 6 | ## Hint 7 | The formula for gravitational force is 8 | 9 | `F = G m^1m^2/r^2` 10 | 11 | Here G is the gravitational constant. Its value is 6.673*10-11 12 | 13 | So, take three input from the users. The mass of the first object, the mass of the second object and the distance between them. 14 | 15 | ## Solution 16 | 17 | ```python 18 | mass1 = float(input("First mass: ")) 19 | mass2 = float(input("Second mass: ")) 20 | 21 | r = float(input("Distance between the objects: ")) 22 | 23 | G = 6.673*(10**-11) 24 | force =(G*mass1*mass2)/(r**2) 25 | 26 | print("The gravitational force is:", round(force, 5),"N") 27 | ``` 28 | 29 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 30 | 31 | ## Explanation 32 | The calculation is simple. Only two things need to be learned from this. 33 | 34 | `Have a look, how we wrote 6.673*10-^11` 35 | 36 | Also, note that while displaying the force, we used a round function. In the round function, we passed the force, the variable we want to display. Then we passed another parameter to tell, how many decimal points we want to display. 37 | 38 | 39 |   40 | [![Next Page](../assets/next-button.png)](Triangle-Area.md) 41 |   42 | 43 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Computations/Complex-Interest.md: -------------------------------------------------------------------------------- 1 | # 6.2: Compound Interest 2 | 3 | ## The Problem 4 | Take money borrowed, interest and duration as input. Then, compute the compound interest rate. 5 | 6 | ### Hint 7 | Compound interest formula is: 8 | ```python 9 | A = P(1+r/100)t 10 | ``` 11 | 12 | Here, P is the principal amount; it is the amount that you borrowed. r is the interest rate in percentage and t is the time. 13 | 14 | ## Solution 15 | 16 | ```python 17 | def compound_interest(principle, rate, time): 18 | interest = principle * ((1 + rate / 100) ** time) 19 | return interest 20 | 21 | principle = int(input("Money you borrowed: ")) 22 | interest_rate = float(input("Interest Rate: ")) 23 | time = float(input("Overall Duration: ")) 24 | 25 | total_due = compound_interest(principle, interest_rate, time) 26 | 27 | print("Interest Amount is:", total_due) 28 | ``` 29 | 30 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 31 | 32 | ## Explanation 33 | Inside the function, just look into the power calculation. 34 | 35 | **(1 + rate / 100) time** 36 | 37 | Rest of the part should be easy for you. 38 | 39 | ## Take Away 40 | To apply the same power on multiple things, put them inside parentheses and then apply the power. 41 | 42 | 43 |   44 | [![Next Page](../assets/next-button.png)](Calculate-Grades.md) 45 |   46 | 47 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 48 | -------------------------------------------------------------------------------- /Conversions/Miles-to-Kilometers.md: -------------------------------------------------------------------------------- 1 | # Miles to Kilometers 2 | 3 | ## The Problem 4 | Convert miles to kilometers. 5 | 6 | ## Hints 7 | I am telling you just one thing: 8 | 9 | 1 mile = 1.609344 kilometers 10 | 11 | Now, think what you can do with this information. 12 | 13 | ## The solution 14 | 15 | ```python 16 | miles = float(input("Enter distance in miles: ")) 17 | kilometers = miles*1.609344 18 | print("Distance in Kilometers:", kilometers) 19 | ``` 20 | 21 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 22 | 23 | ## Explanation 24 | Just take a number from the user. Allow the user to enter a float number. Then, multiply that number by 1.609344. Keep the multiplication in the kilometers variable. 25 | 26 | The kilometers is your answer. 27 | 28 | Oh, wait...you are not done yet! 29 | 30 | Now, tell me, if you have to do this using a function, how will you do that? 31 | 32 | Go to the code editor and then try it yourself. When you do it yourself without following instructions, you will understand it. You will be able to feel the code and that is a big step in becoming a programmer! 33 | 34 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 35 | 36 | 37 |   38 | [![Next Page](../assets/next-button.png)](Celsius-to-Fahrenheit.md) 39 |   40 | 41 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 42 | 43 | -------------------------------------------------------------------------------- /Medium/Check-palindrome.md: -------------------------------------------------------------------------------- 1 | # 9.1 Check Palindrome 2 | 3 | ## The problem 4 | 5 | Check whether the string is a palindrome. 6 | 7 | 8 | ## Hint 9 | A palindrome is a word, phrase, or sequence that reads the same backward as forwards. For example, the word- madam 10 | 11 | If you read it backward it will be madam as well. Similarly, eye, rotor, kayak, racecar, level, etc. are palindromes. 12 | 13 | To check a palindrome, just reverse the word or the phrase, and then check whether the reversed string is the same as the initial string. 14 | 15 | If both are the same, it will be a palindrome. 16 | 17 | ## Solution 18 | ```python 19 | my_str = input('String to check: ') 20 | rev_str = my_str[::-1] 21 | 22 | if my_str == rev_str: 23 | print("It is palindrome") 24 | else: 25 | print("It is not palindrome") 26 | ``` 27 | 28 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 29 | 30 | ## Explanation 31 | We used the shortcut method to get the reversed string. And then we compared using if-else. 32 | 33 | That's it. 34 | 35 | The problem might sound complicated at the beginning. However, if you know the technique, the solution could become easier. 36 | 37 | So, the more you explore, the more you try, the more shortcut trick you will master. 38 | 39 | 40 |   41 | [![Next Page](../assets/next-button.png)](Dictionary-of-cubes.md) 42 |   43 | 44 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 45 | 46 | -------------------------------------------------------------------------------- /User-Submitted/Calculate-age.md: -------------------------------------------------------------------------------- 1 | # 13.3: Calculate age 2 | 3 | ## The Problem 4 | Take a birthday of a person and then calculate the age. 5 | 6 | This code and solution is contributed by Programmer Karim. 7 | 8 | ## Hints 9 | Take the user birthday. Use the same method that you used in the previous problem. 10 | 11 | Then, to calculate age, get the days and then divide the days by 365. 12 | 13 | (This is not the perfect way to calculate age...Feel free to find better solutions) 14 | 15 | ## Solution 16 | This code is contributed by Programmer Karim... 17 | 18 | ```python 19 | from datetime import datetime 20 | import time 21 | 22 | 23 | def calculate_age(born): 24 | today = datetime.today() 25 | days = (today-born).days 26 | age = days // 365 27 | return age 28 | 29 | def get_user_birthday(): 30 | date_str = input("Enter your birth date in DD/MM/YYYY: ") 31 | try: 32 | birthday = datetime.strptime(date_str, "%d/%m/%Y") 33 | except TypeError: 34 | birthday = datetime.datetime(*(time.strptime(date_str, "%d/%m/%Y")[0:6])) 35 | return birthday 36 | 37 | birthday = get_user_birthday() 38 | age = calculate_age(birthday) 39 | print("Your age: ", age) 40 | ``` 41 | 42 | ## Explanation 43 | You used the datetime earlier. And then, calculated the days between the birth date and today’s date. 44 | 45 | Then, divide the days by 365 to get the age! 46 | 47 | 48 | 49 |   50 | [![Next Page](../assets/next-button.png)](../README.md) 51 |   52 | 53 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Medium/Dictionary-of-cubes.md: -------------------------------------------------------------------------------- 1 | # 9.2: Cube Sum 2 | 3 | ## The Problem 4 | With a given integral number n, write a program to calculate the sum of cubes. 5 | 6 | ## Hints 7 | Cubes mean power of 3. Hence, the cube of 9 is 9**3. 8 | 9 | Here you have to calculate the cube of a series. If you want to calculate the cube up to the number n. The series will look like- 10 | 11 | `1^3+2^3+3^3+4^3+ .. .. .. +n^3` 12 | 13 | If you remembered the sum of the square, this one will be easier for you. 14 | 15 | ## The solution 16 | 17 | ```python 18 | def cube_sum(num): 19 | sum = 0 20 | for n in range(num+1): 21 | sum = sum + n**3 22 | return sum 23 | 24 | 25 | user_num = int(input('Enter a number: ')) 26 | result = cube_sum(user_num) 27 | print('Your sum of cubes are: ', result) 28 | ``` 29 | 30 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 31 | 32 | ## Think Different 33 | There is an alternative solution to calculate the sum of cube of the n numbers. You can use 34 | 35 | `(n*(n+1)/2)^2` 36 | 37 | 38 | ## Alternative Solution 39 | 40 | ```python 41 | n = int(input('Enter a number: ')) 42 | sum = (n*(n+1)/2)**2 43 | print('Your sum of cubes are: ', sum) 44 | ``` 45 | 46 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 47 | 48 | ## Take Away 49 | Sum of a series might have an easier formula. 50 | 51 |   52 | [![Next Page](../assets/next-button.png)](Armstrong-number.md) 53 |   54 | 55 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 56 | 57 | 58 | -------------------------------------------------------------------------------- /Easy-ones/Math-Power.md: -------------------------------------------------------------------------------- 1 | 2 | # Math Power 3 | 4 | ## The problem 5 | Take two numbers from the users. Calculate the result of second number power of the first number. 6 | 7 | ## Hints 8 | To power, you will need to use two asterisks symbols (two multiplication symbols). For example 4 to the power 3 will be 9 | 10 | 11 | `result = 4**3` 12 | 13 | ## Solution 14 | ```python 15 | base_num = int(input('Give me the base number: ')) 16 | power_num = int(input('Give me the power number: ')) 17 | result = base_num ** power_num 18 | print('Your result is: ', result) 19 | ``` 20 | 21 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 22 | 23 | ## Think Different 24 | Python has a built-in function named `pow`. The pow is a short form of the word power. And you can pass 2 numbers to the pow function. It will give you the second number as a power of the first number. 25 | 26 | 27 | ## Alternative Solution 28 | Python has a built-in function named pow. The pow is a short form of the word power. And you can pass 2 numbers to the pow function. It will give you the second number as a power of the first number. 29 | 30 | ```python 31 | base_num = int(input('Give me the base number: ')) 32 | power_num = int(input('Give me the power number: ')) 33 | result = pow(base_num, power_num) 34 | print('Your result is: ', result) 35 | ``` 36 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 37 | 38 |   39 | [![Next Page](../assets/next-button.png)](Random-Number.md) 40 |   41 | 42 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 43 | 44 | -------------------------------------------------------------------------------- /Loop-Related/Sum-of-elements.md: -------------------------------------------------------------------------------- 1 | # 3.1: Sum of elements 2 | 3 | ## The Problem 4 | For a given list, get the sum of each number in the list 5 | 6 | ## Hint 7 | Should be simple for you. Declare a sum variable. Then just loop through the list and add it to the sum. 8 | 9 | ## The solution 10 | ```python 11 | def get_sum(nums): 12 | sum = 0 13 | for num in nums: 14 | sum = sum + num 15 | return sum 16 | 17 | 18 | nums = [13,89,65,42,12,11,56] 19 | 20 | total = get_sum(nums) 21 | print("The total of each elements:",total) 22 | ``` 23 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 24 | 25 | ## Explanation: 26 | It’s super simple. 27 | 28 | You got a list. Loop through the list. You have done that multiple times while learning Fundamentals. 29 | 30 | Declare a variable named sum before the loop. And inside the loop, add the number to the sum variable. 31 | 32 | And then finally return the sum. 33 | 34 | That’s it. 35 | Super easy. Even your grandma can do it. 36 | 37 | ## Shortcut 38 | 39 | There is an easier way to get sum of all numbers in a list. You can just pass the list of numbers to the sum function. 40 | ```python 41 | nums = [13, 11, 16, 78, 31, 128] 42 | 43 | total = sum(nums) 44 | print(total) 45 | ``` 46 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 47 | 48 | ## Quiz 49 | 50 | What is the shortcut way to sum all the numbers in a list? 51 | 52 | 1. Loop through the items 53 | 2. Use the sum function 54 | 3. Use a calculator 55 | 56 |
57 | Show Answer 58 |

The answer is: 2

59 |
60 | 61 | ## Take Away 62 | 63 | Use the sum function to sum all the numbers in a list. 64 | 65 | 66 |   67 | [![Next Page](../assets/next-button.png)](Largest-element-of-a-list.md) 68 |   -------------------------------------------------------------------------------- /Prime-number/Prime-Factors.md: -------------------------------------------------------------------------------- 1 | # Prime Factors 2 | 3 | ## The problem 4 | Ask the user to enter a number. Then find all the primes factors for the number. 5 | 6 | ## Hints 7 | A prime factor is a prime number that could divide a number. 8 | 9 | For example, if you think about the number 35. It has two prime factors: 5 and 7. 10 | 11 | Both 5 and 7 are prime numbers. And they can divide the number 35. 12 | 13 | Similarly, for the number 10, you will have two factors 2 and 5. 14 | 15 | To find the factor, you will run a while loop. 16 | 17 | Before the loop, add an empty list to store the prime number. And a variable named divisor whose starting value is 2. 18 | 19 | In the loop, you will start dividing with the number by the divisor. If the number gets divided, add it in the factors list. 20 | 21 | If it is not divided, increase the divisor by 1. 22 | 23 | ## The Solution 24 | ```python 25 | def get_prime_factors(n): 26 | factors = [] 27 | divisor = 2 28 | while n > 2: 29 | if(n % divisor == 0): 30 | factors.append(divisor) 31 | n = n / divisor 32 | else: 33 | divisor = divisor + 1 34 | return factors 35 | 36 | num = int (input('Enter your number: ')) 37 | factors = get_prime_factors(num) 38 | print(factors) 39 | ``` 40 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 41 | 42 | ## take Away 43 | Prime factor is a prime number that divides the provided number. 44 | 45 |   46 | [![Next Page](../assets/next-button.png)](Smallest-prime-factor.md) 47 |   48 | 49 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Prime-number/Prime-Numbers.md: -------------------------------------------------------------------------------- 1 | # 7.2 All Prime Numbers 2 | 3 | ## the problem 4 | Ask the user to enter a number. Then find all the primes up to that number. 5 | 6 | ## Hints 7 | 8 | You can use the previous is_prime function that you created in the previous problem. Now create a new function named all_primes. In that function, run a for loop. 9 | 10 | For each number, call the is_prime function. If the return is True to add it in the prime list. 11 | 12 | ## The Solution 13 | ```python 14 | def is_prime(num): 15 | for i in range(2,num): 16 | if (num % i) == 0: 17 | return False 18 | return True 19 | 20 | def all_primes(num): 21 | primes = [] 22 | for n in range(2,num+1): 23 | if is_prime(n) is True: 24 | primes.append(n) 25 | return primes 26 | 27 | num = int(input("Enter upper limit: ")) 28 | primes = all_primes(num) 29 | print(primes) 30 | ``` 31 | 32 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 33 | 34 | ## Explanation 35 | The first one is the is_prime that you have seen before. Now we created another function named all_primes. In the all_primes function, we create a list named primes to hold the prime numbers. Then run a for loop using range. 36 | 37 | The range starts at 2 and runs until num+1 so that it goes until num. We are starting at 2. Because 2 is the first prime number. 38 | 39 | Inside the loop, call the is_prime function and check the return. If the return is True then append the n in the primes list. 40 | 41 | Then return the primes list. 42 | 43 | That’s it. 44 | 45 |   46 | [![Next Page](../assets/next-button.png)](Prime-Factors.md) 47 |   48 | 49 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Number-Related/Sum-of-digits.md: -------------------------------------------------------------------------------- 1 | 2 | # Sum of Elements 3 | 4 | ## The problem 5 | For a given list, get the sum of each number in the list 6 | 7 | ## Hints 8 | Should be simple for you. Declare a sum variable. Then just loop through the list and add it to the sum. 9 | 10 | ## Solution 11 | ```python 12 | def get_sum(nums): 13 | sum = 0 14 | for num in nums: 15 | sum = sum + num 16 | return sum 17 | 18 | 19 | nums = [13,89,65,42,12,11,56] 20 | 21 | total = get_sum(nums) 22 | print("The total of each elements:",total) 23 | ``` 24 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 25 | 26 | ## Explanation 27 | It’s super simple. 28 | 29 | You got a list. Loop through the list. You have done that multiple times while learning Fundamentals. 30 | 31 | Declare a variable named sum before the loop. And inside the loop, add the number to the sum variable. 32 | 33 | And then finally return the sum. 34 | 35 | That’s it. 36 | Super easy. Even your grandma can do it. 37 | 38 | ## Shortcut 39 | There is an easier way to get sum of all numbers in a list. You can just pass the list of numbers to the sum function. 40 | 41 | ```python 42 | nums = [13, 11, 16, 78, 31, 128] 43 | 44 | total = sum(nums) 45 | print(total) 46 | ``` 47 | 48 | ## Quiz 49 | What is the shortcut way to sum all the numbers in a list? 50 | 51 | 1. Loop through the items 52 | 2. Use the sum function 53 | 3. Use a calculator 54 | 55 |
56 | Show Answer 57 |

The answer is : 2

58 |
59 | 60 | ## Take Away 61 | Use the sum function to sum all the numbers in a list. 62 | 63 |   64 | [![Next Page](../assets/next-button.png)](../Loop-Related/Sum-of-elements.md) 65 |   66 | 67 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Medium/Least-Common-Multiple.md: -------------------------------------------------------------------------------- 1 | # 9.5: LCM 2 | 3 | ## The Problem 4 | For two numbers, calculate the least common multiple (LCM). 5 | 6 | ## Hint 7 | The multiples of 4 and 5 are below: 8 | 9 | Multiple of `4: 4, 8, 12, 16, 20 , 24 28 ,32, 36, 40, 44` 10 | Multiple of `5: 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 ` 11 | 12 | Now, what are the common multiples? The common number that exists in multiple of 4 and 5 are: 13 | 14 | Common multiples: `20, 40, 60, 80` 15 | 16 | So, you can tell the smallest common multiple is 20. Hence, the least common multiple(LCM) of 4 and 5 is 20. 17 | 18 | ## The solution 19 | ```python 20 | def calculate_lcm(x, y): 21 | lcm = max(x,y) 22 | while lcm % x != 0 or lcm % y != 0: 23 | lcm += 1 24 | return lcm 25 | 26 | num1 = int(input("First number: ")) 27 | num2 = int(input("Second number: ")) 28 | lcm = calculate_lcm(num1, num2) 29 | 30 | print(f"LCM of {num1} and {num2} is: {lcm}") 31 | ``` 32 | 33 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 34 | 35 | ## Explanation 36 | First, we calculate the greater number, because LCM will be equal or higher than the highest of the two numbers. 37 | 38 | Another important point is: we know where the loop will start. It will be the highest of the two numbers. However, we don’t know where it will stop. That’s why we can’t use a for loop with a range. 39 | 40 | Instead, we used a while loop. 41 | 42 | In the while loop, if we can not divide the lcm by the first number or the second number, we keep increasing the lcm. 43 | 44 | That's it. 45 | 46 | It's actually much easier than it seems. 47 | 48 | 49 | 50 |   51 | [![Next Page](../assets/next-button.png)](../Programming-Contest/Programming-Contest.md) 52 |   53 | 54 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 55 | 56 | -------------------------------------------------------------------------------- /Conversions/Decimal-to-binary-recursive.md: -------------------------------------------------------------------------------- 1 | # 4.4: Decimal to Binary (recursive) 2 | 3 | ## The Problem 4 | Convert a decimal number to binary number using a recursive function. 5 | ### Hints 6 | After coding for a while, recursive will become fun. Until then, recursive functions might feel like confusing magic. 7 | 8 | So, don’t worry if you felt confused. You are not alone. I am in the same condition as well. 9 | 10 | ## Recursive 11 | ```python 12 | def dec_to_binary(n): 13 | if n > 1: 14 | dec_to_binary(n//2) 15 | print(n % 2,end = '') 16 | ``` 17 | 18 | ## decimal number 19 | ```python 20 | num = int(input("Your decimal number: ")) 21 | dec_to_binary(num) 22 | print(" ") 23 | ``` 24 | 25 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 26 | 27 | ## Explanation 28 | The core part of the logic is simple. If the number is greater than 1, call the dec_to_binary function again. And, while calling, send the result of dividing operation as the input number. 29 | 30 | If you remember, the while loop in the previous code problem is similar. In the while loop, we were going back to the next iteration with n = n//2 31 | 32 | Here we are calling the same function with the n//2 33 | 34 | In the previous code problem (iterative), we are putting the remainder in a list. Here, we are printing it right away. 35 | 36 | While printing, we have one extra thing called end=''. 37 | The purpose of end='' is to print the output in the same line. If you don’t add end='', every print output will be displayed in a new line. 38 | 39 | 40 | ## Take Away 41 | There are multiple ways to format the print string. Google it, when needed. 42 | 43 | 44 |   45 | [![Next Page](../assets/next-button.png)](../Solution-Strategy.md) 46 |   47 | 48 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 49 | -------------------------------------------------------------------------------- /Computations/Triangle-Area.md: -------------------------------------------------------------------------------- 1 | ## Triangle Area 2 | --- 3 | 4 | # The problem 5 | Take three sides of a triangle. And then calculate the area of the triangle. 6 | 7 | ## How it works 8 | To calculate the area of the triangle. First, calculate the half of the perimeter. Here perimeter is the sum of each side of the triangle. 9 | 10 | Let’s call it s. 11 | 12 | Then you have to perform square root of the formula like below- 13 | 14 | ```python 15 | s = (a+b+c)/2 16 | area = √(s(s-a)*(s-b)*(s-c)) 17 | ``` 18 | 19 | ## the code 20 | ```python 21 | import math 22 | 23 | a = float(input('Enter first side: ')) 24 | b = float(input('Enter second side: ')) 25 | c = float(input('Enter third side: ')) 26 | 27 | # calculate the semi-perimeter 28 | s = (a + b + c) / 2 29 | 30 | # calculate the area 31 | area = math.sqrt(s*(s-a)*(s-b)*(s-c)) 32 | print('Area of your triangle is ', area) 33 | ``` 34 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 35 | 36 | ## Explanation 37 | To calculate the square root. We used the math module. And call math.sqrt. 38 | 39 | ```python 40 | import math 41 | print ( math.sqrt(4) ) 42 | ``` 43 | 44 | This will give you 2 as output. 45 | 46 | Similarly, math.sqrt(25) will give 5 as output. 47 | 48 | This is something new you have learned this time. 49 | 50 | ## Quiz 51 | 52 | How would you calculate the square root of a number. 53 | 1. Use math.square.root 54 | 2. Use math.sqroot 55 | 3. Use math.sqrt 56 | 57 |
58 | Show Answer 59 |

The answer is: 3

60 |
61 | 62 | ## take Away 63 | The math module has a lot of math-related functionalities. 64 | 65 | 66 |   67 | [![Next Page](../assets/next-button.png)](../Prime-number/Check-Prime.md) 68 |   69 | 70 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 71 | -------------------------------------------------------------------------------- /Loop-Related/Largest-element-of-a-list.md: -------------------------------------------------------------------------------- 1 | 2 | # Largest element of a list 3 | 4 | ## The problem 5 | Find the largest element of a list. 6 | 7 | ## Hints 8 | Take the first element as the largest number. Then loop through the list and compare each element. 9 | 10 | 11 | ## Solution 12 | ```python 13 | def get_largest(nums): 14 | largest = nums[0] 15 | for num in nums: 16 | if num > largest: 17 | largest = num 18 | return largest 19 | 20 | my_nums = [0,15,68,1,0,-55] 21 | 22 | largest = get_largest(my_nums) 23 | 24 | print('The largest number is: ', largest) 25 | ``` 26 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 27 | 28 | ## Explanation 29 | In the beginning, consider the first element of the list as the largest element. 30 | 31 | Don’t set the largest value to 0. If every number in the list is a negative number, your return will be wrong. 32 | 33 | Then just loop through the list and compare. If the current number is greater than the largest number, set it as the largest number. 34 | 35 | That’s it. Easy peasy. 36 | 37 | ## Shortcut 38 | One shortcut is to pass a list of numbers to the max function. This will return the largest number in the list. 39 | 40 | ```python 41 | nums = [41, 69, 23, 45, 19, 8] 42 | 43 | largest = max(nums) 44 | print(largest) 45 | ``` 46 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 47 | 48 | ## Quiz 49 | What is the easiest way to find the largest number in a list? 50 | 1. Use the max function 51 | 2. Use the min function 52 | 3. Use the sum function 53 | 54 | 55 | ## Take Away 56 | 57 | Use the max function to get the largest number in a list. 58 | 59 |   60 | [![Next Page](../assets/next-button.png)](Sum-of-squares.md) 61 |   62 | 63 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 64 | 65 | -------------------------------------------------------------------------------- /Easy-ones/User-input-to-Number.md: -------------------------------------------------------------------------------- 1 | # 100 Plus Python Coding Problems With Solutions 2 | --- 3 | 4 | # User input to Number 5 | 6 | ## The problem 7 | Take two inputs from the user. One will be an integer. The other will be a float number. Then multiply them to display the output. 8 | 9 | ## Hints 10 | Use input. By default, input gives you a string. Then use int and float to convert the input to a number. And then multiply them. That’s it. 11 | 12 | ## Solution 13 | ```python 14 | int_text = input("Give me an integer number: ") 15 | int_num = int(int_text) 16 | float_text = input("Give me a float number: ") 17 | float_num = float(float_text) 18 | result = int_num * float_num 19 | print("Your result is: ", result) 20 | ``` 21 | 22 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 23 | 24 | ## Shortcut 25 | > You wrote input in one line and then in the next line you used int or float to convert the number. You can write the two lines in one line. Like below 26 | 27 | ```python 28 | int_num = int(input('Give me an integer number: ')) 29 | float_num = float(input('Give me a float number: ')) 30 | result = int_num * float_num 31 | print('Your result is: ', result) 32 | ``` 33 | 34 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 35 | 36 | ## Going Forward 37 | Going forward, we will write input and conversion in one line. 38 | 39 | ## Quiz 40 | Which one is used to convert string to a number? 41 | 42 | 1. number 43 | 2. convert 44 | 3. int or float 45 | 46 |
47 | Show Answer 48 |

The answer is : 3

49 |
50 | 51 | ## Take Away* 52 | 53 | Use int or float to convert user input to a number.
54 | 55 |   56 | [![Next Page](../assets/next-button.png)](Math-Power.md) 57 |   58 | 59 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 60 | -------------------------------------------------------------------------------- /Solution-Strategy.md: -------------------------------------------------------------------------------- 1 | 2 | # 5. Solution Strategy 3 | 4 | --- 5 | 6 | ## Practice 7 | The more problems you solve, the better developer you will become. 8 | 9 | However, to become good at solving problems, you will need 3P(practice, patience, and perseverance). 10 | 11 | ## Read the problem 12 | Whenever you start solving a new problem: 13 | 14 | Read the problem at least 3 times. Try to understand the problem. If there is a note, read that. And then, try to think about the problem in your own words. 15 | 16 | ## Input-Output 17 | The second step is to think about possible input and possible output. If possible, think about 3 inputs and their output. 18 | 19 | For example, if you think about the second largest number of a list...for the list below, 82 would be the answer. 20 | 21 | `[31, 15, 34, 87, 22, 82, 56]` 22 | 23 | ## Write the steps 24 | If you see a large problem for the first time, write the solution steps in a paper...This will save your time while coding. 25 | 26 | ## Incremental 27 | While coding, use the print command to see the output. Most of the time, write a few lines, and then write a print command to see the output. 28 | 29 | If you see the expected output, then delete the print command and go to the next step. 30 | 31 | ## Comment 32 | If you are solving a large problem, don’t forget to write a few useful comments...This will help you to debug. 33 | 34 | ## Google 35 | Even if you have a solution, don’t consider it as the best solution. Instead, Google it. You will be surprised by seeing other people’s solution. You will learn a lot from other available solutions out there. 36 | 37 | ## Keep doing 38 | There is just one way to become good at problem-solving - Just solve more problems. 39 | 40 | ## Take away 41 | Keep solving more problems. 42 | 43 |   44 | [![Next Page](assets/next-button.png)](Computations/Simple-Interest.md) 45 |   46 | 47 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Easy-ones/Temporary-variable.md: -------------------------------------------------------------------------------- 1 | 2 | # Swap two variables 3 | 4 | ## The problem 5 | > Swap two variables.

To swap two variables: the value of the first variable will become the value of the second variable. On the other hand, the value of the second variable will become the value of the first variable. 6 | 7 | ## Hints 8 | To swap two variables, you can use a temp variable. 9 | 10 | ## Solution 11 | 12 | ```python 13 | a = 5 14 | b = 7 15 | print('a, b', a, b) 16 | # swap these two 17 | temp = a 18 | a = b 19 | b = temp 20 | print('a, b', a, b) 21 | ``` 22 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 23 | 24 | ## Shortcut 25 | You can use a python shortcut to swap two variables as well. How this works, would be a little bit tricky for you. So, for now, just remember it. 26 | 27 | ```python 28 | x = 12 29 | y = 33 30 | print('x, y', x, y) 31 | #swap these two 32 | x, y = y, x 33 | print('x, y', x, y) 34 | ``` 35 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 36 | 37 | ## Another solution 38 | If both the variable is number, you can apply other tricks. Like the one below- 39 | 40 | ```python 41 | a = 5 42 | b = 7 43 | print('a, b', a, b) 44 | # swap these two 45 | a = a + b 46 | b = a - b 47 | a = a - b 48 | print('a, b', a, b) 49 | ``` 50 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 51 | 52 | ## Quiz 53 | How would you swap two variables? 54 | 55 | 1. Using two temporary variables 56 | 2. My family doesn’t permit swapping 57 | 3. Use something like: a, b= b, a 58 | 59 |
60 | Show Answer 61 |

The answer is : 3

62 |
63 | 64 | ## Take Away 65 | Use temp variable to swap two variables.
66 | 67 |   68 | [![Next Page](../assets/next-button.png)](../Number-Related/max-of-two.md) 69 |   70 | 71 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 72 | 73 | 74 | -------------------------------------------------------------------------------- /Loop-Related/Remove-duplicate-Chars.md: -------------------------------------------------------------------------------- 1 | 2 | ## Remove duplicate characters 3 | 4 | ## The problem 5 | For a given string, remove all duplicate characters from that string. 6 | 7 | ## Hints 8 | Create a result string. Then loop through the string and check whether the current character not in the string. If it is not, then add it. Otherwise, it’s already there adding it will make it duplicate. 9 | 10 | ## Solution 11 | ```python 12 | def remove_duplicate(your_str): 13 | result = '' 14 | for char in your_str: 15 | if char not in result: 16 | result += char 17 | return result 18 | 19 | user_input = input('what is your string:') 20 | 21 | no_duplicate = remove_duplicate(user_input) 22 | print('Without duplicate: ', no_duplicate) 23 | ``` 24 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 25 | 26 | ## Explanation 27 | We just created a function. In the function, we are taking a string as an input. 28 | 29 | Inside the function, we created a variable named result with initial value as an empty string. 30 | 31 | Our task is to loop through the input string and then for each character/letter check whether the character (char) not in the result. If it is not in the result, we add it to the result string. 32 | 33 | If it is already added in the result string, we don’t add it to it. Because, if we add it again, it will become a duplicate. 34 | 35 | So, the overall task is very simple. 36 | 37 | If needed, go to the code playground type the code multiple times and you will understand it. 38 | 39 | ## Quiz 40 | How would you check whether a character exists in a string? 41 | 42 | 1. not in 43 | 2. == 44 | 3. index 45 | 46 |
47 | Show Answer 48 |

The answer is : 1

49 |
50 | 51 | ## Take Away 52 | The not in is just the opposite check of in. 53 | 54 | 55 |   56 | [![Next Page](../assets/next-button.png)](../Conversions/Miles-to-Kilometers.md) 57 |   58 | 59 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 60 | 61 | -------------------------------------------------------------------------------- /Simple-Game/Cows-and-bulls(4digits).md: -------------------------------------------------------------------------------- 1 | # 11.4: Bulls and Cows (4 digits) 2 | 3 | ## The Problem 4 | 5 | Create a bulls and cows game for guessing a randomly generated 4 digits number. 6 | 7 | ## Hints 8 | This one should be easier for you. Just make sure that the random number is four digits. Hence, it should be between 1000 and 9999. 9 | 10 | To calculate the bulls and cows, 11 | 12 | ## Solution 13 | ```python 14 | import random 15 | 16 | secret_number = str(random.randint(1000, 9999)) 17 | print("Guess the number. It contains 4 digits.") 18 | 19 | remaining_try = 7 20 | 21 | def get_bulls_cows(number, user_guess): 22 | bulls_cows = [0,0] #cows, then bulls 23 | for i in range(len(number)): 24 | if user_guess[i] == number[i]: 25 | bulls_cows[0] += 1 26 | elif user_guess[i] in number: 27 | bulls_cows[1] += 1 28 | return bulls_cows 29 | 30 | while remaining_try > 0: 31 | player_guess = input("Enter your guess: ") 32 | 33 | if player_guess == secret_number: 34 | print("Yay, you guessed it!") 35 | print("YOU WIN.") 36 | break 37 | else: 38 | bulls_cows = get_bulls_cows(secret_number,player_guess) 39 | 40 | print("Bulls: ",bulls_cows[0]) 41 | print("Cows: ",bulls_cows[1]) 42 | 43 | remaining_try -= 1 44 | 45 | if remaining_try < 1: 46 | print("You lost the game.") 47 | break 48 | ``` 49 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 50 | 51 | ## Explanation 52 | After solving the Bulls and cows for 2 digits, this one should become easier for you. 53 | 54 | However, solving this one by using if-else that we did for 2 digit bulls and cows game will become very harder. 55 | 56 | That's why we created the get_bulls_cows function. Inside that, we created a loop. For each digit, check the digit in the same place. 57 | 58 | 59 |   60 | [![Next Page](../assets/next-button.png)](Word-completion.md) 61 |   62 | 63 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Harder/Simple-Calculator.md: -------------------------------------------------------------------------------- 1 | # 12.1 Simple Calculator 2 | 3 | ## The task 4 | Create a simple calculator. That will be able to take user input of two numbers and the operation the user wants to perform. 5 | 6 | ## Solution strategy 7 | Create a bunch of functions to perform add, subtract, multiply, division or modulo. 8 | 9 | Then take two numbers from the user and the operation he/she wants to perform. Either +,-,*,/ or %. 10 | 11 | Then call the appropriate function based on the operation. 12 | 13 | Think for a few minutes and try it yourself first. 14 | 15 | ## The solution 16 | ```python 17 | def add(num1, num2): 18 | return num1 + num2 19 | 20 | def subtract(num1, num2): 21 | return num1 - num2 22 | 23 | def multiply(num1, num2): 24 | return num1 * num2 25 | 26 | def divide(num1, num2): 27 | return num1 / num2 28 | 29 | def modulo(num1, num2): 30 | return num1 % num2 31 | 32 | # Take input from the user 33 | num1 = int(input("Enter first number: ")) 34 | operation = input("What you want to do(+, -, *, /, %):") 35 | num2 = int(input("Enter second number: ")) 36 | 37 | result = 0 38 | if operation == '+': 39 | result = add(num1,num2) 40 | elif operation == '-': 41 | result = subtract(num1,num2) 42 | elif operation == '*': 43 | result = multiply(num1,num2) 44 | elif operation == '/': 45 | result = divide(num1,num2) 46 | elif operation == '%': 47 | result = modulo(num1,num2) 48 | else: 49 | print("Please enter: +, -, *, / or %") 50 | 51 | print(num1, operation, num2, '=', result) 52 | ``` 53 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 54 | 55 | 56 | ## How it works 57 | You saw five functions to add, subtracts, etc. Those are easy. 58 | 59 | Then we are taking user inputs. Three inputs. They are easy too. 60 | 61 | Then we have if-elif-else. And based on the operation, we call the right method to perform the task. 62 | 63 | That’s it. 64 | 65 | 66 |   67 | [![Next Page](../assets/next-button.png)](Password-generator.md) 68 |   69 | 70 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Reverse/Reverse-String-(stack).md: -------------------------------------------------------------------------------- 1 | # 8.2 Reverse a string (stack) 2 | 3 | ## The Problem 4 | Reverse a string using a stack. 5 | 6 | ## Hint 7 | This is a little fancier way to reverse the string. However, this will help you understand one concept called stack. 8 | 9 | If you are not familiar with the stack. Don’t worry. We have it covered in the Data Structure galaxy. 10 | 11 | For now, just remember, you have to use .pop() method to get the last element of the list. 12 | 13 | ## Solution 14 | ```python 15 | def reverse_stack(str): 16 | # Create an empty stack (list to use as stack) 17 | stack = [] 18 | # Push all characters of string to stack 19 | for char in str: 20 | stack.append(char) 21 | # Pop all characters of string 22 | # and add it to the rev 23 | rev = '' 24 | while len(stack) > 0: 25 | last = stack.pop() 26 | rev = rev + last 27 | # print(last, rev) 28 | 29 | return rev 30 | usr_str = input('What is your string:') 31 | reverse = reverse_stack(usr_str) 32 | print('Reversed is: ', reverse) 33 | ``` 34 | 35 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 36 | 37 | ## Explanation 38 | In the function, we declared a list named stack. And then we loop through the input string and put every character of the string inside the list. 39 | 40 | Then we have an empty string. 41 | 42 | Later we ran a while loop. Inside the while loop, we used the .pop() method to get the last element of the list. 43 | 44 | Then we added the last element to the reverse string. 45 | 46 | The overall idea is very simple, take the last element and add keep adding it. 47 | 48 | Hence, the last element will come first and you will put it in the first position. And then, in the next iteration, the second to the last element will be coming and will get added. 49 | 50 | If needed, add a print command to see how it works. 51 | 52 |   53 | [![Next Page](../assets/next-button.png)](Reverse-String-(recursive).md) 54 |   55 | 56 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 57 | 58 | -------------------------------------------------------------------------------- /Number-Related/max-of-two.md: -------------------------------------------------------------------------------- 1 | 2 | # max of two 3 | 4 | ## The problem 5 | Find the max number of two numbers. 6 | 7 | ## Hints 8 | Ask the user to enter two numbers. 9 | 10 | Then, you can run a comparison to compare which one is larger. 11 | 12 | Think about it and try yourself first. 13 | 14 | ## Solution 15 | ```python 16 | num1 = int(input("First number: ")) 17 | num2 = int(input("Second number: ")) 18 | if (num2 >= num1): 19 | largest = num2 20 | else: 21 | largest = num1 22 | print("Largest number you entered is: ", largest) 23 | ``` 24 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 25 | 26 | ## Shortcut 27 | Another simple and alternative solution is to send the numbers to the max function. 28 | ```python 29 | num1 = int(input("First number: ")) 30 | num2 = int(input("Second number: ")) 31 | largest = max(num1, num2) 32 | print("Largest number you entered is: ", largest) 33 | ``` 34 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 35 | 36 | ## Alternative Solution 37 | Another alternative approach is to use the floor method from the math module. If you pass a number with a fraction to the math.floor function, it will return you the integer part of the number. 38 | 39 | For example, 40 | ```python 41 | import math 42 | result = math.floor(3.4) 43 | print(result) 44 | ``` 45 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 46 | 47 | ## Alternative Solution 48 | Do you want to see your name in this app as a contributor? 49 | 50 | If yes, find a better solution or alternative solution of any of the problems here. Then, send your solution with a code explanation to programming.hero1@gmail.com 51 | 52 | If your solution is approved by the Team Programming Hero, we will publish your solution in the app with your name as the contributor. 53 | 54 | > That would be fun. 55 | 56 |   57 | [![Next Page](../assets/next-button.png)](Max-of-three.md) 58 |   59 | 60 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 61 | 62 | -------------------------------------------------------------------------------- /Reverse/Reverse-Number.md: -------------------------------------------------------------------------------- 1 | # Reverse a number 2 | 3 | ## The problem 4 | Reverse a number. 5 | 6 | ## Hints 7 | You have to think differently to reverse a number. 8 | 9 | Let’s say you have the number 123. 10 | 11 | `123 = 1 * 100 + 2 * 10 + 3` 12 | 13 | First, you have to take the last digit of the number. You have done this many times. Just divide the number by 10 and get the remainder. 14 | 15 | Once you have the last digit. Add it to a variable named reverse. 16 | 17 | And get rid of the last digit by performing a floor division by 10. Then your 123//10 will make the remaining number 12. 18 | 19 | In the next step, get the last digit of the remaining number (12) which will be 2. Multiply the reverse number by 10 and then add the new last digit. That's why your new reverse number will become 30+2. This will be 32. 20 | 21 | Once again perform a division on the remaining number. 12//10 will make it 1. 22 | 23 | In the last step, multiply the reverse number by 10. It will become 320 and add the remainder 1. The total number will become 320 + 1 = 321. 24 | 25 | This is the overall process to reverse a number. 26 | 27 | Now, think about the overall algorithm. 28 | 29 | ## Solution 30 | 31 | ```python 32 | def reverse_num(num): 33 | reverse = 0 34 | while(num>0): 35 | last_digit = num%10 36 | reverse = reverse*10 + last_digit 37 | num = num//10 38 | return reverse 39 | 40 | n=int(input("Enter number: ")) 41 | reverse = reverse_num(n) 42 | print("Reverse of the number:",reverse) 43 | ``` 44 | 45 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 46 | 47 | ## Explanation 48 | Once you understand the hint, the algorithm will become super easy. 49 | 50 | Inside the while loop, we are getting the last digit by getting remainder. Then we set the new reverse number by multiplying the previous reverse number by 10 and then we are adding the last digit. 51 | 52 | Finally, set the new value of the num by dividing by 10. 53 | 54 | That’s it. 55 | 56 |   57 | [![Next Page](../assets/next-button.png)](Reverse-word.md) 58 |   59 | 60 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 61 | 62 | -------------------------------------------------------------------------------- /Easy-ones/Floor-Division.md: -------------------------------------------------------------------------------- 1 | 2 | # Floor division 3 | 4 | ## The problem 5 | Find the floor division of two numbers. 6 | 7 | ## Hints 8 | Floor division means the integer part of a division operation. For example, if you divide 17/5 the quotient will be 3.4. 9 | 10 | **Here the integer part is 3.** 11 | 12 | > So, you have to find the integer part of the division operation. 13 | 14 | ## Solution 15 | 16 | ```python 17 | num1 = int(input('Enter the first number: ')) 18 | num2 = int(input('Enter the second number: ')) 19 | 20 | result = num1//num2 21 | print(result) 22 | ``` 23 | 24 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 25 | 26 | ## Explanation 27 | When you divide one number by another you get two things. One is called the integer part of the division. Another is the remainder. 28 | 29 | To get the quotient (result without the remainder), you can use two-division symbols. 30 | ```python 31 | print(37//10) 32 | 37 = 3*10+7 33 | ``` 34 | 35 | ## Think different 36 | Another alternative approach is to use the floor method from the math module. If you pass a number with a fraction to the math.floor function, it will return you the integer part of the number. 37 | 38 | For example, 39 | ```python 40 | import math 41 | result = math.floor(3.4) 42 | print(result) 43 | ``` 44 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 45 | 46 | #### Alternative Solution 47 | Now, you can use the floor function to get the floor the division. Like below- 48 | ```python 49 | import math 50 | num1 = int(input('Enter the first number: ')) 51 | num2 = int(input('Enter the second number: ')) 52 | 53 | result = math.floor(num1/num2) 54 | print(result) 55 | ``` 56 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 57 | 58 | ## Quiz 59 | How will you get a floor division? 60 | 61 | 1. // 62 | 2. / 63 | 3. % 64 | 65 |
66 | Show Answer 67 |

The answer is : 1

68 |
69 | 70 | ## Take Away 71 | Use // to get floor division. 72 | 73 |   74 | [![Next Page](../assets/next-button.png)](Temporary-variable.md) 75 |   76 | 77 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 78 | 79 | -------------------------------------------------------------------------------- /Harder/Password-generator.md: -------------------------------------------------------------------------------- 1 | # 12.2: Password generator 2 | 3 | ## The Problem 4 | 5 | Generate a password. Your password may contain letters in uppercase or lowercase. It also may contain digits or special characters. 6 | 7 | ## Hints 8 | To select a random character from a string, you can import random. Then use the random. choice method. This will select a character from the provided string. 9 | 10 | Also, you can import the string module. 11 | 12 | This is not just the string type variable. Instead, it has a lot of extra functionalities. 13 | 14 | For example, you can use string.ascii_letters to get all the characters a to z both in lowercase and upper case. Similarly, you can use string.digits to get all the single digits. Also, you can use string.punctuation to get all the special characters. 15 | 16 | ```python 17 | import string 18 | print('All letters') 19 | print(string.ascii_letters) 20 | print('all digits') 21 | print(string.digits) 22 | print('all special characters') 23 | print(string.punctuation) 24 | ``` 25 | 26 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 27 | 28 | ## Solution 29 | ```python 30 | import string 31 | import random 32 | 33 | def generate_password(size): 34 | all_chars = string.ascii_letters + string.digits + string.punctuation 35 | password = '' 36 | for char in range(size): 37 | rand_char = random.choice(all_chars) 38 | password = password + rand_char 39 | return password 40 | 41 | pass_len = int(input('How many characters in your password?')) 42 | new_password = generate_password(pass_len) 43 | print('Your new password: ', new_password) 44 | ``` 45 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 46 | 47 | 48 | ## Explanation 49 | The solution is rather simple. We imported the random module and the string module. Then we created a long string by joining all ascii_letters, digits and special characters. 50 | 51 | For that, we ran a for loop. In the loop, we select a random letter from the all_chars. To select a random character, we used random.choice. Then we add the random character to the password. 52 | 53 | 54 |   55 | [![Next Page](../assets/next-button.png)](Password-with-requirements.md) 56 |   57 | 58 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Simple-Game/Word-hangman.md: -------------------------------------------------------------------------------- 1 | # 11.6: Word hangman [Premium] 2 | 3 | ## The problem 4 | Create a word hangman game. 5 | 6 | ## Hint 7 | The word hangman is a simple game. You will provide a word with the first letter. Rest of the letters will be blank. For example, 8 | 9 | `s_ _ _ _ _ _ _ _ _` 10 | 11 | The player has to guess the letters in the word. And the player will guess one letter at a time. If the letter exists in the word, it will appear to the player. 12 | 13 | If the letter doesn’t exist in the word, it will be counted as a wrong try. 14 | 15 | If the player exceeds the number of the wrong tries, the player will lose. 16 | 17 | Otherwise, the player will win. 18 | 19 | ## Solution 20 | ```python 21 | import random 22 | 23 | def selected_a_word(): 24 | words = ['working', 'hard', 'makes', 'things', 'easier', 'congrats', 'programming', 'hero'] 25 | word = words[random.randint(0, len(words)-1)] 26 | return word 27 | 28 | def get_blank_word(word): 29 | blank_word = word[0] 30 | for i in range(1, len(word)): 31 | blank_word += '_' 32 | return blank_word 33 | 34 | def word_hangman(word, so_far, letter, try_left): 35 | bad_try = True 36 | for i in range(0, len(word)): 37 | if word[i] == letter: 38 | so_far = so_far[:i]+letter+so_far[i+1:] 39 | bad_try = False 40 | if bad_try is True: 41 | try_left -= 1 42 | print('Wrong Try Left: ', try_left) 43 | print('so far you got: ', so_far) 44 | if word == so_far: 45 | print('YAY!!! You Win') 46 | elif try_left == 0: 47 | print('Opps!!! You Lost') 48 | else: 49 | next_letter = input ('Enter your next letter: ') 50 | word_hangman(word, so_far, next_letter, try_left) 51 | 52 | # play the game 53 | word = selected_a_word() 54 | clue_word = get_blank_word(word) 55 | word_hangman(word, clue_word, word[0], 5) 56 | ``` 57 | 58 | ## Explanation 59 | Take a deep breather. Start reading the code. 60 | 61 | Reading other’s code and trying to understand will be a common activity for a programmer. 62 | 63 | So, practice it here. 64 | 65 | If you don't understand any line, feel free to ask us a question. 66 | 67 |   68 | [![Next Page](../assets/next-button.png)](../Harder/Simple-Calculator.md) 69 |   70 | 71 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Prime-number/Check-Prime.md: -------------------------------------------------------------------------------- 1 | # 7.1 Check Prime: 2 | 3 | ## The problem 4 | For a given number, check whether the number is a prime number or not. 5 | 6 | ## Hint 7 | A number is a prime number. If that number is only divisible by 1 and the number itself. 8 | 9 | This means a prime number is not divisible by any numbers between 1 and the number itself. 10 | 11 | So, to check prime, you can start dividing the number from 2. And then increase it by 1. If the number gets divided, then it’s not a prime number. 12 | 13 | ## The solution 14 | 15 | ```python 16 | def is_prime(num): 17 | for i in range(2,num): 18 | if (num % i) == 0: 19 | return False 20 | return True 21 | 22 | 23 | num = int(input("Enter a number: ")) 24 | 25 | check_prime = is_prime(num) 26 | 27 | if check_prime: 28 | print('Your number is a Prime') 29 | else: 30 | print('Your number is not a Prime') 31 | ``` 32 | 33 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 34 | 35 | ### Explanation 36 | The core part of the algorithm is the is_prime function. 37 | 38 | There we start a for loop the range(2, num). Because we want to know that the number is not divisible by any number except 1 or the number itself. 39 | 40 | For example, 29. It’s only divisible by 1 or 29. 41 | 42 | So, to test whether the number is divisible by any number greater than 1. We started the for loop from 2. And then going to before the num variable. 43 | 44 | To check the number divisible, we check the remainder to be equal to 0. If the number is gets divided, the remainder will be 0. 45 | 46 | So, if the number gets divided by, it’s not a prime number. That’s why we will return False. 47 | 48 | If the number doesn’t get divided by any numbers smaller than the number, it won’t go inside the if block. It will finish all the numbers up to the num. 49 | 50 | Then we will return True. Because it didn’t get divided, by any numbers. Hence, it will be a prime number. 51 | 52 | ## Many Solution 53 | There are other solutions to this problem as well. We encourage you to google, “check prime number python”. In that way, you will learn a lot. 54 | 55 | ## Take Away 56 | A prime number is only divisible by 1 and the number itself. 57 | 58 |   59 | [![Next Page](../assets/next-button.png)](Prime-Numbers.md) 60 |   61 | 62 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 63 | -------------------------------------------------------------------------------- /Number-Related/Max-of-three.md: -------------------------------------------------------------------------------- 1 | 2 | # max of three 3 | 4 | ## The problem 5 | Find the largest of the three numbers. 6 | 7 | ## Hints 8 | Ask the user to enter three numbers. 9 | 10 | Then, you can run multiple comparisons to compare which one is the largest. 11 | 12 | At first, you can consider that the first number is the largest. 13 | 14 | Then compare the second number with the first number and the third number. If the second number is greater or equal to the first number and the second number is greater or equal to the third number, then the second number is the largest. 15 | 16 | Similarly, compare the third number with the first or second number. 17 | 18 | Otherwise, the first number will be the largest. 19 | 20 | Think about it. And try yourself first. 21 | 22 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 23 | 24 | ## Solution 25 | ```python 26 | num1 = int(input("First number: ")) 27 | num2 = int(input("Second number: ")) 28 | num3 = int(input("Third number: ")) 29 | 30 | largest = num1 31 | 32 | if (num2 >= num1) and (num2 >= num3): 33 | largest = num2 34 | elif (num3 >= num1) and (num3 >= num2): 35 | largest = num3 36 | else: 37 | largest = num1 38 | 39 | print("Largest number you entered is: ",largest) 40 | ``` 41 | 42 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 43 | 44 | #### Shortcut 45 | Another simple and alternative solution is to send the numbers to the max function. 46 | ```python 47 | num1 = int(input("First number: ")) 48 | num2 = int(input("Second number: ")) 49 | num3 = int(input("Third number: ")) 50 | 51 | largest = max(num1, num2, num3) 52 | 53 | print("Largest number you entered is: ",largest) 54 | ``` 55 | 56 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 57 | 58 | ## Quiz 59 | What is the easiest way to find out the largest number? 60 | 61 | 1. Use multiple if-else conditions 62 | 2. Use the max function 63 | 3. All numbers are created equal 64 | 65 |
66 | Show Answer 67 |

The answer is : 2

68 |
69 | 70 | ## Take Away 71 | Use the max function to get the largest number. 72 | 73 |   74 | [![Next Page](../assets/next-button.png)](Average-of-numbers.md) 75 |   76 | 77 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Harder/Password-with-requirements.md: -------------------------------------------------------------------------------- 1 | # 12.3: Password with Requirements [Premium] 2 | 3 | ## The Problem 4 | Generate a password that has a minimum of one uppercase, one lowercase, one digit, and one special character 5 | 6 | ## Hint 7 | This one is easier. The string module has two more things to get the uppercase and lower case characters... 8 | 9 | ```python 10 | import string 11 | print('All letters') 12 | print(string.ascii_letters) 13 | print('Al lowercase characters') 14 | print(string.ascii_lowercase) 15 | print('All uppercase characters') 16 | print(string.ascii_uppercase) 17 | ``` 18 | 19 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 20 | 21 | ## Solution 22 | ```python 23 | import random 24 | import string 25 | 26 | def randomPassword(size): 27 | all_chars = string.ascii_letters + string.digits + string.punctuation 28 | password = "" 29 | password += random.choice(string.ascii_lowercase) 30 | password += random.choice(string.ascii_uppercase) 31 | password += random.choice(string.digits) 32 | password += random.choice(string.punctuation) 33 | 34 | for i in range(size-4): 35 | password += random.choice(all_chars) 36 | return password 37 | 38 | pass_len = int(input("What would be the password length? ")) 39 | print ("First Random Password is:", randomPassword(pass_len)) 40 | print ("Second Random Password is:", randomPassword(pass_len)) 41 | print ("Third Random Password is:", randomPassword(pass_len)) 42 | ``` 43 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 44 | 45 | ## Explanation 46 | The all_chars variable is the same as before. After that, we created a password variable with an empty string. 47 | 48 | Then, we added random choice from lowercase. This way, we will make sure that at least one lowercase character is added in the password. Similarly, we added one uppercase, one digit, and one special character. 49 | 50 | After that, we ran a for loop to select random characters from the all_chars. This is similar to the previous problem. We just ran the for loop for (size-4) times, because we already added 4 characters in the password before the loop. 51 | 52 | Make sense? 53 | 54 | Keep going. Only a few more left. I am sure you can do it 55 | 56 | 57 |   58 | [![Next Page](../assets/next-button.png)](Permutations.md) 59 |   60 | 61 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Number-Related/Divisible-by-3-and-5.md: -------------------------------------------------------------------------------- 1 | 2 | ## 2.4: Divisible by 3 and 5 3 | 4 | #### The problem 5 | For a given number, find all the numbers smaller than the number. Numbers should be divisible by 3 and also by 5. 6 | 7 | ## Hints 8 | So, you have to check two conditions: make sure the number is divisible by 3, and also by 5. 9 | Hence, you will need to use two conditions. 10 | 11 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 12 | 13 | #### Solution 14 | ```python 15 | def divisible_by_3and5(num): 16 | result = [ ] 17 | for i in range(num): 18 | if i%3 == 0 and i%5 == 0: 19 | result.append(i) 20 | return result 21 | 22 | num = int (input('Enter your number: ')) 23 | result = divisible_by_3and5(num) 24 | print(result) 25 | ``` 26 | 27 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 28 | 29 | ## Explanation 30 | This one is easy. We took an input number from the user, and then pass it to a function. 31 | 32 | In the function, we have a list and we ran a loop. This loop runs a range. This means we will get nufrom 0 to num. Here, num is the number that the user entered. 33 | 34 | Inside the loop, we have an if block. In the if block, we have two conditions. One condition says i % 3 ==0 35 | 36 | This means if you divide i by 3 and there will not be any remainder. If there is no remainder then the number is divisible by 3. 37 | 38 | Similarly, i%5==0 means the number is divisible by 5. 39 | 40 | As we have and between both conditions, to go, insider, the if-block, the i has to be divisible by 3 and also has to be divisible by 5. 41 | 42 | Inside the if block, we are appending the number in the result list. And then return the list. 43 | 44 | That’s how we are getting every number divisible by 3 and 5. 45 | 46 | > Isn’t it simple and cool? 47 | 48 | ## Quiz 49 | If you want to get the numbers that are either divisible by 3 or divisible by 7, which condition will you use? 50 | 51 | 1. n%3 == 0 or n%7 ==0 52 | 2. n%3 == 0 and n%7 ==0 53 | 3. n%3 == 0 || n%7 ==0 54 | 55 |
56 | Show Answer 57 |

The answer is : 1

58 |
59 | 60 | ## Take Away 61 | Remainder(%) will be 0, if the number gets divided. 62 | 63 |   64 | [![Next Page](../assets/next-button.png)](Sum-of-digits.md) 65 |   66 | 67 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Simple-Game/Rock-paper-scissor.md: -------------------------------------------------------------------------------- 1 | # 11.2: Rock Paper Scissor 2 | 3 | ## The Problem 4 | Build s simple Rock paper Scissor game. 5 | 6 | ## Hint 7 | You will write a lot of if-else. Think about it in a way that if the first player types rock, which one the second player should have picked to win the game. Otherwise, the second player will lose the game. 8 | 9 | If you are not familiar with Rock Paper Scissors, you might be living under a rock for a while. 10 | 11 | Don’t worry. You always have google. If you don’t know about the game, Google it. 12 | 13 | ## Solution 14 | 15 | ```python 16 | def get_winner(p1, p2): 17 | if p1 == p2: 18 | return "It's a tie!" 19 | elif p1 == 'rock': 20 | if p2 == 'scissors': 21 | return "First player wins!" 22 | else: 23 | return "Second Player wins!" 24 | elif p1 == 'scissors': 25 | if p2 == 'paper': 26 | return "First player win!" 27 | else: 28 | return"Second player wins!" 29 | elif p1 == 'paper': 30 | if p2 == 'rock': 31 | return "First player wins!" 32 | else: 33 | return "Second player win!" 34 | else: 35 | return "Invalid input!" 36 | 37 | player1 = input("First player: rock, paper or scissors: ") 38 | player2 = input("Second Player: rock, paper or scissors: ") 39 | 40 | print(get_winner(player1, player2)) 41 | ``` 42 | 43 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 44 | 45 | ## Explanation 46 | We took 2 players' input. Send those to the get_winner function. Inside the function, first, we check whether both the player entered the same thing. Then it will become a tie. 47 | 48 | The next step is to check whether the first player entered the rock. Then we see the second player input. If the second player entered scissor, the second player is the winner. Otherwise, the first player is the winner 49 | 50 | We repeated this two more times. And checked whether the first player entered paper or scissor. And based on the first player input, we compared the second player’s input. 51 | 52 | Now think for 5 minutes, is there any different way you could have written the if-else logic here. 53 | 54 | If you find a way, add a question here so that everyone can see your code. 55 | 56 | 57 | 58 |   59 | [![Next Page](../assets/next-button.png)](Cows-and-bulls.md) 60 |   61 | 62 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Number-Related/Average-of-numbers.md: -------------------------------------------------------------------------------- 1 | 2 | # Average of numbers 3 | 4 | ## The problem 5 | Take numbers from a user and show the average of the numbers the user entered. 6 | 7 | ## Hints 8 | To solve this problem. 9 | 10 | First, ask the user - How many numbers you want to enter? 11 | 12 | Then, run a for-loop. Each time, take input from the user and put it in a list. 13 | 14 | Once you get all the numbers, you can send the list to the sum function. The sum function will add all the numbers and give you the total. 15 | 16 | Finally, divide the total by the number of elements the user entered. 17 | 18 | That’s it, you will get the answer. 19 | 20 | Want to try it yourself first? Go to the code editor and try it. 21 | 22 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 23 | 24 | 25 | ## Solution 26 | ```python 27 | len = int(input("How many numbers do you want to enter? ")) 28 | 29 | nums = [] 30 | 31 | for i in range(0, len): 32 | element = int(input("Enter element: ")) 33 | nums.append(element) 34 | 35 | total = sum(nums) 36 | avg = total/len 37 | print("Average of elements you entered",round(avg,2)) 38 | ``` 39 | 40 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 41 | 42 | ## Explanation 43 | First, ask the user how many numbers he/she wants to enter. Once we have the number, run a for loop. To collect the numbers. 44 | 45 | While collecting the numbers, we are adding those in the list called nums. 46 | 47 | Then we pass the list to the sum function. The sum function returns us the sum of each number in the list 48 | 49 | Eventually, we divide the total by the number of elements to get the average. 50 | 51 | ## Another Solution 52 | ```python 53 | len=int(input("How many numbers you want to enter: ")) 54 | 55 | total = 0 56 | 57 | for i in range(0,len): 58 | elem=int(input("Enter element: ")) 59 | total += elem 60 | 61 | avg = total/len 62 | print("Average of elements you entered",round(avg,2)) 63 | ``` 64 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 65 | 66 | ## Explanation 67 | In the second approach, other than directly adding to the list, we are adding it to the total variable. And then we are dividing it by the number of input the user entered. 68 | 69 | ## Take Away 70 | > To get the average, calculate the total and divide by the number of elements. 71 | 72 |   73 | [![Next Page](../assets/next-button.png)](Divisible-by-3-and-5.md) 74 |   75 | 76 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Medium/Armstrong-number.md: -------------------------------------------------------------------------------- 1 | # 9.3: Armstrong number. 2 | 3 | ## The problem 4 | Check whether a number is an Armstrong number. 5 | 6 | ## Armstrong number 7 | Armstrong is a special number. 8 | 9 | A number is an Armstrong Number or narcissistic number if it is equal to the sum of its own digits raised to the power of the number of digits. 10 | 11 | Think about the number 371. The total number of digits is 3. Now, for each digit, put the power of 3 and then add them. It will be: 12 | 13 | ```python 14 | 33 + 73 + 13 15 | = 27 + 343 + 1 16 | = 371 17 | ``` 18 | Hence, 371 is an Armstrong number. 19 | 20 | Similarly, 1634 is another Armstrong number, because the total number of digits is 4. Now, power each digit and sum them. You will get the Armstrong number. 21 | 22 | ```python 23 | = 14 + 64 + 34 + 44 24 | = 1 + 1296 + 81 + 256 25 | = 1634 26 | ``` 27 | 28 | 29 | ## The solution 30 | 31 | ```python 32 | def check_armstrong(num): 33 | order = len(str(num)) 34 | 35 | sum = 0 36 | 37 | # use temp to find each digit. Then power it 38 | temp = num 39 | while temp > 0: 40 | digit = temp % 10 41 | sum += digit ** order 42 | temp //= 10 43 | return num == sum 44 | 45 | num = int(input('Enter your number: ')) 46 | 47 | if check_armstrong(num): 48 | print(num,"is an Armstrong number") 49 | else: 50 | print(num,"is not an Armstrong number") 51 | ``` 52 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 53 | 54 | ## Explanation 55 | Three things could be new for you. 56 | 57 | First of all, to know the total number of digits in a number, we converted the number to a string. We used the str(num) to convert it to the string. And then we pass it to the len function. 58 | 59 | This mean, len function will tell you how many characters are on that string. 60 | 61 | Another thing we did is to store the input number in a temp variable. We had to do that because, while getting the digits, we are dividing the number by 10. 62 | 63 | However, at the end, we need to compare the result of the while loop in the sum with the original number. 64 | 65 | That's why, we set the num to the temp variable. And then, we divided the temp variable. In this way, the num variable remained the same. 66 | 67 | Finally, return num == sum means if num is equal to the sum, it will return True. Otherwise, it will return False. 68 | 69 | Hence, return num == sum is a shortcut method of: 70 | 71 | ```python 72 | result = False 73 | if num == sum: 74 | result = True 75 | else: 76 | result = False 77 | return result 78 | ``` 79 | 80 | 81 |   82 | [![Next Page](../assets/next-button.png)](Greatest-common-divisor.md) 83 |   84 | 85 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 86 | 87 | 88 | -------------------------------------------------------------------------------- /Medium/Greatest-common-divisor.md: -------------------------------------------------------------------------------- 1 | # 9.4 GCD: 2 | 3 | ## The Problem 4 | Calculate the greatest common divisor (gcd) of two numbers. 5 | 6 | ## Hint 7 | In mathematics, the greatest common divisor (gcd) of two or more integers is the largest positive integer that divides each of the integers. 8 | 9 | For example, the gcd of 8 and 12 is 4. Because 4 is the largest number that divides both 8 and 12. 10 | 11 | Similarly, 15 is the gcd of 15 and 45. Because 15 divides both 15 and 45. 12 | 13 | ## The Solution 14 | ```python 15 | def compute_gcd(x, y): 16 | smaller = min(x,y) 17 | gcd = 1 18 | for i in range(1, smaller+1): 19 | if((x % i == 0) and (y % i == 0)): 20 | gcd = i 21 | return gcd 22 | 23 | num1 = int(input("Enter first number: ")) 24 | num2 = int(input("Enter second number: ")) 25 | 26 | gcd = compute_gcd(num1, num2) 27 | 28 | print("GCD of", num1,"and", num2,"is", gcd) 29 | ``` 30 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 31 | 32 | ## Explanation 33 | Since we are trying to find the number that divides both numbers, the dividing number will as big as the smallest number. 34 | 35 | Let’s say you want to get the common divisor of 8 and 24. Their common divisor can not be higher than 8,because the numbers greater than 8 can not divide 8. Hence, the gcd will be smaller or equal to the smallest of the two numbers. 36 | 37 | So, inside the compute_gcd method, we are getting the smallest number by using the min function. 38 | 39 | Then, we are running a for loop on the range. The range starts at 1 and goes until the smaller number. 40 | 41 | Inside the loop, we are dividing both numbers by i. If i divides both numbers, we update the gcd, otherwise, we keep going. 42 | 43 | Eventually, we return the gcd. That's it. 44 | 45 | ## Recursive GCD 46 | You can use a recursive method to calculate gcd. Just see the solution and know that you can also do it this way. 47 | 48 | ```python 49 | def gcd(a,b): 50 | if(b==0): 51 | return a 52 | else: 53 | return gcd(b,a%b) 54 | a=int(input("Enter first number:")) 55 | b=int(input("Enter second number:")) 56 | GCD=gcd(a,b) 57 | print("GCD is: ", GCD) 58 | ``` 59 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 60 | 61 | ## built-in GCD 62 | Python has a built-in method to calculate gcd. You can use that one as well. 63 | 64 | ```python 65 | import math 66 | 67 | a = int(input("Enter first number:")) 68 | b = int(input("Enter second number:")) 69 | 70 | gcd = math.gcd(a,b) 71 | print(gcd) 72 | ``` 73 | 74 | 75 | 76 |   77 | [![Next Page](../assets/next-button.png)](Least-Common-Multiple.md) 78 |   79 | 80 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 81 | 82 | 83 | -------------------------------------------------------------------------------- /Loop-Related/Second-smallest.md: -------------------------------------------------------------------------------- 1 | 2 | # Second smallest element 3 | 4 | ## The problem 5 | For a list, find the second smallest element in the list 6 | 7 | ## Hints 8 | If you understand the process of finding the second largest element, this will be a piece of cake for you. 9 | 10 | ## Solution 11 | ```python 12 | def get_second_smallest(nums): 13 | smallest = nums[0] 14 | second_smallest = nums[0] 15 | for i in range(1,len(nums)): 16 | if nums[i] < smallest: 17 | second_smallest = smallest 18 | smallest = nums[i] 19 | elif nums[i] < second_smallest: 20 | second_smallest = nums[i] 21 | return second_smallest 22 | 23 | my_nums = [44,11,83,29,25,76,88] 24 | second_smallest = get_second_smallest(my_nums) 25 | print("Second smallest number is : ", second_smallest) 26 | ``` 27 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 28 | 29 | ## Explanation 30 | We declared two variables. One is called the smallest. Another one is second_smallest. We started both with the value of the first element of the list. 31 | 32 | Then we ran a for loop on the range. The range looks like range(1, len(nums)). We started the list with 1 because we already set the first value as the smallest and the second smallest. Then the upper value of the range is len(num). 33 | 34 | This means the range should run up to the length of the list. 35 | 36 | Inside the loop, we have an if-else block. If the current element num[ i ] smaller than the current smallest element. This means the current smallest element will become the new second smallest element. And the current element will become the smallest element. 37 | 38 | On the other hand, if the current value is smaller than the current second smallest element, we just set the current value as the second smallest element. 39 | 40 | That's it. 41 | 42 | ## Think DIfferent 43 | 44 | You can use the previous clever solution to find the second smallest number as well. 45 | 46 | Please note that, by removing an element from the list, you are changing the original list. If you don’t want to modify the list (you might need the list for other operations), you should use the comparison method. 47 | 48 | ## Clever Solution 49 | 50 | ```python 51 | nums = [2, 15, 14, 71, 52, 209, 551] 52 | nums.remove(min(nums)) 53 | second_smallest = min(nums) 54 | print(second_smallest) 55 | ``` 56 | 57 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 58 | 59 | ## Take Away 60 | If you know multiple solutions to a problem, you can apply the right solution based on the situation. 61 | 62 |   63 | [![Next Page](../assets/next-button.png)](Remove-duplicate-Chars.md) 64 |   65 | 66 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 67 | 68 | -------------------------------------------------------------------------------- /Simple-Game/Guess-game.md: -------------------------------------------------------------------------------- 1 | # 11.1: Guessing Game 2 | 3 | ## The Problem 4 | Build a simple guessing game where it will continuously ask the user to enter a number between 1 and 10. 5 | 6 | If the user's guesses matched, the user will score 10 points, and display the score. 7 | If the users' guess doesn’t match display the generated number. 8 | 9 | Also, if the user enters “q” then stop the game. 10 | 11 | ## Hints 12 | This type of situation, where you don’t know how many times a user will try to make the guess. You have to use an infinity loop. And you will have a special condition to break the loop. 13 | 14 | The following is an infinity loop. 15 | 16 | ```python 17 | while True: 18 | print(True) 19 | ``` 20 | 21 | Also, to create a random integer number, you will need to import the random module. In the random module, you can call the randint to generate a random integer. Besides, you can enter two parameters. 22 | 23 | The first parameter will be the smallest number and the last number is the highest number. Hence, every time you run the following code you will see a different number between 20 to 40. 24 | 25 | ```python 26 | import random 27 | 28 | rand = random.randint(20,40) 29 | print(rand) 30 | ``` 31 | 32 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 33 | 34 | The above code will generate a random number between 20 and 40. 35 | 36 | ## Solution 37 | ```python 38 | import random 39 | print('To stop anytime, enter: q') 40 | score = 0 41 | while True: 42 | num = random.randint(0,10) 43 | guess = input("Guess a number between 0 to 10: ") 44 | if guess == 'q': 45 | print('Game Over.') 46 | break 47 | guess_num = int(guess) 48 | if guess_num is num: 49 | print('CONGRATS, you guessed it correctly') 50 | score += 10 51 | print('Your new score:', score) 52 | else: 53 | print('Your guess did not match') 54 | print('The number was:', num) 55 | ``` 56 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 57 | 58 | ## Explanation 59 | Since we want to create a random number between 0 to 10 we used randint(0,10) 60 | 61 | Also, we have special if. In that if, we compare whether user input is ‘q’. If it is the q, we break the while loop. 62 | 63 | After that, we use the int to convert the user input to an integer number. We didn’t put the int directly on the user input. Because the user might enter q. And if you pass the q to the int method, it will throw an error. 64 | 65 | That’s why we put the int after the check for q. 66 | 67 | The bottom part of the code should be easier for you. 68 | 69 | 70 |   71 | [![Next Page](../assets/next-button.png)](Rock-paper-scissor.md) 72 |   73 | 74 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Loop-Related/Second-Largest.md: -------------------------------------------------------------------------------- 1 | 2 | # Second Largest 3 | 4 | ## The problem 5 | For a list, find the second largest number in the list. 6 | 7 | ## Hints 8 | Finding the largest number was super easy. To find the second largest number, you have to keep track of two variables. Then compare. 9 | 10 | ## Solution 11 | ```python 12 | def get_second_largest(nums): 13 | largest = nums[0] 14 | second_largest = nums[0] 15 | for i in range(1,len(nums)): 16 | if nums[i] > largest: 17 | second_largest = largest 18 | largest = nums[i] 19 | elif nums[i] > second_largest: 20 | second_largest = nums[i] 21 | return second_largest 22 | 23 | my_nums = [44,11,83,29,25,76,88] 24 | second_largest = get_second_largest(my_nums) 25 | print("Second highest number is : ",second_largest) 26 | ``` 27 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 28 | 29 | ## Explanation 30 | We declared two variables. One is called the largest. Another one is second_largest. We started both with the value of the first element of the list. 31 | 32 | Then we ran a for loop on range. The range looks like range(1, len(nums)). We started the list with 1 because we already set the first value as the largest and second_largest. Then the upper value of the range is len(nums). 33 | 34 | This means the range should run up to the length of the list. 35 | 36 | Inside the loop, we have an if-else block. If the current element num[ i ] greater than the current largest element. This means the current largest element will become the new second largest element. And the current element will become the largest element. 37 | 38 | On the other hand, if the current value is greater than the current second largest element, we just set the current value as the second largest element. 39 | 40 | That's it. 41 | 42 | ## Think Different 43 | Can you think about any way to solve this problem? 44 | 45 | One clever solution could be, 46 | 47 | Step 1: Use the max function to find the largest number. 48 | Step 2: Remove the max number from the list 49 | Step 3: From the remaining numbers, find the max number. As you already remove the previous max number, this max number will be the second-largest number. 50 | 51 | How cool is this solution? 52 | 53 | ## Clever Solution 54 | 55 | ```python 56 | nums = [5, 12, 54, 87, 55, 69, 23, 17] 57 | nums.remove(max(nums)) 58 | second_largest = max(nums) 59 | print(second_largest) 60 | ``` 61 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 62 | 63 | 64 | ## Take Away 65 | Clever ways to solve problems will make you happier. 66 | 67 |   68 | [![Next Page](../assets/next-button.png)](Second-smallest.md) 69 |   70 | 71 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 72 | 73 | -------------------------------------------------------------------------------- /Harder/Permutations.md: -------------------------------------------------------------------------------- 1 | # 12.4: Permutation [Premium] 2 | 3 | ## The Problem 4 | Find all permutation of a given list. 5 | 6 | ## Hints 7 | In mathematics, the permutation is the way to find all possible order of elements 8 | 9 | For example, you have 1,2,3. Now all possible order is 10 | 1,2,3 11 | 1,3,2 12 | 2,1,3 13 | 2,3,1 14 | 3,1,2 15 | 3,2,1 16 | 17 | So, the permutation is a way to find all the possible order or sequence of elements. 18 | 19 | ```python 20 | lst = [2,3,4,6,11,16,7,8] 21 | before = lst[:4] 22 | after = lst[4+1:] 23 | excluding = before + after 24 | print(excluding) 25 | print('-----------------------') 26 | print(lst[:4] + lst[4+1]) 27 | ``` 28 | 29 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 30 | 31 | 32 | ## Solution 33 | 34 | ```python 35 | def get_permutation(lst): 36 | # For an empty list, there is no permutation 37 | if len(lst) == 0: 38 | return [] 39 | # list with one element will have only one 40 | # permutation 41 | if len(lst) == 1: 42 | return [lst] 43 | # Create an empty list to store permutation 44 | perms = [] 45 | for i in range(len(lst)): 46 | # Extract current elemnt from the list. 47 | current = lst[i] 48 | # Recursively call permutation for the 49 | # remaining list 50 | rem_list = lst[:i] + lst[i+1:] 51 | rem_perm = get_permutation(rem_list) 52 | # Generate permutations by adding first element 53 | for p in rem_perm: 54 | perms.append([current] + p) 55 | return perms 56 | # now test the function 57 | data = [1,2,3] 58 | for perm in get_permutation(data): 59 | print (perm) 60 | ``` 61 | 62 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 63 | 64 | 65 | ## Explanation 66 | In the code, we are getting the current element by lst[i]. 67 | Then we create the remaining list by lst[:i] + lst[i+1:] 68 | 69 | We stored it in the remaining list named as rem_list. After that, we recursively call the get_permutation. This will give permutation for the rest elements. We stored that in the rem_perm list. 70 | 71 | Finally, we have to join the current element with the rest of the list. To do so, we have added [current] + p 72 | 73 | The loop variable p is a list. Whereas the current is an element. You can not add 74 | 75 | `3 + [2,1]` 76 | 77 | That’s why we put current in a list as well so that it becomes a list as well. In the following example, 78 | 79 | `[3] + [2, 1]` 80 | 81 | This will create [3, 2, 1]. That’s why we did [current] + p to make the final permutation. 82 | 83 | This code is harder. It will take a few practices to become comfortable with it. 84 | 85 | 86 |   87 | [![Next Page](../assets/next-button.png)](Generate-Sentences.md) 88 |   89 | 90 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Reverse/Reverse-String.md: -------------------------------------------------------------------------------- 1 | ## Reverse a string 2 | 3 | ## The problem 4 | Reverse a string. 5 | 6 | If the input is: Hello World. 7 | 8 | The output should be: .dlroW olleH 9 | 10 | ## Hints 11 | A string is almost like a list. A list of characters. You can access any element by index. You can run a for loop on it. You will get each character as an element. Besides, you can also add two string or join two characters 12 | 13 | 14 | ```python 15 | my_string = "You are my Hero" 16 | 17 | print('Access by index') 18 | print('-------------') 19 | print(my_string[0]) 20 | print(my_string[1]) 21 | print(my_string[4]) 22 | 23 | print('Run a for loop') 24 | print('-------------') 25 | for char in my_string: 26 | print(char) 27 | 28 | print('now join strings') 29 | print('-------------') 30 | print('hello' + 'world') 31 | print('h'+'w') 32 | ``` 33 | 34 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 35 | 36 | However, a string is not a list. 37 | 38 | ## Solution 39 | 40 | ```python 41 | def reverse_string(str): 42 | reverse = "" 43 | for char in str: 44 | reverse = char + reverse 45 | return reverse 46 | 47 | str = input("Enter your string: ") 48 | result = reverse_string(str) 49 | print(result) 50 | ``` 51 | 52 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 53 | 54 | ## Explanation 55 | A simple trick will make it work. 56 | 57 | Let’s say you wrote “Hello world.” if you break down it or run a for loop and get each character at a time. You will get first H and then e and then l … and so on. 58 | 59 | Now, while adding each character add it before the previous one. 60 | 61 | Hence, first you will have H. then while adding e, you will add it before the H. It will become eH. 62 | 63 | Then “l” will come and add it before. This will give you- 64 | 65 | leH 66 | 67 | Keep doing it. And you will get 68 | 69 | .dlroW olleH 70 | 71 | Now if you look at the code you will understand why we did- 72 | 73 | reverse = char + reverse 74 | 75 | ## Quiz 76 | What does it mean by the word immutable? 77 | 1. Immortal 78 | 2. Not changeable 79 | 3. Changeable 80 | 81 | 82 |
83 | Show Answer 84 |

The answer is: 2

85 |
86 | 87 | ## Quiz 88 | Will you be able to append, remove or set a character to string by the index? 89 | 90 | 1. Yes 91 | 2. No 92 | 3. Sometimes 93 | 94 |
95 | Show Answer 96 |

The answer is: 2

97 |
98 | 99 | ## take Away 100 | A string is an immutable ordered sequence. 101 | 102 | 103 |   104 | [![Next Page](../assets/next-button.png)](Reverse-String-(stack).md) 105 |   106 | 107 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 108 | -------------------------------------------------------------------------------- /Loop-Related/Sum-of-squares.md: -------------------------------------------------------------------------------- 1 | 2 | # Sum of squares 3 | 4 | ## The problem 5 | Take a number as input. Then get the sum of the numbers. If the number is n. Then get 6 | 7 | `0^2+1^2+2^2+3^2+4^2+.............+n^2` 8 | 9 | ## Hints 10 | Once again, run a for loop with a range. Inside the loop, use the power of 2. Then add that power to a sum variable. That’s it. 11 | 12 | ## Solution 13 | ```python 14 | def square_sum(num) : 15 | sum = 0 16 | for i in range(num+1) : 17 | square = (i ** 2) 18 | sum = sum + square 19 | 20 | return sum 21 | 22 | num = int(input('Enter a number: ')) 23 | sum = square_sum(num) 24 | 25 | print('sum of square numbers is ', sum) 26 | ``` 27 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 28 | 29 | ## Explanation 30 | This one is super easy. You declared a variable sum with an initial value 0. 31 | 32 | Then you run a for loop. This loop will run for range (num +1). 33 | 34 | The reason we are running it until num + 1. Because, we know that the range will stop just before the number inside it. 35 | 36 | For example, if we want to run the loop including 10. We should write 11 inside the range function. 37 | 38 | If needed, go to the code editor and run the following code. 39 | 40 | ```python 41 | for i in range(10): 42 | print(i) 43 | ``` 44 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 45 | And then run 46 | ```python 47 | for i in range(11): 48 | print(i) 49 | ``` 50 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 51 | 52 | 53 | ## Shortcut 54 | Sometimes there could be a better and easier way to solve a problem. For example, there is a simple math formula to calculate the sum of the square. 55 | 56 | This is the reason, you should search online and learn from different sources. This will make you stronger and better. 57 | 58 | If you want to calculate the sum of the square of n numbers, the formula is- 59 | 60 | `n*(n+1)(2*n+1)/6` 61 | 62 | Now you can use this formula. 63 | ```python 64 | def sum_of_square2(n): 65 | sum = n*(n+1)*(2*n+1)/6 66 | return sum 67 | 68 | num = int(input('Enter a number: ')) 69 | sum = sum_of_square2(num) 70 | print('Your sum of Square is: ', sum) 71 | ``` 72 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 73 | 74 | ## Quiz 75 | 76 | Why you should learn problem-solving from multiple sources? 77 | 78 | 1. To utilize the internet bill correctly 79 | 2. To learn alternative solutions 80 | 3. To pretend that you are busy 81 | 82 |
83 | Show Answer 84 |

The answer is : 2

85 |
86 | 87 | ## Take Away 88 | Knowing shortcut ways will make you smarter and faster. 89 | 90 |   91 | [![Next Page](../assets/next-button.png)](Second-Largest.md) 92 |   93 | 94 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 95 | 96 | -------------------------------------------------------------------------------- /Reverse/Reverse-String-(recursive).md: -------------------------------------------------------------------------------- 1 | ## Reverse a string (recursion) 2 | 3 | ## The problem 4 | Reverse a string using a recursive function. 5 | 6 | ## Hints 7 | You already know the recursive function. So, it would be easier. 8 | 9 | Also, let’s talk a little bit about the range selector (also known as slicing). 10 | 11 | For example, you want to select a range of elements. You can define a range by using colon inside the third bracket. 12 | 13 | In the code below, you are selecting from position 3 to just before 7 position by writing str[3:7] 14 | 15 | Similarly, you will get 3:11 to get elements from the 3rd index to 10th index. 16 | 17 | While selecting by index, you can skip the first number. In that case, it will start from the beginning. Similarly, if you skip the last number, then it will go to the end of the string. 18 | 19 | If you skip both, you will get the full string. 20 | 21 | ```python 22 | str ='Hello young Programmer' 23 | print(str[3:7]) 24 | print(str[3:11]) 25 | 26 | print ('skip range index') 27 | 28 | print(str[:11]) 29 | print(str[3:]) 30 | 31 | print(str[:]) 32 | ``` 33 | 34 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 35 | 36 | 37 | ## Solution 38 | ```python 39 | def reverse_recur(str): 40 | if len(str) == 0: 41 | return str 42 | else: 43 | return reverse_recur(str[1:]) + str[0] 44 | 45 | str = input("Enter your string: ") 46 | rev_str = reverse_recur(str) 47 | print ('Reverse of your string: ', rev_str) 48 | ``` 49 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 50 | 51 | ## Explanation 52 | In the recursive function, look at the else condition. We are selecting str[1:] 53 | 54 | This means we are selecting the string from the first index and send it recursively to the reverse_recur function. 55 | 56 | And we are adding str[0] at the end. Here, str[0] is the first element. And you are adding at the end. 57 | 58 | This means you are adding the first element at the end. And if you keep doing it every time and add the first element at the last. You will create the reversed string. 59 | 60 | 61 | ## Smart Solution 62 | There is a smarter and quicker solution to reverse a string. 63 | 64 | I asked my grandma how the code below works. She didn't’ answered. Instead, she sent a letter to Google through her private pigeon. 65 | 66 | ```python 67 | txt = "Welcome to the Jungle" 68 | print(txt[::-1]) 69 | ``` 70 | 71 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 72 | 73 | ## Quiz 74 | How would you slide a string and get the first five characters? 75 | 76 | 1. Str[1:6] 77 | 2. Str[1:5] 78 | 3. Str[:5] 79 | 80 | 81 |
82 | Show Answer 83 |

The answer is: 3

84 |
85 | 86 | ## take Away 87 | You can slice a string to get a part of the string. 88 | 89 | 90 | 91 |   92 | [![Next Page](../assets/next-button.png)](Reverse-Number.md) 93 |   94 | 95 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 96 | 97 | -------------------------------------------------------------------------------- /Reverse/Reverse-word.md: -------------------------------------------------------------------------------- 1 | # Reverse word order 2 | 3 | ## The problem 4 | Reverse the word in a sentence. 5 | 6 | For example, if the input string is “Hello young Programmer”, the output will be “Programmer young Hello” 7 | 8 | ## Hints 9 | There are two methods that you can call on a string. One is called split(). If you don’t pass any parameter to this function, it will split the string by whitespace. 10 | 11 | The opposite of split is join. You can write what you want to join string by. And then pass the array to it. 12 | 13 | For example, if you want to join a list elements by whitespace, you can write the whitespace and then join like bellow 14 | 15 | ```python 16 | str ='Hello young Programmer' 17 | words = str.split() 18 | print(words) 19 | 20 | together = ' '.join(words) 21 | print(together) 22 | ``` 23 | 24 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 25 | 26 | ## Solution 27 | 28 | ```python 29 | def reverse_words(sentence): 30 | words = sentence.split() 31 | words.reverse() 32 | return " ".join(words) 33 | 34 | usr_input = input("Enter a sentence: ") 35 | reverse = reverse_words(usr_input) 36 | print('Reversed words are: ', reverse) 37 | ``` 38 | 39 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 40 | 41 | ## Explanation 42 | Once you understand the split and the join method, this code should become super easy for you. 43 | 44 | If you have any confusion, let us know. 45 | 46 | ## Quiz 47 | 48 | What is the purpose of the split method on a string? 49 | 50 | 1. Breakdown string into elements of a list 51 | 2. Split the string if it has no whitespace 52 | 3. Adds some splitting characteristics 53 | 54 |
55 | Show Answer 56 |

The answer is: 1

57 |
58 | 59 | 60 | ## Quiz 61 | How would you split a string by the character a? 62 | 63 | 1. str.split() 64 | 2. str.split(“ ”) 65 | 3. str.split(‘a’) 66 | 67 |
68 | Show Answer 69 |

The answer is: 3

70 |
71 | 72 | ## Quiz 73 | 74 | How would you join each string in the words list by the hyphen(-)? 75 | 1. words.join(“-”) 76 | 2. words.split(“-”) 77 | 3. “-”.join(words) 78 | 79 |
80 | Show Answer 81 |

The answer is: 3

82 |
83 | 84 | ## Reverse domain 85 | 86 | Let’s say, you have the website name www.programming-hero.com 87 | 88 | Now you want to reverse the domain name: com.programming-hero.www 89 | 90 | ```python 91 | site='www.programming-hero.com' 92 | parts = site.split('.') 93 | parts.reverse() 94 | rev = '.'.join(parts) 95 | print(rev) 96 | ``` 97 | 98 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 99 | Alternatively, you can write the whole code in one line. 100 | 101 | ```python 102 | site='www.programming-hero.com' 103 | rev = '.'.join(reversed(site.split('.'))) 104 | print(rev) 105 | ``` 106 | 107 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 108 | 109 | ## take Away 110 | The split method breaks string into elements of a list. 111 | 112 | 113 | 114 |   115 | [![Next Page](../assets/next-button.png)](../Medium/Check-palindrome.md) 116 |   117 | 118 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 119 | 120 | -------------------------------------------------------------------------------- /Conversions/Decimal-to-binary.md: -------------------------------------------------------------------------------- 1 | # 4.4: Decimal to Binary 2 | 3 | ## The Problem 4 | Convert a decimal number to binary number. 5 | 6 | ## Decimal vs Binary 7 | 8 | The numbers that we use every day is called decimal number. A decimal number could have any of the 10 digits (0, 1, 2, 3, 4,5 6, 7, 8, 9). 9 | 10 | Hence, 232, 789, 176, 511, etc. are decimal numbers. 11 | 12 | Sometimes, decimal numbers are called 10-based numbers. Because you could have 10 different digits in your number. 13 | 14 | On the other hand, binary numbers use two digits (0 and 1). The word binary means two. And binary numbers use two digits. Just one and 0. Buy using these two digits binary number system can represent any numbers. 15 | 16 | Some example of binary numbers re 10001, 11101, 100001, etc. 17 | ## Hints 18 | To convert a decimal number to a binary number, you have to keep dividing the number by 2. 19 | 20 | While dividing, you will keep the remainder. These remainders will be used to build a binary number. 21 | 22 | Then, reverse the order of the reminder, to get the binary number. 23 | 24 | ## Solution 25 | I looked into the below code 7 times to understand. Still trying to figure it out...So, spend some time here to look at the code: 26 | 27 | ```python 28 | def dec_to_binary(n): 29 | bits = [] 30 | 31 | while n > 0: 32 | bits.append(n%2) 33 | n = n // 2 34 | bits.reverse() 35 | 36 | binary = '' 37 | for bit in bits: 38 | binary += str(bit) 39 | return binary 40 | 41 | 42 | num = int(input("Your decimal number: ")) 43 | binary = dec_to_binary(num) 44 | print("Your binary is:", binary) 45 | ``` 46 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 47 | 48 | ## Explanation 49 | You have seen the remainder and dividing the number with // before. That is the core part of this decimal to a binary algorithm. 50 | 51 | So, this part should be easier for you: 52 | 53 | ```python 54 | bits = [ ] 55 | while n > 0: 56 | bits.append(n%2) 57 | n = n // 2 58 | ``` 59 | 60 | The reminder is n%2. And, the result(floor division) of dividing by two is n//2. 61 | 62 | Rest is simple. 63 | 64 | You reverse the bits array by calling bits.reverse(). 65 | 66 | After reverse, you have the digits of the binary. To display the binary bits as one number, you can declare a string called binary. 67 | 68 | Then, run a for loop over your bits array and join the binary bit as a string. 69 | 70 | We used the str method to convert number to a string. If we didn’t convert the digit to a string, it will be added as a number. 71 | 72 | That’s why, '1'+'1'+'1'+'0'+'0'+'1' will become '111001'. If you didn’t convert each digit to string it will become 4 (1+1+1+0+0+1) 73 | 74 | Try to read this explanation and code multiple times. And, if needed, come back again and again. We don’t expect you to understand everything in your first try. 75 | 76 | If you keep trying and revisiting , again and again, these will start making sense. 77 | 78 | 79 | ## Quiz 80 | 81 | What is a binary number? 82 | 1. Numbers written on a trash bin 83 | 2. Numbers that use 0,1 only 84 | 3. Numbers with any 2 digits 85 | 86 |
87 | Show Answer 88 |

The answer is : 2

89 |
90 | 91 | ## Take Away 92 | Binary numbers use 0 and 1 only. 93 | 94 | 95 | 96 |   97 | [![Next Page](../assets/next-button.png)](Decimal-to-binary-recursive.md) 98 |   99 | 100 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 101 | -------------------------------------------------------------------------------- /Simple-Game/Cows-and-bulls.md: -------------------------------------------------------------------------------- 1 | # 11.3: Cows and Bulls (2 digits) 2 | 3 | ## The Problem 4 | Create a Cows and Bulls game. 5 | 6 | ## Hints 7 | The Cows and Bulls is a number guessing game. In this game, the user guesses a number. Usually, the number will be a 4 digit number. However, here we will try with 2 digits number first. 8 | 9 | So, if the user guesses the number correctly, he/she will win. If they didn’t guess the exact number, then you will calculate the bulls and cows. 10 | 11 | Let’s say the number is: 8529 12 | 13 | And your guess is: 3428 14 | 15 | If you guess any position digit currently, you will get a bull. For your guess, you didn’t match the first, second or the last digit. However, you guess the 3rd digit correctly. Hence your bulls will be 1. 16 | 17 | On the other hand, if one or more digits of the secret number exist in your guess, you will get a cow. 18 | 19 | In summary, if you matched the digit with the position, you will get a bull. On the other hand, if you have the digit in a different place, it will cows. 20 | 21 | In the above guess, you have 8 in the 4th position. The 8 exists in the secret number in a different location, that's why you will get 1 cow. 22 | 23 | Hence your score will be: 24 | Bulls: 1 25 | Cows: 1 26 | 27 | 28 | Another example- 29 | Secret number: 4271 30 | player's try: 1234 31 | Answer: 1 bull and 2 cows. (The bull is "2", the cows are "4" and "1".) 32 | 33 | 34 | ## Solution 35 | ```python 36 | import random 37 | 38 | secret_number = str(random.randint(10, 99)) 39 | print("Guess the number. It contains 2 digits.") 40 | 41 | remaining_try = 7 42 | 43 | while remaining_try > 0: 44 | player_guess = input("Enter your guess: ") 45 | 46 | if player_guess == secret_number: 47 | print("Yay, you guessed it!") 48 | print("YOU WIN.") 49 | break 50 | else: 51 | bulls = 0 52 | cows = 0 53 | 54 | if player_guess[0] == secret_number[0]: 55 | bulls += 1 56 | if player_guess[1] == secret_number[1]: 57 | bulls += 1 58 | if player_guess[0] == secret_number[1]: 59 | cows += 1 60 | if player_guess[1] == secret_number[0]: 61 | cows += 1 62 | 63 | print("Bulls: ",bulls) 64 | print("Cows: ",cows) 65 | 66 | remaining_try -= 1 67 | 68 | if remaining_try < 1: 69 | print("You lost the game.") 70 | break 71 | ``` 72 | 73 | ## Explanation 74 | In the beginning, we created a secret number. Then asked the user to guess the number. Also, we set the variable remaining_try to 7. 75 | 76 | This means we allow the player to guess the number 7 times. If he/she guesses the correct number within 7 tries, he/she will win. Otherwise, lose. 77 | 78 | We ran the while loop for remaining_try. If the user’s guess is correct, we say you win. 79 | 80 | Otherwise, we start counting bulls and cows. For that purpose, we start at 0 bulls and 0 cows. 81 | 82 | For bulls, we check whether the first digit in the secret_number matches with the player’s guessed number. If it matches, we increase the bulls by 1. 83 | 84 | Also, check the 2nd digit of the secret number with the player’s guess. If that matches, we increase the bulls by 1. 85 | 86 | For cows, we check whether the first digit of the secret number matches with the second digit of the player guess. Increase cows by 1. Similarly, if the 2nd digit matches with the first digit of the guess number, increase cows by 1. 87 | 88 | In summary, bulls are guessed digit matches for the same position in the secret number. And cows are the digits that match in any other position other than the current position. 89 | 90 | Then we display the bulls and cows. 91 | 92 | Finally, if there is no remaining try (remaining try < 1), print that user lost. 93 | 94 | 95 |   96 | [![Next Page](../assets/next-button.png)](Cows-and-bulls(4digits).md) 97 |   98 | 99 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /Simple-Game/Word-completion.md: -------------------------------------------------------------------------------- 1 | # 11.5: Word Completion [premium] 2 | 3 | ## The Problem 4 | Build a simple word completion game. 5 | 6 | You will be given a partial word with hyphens. It’s a clue to an actual word. For example, 7 | 8 | -a-e 9 | 10 | Now you have to provide a word that matches with the exposed letters. If you provide any of these words- “game” or “fame” or “lame”, your answer will be correct. 11 | 12 | Because all these words have the letter “a” in the second position and the letter “e” in the fourth position. 13 | 14 | So, the idea is, your provided word has to have the same letter as the clue. 15 | 16 | S-2: hint 17 | You have to write a function that takes the clue word with hyphens and the provided answer. 18 | 19 | Then check whether the provided answer has the same letters in the same position as the clue word. 20 | 21 | And you can ignore the position with hyphens. Because the user can put any letter there. 22 | 23 | 24 | 25 | ## Solution 26 | 27 | ```python 28 | import random 29 | 30 | def get_a_clue(): 31 | clues = ['-a-e', 'y-ll-w', 's-mm-r', 'wi-t-r','s-n-y', 'l-v-','-i-e'] 32 | position = random.randint(0, len(clues)-1) 33 | clue = clues[position] 34 | return clue 35 | 36 | def check_word_match(clue, guess): 37 | if len(clue) != len(guess): 38 | return False 39 | for i in range (len(clue)): 40 | if clue[i] != '-' and clue[i ]!= guess[i]: 41 | return False 42 | return True 43 | 44 | # start the game 45 | word_clue = get_a_clue() 46 | print('Your word clue:', word_clue) 47 | answer = input('What would be the word: ') 48 | 49 | is_matched = check_word_match(word_clue, answer) 50 | 51 | if is_matched is True: 52 | print('WOW!!! You win') 53 | else: 54 | print('Opps! you missed it.') 55 | ``` 56 | 57 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 58 | 59 | 60 | ## Explanation 61 | 62 | We have two functions here. 63 | 64 | The first one is providing a clue. We have a list of clues. Under that, we generate a random position. 65 | 66 | Inside the randint function, you may notice that the second parameter is len(clues) -1. 67 | 68 | Let’s say you have a list with 5 elements. The length of the list is 5. However, as the index starts from 0. The index of the last element will be 4. It will start at 0. And the positions are 0, 1, 2, 3, 4. 69 | 70 | That’s why, if the len of the list is 5, the index of the last element will be 4. That is 5-1. Hence you see len(clues) - 1. 71 | 72 | 73 | We have another function, check_word_match. In that function, we are taking two parameters, clue and the guess. 74 | 75 | If the length of the clue and the length of the guess is not equal. We are sure that your guess is not correct. Because the clue and the guess should have the same number of elements. 76 | 77 | After that, we check the letters without hyphens for each position. That’s why we need a for loop. 78 | 79 | In the place of hyphens, there could be any letter. Hence, you have to match the letter with non-hyphens. That’s why the if has two conditions. 80 | 81 | The first condition is to check whether it is not a hyphen. The second condition is to check whether the letter in the clue matches with the letter of the guess. If the letter doesn’t match, this means your guess doesn’t match with the exposed characters in the clue. 82 | 83 | That’s why we return False immediate. 84 | 85 | Otherwise, the for loop will finish and you will return True at the end of the function. 86 | 87 | Code underneath the function should be easier for you. 88 | 89 | ## Quiz 90 | 91 | What should be the highest index of a list using len? 92 | 93 | ```python 94 | nums = [12, 56, 34, 71, 23, 17] 95 | 96 | len(nums) 97 | len(nums) +1 98 | len(nums) - 1 99 | 100 | The answer is: 3 101 | ``` 102 | 103 | 104 |   105 | [![Next Page](../assets/next-button.png)](Word-hangman.md) 106 |   107 | 108 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /User-Submitted/Birthday-remaining.md: -------------------------------------------------------------------------------- 1 | # 13.2 Birthday remaining 2 | 3 | ## The problem 4 | Calculate how many days are remaining for the next birthday. 5 | 6 | This code is submitted by Programmer Karim. 7 | 8 | ## Hints 9 | Let’s say, you want to display 5 random numbers. You can write the code like below: 10 | 11 | ```python 12 | import random 13 | print(random.randint(0,10)) 14 | print(random.randint(10,20)) 15 | print(random.randint(20,30)) 16 | print(random.randint(30,40)) 17 | print(random.randint(40,50)) 18 | ``` 19 | 20 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 21 | 22 | 23 | The problem is that, in every line, you have to type random.randint. As a programmer, you will be lazy. Hence, while import you can import the randint directly in your code. For that, you have to say, from random import randint. Like below: 24 | 25 | ```python 26 | from random import randint 27 | print(randint(0,10)) 28 | print(randint(10,20)) 29 | print(randint(20,30)) 30 | print(randint(30,40)) 31 | print(randint(40,50)) 32 | ``` 33 | 34 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 35 | 36 | ## Solution 37 | The code below is submitted by Programmer Karim... 38 | 39 | ```python 40 | from datetime import datetime 41 | import time 42 | 43 | def get_user_birthday(): 44 | date_str = input("Enter your birth date in DD/MM/YYYY: ") 45 | try: 46 | birthday = datetime.strptime(date_str, "%d/%m/%Y") 47 | except TypeError: 48 | birthday = datetime.datetime(*(time.strptime(date_str, "%d/%m/%Y")[0:6])) 49 | return birthday 50 | 51 | def days_remaining(birth_date): 52 | now = datetime.now() 53 | current_year = datetime(now.year, birth_date.month, birth_date.day) 54 | days = (current_year - now).days 55 | if days < 0: 56 | next_year = datetime(now.year+1, birth_date.month, birth_date.day) 57 | days = (next_year - now).days 58 | return days 59 | 60 | birthday = get_user_birthday() 61 | next_birthday = days_remaining(birthday) 62 | print("Your birthday is coming in: ", next_birthday, " days") 63 | ``` 64 | 65 | ## Explanation 66 | We imported the datetime method from the datetime module. It has a lot of good functionalities. 67 | 68 | First, in the get_user_birthday function, we take user birthday in a format of DD/MM/YYYY. This means we are telling the user to insert the date of his/her birthday first then a forward slash followed by the month and year in four-digits. 69 | 70 | Then, we convert the birthday in a python date by using datetime.strptime function. To that function, we have to pass the date in the string and the format of the date. The format of the date is %d/%m/%Y. 71 | 72 | 73 | We also imported time. Sometimes, strptime gives some Type exception. For that reason, we put a try-except there. 74 | 75 | 76 | If you convert a date string to a python date, then you can easily access the day, month and year and do some extra calculation over the date. 77 | 78 | We have another method called days_remaining. In that method, we first get the date of now. This is actually the date of the time right now. It is the time when you run the code. 79 | 80 | Then, you calculate the users' birthday of the current year. It’s simple...you just take the year from now (current year) with month and date from users' birthday. 81 | 82 | The next thing is very simple...you just get the difference between the user’s birthday in the current year and now, and ultimately get the days. 83 | 84 | If the birthday for this year is over, the difference will be negative, because the birthday for this year has already passed. Then, we get the birthday for the next year by adding one(1) with the current year and with the month and the day from the birth date. 85 | 86 | At last, we calculate the difference. 87 | 88 | That’s a pretty simple way to get the days remaining for your next birthday. 89 | 90 | Isn’t it cool? 91 | 92 | 93 |   94 | [![Next Page](../assets/next-button.png)](Calculate-age.md) 95 |   96 | 97 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /User-Submitted/Simple-Clock.md: -------------------------------------------------------------------------------- 1 | # 13.1 Simple Digital Clock 2 | 3 | ## The Task 4 | Create a simple clock. 5 | 6 | This problem and code example is created by Steine Chaos / Malte Lupin. 7 | 8 | If you want to see your name as a contributor to the app, find a code example that we don’t have here, and email it at programming.hero1@gmail.com 9 | 10 | ## Solution Strategy 11 | Two things would be new for you here. 12 | 13 | Let’s talk about the global keyword first. 14 | 15 | Inside a function, you can use two types of variables: any variable declared inside the function or the parameter. 16 | 17 | If you want to set or update the value of any variable declared outside of the function, you can either pass it as a parameter. In that case, it will not update the value of the variable outside of the function. In the code below, you will have the value of x remained as 5. 18 | 19 | ```python 20 | x = 5 21 | 22 | def double_global_x(x): 23 | x = 2*x 24 | return x 25 | 26 | print('use global keyword') 27 | result = double_global_x(x) 28 | print(result) 29 | print('Updated value of x:', x) 30 | ``` 31 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 32 | 33 | If you want to change the value of the variable inside a function, you have to use the global keyword. Just add a new line and put global and then the name of the variable. In that case, python will know that it will read and set value from the variable outside of the function. For example, in the code below the value of the x will become 10. 34 | 35 | ```python 36 | x = 5 37 | 38 | def double_global_x(): 39 | global x 40 | x = 2*x 41 | return x 42 | 43 | print('use global keyword') 44 | double_global_x() 45 | print(result) 46 | print('Updated value of x:', x) 47 | ``` 48 | 49 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 50 | 51 | ## Time module 52 | There is a built-in module in python named time. It has a lot of time-related functionalities. One function on the time module is called sleep. The sleep takes a parameter named sec. 53 | 54 | You can pause your function wait for a certain number of sec and then execute it. 55 | 56 | For example, we can run an infinite loop and after every second you can print Tick 57 | 58 | ```python 59 | import time 60 | 61 | while True: 62 | time.sleep(1) 63 | print('Tick') 64 | ``` 65 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 66 | 67 | ## The solution 68 | This code is submitted by: Steine Chaos / Malte Lupin 69 | 70 | ```python 71 | import time 72 | 73 | print('Simple clock made by Steine Chaos:\n') 74 | 75 | hour = int(input("Type in the current hour:")) 76 | minute = int(input("Type in the current minute:")) 77 | second = int(input("Type in the current second:")) 78 | 79 | def display(): 80 | print(hour, ':', minute, ':', second) 81 | 82 | def add(): 83 | global hour 84 | global minute 85 | global second 86 | 87 | second=second+1 88 | if second==60: 89 | minute=minute+1 90 | second=0 91 | if minute==60: 92 | hour=hour+1 93 | minute=0 94 | if hour==24: 95 | hour=0 96 | 97 | print('\n') 98 | 99 | while True: 100 | time.sleep(1) 101 | add() 102 | display() 103 | ``` 104 | **[Try it on Programming Hero](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)** 105 | 106 | ## How it works 107 | The code is simple...We imported the time module, then, asked the user for three inputs: for the current hour, minute, and second. 108 | 109 | Then, we have two methods: One to display hour, minute and second. Another, to add a second. We will look at the add method a little later. First, look at the bottom part. 110 | 111 | We have an infinite loop, because we don’t want the clock to stop. We want the clock to run forever until we stop it. 112 | 113 | And, inside the loop, we call the display and the add method and then, we call the time.sleep and pass 1. 114 | 115 | By default, the while loop runs immediately. However, inside the while loop, the first thing we have is the time.sleep(1). This is telling the loop to wait for 1 second. Then we are calling the add function. When add is done, we are calling the display function to display the hour, minute, and seconds. 116 | 117 | The add function is a bit interesting... 118 | 119 | We are accessing three global variables: hour, minute, and second, and we are increasing the second by 1. 120 | 121 | This is the main task. However, if the second becomes 60, we increase a minute and set second to 0, because 60 seconds are equal to a minute. 122 | 123 | Similarly, if the minute becomes 60, we add 1 hour and set minutes to 0, because 60 minutes is 1 hour. 124 | 125 | Finally, if the hour is 24, we reset the hour to 0. Because, in a digital clock, there are no 24 or 25 hours. After 23 hours, it becomes 0 hours again (the midnight!) 126 | 127 | 128 |   129 | [![Next Page](../assets/next-button.png)](Birthday-remaining.md) 130 |   131 | 132 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 100 Plus Python Coding Problems With Solutions 2 | --- 3 | Welcome to the Python problem-solving world. 4 | 5 | Here, we will take a real-world coding related problem. We will think about the process of solving the problem and then we will solve it. After the solution, we will explain the answer. 6 | 7 | ## Table of Contents: 8 | 9 | ## 1 -  Easy ones 10 | * **[1.1](Easy-ones/User-input-to-Number.md "Convert input")** - **[Convert input](/Easy-ones/User-input-to-Number.md)** 11 | * **[1.2](Easy-ones/Math-Power.md "Math power")** - **[Math power](/Easy-ones/Math-Power.md)** 12 | * **[1.3](Easy-ones/Random-Number.md "Random Number")** - **[Random Number](/Easy-ones/Random-Number.md)** 13 | * **[1.4](Easy-ones/Floor-Division.md "Floor Division")** - **[Floor Division](/Easy-ones/Floor-Division.md)** 14 | * **[1.5](Easy-ones/Temporary-variable.md "Temporary variable")** - **[Temporary variable](/Easy-ones/Temporary-variable.md)** 15 | 16 | 17 | ## 2 -  Number Related 18 | * **[2.1](Number-Relate "Max of two")** - **[Max of two](/Number-Relate)** 19 | * **[2.2](Number-Related/Math-Power.md "Max of three")** - **[Max of three](/Number-Related/User-input-to-Number.md)** 20 | * **[2.3](Number-Related/Math-Power.md "Average of numbers")** - **[Average of numbers](/Number-Related/Math-Power.md)** 21 | * **[2.4](Number-Related/Divisible-by-3-and-5.md "Divisible by 3 and 5")** - **[Divisible by 3 and 5](/Number-Related/Divisible-by-3-and-5.md)** 22 | * **[2.5](Number-Related/Sum-of-digits.md "Sum of digits")** - **[Sum of digits](Loop-Related/Sum-of-digits.md)** 23 | 24 | 25 | ## 3 -  Loop Related 26 | * **[3.1](Loop-Related/Coin-sum.md "Sum of elements")** - **[Sum of elements](Loop-Related/Coin-sum.md)** 27 | * ### 2 -  Number Related - 6-10 28 | * **[2.1](Number-Related/max-of-two.md "Max of two")** - **[Max of two](Number-Related/max-of-two.md)** 29 | * **[2.2](Number-Related/Max-of-three.md "Max of three")** - **[Max of three](Number-Related/Max-of-three.md)** 30 | * **[2.3](Number-Related/Average-of-numbers.md "Average of numbers")** - **[Average of numbers](Number-Related/Average-of-numbers.md)** 31 | * **[2.4](Number-Related/Divisible-by-3-and-5.md "Divisible by 3 and 5")** - **[Divisible by 3 and 5](Number-Related/Divisible-by-3-and-5.md)** 32 | * **[2.5](Number-Related/Sum-of-digits.md "Sum of digits")** - **[Sum of digits](Number-Related/Sum-of-digits.md)** 33 | 34 | 35 | * ### 3 -  Loop Related - 11-16 36 | * **[3.1](Loop-Related/Sum-of-elements.md "Sum of elements")** - **[Sum of elements](Loop-Related/Sum-of-elements.md)** 37 | * **[3.2](Loop-Related/Largest-element-of-a-list.md "Largest element of a list")** - **[Largest element of a list](Loop-Related/Largest-element-of-a-list.md)** 38 | * **[3.3](Loop-Related/Sum-of-squares.md "Sum of squares")** - **[Sum of squares](Loop-Related/Sum-of-squares.md)** 39 | * **[3.4](Loop-Related/Second-Largest.md "Second Largest")** - **[Second Largest](Loop-Related/Second-Largest.md)** 40 | * **[3.5](Loop-Related/Second-smallest.md "Second Smallest")** - **[Second Smallest](Loop-Related/Second-smallest.md)** 41 | * **[3.6](Loop-Related/Remove-duplicate-Chars.md "Remove duplicate Chars")** - **[Remove duplicate Chars](Loop-Related/Remove-duplicate-Chars.md)** 42 | 43 | 44 | ## 4 -  Conversions 45 | * **[4.1](Conversions/Celsius-to-Fahrenheit.md "Miles to Kilometers")** - **[Miles to Kilometers](Conversions/Celsius-to-Fahrenheit.md)** 46 | * **[4.2](Conversions/Decimal-to-binary.md "Celsius to Fahrenheit")** - **[Celsius to Fahrenheitt](Conversions/Decimal-to-binary.md)** 47 | * **[4.3](Conversions/Decimal-to-binary-recursive.md "Decimal to binary")** - **[Decimal to binary](Conversions/Decimal-to-binary-recursive.md)** 48 | * ### 4 -  Conversions - 17-21 49 | * **[4.1](Conversions/Miles-to-Kilometers.md "Miles to Kilometers")** - **[Miles to Kilometers](Conversions/Miles-to-Kilometers.md)** 50 | * **[4.2](Conversions/Celsius-to-Fahrenheit.md "Celsius to Fahrenheit")** - **[Celsius to Fahrenheitt](Conversions/Celsius-to-Fahrenheit.md)** 51 | * **[4.3](Conversions/Decimal-to-binary.md "Decimal to binary")** - **[Decimal to binary](Conversions/Decimal-to-binary.md)** 52 | * **[4.4](Conversions/Decimal-to-binary-recursive.md "Decimal to binary (recursive)")** - **[Decimal to binary (recursive)](Conversions/Decimal-to-binary-recursive.md)** 53 | * **[4.5](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp "Binary to decimal")** - **[Binary to decimal](premium)** **- Premium Access** 54 | 55 | ## [5](Solution-Strategy.md "Solution Strategy") -  [Solution Strategy](Solution-Strategy.md) 56 | 57 | ## 6 -  Computations 58 | 59 | * **[6.1](Computations/Simple-Interest.md "Simple Interest")** - **[Simple Interest](Computations/Simple-Interest.md)** 60 | * **[6.2](Computations/Complex-Interest.md "Complex Interest")** - **[Complex Interest](Computations/Complex-Interest.md)** 61 | * **[6.3](Computations/Calculate-Grades.md "Calculate Grades)")** - **[Calculate Grades](Computations/Calculate-Grades.md)** 62 | * **[6.4](Computations/Gravitational-Force.md "Gravitational Force)")** - **[Gravitational Force](Computations/Gravitational-Force.md)** 63 | * **[6.5](Computations/Triangle-Area.md "Triangle Area)")** - **[Triangle Area](Computations/Triangle-Area.md)** 64 | 65 | ## 7 -  Prime number 66 | 67 | * **[7.1](Prime-number/Check-Prime.md "Check Prime")** -   **[Check Prime](Solution-Strategy.md)** 68 | * **[7.2](Prime-number/Prime-Numbers.md "Prime Numbers")** -   **[Prime Numbers](Prime-number/Prime-Numbers.md)** 69 | * **[7.3](Prime-number/Prime-factors.md "Prime factors")** -   **[Prime factors](Prime-number/Prime-factors.md)** 70 | * **[7.4](Prime-number/Smallest-prime-factor.md "Smallest prime factor")** -   **[Smallest prime factor](Prime-number/Smallest-prime-factor.md)** 71 | 72 | ## 8 -  Reverse 73 | 74 | * **[8.1](Reverse/Reverse-String.md "Reverse String")** -   **[Reverse String](Reverse/Reverse-String.md)** 75 | * **[8.2](Reverse/Reverse-String-(stack).md "Reverse String (stack)")** -   **[Reverse String (stack)](Reverse/Reverse-String-(stack).md)** 76 | * **[8.3](Reverse/Reverse-String-(recursive).md "Reverse String (recursive)")** -   **[Reverse String (recursive)](Reverse/Reverse-String-(recursive).md)** 77 | * **[8.4](Reverse/Reverse-Number.md "Reverse Number")** -   **[Reverse Number](Reverse/Reverse-Number.md)** 78 | * **[8.5](Reverse/Reverse-word.md "Reverse word")** -   **[Reverse word](Reverse/Reverse-word.md)** 79 | 80 | ## 9 -  Medium 81 | 82 | * **[9.1](Medium/Check-palindrome.md "Check palindrome")** -   **[Check palindrome](Medium/Check-palindrome.md)** 83 | * **[9.2](Medium/Dictionary-of-cubes.md "Dictionary of cubes")** -   **[Dictionary of cubes](Medium/Dictionary-of-cubes.md)** 84 | * **[9.3](Medium/Armstrong-number.md "Armstrong number")** -   **[Armstrong number](Medium/Armstrong-number.md)** 85 | * **[9.4](Medium/Greatest-common-divisor.md "Greatest common divisor")** -   **[Greatest common divisor](Medium/Greatest-common-divisor.md)** 86 | * **[9.5](Medium/Least-Common-Multiple.md "Least Common Multiple")** -   **[Least Common Multiple](Medium/Least-Common-Multiple.md)** 87 | 88 | ## [10](Programming-Contest.md "Programming Contest") -  [Programming Contest](Programming-Contest.md) 89 | 90 | ## 11 -  Simple Game 91 | 92 | * **[11.1](Simple-Game/Guess-game.md "Guess game")** -   **[Guess game](Simple-Game/Guess-game.md)** 93 | * **[11.2](Simple-Game/Rock-paper-scissor.md "Rock paper scissor")** -   **[Rock paper scissor](Simple-Game/Rock-paper-scissor.md)** 94 | * **[11.3](Simple-Game/Cows-and-bulls.md "Cows and bulls")** -   **[Cows and bulls](Simple-Game/Cows-and-bulls.md)** 95 | * **[11.4](Simple-Game/Cows-and-bulls(4digits).md "Cows and bulls(4digits)")** -   **[Cows and bulls(4digits)](Simple-Game/Cows-and-bulls(4digits).md)** 96 | * **[11.5](Simple-Game/Word-completion.md "Word completion")** -   **[Word completion](Simple-Game/Word-completion.md)** 97 | * **[11.6](Simple-Game/Word-hangman.md "Word hangman")** -   **[Word hangman](Simple-Game/Word-hangman.md)** 98 | 99 | ## 12 -  Harder 100 | 101 | * **[12.1](Harder/Simple-Calculator.md "Simple Calculator")** -   **[Simple Calculator](Harder/Simple-Calculator.md)** 102 | * **[12.2](Harder/Password-generator.md "Password generator")** -   **[Password generator](Harder/Password-generator.md)** 103 | * **[12.3](Harder/Password-with-requirements.md "Password with requirements")** -   **[Password with requirements](Harder/Password-with-requirements.md)** 104 | * **[12.4](Harder/Permutations.md "Permutations")** -   **[Permutations](Harder/Permutations.md)** 105 | * **[12.5](Harder/Simple-Calculator.md "Generate Sentences")** -   **[Generate Sentences](Harder/Simple-Calculator.md)** 106 | 107 | 108 | ## 13 -  User Submitted 109 | 110 | * **[13.1](User-Submitted/Simple-Clock.md "Simple Digital Clock")** -   **[Simple Clock](User-Submitted/Simple-Clock.md)** 111 | * **[13.2](User-Submitted/Birthday-remaining.md "Birthday-remaining.md")** -   **[Birthday-remaining.md](User-Submitted/Birthday-remaining.md)** 112 | * **[13.3](User-Submitted/Calculate-age.md "Calculate age")** -   **[Calculate age](User-Submitted/Calculate-age.md)** 113 | 114 | 115 | > Only half of the task is done. More problems are coming. If you want to add more problems, feel free to send a pull request. 116 | 117 | 118 | ### Many solutions 119 | Here we are focusing on the thinking and strategies to solve a problem. However, every problem could be solved in multiple ways. And other solutions could be better and faster. 120 | 121 | Always keep an open mind to learn multiple solutions to solve a problem. 122 | 123 | ### Let’s Start 124 | > Let’s start your journey. Let’s solve every problem you face. 125 | 126 | *Let’s do it.* 127 | 128 |   129 | [![Next Page](assets/next-button.png)](/Easy-ones/User-input-to-Number.md) 130 |   131 | 132 | tags: `programming-hero` `python` `python3` `problem-solving` `programming` `coding-challenge` `interview` `learn-python` `python-tutorial` `programming-exercises` `programming-challenges` `programming-fundamentals` `programming-contest` `python-coding-challenges` `python-problem-solving` 133 | --------------------------------------------------------------------------------