Skip to content

Shahupdates/PracticePythonProblems

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

96 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Practice Python Problems

Program image

Practice Python Problems

Welcome to the Practice Python Problems repository! This repository is dedicated to providing a collection of Python problems suitable for beginners or those looking to improve their Python skills. The problems included here range from being designed to be simpler than even the easiest problems found on platforms like LeetCode, to medium level problems, to hard level problems, and then to problems that even the most advanced leetcoder would have to think twice when completing.

How to Use the Repository

File Structure

The repository is organized into different difficulty levels: easy, medium, hard, and extreme. Each difficulty level has a corresponding folder containing the Python problem files.

  • easy: Contains easy-level Python problem files.
  • medium: Contains medium-level Python problem files.
  • hard: Contains hard-level Python problem files.
  • extreme: Contains extreme-level Python problem files.

Running the Flask Application

The app.py file in the root directory of the repository contains a Flask application that serves the problem files and an index.html file. The problem files can be accessed through the URL route /difficulty/problem<number>.py, where difficulty is one of the difficulty levels (easy, medium, hard, extreme), and number is the problem number.

To run the Flask application, follow these steps:

  1. Install Flask if you haven't already. You can install Flask by running pip install flask in your terminal.

  2. Open a terminal or command prompt and navigate to the root directory of the repository.

  3. Run the following command to start the Flask application: python app.py

The Flask application will start running on http://localhost:5000.

Accessing the Problem Files

Once the Flask application is running, you can access the problem files by opening a web browser and navigating to http://localhost:5000/difficulty/problem<number>.py, where difficulty is one of the difficulty levels (easy, medium, hard, extreme), and number is the problem number.

For example, to access the second problem in the easy difficulty level, you would navigate to http://localhost:5000/easy/problem2.py.

Viewing the Problem List

The index.html file in the root directory of the repository contains a table that lists all the problems in the repository. Each problem is represented by a clickable cell in the table.

To view the problem list, open a web browser and navigate to http://localhost:5000 (when the Flask application is running).

Clicking on a problem cell will open a popup displaying the code for that problem.

Problem List

Here is the list of the 80 problems available in this repository:

Problem List
Easy Problems Medium Problems Hard Problems Extreme Problems
1. Sum of Two Numbers 21. Validate Email Address 41. Check if a Number is a Strong Number 61. Find the Longest Common Subsequence
2. Reverse a String 22. Find the Second Largest Number 42. Find the Prime Factors of a Number 62. Find the Longest Palindromic Substring
3. Count Vowels in a String 23. Find Common Elements in Lists 43. Sort a List of Strings by Length 63. Calculate the Minimum Cost Path in a Matrix
4. Check if a Number is Even or Odd 24. Reverse a Sentence 44. Find the GCD of Two Numbers 64. Find the Number of Islands
5. Calculate the Factorial of a Number 25. Check if a Number is a Perfect Square 45. Generate the nth Fibonacci Number 65. Find the Longest Increasing Path in a Matrix
6. Check if a String is Palindrome 26. Find the Union of Two Lists 46. Reverse a Number 66. Find the Number of Islands II
7. Find the Maximum Number in a List 27. Find the Longest Word in a Sentence 47. Count the Palindrome Substrings in a String 67. Find the Maximum XOR of Two Numbers in an Array
8. Check if a Year is a Leap Year 28. Convert Decimal to Binary 48. Calculate the Power Set of a Set 68. Count the Number of Valid Sudoku Boards
9. Calculate the Average of a List of Numbers 29. Find the Common Characters in Strings 49. Implement a Binary Search Algorithm 69. Find the Kth Largest Element in an Array
10. Find the Square Root of a Number 30. Count Words in a Text File 50. Implement Bubble Sort 70. Find the Median of Two Sorted Arrays
11. Generate Fibonacci Series 31. Find the Mode of a List 51. Find the Longest Increasing Subsequence 71. Implement a Stack using Queues
12. Check if a Number is Prime 32. Reverse the Order of Words in a Sentence 52. Check if a String is a Valid Parenthesis Expression 72. Count the Number of Submatrices Summing to Target
13. Calculate the Power of a Number 33. Calculate the Factorial of a Large Number 53. Calculate the Area of a Polygon 73. Find the K Closest Points to the Origin
14. Check if a String is a Pangram 34. Find the Median of a List 54. Find the Smallest Missing Positive Integer 74. Check if a Binary Tree is Balanced
15. Count Words in a String 35. Remove Duplicates from a List 55. Reverse Words in a Sentence 75. Serialize and Deserialize a Binary Tree
16. Find the Smallest Number in a List 36. Check if a Number is a Perfect Number 56. Find the Intersection of Two Sets 76. Find the Longest Increasing Subsequence
17. Check if a String is a Palindrome Number 37. Count the Occurrences of Each Element in a List 57. Check if a String is an Anagram 77. Calculate the Minimum Window Substring
18. Calculate the Sum of Digits in a Number 38. Generate Random Password 58. Calculate the Maximum Subarray Sum 78. Find the Duplicate Number
19. Reverse a List 39. Find the Difference between Two Lists 59. Find the Kth Smallest Element in a List 79. Find the First Missing Positive
20. Count the Occurrences of an Element in a List 40. Find the Largest Prime Factor of a Number 60. Calculate the Edit Distance between Two Strings 80. Find the Celebrity

