Day 3/365: Comparing Two Strings Character by Character 🧵🧠 Today I worked on a simple but fundamental logic problem: checking if two strings are the same, without directly using a built-in equality check. First, I compare the lengths of both strings. If lengths differ, they can’t be the same. If lengths match, I loop through each index and compare characters one by one. If any character is different, I break and print that the strings are not the same. If the loop finishes without finding a mismatch, the else block of the for loop runs and prints that the strings are the same. The interesting part is the for-else in Python. The else only runs when the loop completes normally (no break). This makes it a clean way to express: “if I didn’t find any mismatch in the entire loop, then the strings are equal.” Day 3 done ✅ 362 more to go. #100DaysOfCode #365DaysOfCode #Python #LogicBuilding #StringComparison #ForElse #CodingJourney #LearnInPublic #AspiringDeveloper
Comparing Strings Character by Character in Python
More Relevant Posts
-
Most implementations of the State pattern in Python look very “clean”. Lots of small classes. A base interface. One class per state. But if you’ve ever worked with one in a real project, you know the downside: transitions are scattered, behaviour is hard to see in one place, and adding new states often means touching multiple files. In today’s video, I rebuild the State pattern in a very different way. Instead of relying on inheritance, I make the state machine explicit as data and use decorators to define transitions. The result is a small, reusable engine where the entire flow becomes visible at a glance. If you’re interested in writing Python that’s easier to reason about and extend, this is a pattern worth understanding. 👉 Watch here: https://lnkd.in/e9Y3xGNF. #python #softwaredesign #designpatterns #statemachine #cleancode
To view or add a comment, sign in
-
-
Most implementations of the State pattern in Python look very “clean”. Lots of small classes. A base interface. One class per state. But if you’ve ever worked with one in a real project, you know the downside: transitions are scattered, behaviour is hard to see in one place, and adding new states often means touching multiple files. In today’s video, I rebuild the State pattern in a very different way. Instead of relying on inheritance, I make the state machine explicit as data and use decorators to define transitions. The result is a small, reusable engine where the entire flow becomes visible at a glance. If you’re interested in writing Python that’s easier to reason about and extend, this is a pattern worth understanding. 👉 Watch here: https://lnkd.in/eg22yEHR. #python #softwaredesign #designpatterns #statemachine #cleancode
To view or add a comment, sign in
-
-
Day 41/100 – #100DaysOfCode 🚀 Solved LeetCode #2529 – Maximum Count of Positive Integer and Negative Integer (Python). Today I practiced simple counting logic to determine whether positive or negative numbers are more in the array. Approach: 1) Initialize two counters: neg = 0 and pos = 0. 2) Traverse the array element by element. 3) If the number is negative, increment neg. 4) If the number is positive, increment pos. 5) Return the maximum of neg and pos. Time Complexity: O(n) Space Complexity: O(1) Strengthening fundamentals with simple counting techniques 💪 #LeetCode #Python #DSA #Arrays #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
🅸🅽🅿🆄🆃 & 🅾🆄🆃🅿🆄🆃 🅵🆄🅽🅲🆄🆃🅸🅾🅽🆂 📦 Definition: In Python, Input is how we get data from the user into our program, and Output is how the program displays information back to the user. 🏠 Real-World Example: Think of a Vending Machine. Input: You press the buttons to tell the machine you want a "B3" (snack code). Output: The machine displays "Price: $1.50" on the screen and then drops your chips! 🍟 Without that type interaction, the machine is just a silent box of snacks. Here is how we do it in Python: 👉 input(): This function pauses the program and waits for you to type something. Python always treats input as a string by default, so if we want numbers, we will need to wrap it in an int() or float(). 👉 print(): It takes whatever is inside the parentheses and splashes it onto the screen for the world to see. #python #inputoutput #codingtips #pythonsimplified #datananalytics #learnpython
To view or add a comment, sign in
-
-
Day 50/100 – #100DaysOfCode 🚀 Solved LeetCode #28 – Find the Index of the First Occurrence in a String (Python). Today I practiced string matching using a brute-force approach to find the first occurrence of a substring. Approach: 1) Traverse the main string (haystack). 2) For each index, try to match the substring (needle). 3) Compare characters one by one. 4) If all characters match, return the starting index. 5) If mismatch occurs, break and move to the next index. 6) If no match is found, return -1. Time Complexity: O(n × m) Space Complexity: O(1) Understanding basic string matching techniques step by step 💪 #LeetCode #Python #DSA #Strings #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Day 42/100 – #100DaysOfCode 🚀 Solved LeetCode #2574 – Left and Right Sum Differences (Python). Today I practiced prefix sum logic to calculate the absolute difference between left and right sums for each index. Approach: 1) Calculate the total sum of the array. 2) Initialize leftSum = 0. 3) Traverse the array. 4) For each index, compute rightSum = total - leftSum - nums[i]. 5) Calculate the absolute difference and append it to the result. 6) Update leftSum by adding nums[i]. Time Complexity: O(n) Space Complexity: O(n) Understanding prefix sum helps solve problems efficiently 💪 #LeetCode #Python #DSA #Arrays #PrefixSum #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Day 46/100 – #100DaysOfCode 🚀 Solved LeetCode #2733 – Neither Minimum nor Maximum (Python). Today I practiced sorting and simple array logic to find any element that is neither the minimum nor the maximum in the array. Approach: 1) Sort the array. 2) Check the length of the array. 3) If the size is less than or equal to 2, return -1 (no valid element exists). 4) Otherwise, return the second element as it will be neither min nor max. Time Complexity: O(n log n) Space Complexity: O(1) Understanding how sorting simplifies boundary-based problems 💪 #LeetCode #Python #DSA #Arrays #Sorting #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
You inherit a base class with twelve methods. Your subclass needs three. You implement the other nine as raise NotImplementedError and call it a day. That's the smell ISP is trying to prevent. Interface Segregation says clients shouldn't depend on methods they don't use. A fat interface forces every implementer to carry weight that doesn't belong to them, and every caller to reason about behavior that isn't relevant. ⚖️ In my latest article I break down what small interfaces look like in Python: Protocols, ABCs, and the practical line between "split this" and "you're over-engineering." 🧩 https://lnkd.in/eKw68T9S What's the worst case of NotImplementedError you've had to live with? 👀 #Python #SoftwareEngineering #SOLID #DataScience #CleanCode
To view or add a comment, sign in
-
Day 8/365: Reversing a List Without Using Built‑in Functions 🔁🧠 Today I revisited a very common operation in programming: reversing a list. But instead of using Python’s built‑in methods like reverse() or slicing (n[::-1]), I implemented the logic manually using two pointers and a while loop. Why this exercise matters: 1. It forces me to think about how data is stored and changed in memory. 2. I practiced the two‑pointer technique, which is used a lot in array and string problems. 3. It reminded me that behind every “simple” built‑in method, there is an actual algorithm. Day 8 done ✅ 357 more to go. If you have similar two‑pointer problems (like checking palindromes or partitioning arrays), send them my way — I’d love to try them next. #100DaysOfCode #365DaysOfCode #Python #LogicBuilding #TwoPointers #ListManipulation #CodingJourney #LearnInPublic #AspiringDeveloper
To view or add a comment, sign in
-
-
🚨 This behavior of Python might look like a BUG… but it isn’t actually. a = 10 b = 10 print(id(a)) print(id(b)) 👉 Same memory location 😲 “Why do we have two variables pointing to the same memory location?!” Here comes the second one and things get interesting 👇 a = [1, 2, 3] b = a b.append(4) print(a) # [1, 2, 3, 4] 🔥 👉 Hmmm… why did ‘a’ change?! 💡 Explanation: ⭐ id() returns the identity of an object ⭐ Python reuses memory locations for immutable values ⭐ For mutable objects however, there is no copying, just pointers! ⚠️ The misconception: Most people believe ‘=’ copies objects in variables. 👉 Nope! ✅ Solution: b = a.copy() Now the two variables are separate ✅ 🔥 Consequence: It can seriously mess up your program’s logic! Ever got caught by such a ghost bug in Python? 👇 #CodeWithSujith #Python #Programming #Coding #PythonTricks #LearnPython #PythonBeginner #100DaysOfCode #DeveloperJourney
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
what about str1==str2? The code you wrote is the same what __len__ and __eq__ does or (==)