Skip to content

Instantly share code, notes, and snippets.

@qmmr
Last active June 24, 2021 04:44
Show Gist options
  • Save qmmr/ca6896428b4f9378d738ea8933fc048b to your computer and use it in GitHub Desktop.
Save qmmr/ca6896428b4f9378d738ea8933fc048b to your computer and use it in GitHub Desktop.
Python Homework Assignment #4: Lists
"""
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']
@Amit-Dhussa
Copy link

Thank you so much, sir, for helping me to understand the concept of this question.

@JoshTurk2020
Copy link

The addToLeftovers function is superfluous. Here is how I solvedit:

myUniqueList = []
myLeftovers = []

def addList(newThing):
if newThing in myUniqueList:
myLeftovers.append(newThing)
return False
else:
myUniqueList.append(newThing)
return True

def add2Leftovers(newThing): # This is an uncessary step, right?

I just cut it out and made the addList function filter to both.

myLeftovers.append(newThing)

print(myUniqueList) # []
print(addList("Sekiro")) # returns 'True' since it's a new item
print(addList("Ghosts of Tsushima")) # returns 'True' since it's a new item
print(addList("Sekiro")) # returns 'False' since it's already been added
print(myUniqueList) # This includes the new entries
print(myLeftovers) # This includes any repeated entries

Can someone explain why the answer includes the superfluous "addToList" function?

@UcheAnyiam
Copy link

UcheAnyiam commented Dec 10, 2020

You are a star, thanks for this!

@NadaBatt
Copy link

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.

@JoshTurk2020
Copy link

JoshTurk2020 commented Dec 27, 2020 via email

@NadaBatt
Copy link

NadaBatt commented Dec 27, 2020 via email

@JoshTurk2020
Copy link

JoshTurk2020 commented Dec 27, 2020 via email

@NadaBatt
Copy link

NadaBatt commented Dec 27, 2020 via email

@NadaBatt
Copy link

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 notifications@github.com 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?

@JoshTurk2020
Copy link

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?

@NadaBatt
Copy link

NadaBatt commented Dec 27, 2020 via email

@mackan84
Copy link

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

@UcheAnyiam
Copy link

UcheAnyiam commented Feb 21, 2021

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 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

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

@aajupro
Copy link

aajupro commented Feb 26, 2021

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

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']

@anonymousincognito
Copy link

Thank you, I don't know the word "in" so I can't do the homework

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment