Skip to main content
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 ...
Martin R's user avatar
  • 23.3k
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 ...
Gareth Rees's user avatar
  • 49.6k
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 ...
AJNeufeld's user avatar
  • 33.9k
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 ...
Marc's user avatar
  • 5,614
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 ...
ggorlen's user avatar
  • 3,948
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 ...
Peilonrayz's user avatar
  • 43.4k
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 ...
VisualMelon's user avatar
  • 7,431
16 votes
Accepted

List all possible permutations from a python dictionary of lists

The tool you need here is itertools.product: ...
Gareth Rees's user avatar
  • 49.6k
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 ...
t3chb0t's user avatar
  • 44.3k
16 votes
Accepted

Implementing a rectangle class

PEP-8 Class names should be CapWords, so instead of rectangle you should have Rectangle. ...
AJNeufeld's user avatar
  • 33.9k
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 ...
alecxe's user avatar
  • 17.3k
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 ...
Josiah's user avatar
  • 6,006
15 votes
Accepted

Sort int array by frequency then value

Headers ...
Loki Astari's user avatar
  • 95.7k
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 ...
Martin R's user avatar
  • 23.3k
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 ...
kyrill's user avatar
  • 1,570
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, ...
omaraloraini's user avatar
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 ...
wchargin's user avatar
  • 1,169
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 ...
Graipher's user avatar
  • 41k
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 ...
Jerry Coffin's user avatar
  • 33.6k
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 ...
Gareth Rees's user avatar
  • 49.6k
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 ...
200_success's user avatar
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: ...
ChatterOne's user avatar
  • 2,805
13 votes

Using GetHashCode in Equals

I think you are missing the point. Different objects can produce same hashcode. This can happen: ...
Nikita B's user avatar
  • 13k
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 ...
indi's user avatar
  • 12.4k
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 ...
301_Moved_Permanently's user avatar
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. ...
Cireo's user avatar
  • 239
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 ...
user673679's user avatar
  • 11.8k
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 ...
Edward's user avatar
  • 66.5k
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 ...
Graipher's user avatar
  • 41k

Only top scored, non community-wiki answers of a minimum length are eligible