Contributing

If you would like to contribute to this repository, you can do so by adding new problems or improving the existing ones. To contribute, follow these steps:

  1. Fork the repository on GitHub.

  2. Create a new branch for your changes.

  3. Add your new problems or make improvements to the existing ones.

  4. Commit and push your changes to your forked repository.

  5. Submit a pull request, describing your changes and why they should be merged.

License

The Practice Python Problems repository is licensed under the MIT License.

Feel free to use, modify, and distribute the problem files for personal or educational purposes.

Full problem list with descriptions

Here is the list of the 80 problems available in this repository:

Easy Problems Problem 1: Sum of Two Numbers Problem Description: Write a program that takes two numbers as input and prints their sum. Solution (problem1.py):

Problem 2: Reverse a String Problem Description: Write a program that takes a string as input and prints its reverse. Solution (problem2.py):

Problem 3: Count Vowels in a String Problem Description: Write a program that takes a string as input and counts the number of vowels (a, e, i, o, u) in it. Solution (problem3.py):

Problem 4: Check if a Number is Even or Odd Problem Description: Write a program that takes a number as input and checks whether it is even or odd. Solution (problem4.py):

Problem 5: Calculate the Factorial of a Number Problem Description: Write a program that takes a number as input and calculates its factorial. Solution (problem5.py):

Problem 6: Check if a String is Palindrome Problem Description: Write a program that takes a string as input and checks whether it is a palindrome (reads the same forwards and backwards). Solution (problem6.py):

Problem 7: Find the Maximum Number in a List Problem Description: Write a program that takes a list of numbers as input and finds the maximum number in the list. Solution (problem7.py):

Problem 8: Check if a Year is a Leap Year Problem Description: Write a program that takes a year as input and checks whether it is a leap year. Solution (problem8.py):

Problem 9: Calculate the Average of a List of Numbers Problem Description: Write a program that takes a list of numbers as input and calculates their average. Solution (problem9.py):

Problem 10: Find the Square Root of a Number Problem Description: Write a program that takes a number as input and calculates its square root. Solution (problem10.py):

Problem 11: Generate Fibonacci Series Problem Description: Write a program that takes a number as input and generates the Fibonacci series up to that number. Solution (problem11.py):

Problem 12: Check if a Number is Prime Problem Description: Write a program that takes a number as input and checks whether it is a prime number. Solution (problem12.py):

Problem 13: Calculate the Power of a Number Problem Description: Write a program that takes a base number and an exponent as input and calculates the power of the base number raised to the exponent. Solution (problem13.py):

Problem 14: Check if a String is a Pangram Problem Description: Write a program that takes a string as input and checks whether it is a pangram (contains all the letters of the alphabet). Solution (problem14.py):

Problem 15: Count Words in a String Problem Description: Write a program that takes a string as input and counts the number of words in it. Solution (problem15.py):

Problem 16: Find the Smallest Number in a List Problem Description: Write a program that takes a list of numbers as input and finds the smallest number in the list. Solution (problem16.py):

