2022 Developer Survey is open! Take survey.
84 votes
Accepted

Counting all the vowels in a passage of text

Change to lower once: text.lower() instead of t.lower() inside loop. Use t in vowels to ...
user avatar
  • 703
40 votes
Accepted

Java enum containing a hash map for look up

Your code is very neat and clean, there is only one thing that I would change: private static Map<String, String> mMap; to ...
user avatar
40 votes
Accepted

Incrementing Integers in Map

You have several different options for this: Guava Google's Guava Library introduces the idea of a Multiset which is capable of counting the occurrences, and also provides a couple of other features....
user avatar
24 votes

Java enum containing a hash map for look up

You could change: if (mMap.containsKey(code)) { return mMap.get(code); } return null; to: return mMap.get(code); since ...
user avatar
  • 29.7k
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 ...
user avatar
  • 22.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 ...
user avatar
  • 49.2k
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 ...
user avatar
  • 30.4k
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 ...
user avatar
  • 5,487
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 ...
user avatar
  • 2,733
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 ...
user avatar
  • 41.6k
17 votes

HashTable implementation

Implementing a hash table This hash table implementation is a bit limited: it supports only String keys and int values. It would be good to generalize it. When ...
user avatar
  • 107k
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 ...
user avatar
  • 7,086
16 votes
Accepted

List all possible permutations from a python dictionary of lists

The tool you need here is itertools.product: ...
user avatar
  • 49.2k
15 votes

Java enum containing a hash map for look up

This may be just a matter of preference, but it would seem more useful to me to have the map store the instance of Maps rather than the description directly, ...
user avatar
  • 411
15 votes
Accepted

Check for null/empty in dictionary

Each of these: ...
user avatar
  • 10.6k
15 votes

DuplicateDictionary - A dictionary-like class which allows duplicates

Such a data structure already exists, and is called ILookup<TKey, TElement>. It can be created using the ToLookup ...
user avatar
  • 1,167
15 votes
Accepted

Dictionary GetValueOrDefault

I'm pretty sure there's no better way to do this but I thought a consensus on here might be nice. Let me know what you think. At first glance it seems to be ok but one could assume that he/she is ...
user avatar
  • 49.9k
15 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 ...
user avatar
  • 43.8k
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 ...
user avatar
  • 17k
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 ...
user avatar
  • 5,681
15 votes
Accepted

Sort int array by frequency then value

Headers ...
user avatar
  • 91.5k
15 votes
Accepted

Implementing a rectangle class

PEP-8 Class names should be CapWords, so instead of rectangle you should have Rectangle. ...
user avatar
  • 30.4k
14 votes

Is this the right way to implement a hashmap?

No, this is not the correct way to implement a hash map in Java, and especially not to implement the java.util.Map interface. You aren't even explicitly ...
user avatar
  • 12.4k
14 votes
Accepted

Convert a list of dictionaries to a dictionary of dictionaries

While using methods with side effects in list- or dict-comprehensions is generally frowned upon, in this case you could make use of dict.pop to get the ...
user avatar
  • 1,704
14 votes

HashTable implementation in C++

First and foremost there is std::unordered_map. If you just need a hash map, use it. What's wrong Your hash is an int, if your string consists of negative chars ...
user avatar
  • 762
14 votes
Accepted

Adding to a dictionary and skipping duplicate keys

Generating and handling exceptions is considered to be an expensive operation, so if(! x.ContainsKey()) is better. Yeah, the code example I see in MSDN uses try/...
user avatar
  • 6,530
14 votes
Accepted

Simple C# HashTable Implementation

A few small points: public class HashTable<T, TU> Use more mnemonic names, like TKey, TValue here. ...
user avatar
  • 14.6k
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 ...
user avatar
  • 22.3k
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, ...
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 ...
user avatar
  • 1,159

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