Codative π
Open in Telegram
Programmer | Orthodox Christian | Reader Resources: @codative_resources
Show more1 303
Subscribers
No data24 hours
+17 days
+530 days
Posts Archive
1 303
Repost from Henok | Neural Nets
48th ICPC World Finals was completed yesterday and Peking University got the first place.
Here are the problems for this year. I went over them and there is like 1 question I've tried and barely solved, they are hardπ°
https://scoreboard.icpc.global/2024/problemset.pdf
1 303
functools is crazy surprising me again with
accumulate a function for calculating the prefix sum of an array1 303
Have You Ever Got Stuck Comparing Values in Python Sorting? Meet cmp_to_key!
If you've been solving problems on LeetCode or coding challenges in general, youβve probably faced a situation where you need custom sorting logicβlike sorting based on multiple conditions. This is where Python's cmp_to_key comes in, and it can save the day!What is cmp_to_key?Imagine you have a comparison function that says,
If this value is smaller than that, return -1; if they're equal, return 0; if it's bigger, return 1.This is called a comparison function (cmp). But modern Python sorting methods like sorted() or .sort() use a key function instead.So how do you use your custom cmp function with sorted()? Simpleβconvert it using cmp_to_key! Example: Sorting Strings by Length, Then AlphabeticallyLetβs say you need to sort a list of words. First, by length. If two words have the same length, you sort them alphabetically.
from functools import cmp_to_key
def compare(a, b):
if len(a) == len(b):
return -1 if a < b else 1 # Compare alphabetically if lengths are equal
return len(a) - len(b) # Compare by length
words = ["apple", "bat", "banana", "pie", "cat"]
sorted_words = sorted(words, key=cmp_to_key(compare))
print(sorted_words) # Output: ['bat', 'cat', 'pie', 'apple', 'banana']
Without cmp_to_key, writing such sorting logic can get messy. But here, you just write your comparison function, and cmp_to_key handles the rest.Example: Custom Sorting NumbersLetβs sort a list of numbers where we want even numbers first, sorted in descending order, and odd numbers second, sorted in ascending order.
def custom_compare(a, b):
if a % 2 == b % 2: # Both even or both odd
return b - a if a % 2 == 0 else a - b # Descending for evens, ascending for odds
return -1 if a % 2 == 0 else 1 # Even before odd
numbers = [3, 1, 4, 10, 5, 6, 7]
sorted_numbers = sorted(numbers, key=cmp_to_key(custom_compare))
print(sorted_numbers) # Output: [10, 6, 4, 1, 3, 5, 7]
How Does It Work?
If a should come before b, return -1.If a and b are equal, return 0.If a should come after b, return 1.
#tips #leetcode #python
@codative1 303
Ever missed contests on many platform and wondered how to get them. There is a good solution for me which gives you all of the upcoming and previous contests on every competitive programming platform. It is called CodeClock and it lets u add the contest that you want to your calendar and you will never miss any again
1 303
Hey people do you want to join developers community arround the world here is one on slack and it is all about go I will try to find others that are more general. Since these kind of networks are really crucial for your career as software developer I highly recommend you to really join them and I will send much more like this soon
Gophers
@codative
1 303
I was crazy and thinking to make a library that can make github as a storage for syncing user data π what do you think?π
1 303
I was looking this week on the codenight forum go topic and there was a hot argument about using pure sql or using orm for handling models and database things what is your idea about this π
1 303
Repost from Techα’α΅
Practice system design problems like you practice DSA on Leetcode
Found this awesome platform on Reddit for practicing system design actively. Watching videos and reading books can get boring, but this site lets you dive right in. Give it a try! Here's the link.
Source: Reddit
@techinethio
#foundthis #systemdesign
1 303
Forgot to say happy new year π
In this year I have planned to make more engaging contents on the channel.
We will have:
- Live streams in different topics with special guests
- Virtual Contests
- Live Coding
- Interesting Projects
Wait for more share to your friends and get ready ππ
@codative
1 303
Hearing about leetcode from the leetcode master (happy new year my friends π)
https://youtu.be/h3nV3I9F2hA?si=5O8Tc4FAMxN7y4NY
Available now! Telegram Research 2025 β the year's key insights 
