You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The student has attempted to use double hashing, which is a good approach for this problem.
The code includes comments and docstrings, which is good practice.
Areas for improvement:
Primary and Secondary Hashing: The primary hash function should use modulo to determine the bucket index, and the secondary hash function should use integer division to determine the index within the bucket. This is the opposite of what you have implemented. For example:
Primary hash: key % primaryBuckets
Secondary hash: key // primaryBuckets (or key // secondaryBuckets as in the reference)
Initialization of Storage: The self.hashset should be initialized as an array of size primaryBuckets (e.g., 1000) with None values. Then, when adding a key, you check if the primary bucket is None and initialize the secondary array accordingly. In your code, you initialized it as a list of booleans, which is incorrect.
Handling the Last Bucket: To handle key=1000000, the reference solution uses a primary bucket size of 1000, and for primary index 0, it creates a secondary array of size 1001. This is because 1000000 % 1000 = 0, and 1000000 // 1000 = 1000. So without the extra slot, the secondary index would be out of bounds. Your solution uses primary bucket size 1001, but the hashing functions are not aligned.
Type Consistency: The self.hashset array should store either None or lists (secondary arrays). You should not store booleans directly in the primary array (except for the special case in the reference, which is handled by having a larger secondary array for one bucket).
Edge Cases: Consider what happens when you add key=0, key=1000, key=1000000. Your current implementation will not handle these correctly.
Suggested corrections:
Change the primary bucket size to 1000 (not 1001) for the primary array.
Initialize self.hashset = [None] * primaryBuckets
For primary hash: key % primaryBuckets
For secondary hash: key // primaryBuckets
When initializing a secondary array, for primary index 0, create an array of size 1001 (to accommodate secondary indices from 0 to 1000), and for other indices, create an array of size 1000.
Alternatively, you can use a primary array of size 1001 and secondary arrays of size 1000, but then you need to adjust the hashing to avoid index out of bounds for key=1000000. The reference solution does it with primary size 1000 and a special case for index 0.
You have attempted to use the double hashing technique, which is a good approach for this problem.
You have provided docstrings and comments, which is helpful for understanding the code.
You have considered the edge case for the last bucket (index1000).
Areas for improvement:
Data Structure: Instead of initializing self.hashset as a list of booleans, it should be a list that can hold either None (or False) or a list (secondary array). In your code, you are trying to assign a list to self.hashset[index1] when it is False, but since self.hashset is initialized as a list of booleans, this will cause a type error. You should initialize self.hashset as a list of None values of length primaryBucket+1 (to include index1000).
Hash Functions: The primary hash function should be modulo 1000 to distribute keys evenly. Your primary hash function uses integer division by 1000, which is not standard. For example, key=1 would give index1=0, key=1000 would give index1=1, but key=1000000 would give index1=1000. The reference solution uses modulo for the primary hash and integer division for the secondary hash. This ensures that for a key k, the primary index is k % 1000 and the secondary index is k / 1000. This way, key=1000000 has primary index0 and secondary index1000.
Handling the First Bucket: The first bucket (index0) should have a secondary array of size 1001 to accommodate key=1000000. In your code, when you create the secondary array for a primary index, you need to check if it is index0 and allocate an array of size 1001, otherwise size 1000.
Type Consistency: When you check if a primary bucket is initialized, you are checking if not self.hashset[index1], which might work if you initialize with None, but not with False. You should initialize the primary array with None and then set it to a list when needed.
Removal and Containment: In the remove method, you are setting the value to False, which is correct, but you need to ensure that the secondary array exists before accessing it. Similarly, for contains, you need to check if the secondary array exists.
Edge Case for Key=1000000: In your current code, for key=1000000, you are setting self.hashset[1000] = True. This is incorrect because the primary array should have a secondary array for each primary index. Instead, for key=1000000, the primary index should be 0 (if using modulo) and secondary index1000. So you should not have a special case for index1000 in the primary array.
Suggested corrections:
Change the primary hash function to key % self.primaryBucket.
Change the secondary hash function to key // self.secondaryBucket.
Initialize self.hashset as [None] * (self.primaryBucket) for indices 0 to 999, but you need to handle index1000? Actually, since primaryBucket is 1000, the modulo operation gives indices from 0 to 999. However, key=1000000 would be stored at primary index0 and secondary index1000. So the primary array should be of size 1000, and for the first bucket, the secondary array should be of size 1001.
Alternatively, you can follow the reference solution exactly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.