#Python tip: Don't write all 6 comparison methods! Define __eq__ and __lt__, then add @total_ordering: from functools import total_ordering @total_ordering class C: def __lt__(self, o): ... def __eq__(self, o): ... You get >, >=, <= for free!
Python tip: Simplify comparison methods with total_ordering
More Relevant Posts
-
Day 22/100 – #100DaysOfCode 🚀 Solved LeetCode #560 – Subarray Sum Equals K (Python). Today I learned how prefix sum and hashmap can be used together to efficiently count subarrays with a given sum. Approach: 1) Initialize count = 0 and curr_sum = 0. 2) Use a hashmap to store prefix sum frequencies (start with {0:1}). 3) Traverse the array and keep adding elements to curr_sum. 4) Check if (curr_sum - k) exists in the hashmap. 5) If it exists, add its frequency to count. 6) Update the hashmap with the current prefix sum. Time Complexity: O(n) Space Complexity: O(n) Understanding prefix sum + hashmap pattern is a game changer 💪 #LeetCode #Python #DSA #HashMap #PrefixSum #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Day 23/100 – #100DaysOfCode 🚀 Solved LeetCode #724 – Find Pivot Index (Python). Today I practiced prefix sum logic to find the index where the sum of elements on the left is equal to the sum on the right. Approach: 1) Calculate the total sum of the array. 2) Initialize left_sum = 0. 3) Traverse the array. 4) For each index, check if left_sum == total_sum - left_sum - nums[i]. 5) If true, return the current index as the pivot. 6) Otherwise, update left_sum by adding nums[i]. 7) If no pivot is found, return -1. Time Complexity: O(n) Space Complexity: O(1) Understanding how prefix sum helps optimize problems efficiently 💪 #LeetCode #Python #DSA #Arrays #PrefixSum #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Practiced Python functions today — *args, **kwargs, return types, and function types. The one thing that clicked — *args is for unknown number of values (stores as tuple), **kwargs is when you don't know the keys either (stores as dict). Simple but I was mixing them up before. Also learned there are actual types of functions — action, transformation, validation. https://lnkd.in/ducSzXzK #Python #LearningInPublic #DataAnalysis
To view or add a comment, sign in
-
Managing Python environments shouldn’t be a nightmare. But for many — it still is. Here’s how to keep things clean, fast, and production-ready in 2026: 🛠️ Use uv or poetry to manage environments & dependencies. Faster, safer, and simpler than old-school pip+venv. ⚡ Speed matters – use polars, numpy, and numba to vectorize heavy loops. Even small tweaks can give 10× performance wins. 🧼 Lint + Format + Type-check = non-negotiable Ruff for linting Black for formatting Pyright or Pyrefly for fast type-checks 💡 Bonus tip: Use Typer to build CLIs in minutes. So clean, it feels like magic. 💬 What’s one Python setup rule you wish you knew earlier? #Python #CodeQuality #Productivity #DevTools #DataEngineering
To view or add a comment, sign in
-
-
Quick update for Pandas users 🐼 Pandas 3.0 (Feb 2026) introduces a dedicated string data type as the default. Historically, Pandas stored string columns using NumPy’s ` object ` dtype, which wasn’t very efficient in terms of performance and memory usage. With Pandas 3.0, the new native string dtype becomes the default, bringing better efficiency and more consistent handling of text data. #DataScience #Python
To view or add a comment, sign in
-
-
Want to read a non-text file with #Python? Specify "rb", for "read binary," to avoid errors. t = open('myfile.zip').read() # error t = open('myfile.zip', 'rb').read() # works! What is t? A *byte* string, not a regular text string. >>> type(t) <class 'bytes'>
To view or add a comment, sign in
-
-
Finding Maximum Sum of k Consecutive Elements in Python Check out this simple sliding window trick to get the maximum sum of k consecutive numbers in an array. I used my own code to solve it: Python Copy code nums = [7,1,5,1,3,2] k = 3 n = len(nums) window = sum(nums[:k]) sums = window for i in range(k,n): window = window - nums[i-k] + nums[i] if sums < window: sums = window print(sums) How it works: Start with the sum of the first k numbers Slide the window by removing the first number and adding the next Keep track of the maximum sum Fast, simple, and efficient! #Python #DSA #Coding #Algorithms #LearnByDoing #DeveloperLife
To view or add a comment, sign in
-
What if you could forecast any CSV without opening a single Python file? Getting a quick forecast usually means writing a full Python script first. Even for a simple dataset, you often need to load the data, configure a model, and run the code. TimeCopilot removes that setup by allowing you to forecast any public CSV directly from the terminal. Just run timecopilot forecast with a URL to the dataset and it handles the rest. You can also specify the LLM to use or ask a business question in plain English from the same command. After running it, you get: • A forecast generated automatically • The best model selected for your data • A plain-English answer to your question #TimeSeries #Forecasting #Python #CLI
To view or add a comment, sign in
-
I recently attempted to create some YouTube videos that explain basic Python scripts. This straightforward code uses the emails' structural profile to extract emails from a natural text file. Here is the YT link: https://lnkd.in/gtjBqve3 GitHub repository link: https://lnkd.in/gCVR3iRe
To view or add a comment, sign in
More from this author
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