-
-
Save qmmr/ca6896428b4f9378d738ea8933fc048b to your computer and use it in GitHub Desktop.
""" | |
pirple.com/python | |
Homework Assignment #4: Lists | |
Create a global variable called myUniqueList. It should be an empty list to start. | |
Next, create a function that allows you to add things to that list. | |
Anything that's passed to this function should get added to myUniqueList, | |
unless its value already exists in myUniqueList. | |
If the value doesn't exist already it should be added and the function should return True. | |
If the value does exist, it should not be added, and the function should return False; | |
Finally, add some code below your function that tests it out. | |
It should add a few different elements, showcasing the different scenarios, | |
and then finally it should print the value of myUniqueList to show that it worked. | |
Extra Credit: | |
Add another function that pushes all the rejected inputs into a separate global array called myLeftovers. | |
If someone tries to add a value to myUniqueList but it's rejected (for non-uniqueness), | |
it should get added to myLeftovers instead. | |
""" | |
myUniqueList = [] | |
myLeftovers = [] | |
def addToList(thing): | |
if thing in myUniqueList: | |
addToLeftovers(thing) | |
return False | |
else: | |
myUniqueList.append(thing) | |
return True | |
def addToLeftovers(thing): | |
myLeftovers.append(thing) | |
# Test the addToList function | |
print(myUniqueList) # [] | |
print(addToList("dog")) # Returns True | |
print(myUniqueList) # ['dog'] | |
print(myLeftovers) # [] | |
# Adding the element that already exists | |
print(addToList("dog")) # Returns False | |
print(myUniqueList) # ['dog'] | |
print(myLeftovers) # ['dog'] | |
# Adding a new element | |
print(addToList("cat")) # Returns True | |
print(myUniqueList) # ['dog', 'cat'] | |
print(myLeftovers) # ['dog'] |
Your work one the coding looks effortless. This is my first time ever for coding and I would like more clarification from you if it is possible.
Thank you for offering your time to help me. I did not really understand the first part of the homework (Next, create a function that allows you to add things to that list. Anything that's passed to this function should get added to myUniqueList, unless its value already exists in myUniqueList. If the value doesn't exist already, it should be added and the function should return True. If the value does exist, it should not be added, and the function should return False) and my attempt on it, myUniqueList = [] def Add_To_myUniqueList(): if items in myUniqueList: myUniqueList.append(thing) return True else: return False print(myUniqueList) print(Add_To_myUniqueList("Book")) print(myUniqueList) On Sun, Dec 27, 2020 at 9:04 PM JoshTurk2020 [email protected] wrote:
…
@JoshTurk2020 commented on this gist. ------------------------------ What exactly did you want to know? On Sun, Dec 27, 2020, 12:58 PM NadaBatt @.> wrote: > @NadaBatt commented on this gist. > ------------------------------ > Good evening Josh Turk, > well I was asking Marcin Kumorek, but since you have answered I would like > more explanation on the assignment if you could. > > Nada Battyour > > On Sun, Dec 27, 2020 at 8:38 PM JoshTurk2020 @.> > wrote: > > > @JoshTurk2020 commented on this gist. > > ------------------------------ > > Are you asking me? > > > > --Josh > > > > "A language is a dialect with an army and a navy." Max Weinreich > > > > James Joshua Pennington, PhD > > Slavic and Eastern European > > Languages and Literatures > > > > > > On Sun, Dec 27, 2020 at 12:29 PM NadaBatt @.***> > > wrote: > > > > > @NadaBatt commented on this gist. > > > ------------------------------ > > > > > > Your work one the coding looks effortless. This is my first time ever > for > > > coding and I would like more clarification from you if it is possible. > > > > > > — > > > You are receiving this because you commented. > > > Reply to this email directly, view it on GitHub > > > < > > > https://gist.github.com/ca6896428b4f9378d738ea8933fc048b#gistcomment-3574369 > > >, > > > or unsubscribe > > > < > > > https://github.com/notifications/unsubscribe-auth/AOGBDTKAMRZWPB2UFQXBN3TSW5VIFANCNFSM4MIPR2FQ > > > > > > . > > > > > > > — > > You are receiving this because you were mentioned. > > Reply to this email directly, view it on GitHub > > < > https://gist.github.com/ca6896428b4f9378d738ea8933fc048b#gistcomment-3574377 > >, > > or unsubscribe > > < > https://github.com/notifications/unsubscribe-auth/ASIUQVOERTGRFNAI4L3O4G3SW5WIVANCNFSM4MIPR2FQ > > > > . > > > > — > You are receiving this because you were mentioned. > Reply to this email directly, view it on GitHub > < https://gist.github.com/ca6896428b4f9378d738ea8933fc048b#gistcomment-3574383 >, > or unsubscribe > < https://github.com/notifications/unsubscribe-auth/AOGBDTNFOKODYPX6P5FTNLDSW5YTRANCNFSM4MIPR2FQ > > . > — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://gist.github.com/ca6896428b4f9378d738ea8933fc048b#gistcomment-3574390, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASIUQVL5HGWBIHFBDVBH3TTSW5ZMTANCNFSM4MIPR2FQ .
Is it correct?
Hi Nada - Each new item that you append to the list should be unique, otherwise it would be a corpus (where you have multiple tokens for given types). We are just making a list of types (unique words). Therefore you want to create two lists using [] and one function that sorts the words.
I used:
myUniqueList = []
myLeftovers = []
def addList(newThing):
if newThing in myUniqueList:
myLeftovers.append(newThing)
return False
else:
myUniqueList.append(newThing)
return True
This function (def) sorts whether the word belongs to the list (unique words) or to the corpus (repeated words). You can then create a separate function later to count all the tokens of the corpus list (myLeftovers) if you want to do corpus analysis (for frequency counts, etc.). Of course, since you already have 1 token in the LIST of unique words, you would need to do a "+1" calculation to ensure it is counted.
Does that answer your questions?
Hello I need some help when I run this code
myUniqueList = []
myLeftovers = []
def addList(newThing):
if newThing in myUniqueList:
myLeftovers.append(newThing)
return False
else:
myUniqueList.append(newThing)
return True
print(myUniqueList) # []
print(addList("Meliodas")) # returns 'True' since it's a new item
print(addList("Escanor")) # returns 'True' since it's a new item
print(addList("Meliodas")) # returns 'False' since it's already been added
print(myUniqueList) # This includes the new entries
print(myLeftovers) # This includes any repeated entries
I get
[]
False
False
False
[]
[]
Can you explain why the list dont get the words added and why I get Syntax Error on the else statement
Hello I need some help when I run this code
myUniqueList = []
myLeftovers = []def addList(newThing):
if newThing in myUniqueList:
myLeftovers.append(newThing)
return False
else:
myUniqueList.append(newThing)
return Trueprint(myUniqueList) # []
print(addList("Meliodas")) # returns 'True' since it's a new item
print(addList("Escanor")) # returns 'True' since it's a new item
print(addList("Meliodas")) # returns 'False' since it's already been added
print(myUniqueList) # This includes the new entries
print(myLeftovers) # This includes any repeated entriesI get
[]
False
False
False
[]
[]Can you explain why the list dont get the words added and why I get Syntax Error on the else statement
Hello I need some help when I run this code
myUniqueList = []
myLeftovers = []def addList(newThing):
if newThing in myUniqueList:
myLeftovers.append(newThing)
return False
else:
myUniqueList.append(newThing)
return Trueprint(myUniqueList) # []
print(addList("Meliodas")) # returns 'True' since it's a new item
print(addList("Escanor")) # returns 'True' since it's a new item
print(addList("Meliodas")) # returns 'False' since it's already been added
print(myUniqueList) # This includes the new entries
print(myLeftovers) # This includes any repeated entriesI get
[]
False
False
False
[]
[]Can you explain why the list dont get the words added and why I get Syntax Error on the else statement
Hey, i've just had a look at your code, and it worked fine. I got the below output:
[]
True
True
False
['Meliodas', 'Escanor']
['Meliodas']
When i copied your code and ran it, i got a few indentation errors, so i tidied it up a bit, i dont know if this helps.
myUniqueList = []
myLeftovers = []
def addList(newThing):
if newThing in myUniqueList:
myLeftovers.append(newThing)
return False
else:
myUniqueList.append(newThing)
return True
print(myUniqueList) # []
print(addList("Meliodas")) # returns 'True' since it's a new item
print(addList("Escanor")) # returns 'True' since it's a new item
print(addList("Meliodas")) # returns 'False' since it's already been added
print(myUniqueList) # This includes the new entries
print(myLeftovers) # This includes any repeated entries
Hello I need some help when I run this code
myUniqueList = []
myLeftovers = []def addList(newThing):
if newThing in myUniqueList:
myLeftovers.append(newThing)
return False
else:
myUniqueList.append(newThing)
return Trueprint(myUniqueList) # []
print(addList("Meliodas")) # returns 'True' since it's a new item
print(addList("Escanor")) # returns 'True' since it's a new item
print(addList("Meliodas")) # returns 'False' since it's already been added
print(myUniqueList) # This includes the new entries
print(myLeftovers) # This includes any repeated entriesI get
[]
False
False
False
[]
[]Can you explain why the list dont get the words added and why I get Syntax Error on the else statement
myUniqueList = []
myLeftovers = []
def addToList(thing):
if thing in myUniqueList:
addToLeftovers(thing)
return False
else:
myUniqueList.append(thing)
return True
def addToLeftovers(thing):
myLeftovers.append(thing)
#Test the addToList function
print(myUniqueList) # []
print(addToList("dog")) # Returns True
print(myUniqueList) # ['dog']
print(myLeftovers) # []
#Adding the element that already exists
print(addToList("dog")) # Returns False
print(myUniqueList) # ['dog']
print(myLeftovers) # ['dog']
#Adding a new element
print(addToList("lion")) # Returns True
print(myUniqueList) # ['dog','lion']
print(myLeftovers) # ['dog']
Thank you, I don't know the word "in" so I can't do the homework
You are a star, thanks for this!