Problem 17: Check if a String is a Palindrome Number Problem Description: Write a program that takes a string as input and checks whether it is a palindrome number (reads the same forwards and backwards). Solution (problem17.py):

Problem 18: Calculate the Sum of Digits in a Number Problem Description: Write a program that takes a number as input and calculates the sum of its digits. Solution (problem18.py):

Problem 19: Reverse a List Problem Description: Write a program that takes a list of elements as input and reverses the order of the elements. Solution (problem19.py):

Problem 20: Count the Occurrences of an Element in a List Problem Description: Write a program that takes a list of elements and an element as input, and counts the number of occurrences of that element in the list. Solution (problem20.py):

Medium Problems Problem 21: Validate Email Address Problem Description: Write a program that takes an email address as input and validates whether it is a valid email address format or not. Solution (problem21.py):

Problem 22: Find the Second Largest Number in a List Problem Description: Write a program that takes a list of numbers as input and finds the second largest number in the list. Solution (problem22.py):

Problem 23: Find Common Elements in Lists Problem Description: Write a program that takes two lists as input and finds the common elements between them. Solution (problem23.py):

Problem 24: Reverse a Sentence Problem Description: Write a program that takes a sentence as input and reverses the order of the words in the sentence. Solution (problem24.py):

Problem 25: Check if a Number is a Perfect Square Problem Description: Write a program that takes a number as input and checks whether it is a perfect square or not. Solution (problem25.py):

Problem 26: Find the Union of Two Lists Problem Description: Write a program that takes two lists as input and finds the union of the two lists (i.e., all unique elements from both lists). Solution (problem26.py):

Problem 27: Find the Longest Word in a Sentence Problem Description: Write a program that takes a sentence as input and finds the longest word in the sentence. Solution (problem27.py):

Problem 28: Convert Decimal to Binary Problem Description: Write a program that takes a decimal number as input and converts it to binary representation. Solution (problem28.py):

Problem 29: Find the Common Characters in Strings Problem Description: Write a program that takes two strings as input and finds the common characters between them. Solution (problem29.py):

Problem 30: Count Words in a Text File Problem Description: Write a program that takes the path to a text file as input and counts the number of words in the file. Solution (problem30.py):

Problem 31: Find the Mode of a List Problem Description: Write a program that takes a list of elements as input and finds the mode (most frequently occurring element) of the list. Solution (problem31.py):

Problem 32: Reverse the Order of Words in a Sentence Problem Description: Write a program that takes a sentence as input and reverses the order of the words while preserving the order of the characters in each word. Solution (problem32.py):

Problem 33: Calculate the Factorial of a Large Number Problem Description: Write a program that takes a large number as input and calculates its factorial. Solution (problem33.py):

Problem 34: Find the Median of a List Problem Description: Write a program that takes a list of numbers as input and finds the median of the list. Solution (problem34.py):

Problem 35: Remove Duplicates from a List Problem Description: Write a program that takes a list of elements as input and removes any duplicate elements from the list. Solution (problem35.py):

Problem 36: Check if a Number is a Perfect Number Problem Description: Write a program that takes a number as input and checks whether it is a perfect number or not. A perfect number is a positive integer that is equal to the sum of its proper divisors. Solution (problem36.py):

Problem 37: Count the Occurrences of Each Element in a List Problem Description: Write a program that takes a list of elements as input and counts the number of occurrences of each element in the list. Solution (problem37.py):

Problem 38: Generate Random Password Problem Description: Write a program that generates a random password of a specified length. The password should contain a mix of uppercase letters, lowercase letters, digits, and special characters. Solution (problem38.py):

Problem 39: Find the Difference between Two Lists Problem Description: Write a program that takes two lists as input and finds the elements that are present in the first list but not in the second list. Solution (problem39.py):

Problem 40: Find the Largest Prime Factor of a Number Problem Description: Write a program that takes a number as input and finds the largest prime factor of that number. Solution (problem40.py):

Hard problems Problem 41: Check if a Number is a Strong Number Problem Description: Write a program that takes a number as input and checks whether it is a strong number or not. A strong number is a number that is equal to the sum of the factorial of its digits. Solution (problem41.py):

