Hello there?
I am extremely new to python, and am trying to attend the lectures about hashing. Please cd you ceck the following code and error, and just tell me how to make it work. Please I am not that advanced user of python. I am very basic.

def create(smallest, largest):
  intSet = []
  for i in range(smallest, largest+1): intSet.append(None)
  return intSet

def insert(intSet, e):
  intSet[e] = 1

def member(intSet, e):
  return intSet[e] == 1

def hashChar(c):
##  (c) is a char
##  function returns a different integer in the range 0-255
##  for each possible value of c 
  return (c)

def cSetCreate():
  cSet = []
  for i in range(0, 255): cSet.append(None)
  return cSet

def cSetInsert(cSet, e):
  cSet[hashChar(e)] = 1

def cSetMember(cSet, e):
  return cSet[hashChar(e)] == 1


cSetInsert(5,4)

Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    cSetInsert(5,4)
  File "C:/Users/bangash/Documents/python files/1lec10.py", line 24, in cSetInsert
    cSet[hashChar(e)] = 1
TypeError: 'int' object does not support item assignment

Any help would be much appreciated.

Recommended Answers

All 3 Replies

Everything is a function except cSetInsert(5,4) so that gets executed. cSetInsert tries to use "5" as the argument, "cSet". However the function treats it as a list: cSet[hashChar(e)]

I think you intended to call cSetCreate() first and pass in the corresponding returned cSet. I don't know what you think "5" is for.

Also, range(0,255) will only create a 255-entry list. If you want all 256 entries, indexed 0 to 255, use range(256).

Thanks very much. I will try it that way. I was following the lectures step by step. I entered the codes exactly as the profesor does. His one works fine but my one always comes up with an error. As i earlier mentioned that I am very basic, and am trying to follow the professor. I am using python 2.7 as he instructed in the notes. those are online courses that i am following, so no way that Ican ask the professor. I follow the codes step bu step, his one always works but my one does have some error everytime. Then I spend nights reading articles to solve my error, but invain. In the end, I am forced to ask for help,,tha way how I did now. Now, here is another code which has got a error,,,professor one's work fine but my one does have error in it. Do you want me to show the code to you so that U might check it up???? I will try post to post that one too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.