22
votes
Accepted
Codewars: Reduce strings to one character
First I would choose a different representation of the colors during
the computation, to avoid the “expensive” COLORS-{a, b}).pop()
operation, which is executed
...
21
votes
Accepted
Adjacency List Graph representation on python
Here are two ways you could represent a graph with weighted edges in Python:
Represent a graph as a mapping from a node \$n\$ to a mapping from neighbouring node \$m\$ to the weight \$w\$ of the edge ...
21
votes
Accepted
Extraction of value from a dict-tree, given a path of arbitrary length
Recursive Approach Review
In this method you are performing too many look-ups.
First, you are looking up the 0th value of p two or three times. List indexing ...
20
votes
Accepted
The cleanest way to print the max value and its key from a dictionary
What would you do differently from this?
PEP 8: Variable names should be lowercase. People -> people.
As @FMc suggested in ...
19
votes
Accepted
Efficient renaming of dict keys from another dict's values - Python
I'd use a dict comprehension:
pretty_dict = {replacement_keys[k]: v for k, v in ugly_dict.items()}
This throws an error if ...
18
votes
Accepted
Merging two dictionaries together whilst adding keys but multiplying values
It's standard to indent Python with 4 spaces. Not following this standard only makes your life and the life of people that have to interact with you harder.
You can just use the optional default ...
17
votes
Accepted
Count the occurrence of each unique word in the file
Efficiency
I won't say much about efficiency - because without a clear use-case it will be hard to know whether possible changes would be worth the effort - but my main concern would be fact that you ...
16
votes
Accepted
List all possible permutations from a python dictionary of lists
The tool you need here is itertools.product:
...
16
votes
Accepted
Sorting dictionary alphabetically
I'm afraid your sorting is in vain because the normal dictionary does not guarantee that the items will be enumerated in the same order as you added them:
For purposes of enumeration, each item in ...
16
votes
Accepted
Implementing a rectangle class
PEP-8
Class names should be CapWords, so instead of rectangle you should have Rectangle.
...
15
votes
Removing entries from a dictionary containing bad words
The current time complexity is \$O(N * M)\$ where \$N\$ is the number of quotes and \$M\$ is the number of bad words. For every single word in a quote you are iterating over all the bad words to check ...
15
votes
Determine if one string is a permutation of the another
I would say yes, use an array.
Arrays afford very good memory locality, which is good for cache performance. They essentially are the most basic and efficient hash map if the key is the index.
You ...
15
votes
Accepted
14
votes
Accepted
Extending CGPoint to conform to Hashable
I can see no advantage of computing the hash value from Int(self.x) and Int(self.y). As you already noticed, truncating the ...
14
votes
Accepted
Creating zip but for dictionaries
A shorter version, functionally equivalent to yours:
def dict_zip(*dicts):
return {k: [d[k] for d in dicts] for k in args[0].keys()}
That's assuming all ...
14
votes
Accepted
Brick Wall Leetcode challenge
You could look at it in a different way, transform the wall of bricks to a wall of edges, in your example it would be
1, 3, 5
3, 4
1, 4
2
3, 4
1, 4, 5
The first, ...
14
votes
Removing entries from a dictionary containing bad words
This code is incorrect:
re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE)
If w contains any metacharacters, then this ...
14
votes
Accepted
Invert a dictionary to a dictionary of lists
You are right, you should not use a list comprehension just for its side-effects. It is not very readable and only confuses the reader of the code.
Instead, just make it ...
14
votes
Accepted
Determine if one string is a permutation of the another
Comparing two maps is linear, so that's not a big problem.
I don't think you've made particularly good use of maps though. As I see it, you have two choices. You could use an ...
14
votes
Accepted
Hashtable Implementation - C
I spotted some bugs:
The functions malloc and realloc might fail if there is not enough memory, and in this case they would ...
14
votes
Python program to find the most frequent letter in a text
If you are going to use the collections module at all (as you did for collections.defaultdict), then why not use ...
13
votes
Accepted
Get value from dictionary given a list of nested keys
Unless I'm missing something you'd like to implement, I would go for a simple loop instead of using recursion:
...
13
votes
Using GetHashCode in Equals
I think you are missing the point. Different objects can produce same hashcode. This can happen:
...
13
votes
Accepted
Income tax rate calculator in c++
This isn’t a bad idea for a learning project, and I’d say it’s implemented quite well.
Before I dive into the code itself, I’m going to do a high-level design review.
Design review
You’re using ...
12
votes
Accepted
Create dictionary with default immutable keys
When reading the title of the question, I wondered: "Why use a dict subclass when a regular class with __slots__ can do?". And judging by the interface, it seems ...
12
votes
Extraction of value from a dict-tree, given a path of arbitrary length
Use a library.
In particular, glom does an admirable job of this, and is actively developed.
...
12
votes
Accepted
Count word frequencies, and print them most-frequent first
Mainly this looks fine.
using namespace std; is a bad habit to get into due to potential name collisions. Instead prefer to type ...
11
votes
Inserting and fetching values slower on an unordered_map in C++ than on a dictionary in Python
Here are some things to improve your code. First, we'll address the performance issue, followed by a number of other things that could be improved.
Use const references where practical
The code ...
11
votes
Accepted
Product of dictionary values
You can use the fact that d.keys() and d.values() are always aligned. This gets rid of both the awkward way of constructing the ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
hash-map × 902python × 296
java × 172
performance × 136
c# × 127
c++ × 100
python-3.x × 74
c × 70
algorithm × 61
beginner × 51
strings × 44
array × 42
programming-challenge × 36
javascript × 34
reinventing-the-wheel × 27
python-2.x × 26
linked-list × 25
collections × 24
ruby × 23
sorting × 23
object-oriented × 19
vba × 19
.net × 17
hashcode × 16
parsing × 15