Problem 42: Find the Prime Factors of a Number Problem Description: Write a program that takes a number as input and finds all the prime factors of that number. Solution (problem42.py):

Problem 43: Sort a List of Strings by Length Problem Description: Write a program that takes a list of strings as input and sorts the list in ascending order based on the length of the strings. Solution (problem43.py):

Problem 44: Find the GCD of Two Numbers Problem Description: Write a program that takes two numbers as input and finds their greatest common divisor (GCD). Solution (problem44.py):

Problem 45: Generate the nth Fibonacci Number Problem Description: Write a program that takes a number n as input and generates the nth Fibonacci number. Solution (problem45.py):

Problem 46: Reverse a Number Problem Description: Write a program that takes a number as input and reverses the digits of the number. Solution (problem46.py):

Problem 47: Count the Palindrome Substrings in a String Problem Description: Write a program that takes a string as input and counts the number of palindrome substrings in the string. Solution (problem47.py):

Problem 48: Calculate the Power Set of a Set Problem Description: Write a program that takes a set of elements as input and calculates its power set, which is the set of all possible subsets of the given set. Solution (problem48.py):

Problem 49: Implement a Binary Search Algorithm Problem Description: Write a program that takes a sorted list of numbers and a target number as input and implements the binary search algorithm to find the index of the target number in the list. If the target number is not found, return -1. Solution (problem49.py):

Problem 50: Implement Bubble Sort Problem Description: Write a program that takes a list of numbers as input and implements the bubble sort algorithm to sort the list in ascending order. Solution (problem50.py):

Problem 51: Find the Longest Increasing Subsequence Problem Description: Write a program that takes a list of numbers as input and finds the length of the longest increasing subsequence in the list. Solution (problem51.py):

Problem 52: Check if a String is a Valid Parenthesis Expression Problem Description: Write a program that takes a string as input and checks whether it is a valid parenthesis expression. A valid expression should have matching opening and closing parentheses. Solution (problem52.py):

Problem 53: Calculate the Area of a Polygon Problem Description: Write a program that takes the coordinates of the vertices of a polygon as input and calculates its area. Solution (problem53.py):

Problem 54: Find the Smallest Missing Positive Integer Problem Description: Write a program that takes a list of integers as input and finds the smallest missing positive integer that does not appear in the list. Solution (problem54.py):

Problem 55: Reverse Words in a Sentence Problem Description: Write a program that takes a sentence as input and reverses the order of the words in the sentence. Solution (problem55.py):

Problem 56: Find the Intersection of Two Sets Problem Description: Write a program that takes two sets as input and finds the intersection of the two sets, i.e., the elements that are common to both sets. Solution (problem56.py):

Problem 57: Check if a String is an Anagram Problem Description: Write a program that takes two strings as input and checks whether they are anagrams of each other. Anagrams are strings that have the same characters but in a different order. Solution (problem57.py):

Problem 58: Calculate the Maximum Subarray Sum Problem Description: Write a program that takes a list of integers as input and calculates the maximum sum of a subarray within the list. Solution (problem58.py):

Problem 59: Find the Kth Smallest Element in a List Problem Description: Write a program that takes a list of numbers and an integer k as input, and finds the kth smallest element in the list. Solution (problem59.py):

Problem 60: Calculate the Edit Distance between Two Strings Problem Description: Write a program that takes two strings as input and calculates the edit distance between the two strings. The edit distance is the minimum number of operations (insertion, deletion, or substitution) required to transform one string into another. Solution (problem60.py):

Extreme Problems Problem 61: Find the Longest Common Subsequence Problem Description: Write a program that takes two strings as input and finds the length of the longest common subsequence between the two strings. Solution (problem61.py):

Problem 62: Find the Longest Palindromic Substring Problem Description: Write a program that takes a string as input and finds the longest palindromic substring within the string. Solution (problem62.py):

Problem 63: Calculate the Minimum Cost Path in a Matrix Problem Description: Write a program that takes a matrix of numbers and finds the minimum cost path from the top-left corner to the bottom-right corner. The cost of a path is the sum of the numbers along the path. Solution (problem63.py):

