On Creating a hash function

Welcome to the Get Help category!

This is where you can ask questions about your code. Some important things to remember when posting in this category :slight_smile:

  • Learn how to ask a good question and get a good answer!
  • Remember to include a link to the exercise you need help with!
  • If someone answers your question, please mark their response as a solution :white_check_mark:
  • Once you understand a new concept, come back and try to help someone else!

so I was on the Creating a Hash Function on code academy when I stumbled on a function I couldn’t do. can someone help tell me why?

https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-102/modules/hash-maps/lessons/hash-maps-implementation/exercises/creating-the-hash-function

the first one doesn’t work, says
Output:
File “script.py”, line 6
def hash(self, key):
^
IndentationError: unindent does not match any outer indentation level

what am I doing wrong? its just the same as the solution

here’s the code

I tried writing code in the lesson but nothing happens when I hit “Run”–no error and the box for item 1 isn’t checked off.

I don’t see any errors with your GH code.

so why does it only work when I put in the full code, is the first question already correct? (At least I think so)

I think I’m misunderstanding here. Are you referring to the code that you have on GH? That code doesn’t throw any errors. If the particular lesson you linked to above is throwing an error I suggest copying your code and then resetting the lesson and trying again. Sometimes that works.

File “script.py”, line 7

                      ^

SyntaxError: unexpected EOF while parsing

This is the error I get after typing this in:

happens on 3 more too, but works only when you solve the entire problem fro some reason, gets stuck on the first question if not

It worked when I put in this code, ty for help

class HashMap:
def init(self, array_size):
self.array_size = array_size
self.array = [None for item in range(array_size)]

def hash(self, key):
return sum(key.encode())

With modular hashing, the hash function is simply h(k) = k mod m for some m (usually, the number of buckets). The value k is an integer hash code generated from the key. If m is a power of two (i.e., m=2p), then h(k) is just the p lowest-order bits of 200 thread count k.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.