LeetCode Progress | 263. Ugly Number (Python) Today I solved “Ugly Number” on LeetCode. Problem: An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5. Given an integer n, return True if it is an ugly number. My approach: I reduced the number by dividing out allowed prime factors. -- If n <= 0, it cannot be ugly -- Repeatedly divided n by 2, 3, and 5 while possible -- If the final value becomes 1, the number is ugly Optimal approach: This method itself is optimal. -- Only prime factors 2, 3, and 5 are removed -- If anything remains afterward, another prime factor exists -- Time complexity is very small because division reduces the number quickly What I learned: -- Prime factorization logic can be applied without explicitly generating primes -- Repeated division is an efficient way to validate number properties -- Handling edge cases (like non-positive numbers) is essential #leetcode #python #dsa #datastructures #algorithms #coding #programming #problemSolving #softwareengineering #computerscience #interviewprep #codinginterview #100daysofcode #pythonprogramming
Vishal Balasubramanian’s Post
More Relevant Posts
-
🎯 Result Processing Made Simple with Python! Built a clean student result checker that processes marks, calculates percentage, and applies passing rules — just like real university grading systems! 📊 Logic Breakdown: 🔹 Takes 5 subject marks as input 🔹 Converts string input into integers using list comprehension 🔹 Calculates total and percentage 🔹 Counts subjects with marks below 35 🔹 Applies conditional rules: - ✅ Pass: No failures + 50%+ overall - ✨ Pass with Grace: 1 failure + 60%+ overall - ❌ Fail: Everything else 💡 Key Concepts Used: - String splitting & type conversion - List comprehension - Loops with conditionals - Sum function - Multiple conditions in if-elif 🤔 Think About: How would you modify this for: - Different passing marks per subject? - Grading system with divisions (1st, 2nd, 3rd)? - Multiple semesters? 👇 Drop your thoughts below! #Python #Programming #StudentResult #Coding #Education #LearnPython #Developer #Tech #ProblemSolving #ListComprehension #ConditionalLogic #BeginnerProjects #BeginnerProjects #PythonProjects #CodingLife #SoftwareDevelopment #Day38
To view or add a comment, sign in
-
Python threads aren't what you think they are. 🤯 I was optimizing a CPU-bound task, expecting threads to speed things up. Instead, performance tanked. What was the deal? Python's Global Interpreter Lock (GIL) allows only one thread to execute Python bytecode at a time. For CPU-bound tasks, threading won't help. Use multiprocessing instead! 🧵🚫. Threads are great for I/O-bound tasks, though. 📡💡 💡 Key Takeaway: Use threading for I/O-bound tasks and multiprocessing for CPU-bound tasks to bypass the GIL. 🐍 Have you been bitten by the GIL? Share your story! 👇 #Django #Python #PythonProgramming #FastAPI #Coding #Programming
To view or add a comment, sign in
-
-
Tuples are one of those Python concepts everyone learns early — but many don’t fully use them. They are: • ordered • immutable • fast and memory-efficient You’ll often see tuples used for: - function returns - coordinates (x, y) - configuration values - data that should not change When you understand what tuples are and when to use them, your code becomes safer and more intentional. This infographic covers the essentials you’ll revisit again and again. Save it for a quick refresh later. #Python #LearnPython #PythonBasics #Programming #Coding #SoftwareEngineering #PythonDevelopers
To view or add a comment, sign in
-
-
Today I practiced on a classic problem — checking whether a year is a leap year. This task helped me understand how multiple conditions work together using logical operators. What I practiced: if / elif / else conditions Logical operators (and, or) Writing clean and efficient logic It’s interesting how a simple real-world rule can turn into a logical program. #Python #PythonLearning #CodingJourney #100DaysOfCode #Programming #BeginnerPython #DeveloperJourney #ProblemSolving #LearningToCode **Can this logic be written in a more optimized or cleaner way?
To view or add a comment, sign in
-
-
🐍 Day 29 — Exception Handling in Python Day 29 of #python365ai ⚠️ Errors happen. Exception handling allows your program to respond gracefully instead of crashing. Example: try: x = int(input("Enter a number: ")) print(10 / x) except: print("Something went wrong") 📌 Why this matters: Robust programs anticipate errors — especially user input and real-world data. 📘 Practice task: Handle division by zero using try and except. #python365ai #ExceptionHandling #PythonBasics #Programming #LearnPython
To view or add a comment, sign in
-
-
🎯 Mastering Default Arguments in Python — Write Cleaner, More Flexible Functions! Just explored how default parameters in Python can make functions more intuitive and reduce repetitive code. Check out these practical examples: 🔹 power() – Calculate squares by default, or any exponent when specified. 🔹 greet() – Personalize greetings while keeping a friendly default. 🔹 calculate_bill() – Apply default tax and discount rates, but customize when needed. Default arguments help create functions that are both user-friendly and flexible, promoting cleaner code and better maintainability. Whether you’re building utilities, APIs, or business logic, this feature is a game-changer! 💡 #Python #Programming #Coding #SoftwareDevelopment #PythonTips #LearnToCode #Functions #Developer #Tech #CleanCode #CodingLife #Day31
To view or add a comment, sign in
-
Day 12 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to convert a decimal number into its binary representation without using built-in functions like bin(). The goal was to understand how number base conversion works internally. What the program does: • Takes a decimal number as input • Uses division and modulo operations • Builds the binary representation manually • Returns the final binary number as a string How the logic works: An empty string binary is initialized to store the result A while loop runs as long as the number is greater than 0 The remainder when dividing by 2 (n % 2) is calculated The remainder is added to the front of the binary string The number is reduced using integer division (n // 2) The loop continues until the number becomes 0 The final binary string is returned Example: Input - 34 Output - Binary representation of 34 is: 100010 Key learnings from Day 12: – Understanding number system conversion – Using modulo and integer division effectively – Building logic step-by-step without built-in shortcuts – Strengthening fundamental programming concepts #100DaysOfCode #Day12 #Python #PythonProgramming #NumberSystems #BinaryConversion #ProblemSolving #CodingPractice #LearnByDoing #ComputerScience #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
Stop Validating Data Manually in Your API #programming #python #coding Learn how to use Path Parameters in FastAPI with automatic type validation. By adding a simple Python type hint (int) to your route function, FastAPI automatically creates a dynamic URL structure and validates incoming requests. If a client tries to access /users/abc, the server rejects it with a 422 error automatically, protecting your code from crashing without any manual if statements.
To view or add a comment, sign in
-
Python Loops Made Simple! 🔄🐍 Why repeat yourself when you can automate? Python loops are the secret to writing efficient code in fewer lines. 1. FOR Loop (The Iterator) Use this when you want to go through a list or a fixed range. Example: for i in range(3): print("Python is fun!") (This will print the message 3 times) 2. WHILE Loop (The Condition Keeper) Use this when you want to keep running as long as a condition is True. Example: count = 1 while count <= 3: print("Loading...") count += 1 (Repeats until count reaches 3) Automation starts with mastering these two! 💻✨ Which one do you use most? Let me know in the comments! 👇 #Python #Coding #Programming #Automation #TechTips #LearnToCode #anshulyadav45
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