Problem 64: Find the Number of Islands Problem Description: Write a program that takes a grid of 0's and 1's as input and finds the number of islands. An island is a group of connected 1's, where connectivity is defined by horizontally or vertically adjacent 1's. Solution (problem64.py):

Problem 65: Find the Longest Increasing Path in a Matrix Problem Description: Write a program that takes a matrix of numbers as input and finds the length of the longest increasing path within the matrix. A path is considered increasing if each number along the path is strictly greater than the previous number. Solution (problem65.py):

Problem 66: Find the Number of Islands II Problem Description: Write a program that takes a grid of 0's and 1's as input and a list of positions where the value changes to 1. For each position, find the number of islands after the change. An island is a group of connected 1's, where connectivity is defined by horizontally or vertically adjacent 1's. Solution (problem66.py):

Problem 67: Find the Maximum XOR of Two Numbers in an Array Problem Description: Write a program that takes an array of integers as input and finds the maximum XOR value of any two numbers in the array. Solution (problem67.py):

Problem 68: Count the Number of Valid Sudoku Boards Problem Description: Write a program that takes a 9x9 Sudoku board as input and counts the number of valid Sudoku boards. A valid Sudoku board must satisfy the following rules: Each row contains the digits 1-9 without repetition. Each column contains the digits 1-9 without repetition. Each of the nine 3x3 sub-grids contains the digits 1-9 without repetition. Solution (problem68.py):

Problem 69: Find the Kth Largest Element in an Array Problem Description: Write a program that takes an array of integers and an integer k as input, and finds the kth largest element in the array. Solution (problem69.py):

Problem 70: Find the Median of Two Sorted Arrays Problem Description: Write a program that takes two sorted arrays as input and finds the median of the two arrays. Solution (problem70.py):

Problem 71: Implement a Stack using Queues Problem Description: Implement a stack data structure using queues. The stack should support the following operations: push, pop, top, and empty. Solution (problem71.py):

Problem 72: Count the Number of Submatrices Summing to Target Problem Description: Write a program that takes a matrix of numbers and an integer target as input, and counts the number of submatrices whose sum is equal to the target. Solution (problem72.py):

Problem 73: Find the K Closest Points to the Origin Problem Description: Write a program that takes a list of points (coordinates) in a 2D plane and an integer k as input, and finds the k closest points to the origin. The distance between two points (x1, y1) and (x2, y2) is given by the formula: distance = sqrt((x2 - x1)^2 + (y2 - y1)^2). Solution (problem73.py):

Problem 74: Check if a Binary Tree is Balanced Problem Description: Write a program that takes the root of a binary tree as input and checks whether the binary tree is balanced. A binary tree is balanced if the heights of its left and right subtrees differ by at most 1. Solution (problem74.py):

Problem 75: Serialize and Deserialize a Binary Tree Problem Description: Write a program that takes the root of a binary tree as input and performs the following operations: Serialize the binary tree into a string representation. Deserialize the string representation back into a binary tree. Solution (problem75.py):

Problem 76: Find the Longest Increasing Subsequence Problem Description: Write a program that takes a list of integers as input and finds the length of the longest increasing subsequence within the list. Solution (problem76.py):

Problem 77: Calculate the Minimum Window Substring Problem Description: Write a program that takes two strings as input, a larger string and a smaller string, and finds the minimum window substring of the larger string that contains all the characters of the smaller string. Solution (problem77.py):

Problem 78: Find the Duplicate Number Problem Description: Write a program that takes a list of integers as input, where the list contains integers in the range 1 to n, and finds the duplicate number in the list. Solution (problem78.py):

Problem 79: Find the First Missing Positive Problem Description: Write a program that takes a list of integers as input and finds the first missing positive integer. The missing integer should be in the range of 1 to n+1, where n is the length of the list. Solution (problem79.py):

Problem 80: Find the Celebrity Problem Description: Suppose you are at a party with n people labeled from 0 to n-1. You are given a 2D matrix called knows, where knows[i][j] is True if person i knows person j, and False otherwise. A celebrity is defined as someone who knows no one but is known by everyone else. Write a program that takes the number of people n and the knows matrix as input and finds the celebrity at the party. If there is no celebrity, return -1. Solution (problem80.py).

About

Easy alternative to Leetcode problems

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published