Day 11 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to find all pairs in a list that add up to a given target sum. The goal was to solve this efficiently without using nested loops. What the program does: • Takes a list of numbers and a target sum as input • Uses a set to track previously seen numbers • Finds pairs whose sum equals the target value • Returns all valid pairs How the logic works: An empty list pairs is created to store valid pairs A set seen is initialized to keep track of visited numbers The list is traversed one element at a time For each number, its complement is calculated as: complement = target_sum - num If the complement already exists in the set, a pair is formed The current number is added to the set for future comparisons After traversal, all valid pairs are returned Example: Numbers: [1, 2, 3, 4, 5, 6] Target Sum: 7 Output: Pairs with sum 7: [(3, 4), (2, 5), (1, 6)] Key learnings from Day 11: – Using sets for efficient lookups – Reducing time complexity from O(n²) to O(n) – Applying complement logic – Writing optimized and clean Python functions #100DaysOfCode #Day11 #Python #PythonProgramming #DataStructures #Algorithms #HashSet #ProblemSolving #CodingPractice #LearningInPublic #BTech #CSE #AIandML #VITBhopal #TechJourney
Python Pair Sum Finder with Set Optimization
More Relevant Posts
-
Day 29 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to find the maximum occurring character in a string. The goal was to count the frequency of each character and determine which one appears the most. What the program does: • Takes a string as input • Counts the frequency of each character using a dictionary • Tracks the character with the highest occurrence • Handles edge cases like an empty string • Returns the most frequent character How the logic works: 1)A dictionary char_counts is used to store character frequencies 2)The program iterates through each character in the string 3)For every character, its count is updated using .get(char, 0) + 1 4)After counting, another loop checks which character has the highest frequency 5)The character with the maximum count is stored and returned 6)If the string is empty, the function returns None Example: Input: "hello world" Output: Maximum occurring character → l Input: "program" Output: Maximum occurring character → r Why this approach works well: – Uses dictionary for fast lookups – Time Complexity: O(n) – Efficient way to track frequencies Key learnings from Day 29: – Using dictionaries for frequency counting – Iterating through strings efficiently – Handling edge cases in programs – Strengthening string manipulation logic #100DaysOfCode #Day29 #Python #PythonProgramming #StringManipulation #Algorithms #ProblemSolving #CodingPractice #DataStructures #InterviewPrep #LearnByDoing #ComputerScience #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
Day 18 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to find the longest word in a given sentence. The goal was to practice string manipulation and comparison logic. What the program does: • Takes a sentence as input • Splits the sentence into individual words • Compares the length of each word • Stores the longest word found • Prints the final result How the logic works: A sentence is defined as a string The sentence is split into a list of words using .split() A variable longest_word is initialized as an empty string The program loops through each word in the list If the length of the current word is greater than or equal to the stored word, it updates longest_word After the loop ends, the longest word is printed Example: Input sentence: The quick orange fox jumps over the lazy dog Output: Longest word: orange Key learnings from Day 18: – Using .split() for string processing – Comparing string lengths – Iterating through lists efficiently – Strengthening logical thinking skills #100DaysOfCode #Day18 #Python #PythonProgramming #StringManipulation #ProblemSolving #CodingPractice #LogicBuilding #LearnByDoing #ComputerScience #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
Day 30 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to check whether two strings are rotations of each other. The idea is that if one string is a rotation of another, it will appear as a substring of the original string concatenated with itself. What the program does: • Takes two strings as input • Checks if both strings have the same length • Concatenates the first string with itself • Verifies if the second string exists inside the concatenated string • Returns True if they are rotations, otherwise False How the logic works: 1)First, the program checks if both strings have equal length 2)If lengths differ, they cannot be rotations 3)The first string is concatenated with itself: temp = string1 + string1 4)If string2 appears as a substring inside temp, then it is a valid rotation 5)The function returns the result (True or False) Example: String 1:"abcde" String 2:"cdeab" Output: True (because "cdeab" is a rotation of "abcde") Another example: String 1:"abc" String 2:"acb" Output:False Why this approach works well: – Uses string concatenation trick – Avoids complex rotation checks – Time Complexity: O(n) Key learnings from Day 30: – Understanding string rotation logic – Using substring checks effectively – Solving problems with simple mathematical insights – Strengthening string manipulation skills #100DaysOfCode #Day30 #Python #PythonProgramming #StringAlgorithms #ProblemSolving #CodingPractice #DataStructures #Algorithms #InterviewPrep #LearnByDoing #ComputerScience #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
🚀 Day 51 of My Python Journey Today I solved Apply Discount to Prices on LeetCode. 🔍 Problem Overview: The problem gives a sentence containing words and prices (values starting with $). The task is to apply a given discount percentage to all valid prices and return the updated sentence. The updated prices must be formatted with exactly two decimal places. 🧠 Approach: • Split the sentence into individual words. • Check whether a word represents a valid price (starts with $ and the remaining characters are digits). • Convert the price to an integer and apply the discount formula. • Format the discounted price to two decimal places and rebuild the sentence. ⚡ Key Learnings: • Practiced string parsing and validation • Improved understanding of formatting numbers in Python • Strengthened problem-solving using conditional logic 📊 Complexity: • Time Complexity: O(n) • Space Complexity: O(n) Under the Guidance of : Rudra Sravan kumar and Manoj Kumar Reddy Parlapalli #Day51 #Python #LeetCode #DataStructures #Algorithms #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Day 17 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to count the number of vowels and consonants in a given string. The goal was to practice string traversal and character classification. What the program does: • Takes a string input from the user • Converts the string to lowercase for consistency • Checks each character individually • Counts vowels and consonants separately • Returns both counts How the logic works: A function count_vowels_consonants(string) is defined Two strings are created: – One containing all vowels (aeiou) – One containing all consonants Two counters are initialized to 0 The program loops through each character in the string If the character is in the vowel string, vowel count increases If the character is in the consonant string, consonant count increases The function returns both counts Example: Input: my name is SATISH KUMAR Output: Vowels: 7 Consonants: 12 Key learnings from Day 17: – Iterating through strings – Using conditional statements effectively – Handling case sensitivity with .lower() – Strengthening basic string manipulation skills #100DaysOfCode #Day17 #Python #PythonProgramming #StringManipulation #ProblemSolving #CodingPractice #LogicBuilding #LearnByDoing #ComputerScience #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
Day 14 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to find the common elements between two lists using sets. The goal was to solve the problem efficiently without using nested loops. What the program does: • Takes two lists as input • Converts both lists into sets • Uses set intersection to find common elements • Converts the result back into a list • Prints the common elements How the logic works: Two lists are defined with numeric values Both lists are converted into sets using set() The intersection operator & is used to find common elements The resulting set is converted back into a list The final list of common elements is displayed Example: List 1:[1, 2, 3, 4, 5] List 2:[4, 5, 6, 7, 8] Output: Common elements:[4, 5] Key learnings from Day 14: – Understanding set operations in Python – Using intersection (&) efficiently – Avoiding nested loops for better performance – Writing clean and optimized code #100DaysOfCode #Day14 #Python #PythonProgramming #SetOperations #DataStructures #ProblemSolving #CodingPractice #LearnByDoing #ComputerScience #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
Day 15 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to find the factorial of a number using recursion. The goal was to understand how recursion works and how a function can call itself to solve a problem. What the program does: • Takes an integer input from the user • Uses recursion to calculate the factorial • Handles the base case properly • Returns the final factorial value How the logic works: The function factorial(n) is defined If n == 0, the function returns 1 (Base Case) Otherwise, the function returns: n * factorial(n - 1) The function keeps calling itself with smaller values The recursion stops when it reaches the base case The final result is printed Example: Input: 7 Output: 5040 (Because 7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040) Key learnings from Day 15: – Understanding recursion and base cases – Learning how function calls are stacked – Breaking a problem into smaller subproblems – Strengthening fundamental algorithm concepts #100DaysOfCode #Day15 #Python #PythonProgramming #Recursion #Algorithms #ProblemSolving #CodingPractice #LearnByDoing #ComputerScience #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
Day 31 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to remove all vowels from a given string. The goal was to practice string traversal and filtering characters based on conditions. What the program does: • Takes a string as input • Checks each character in the string • Removes vowels (a, e, i, o, u) in both lowercase and uppercase • Builds a new string without vowels • Returns the final filtered string How the logic works: 1)A string vowels is defined containing all vowel characters 2)An empty string result_string is created to store the result 3)The program loops through each character in the input string 4)If the character is not a vowel, it is added to result_string 5)The process continues until all characters are checked 6)Finally, the filtered string is returned Example: Input: "Hello World" Output:"Hll Wrld" Another example: Input: "AEIOUaeiou" Output: "" (All characters are vowels) Why this approach works well: – Simple and easy to understand logic – Efficient single-pass traversal – Time Complexity: O(n) Key learnings from Day 31: – Iterating through strings – Filtering characters based on conditions – Working with string concatenation – Strengthening string manipulation fundamentals #100DaysOfCode #Day31 #Python #PythonProgramming #StringManipulation #ProblemSolving #CodingPractice #Algorithms #LearnByDoing #ComputerScience #ProgrammingJourney #DeveloperGrowth #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
#Day 47 of My Python & DSA Journey Today I solved the “Check if Binary String Has at Most One Segment of Ones” problem on LeetCode. 🔍 Problem Overview: Given a binary string containing only 0s and 1s, the task is to determine whether the string contains at most one continuous segment of 1s. If multiple separated segments of 1s exist, the result should be False. 🧠 Approach: I iterated through the string and tracked how many times a new segment of 1s starts. Whenever a 1 appears either at the beginning of the string or immediately after a 0, it indicates the start of a new segment. If more than one such segment is found, the condition fails. ⚡ Key Takeaways: • Strengthened understanding of string traversal • Practiced pattern recognition in binary strings • Improved logical problem-solving using Python 📊 Complexity: • Time Complexity: O(n) • Space Complexity: O(1) Consistently solving problems helps me improve my algorithmic thinking and coding efficiency every day. Looking forward to learning more and solving tougher challenges ahead! Under the Guidance of : Rudra Sravan kumar and Manoj Kumar Reddy Parlapalli #Day47 #Python #LeetCode #DataStructures #Algorithms #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Day 25 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to check whether a list is sorted in ascending order. The goal was to verify the order of elements by comparing each element with the next one. What the program does: • Takes a list as input • Compares each element with the next element • Checks if the list follows ascending order • Returns True if sorted, otherwise False How the logic works: 1)A function is_sorted(lst) is defined 2)A loop runs from the first element to the second-last element 3)Each element is compared with the next element (lst[i] and lst[i+1]) 4)If any element is greater than the next one, the list is not sorted 5)The function immediately returns False 6)If the loop finishes without finding such a case, the function returns True Example: List 1: [1, 2, 3, 4, 5] → Sorted → True List 2: [1, 3, 2, 4, 5] → Not Sorted → False List 3: [] → Sorted → True List 4: [7] → Sorted → True Why this works well: – Only one pass through the list – Time Complexity: O(n) – Works for empty and single-element lists Key learnings from Day 25: – Sequential comparison logic – Handling edge cases in lists – Writing efficient list validation functions – Strengthening algorithmic thinking #100DaysOfCode #Day25 #Python #PythonProgramming #Algorithms #ProblemSolving #DataStructures #CodingPractice #LogicBuilding #LearnByDoing #ComputerScience #InterviewPrep #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
Explore related topics
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