Day 9: Understanding Recursion through Factorials Today’s challenge was all about mastering one of the most elegant concepts in computer science — Recursion. I implemented a recursive function to calculate the factorial of a number — a foundational problem that helps build deeper understanding of how function calls work in a stack-like manner. 📘 Concept Recap: Recursion is when a function calls itself until it reaches a base condition. For factorial, the mathematical definition is: n! = n × (n-1)! with the base case being 1! = 1. 💻 Code Example (Python): def factorial(n): if n == 1: return 1 else: return n * factorial(n - 1) n = int(input()) print(factorial(n)) 📊 Sample Input: 3 📈 Sample Output: 6 ✨ Key Takeaway: Recursion simplifies complex problems by breaking them down into smaller subproblems. Understanding base cases and recursive calls is essential to avoid infinite loops and stack overflows. Every recursive problem teaches patience and logical thinking — both vital for becoming a strong problem solver. #Day9 #100DaysOfCode #Recursion #ProblemSolving #Algorithms #Python #CodingJourney #Developer #Learning
Sumit Dhamane’s Post
More Relevant Posts
-
Sometimes, caught up in work, our files pile up on our computer. As a result, finding a specific file can take forever (especially if it's mixed in with PDFs, videos, images, and other file types). There are programs to help you organize them, but you can also use Python to sort and organize them yourself by extension, name, date, or other criteria. This will save you valuable time. Here's an example of an application you can create to organize your files. It's a small application that organizes the files in a folder into subfolders according to their type (images, documents, videos, others.). Here's a link to my GitHub if you'd like to contribute or download the application: https://lnkd.in/egDizyZF A short demonstration video is available.
To view or add a comment, sign in
-
🚀 Solved a Classic String-Search Problem! I recently worked on the problem “Find the index of the first occurrence of a string in another string” (LeetCode 28 — strStr()), and it was a great exercise in understanding string manipulation and efficient search techniques. 🔍 Problem Summary Given two strings, haystack and needle, the goal is to return the index of the first occurrence of needle within haystack, or -1 if it doesn’t exist. 🔧 My Approach I implemented two solutions: 1️⃣ Basic Sliding Window Approach Iterate through haystack Compare each substring with needle Return the index when a match is found 2️⃣ Optimized Thought Process (KMP-ready) Explored how KMP can reduce time complexity from O(n*m) to O(n) Understood prefix functions and pattern matching fundamentals 🧠 What I Learned Strengthened understanding of string traversal Learned how to optimize search operations Practiced writing clean and readable code Got a deeper understanding of how real search algorithms (like KMP) work behind the scenes Happy to connect with others exploring DSA, algorithms, and problem-solving! 🚀 #️⃣ #dsa #leetcode #coding #programming #python #softwaredevelopment #algorithms #computerscience
To view or add a comment, sign in
-
-
🔁 Ever tried to understand Recursion — but felt like you’re looping forever? Let’s break it down once and for all 👇 --- 🧠 What is Recursion? Recursion is when a function calls itself to solve smaller versions of the same problem — until it reaches a base case. Think of it like standing between two mirrors — the reflection goes deeper and deeper, but there’s always a point where it stops. That stopping point is your base case! --- 💡 Real-World Example: You open a stack of nested boxes. Each box contains another box — until the final one has your gift. To get it, you open the top box (call the function), find another box inside (recursive call), and repeat — until you reach the last box (base case). Then you close each box back as the function returns! --- ⚙️ Simple Code: int factorial(int n) { if (n == 0) return 1; // base case return n * factorial(n - 1); // recursive call } --- 🪄 Why It’s Powerful: Makes complex problems simpler Foundation for divide and conquer algorithms Used in backtracking, tree traversal, dynamic programming, and more --- ⚠️ Quick Tip: Always define your base case properly — otherwise, recursion becomes an infinite loop 🔄 --- 🚀 In short: > “Recursion is thinking smaller to solve bigger.” --- Resources 💡 Resources to Learn Recursion 🎓 Beginner-Friendly Tutorials 1. GeeksforGeeks – Recursion Basics ➜ https://lnkd.in/dAK6p8Hi Covers everything from base case, recursive case, and call stack visualization. 2. Programiz – Learn Recursion in C, C++, Python ➜ https://lnkd.in/d_ZSWQ6m Great for code examples and visualization for beginners. If have any queries comment down 👇 #Recursion #ProgrammingConcepts #DSA #CodeLearning #CProgramming #Developers #LogicBuilding #ComputerScience #TechLearning #LearnByDoing #Sarvastrix #Coding
To view or add a comment, sign in
-
-
When Your Function Decides to Call Itself You ever write a function… and then watch it call itself like it’s trying to have a deep conversation about purpose? That’s recursion. It’s a magical (and slightly chaotic) moment. The first time I understood recursion, I felt like I’d unlocked a cheat code in programming. It’s not just about loops or maths, it’s about trust. You trust your function to handle a smaller version of the same problem without losing its mind halfway. Example: def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1) Recursion taught me patience. Both in code and in life, because sometimes, solving something big just means breaking it into smaller, saner bits and trusting the process to resolve itself. #Programming #Python #Recursion #CSHumor #CodingLife
To view or add a comment, sign in
-
-
🔹 Day 6 of 30 – LeetCode Challenge: Longest Common Subsequence 🔠 Today’s challenge was all about finding the Longest Common Subsequence (LCS) between two strings — one of the most important problems in Dynamic Programming. 🧩 Problem: Given two strings text1 and text2, find the length of their longest subsequence that appears in both. A subsequence keeps the order of characters but doesn’t require them to be consecutive. Example: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: LCS = "ace" 💡 Approach: I used a Dynamic Programming table where: dp[i][j] = LCS length between text1[0:i] and text2[0:j] If characters match → 1 + dp[i-1][j-1] Else → max(dp[i-1][j], dp[i][j-1]) ⚙️ Complexity: Time Complexity: O(m × n) Space Complexity: O(m × n) 🏆 Result: ✅ All test cases passed 💡 Improved understanding of 2D DP table formulation 📚 Learned how to build relationships between subproblems 💬 Learning: The LCS problem is the foundation for many advanced algorithms like Edit Distance, Diff Tools, and DNA sequence alignment. It’s a great exercise to visualize how dynamic programming connects overlapping subproblems! #Day6 #LeetCode #DynamicProgramming #LCS #Python #Algorithms #DataStructures #30DaysOfCode #CodingChallenge #MTech
To view or add a comment, sign in
-
-
List comprehension allows you to create new lists in a single line, making your code more readable, efficient, and concise. It combines loops and conditional logic in a clean, Pythonic way. Here are a few examples I explored: ✅ Generating uppercase alphabets using ASCII values ✅ Printing numbers divisible by both 3 and 5 ✅ Flattening nested lists ✅ Extracting Gmail users from a list of emails ✅ Finding palindromes and squares of even numbers Python makes problem-solving both simple and elegant 10000 CodersAjay Miryala
To view or add a comment, sign in
-
🔹 Day 5 of 30 – LeetCode Challenge: Longest Increasing Subsequence 📈 Today’s problem was all about finding patterns and optimizing logic! I solved the Longest Increasing Subsequence (LIS) problem — a fundamental concept in Dynamic Programming and Binary Search optimization. 🧩 Problem: Given an array of integers, find the length of the longest subsequence where each element is strictly greater than the previous one. Example: Input: nums = [10,9,2,5,3,7,101,18] Output: 4 Explanation: [2,3,7,101] is the longest increasing subsequence. 💡 Approach: There are two ways to solve this: 1. Dynamic Programming (O(n²)) a. For each element, look back at all previous elements. b. Update dp[i] as the length of the LIS ending at that index. 2.Binary Search Optimization (O(n log n)) a. Maintain a list sub representing potential increasing subsequences. b. Use bisect_left to replace elements efficiently. ⚙️ Complexity: Time: O(n log n) Space: O(n) 🏆 Result: ✅ All test cases passed ⚡ Optimized solution using Binary Search 💪 Strengthened understanding of Dynamic Programming and Binary Search combination 💬 Learning: This problem helped me understand how to convert a quadratic DP approach into a logarithmic one by thinking about sorted subsequences and binary search placements — a powerful pattern for future optimization problems. #Day5 #LeetCode #DynamicProgramming #BinarySearch #Python #Algorithms #DataStructures #30DaysOfCode #MTech #CodingChallenge #LIS
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development