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 ...
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
...
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....
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 ...
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
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 ...
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:
...
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, ...
15
votes
Accepted
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 ...
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 ...
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 ...
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
15
votes
Accepted
Implementing a rectangle class
PEP-8
Class names should be CapWords, so instead of rectangle you should have Rectangle.
...
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 ...
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 ...
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 ...
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/...
14
votes
Accepted
Simple C# HashTable Implementation
A few small points:
public class HashTable<T, TU>
Use more mnemonic names, like TKey, TValue here.
...
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
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
hash-map × 858python × 287
java × 164
performance × 127
c# × 117
c++ × 98
python-3.x × 70
c × 64
algorithm × 55
beginner × 49
strings × 43
array × 38
programming-challenge × 33
javascript × 31
python-2.x × 26
linked-list × 24
reinventing-the-wheel × 24
ruby × 23
collections × 22
sorting × 21
object-oriented × 19
comparative-review × 16
vba × 15
parsing × 14
csv × 14