#Simangaliso Collin Mavuso collinmavuso98@gmail.com #QUESTION 1 #We are constructing a key-value pair without using Dictionary type #My logic was to implement list of lists in python were we hold key on 0 index and value on 1 index #in that way we can call our values and manipulate it through our api methods below dictList=[] #I used a class with a oop approach in mind class Dictionary: # we defining call APIs for our Dictionary below def add(key,values):# we assumed that key-value items can be duplicates dictList.append([key])# append key first then attach value after pos=dictList.index([key]) dictList[pos].append(values) print("Entry updated") def get(key):# Gets value with given key for k,v in enumerate(dictList): if key in v[0]: return v[1] #returns 1-index of value return "Key-values do not exists" def delete(key): # deletes specific key from Dictionary for k,v in enumerate(dictList): if key in v: dictList.remove(v) return "Item successfully deleted" return "Item not in list" def contains(key): # Boolean Checks if key exists which holds value for k,v in enumerate(dictList): if key in v[0]: return True return False def length():# We get length of our Dictionary object return len(dictList) #TESTCASES Dictionary.add("name","Simangaliso") Dictionary.add("surname","Mavuso") Dictionary.add("middlename","Collin") print(Dictionary.length()) print(Dictionary.get("middlename")) print(Dictionary.contains("surname")) print(Dictionary.delete('surname')) print(dictList)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question