Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lecture "Organising information: ordered structures", exercise 2 #13

Open
essepuntato opened this issue Nov 23, 2018 · 17 comments
Open
Labels
Exercise The exercises that are introduced in the lectures.

Comments

@essepuntato
Copy link
Contributor

essepuntato commented Nov 23, 2018

Consider to have a stack obtained by processing, one by one, the elements included in the list of the first exercise, i.e. my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]). Describe the status of my_stack after the execution of each of the following operations: my_stack.pop(), my_stack.pop(), my_stack.append("Voldemort").

@essepuntato essepuntato added the Exercise The exercises that are introduced in the lectures. label Nov 23, 2018
@hizclick
Copy link

hizclick commented Nov 23, 2018

excuting my_stuck.pop** will remove the last element of the list, in this case Serveus.
my_stack.pop() again remove "Ron" from the stack.
my_stack.append("Voldemort") will add "Volemort" to the right of the stack.
so the final output of the stack my_stack will be
deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])

@delfimpandiani
Copy link

my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])

# currently my_stack contains five elements:
#        "Severus"])
#        "Ron",
#        "Hermione",
#        "Harry",
# deque(["Draco",

print(my_stack) # check my stack looks right

my_stack.pop() # this removes the element on top of the stack
# currently my_stack contains four elements:
#        "Ron"])
#        "Hermione",
#        "Harry",
# deque(["Draco",

my_stack.pop() # this, again, removes the element on top of the stack
# currently my_stack contains three elements:
#        "Hermione"])
#        "Harry",
# deque(["Draco",

my_stack.append("Voldemort") # this adds a new item ot the stack, on top
# currently my_stack contains four elements:
#        "Voldemort"])
#        "Hermione",
#        "Harry",
# deque(["Draco",

@bluebell94
Copy link

my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) - initial state of the stack

by executing my_stack.pop() for the 1st time- we have to eliminate the lastly added element on the stack in this case "Severus",
so after that the stack will look like this ["Draco", "Harry", "Hermione", "Ron"]

by executing my_stack.pop() for the 2nd time-we have to follow the same procedure, however in this case on top of the stack is "Ron", so we eliminate this element and now our stock should like like this ["Draco", "Harry", "Hermione"]

Lastly, we execute my_stack.append("Voldemort") - so by this we are adding on top of the stack "Voldemort", and the stack has to contain the following 4 elements
deque (["Draco", "Harry", "Hermione", "Voldemort"])

@ilsamoano
Copy link

ilsamoano commented Nov 23, 2018

#Consider to have a stack obtained by processing, one by one, the elements included in the list of the first exercise,
# i.e. ​my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])​.
# Describe the status of my_stack after the execution of each of the following operations:
# ​my_stack.pop()​, ​my_stack.pop()​, my_stack.append("Voldemort")​.


from collections import deque 

Grinwald_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])

Grinwald_stack.pop() 
#this operation modifies the stack "Grinwald_stack" removing the last value "Severus"
#currently the stack "Grinwald_stack" contains four elements
#deque(["Draco", "Harry", "Hermione", "Ron"])

Grinwald_stack.pop() 
#this operation modifies the stack removing the (now new) last value "Ron"
#currently the stack "Grinwald_stack" contains three elements
#deque(["Draco", "Harry", "Hermione"])

Grinwald_stack.append("Voldemort") 
#this operation modifies the stack "Grinwald_stack" adding at the very top the value "Voldemort"
#currently the stack "Grinwald_stack" contains three elements
#deque(["Draco", "Harry", "Hermione", "Voldermort"])

print(Grinwald_stack)


deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])

@simayguzel
Copy link

simayguzel commented Nov 23, 2018

my_stack = deque ( )
my_stack = (["Draco", "Harry", "Hermione", "Ron", "Severus"])
my_stack.pop( )
output is 'Severus' #"Severus" is extracted from my_stack.

Now my_stack is ['Draco', 'Harry', 'Hermione', 'Ron'].

my_stack.pop( )
output is 'Ron' #'Ron' is extracted from my_stack.
#Now my_stack is ['Draco', 'Harry', 'Hermione'].

my_stack.append("Voldemort")
print (my_stack)
output = ['Draco', 'Harry', 'Hermione', 'Voldemort']

So, my_stack after is ['Draco', 'Harry', 'Hermione', 'Voldemort'] .

@DavideApolloni
Copy link

my_stack=deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])

my_stack.pop()
my_stack=deque(["Draco", "Harry", "Hermione", "Ron"])

my_stack.pop()
my_stack=deque(["Draco", "Harry", "Hermione"])

my_stack.append("Voldemort")
my_stack=deque(["Draco", "Harry", "Hermione", "Voldemort"])

@Totaro1996
Copy link

from collections import deque

Characters_stack=deque(["Draco", "Harry", "Hermione", "Severus"])

Characters_stack.pop() #it removes "Severus", the last element in the stack.
#Characters_stack currently contains 4 elements ["Draco", "Herry",
"Hermione", "Ron"]

Characters_stack.pop() #it applies again to the last element and consequently it removes "Ron".
#Characters_stack is now composed by 3 elements
["Draco","Herry","Hermione"]

Characters_stack.append("Voldemort") #it adds a new element

print(Characters_stack)

Characters_stack=deque(["Draco", "Herry", "Hermione", "Voldemort"])

@SeverinJB
Copy link

SeverinJB commented Nov 24, 2018

1 from collections import deque
2 harry_potter_und_ein_stein = deque(["Draco","Harry","Hermione","Ron","Severus"])
3 harry_potter_und_ein_stein.pop()
4 harry_potter_und_ein_stein.pop()
5 harry_potter_und_ein_stein.append("Voldemort")

In line 3, "Severus" is removed from the stack. The stack now contains "Draco", ”Harry", "Hermione", and "Ron".
In line 4, "Ron" is removed from the stack. The stack now contains "Draco", "Harry", and "Hermione".
In line 5, "Voldemort" is added to the stack on the right. The stack now contains "Draco", "Harry", "Hermione", and "Voldemort".

@MattiaSpadoni
Copy link

MattiaSpadoni commented Nov 24, 2018

  • my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
  • my_stack.pop #remove the fist element, in this case "Draco"
    my_stack = deque(["Harry", "Hermione", "Ron", "Severus"])
  • my_stack.pop #remove the fist element, in this case "Harry"
    my_stack = deque(["Hermione", "Ron", "Severus"])
  • my_stack.append ("Voldemort") #add an element on the top of the stack

my_stack = deque(["Voldemort", "Hermione", "Ron", "Severus"]) #final state.

@friendlynihilist
Copy link

from collections import deque 
#first of all I need to import the construct _deque_ from the module _collections_
potter_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) 
#I've created a stack containing five different strings, ordered alphabetically. In this case, "Severus" is the last on top of it.
potter_stack.pop() 
#By using the _.pop_ method I'm removing the variable on top of the pile, in this case "Severus"
potter_stack.pop() 
#Now poor "Ron" is going to be removed.
potter_stack.append("Voldemort") 
#And, through the _.append_ method, evil "Voldemort" is going to be added on top.
print(potter_stack) 
#Printing the result on screen I can see that my stack now contains, in order: "Draco", "Harry", "Hermione", "Voldemort".

@federicabologna
Copy link

harry_potter_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
#stack created with elements in the order I provided
harry_potter_stack.pop()
#take "Severus" out, since is the last element added
harry_potter_stack.pop()
#take "Ron" out, since is the second last element added
harry_potter_stack.append("Voldemort")
#add "Voldemort"
Thus: 'Draco', 'Harry', 'Hermione', 'Voldemort'

from collections import deque

harry_potter_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])
harry_potter_stack.pop()
harry_potter_stack.pop()
harry_potter_stack.append("Voldemort")
print(harry_potter_stack)

deque(['Draco', 'Harry', 'Hermione', 'Voldemort'])

@saraarmaroli
Copy link

my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"])

my stack.pop () # i Remove Severus, the element on the top of my stack
my_stack = deque(["Draco", "Harry", "Hermione", "Ron"])

my stack.pop () # i Remove Ron
my_stack = deque(["Draco", "Harry", "Hermione"])

My_stack.append (“Voldemort”)
my_stack = deque(["Draco", "Harry", "Hermione", “Voldemort”])

@VittoriaMoccia
Copy link

VittoriaMoccia commented Nov 25, 2018

Consider to have a stack obtained by processing, one by one, the elements included in the list of the first exercise, i.e. my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]). Describe the status of my_stack after the execution of each of the following operations: my_stack.pop(), my_stack.pop(), my_stack.append("Voldemort").

First we create a new stack:

my_stack = deque()
my_stack.append("Draco")
my_stack.append("Harry")
my_stack.append("Hermione")
my_stack.append("Ron")
my_stack.append("Severus")

print(my_stack)

Result: ['Draco', 'Harry', 'Hermione', 'Ron', 'Severus']

Since a stack is a particular list seen from bottom to top:

my_stack.pop() --> this first operation would remove "Severus", since it's the last item added
my_stack.pop() --> this time would delete "Ron", which was added right before "Severus"
my_stack.append("Voldemort") --> this would add a new item to the stack, so the final result will be:

['Draco', 'Harry', 'Hermione', 'Ron', 'Voldemort']

@MilenaCorbellini
Copy link

from collections import deque
characters_stack = deque()
characters_stack.append("Draco")
characters_stack.append("Harry")
characters_stack.append("Hermione")
characters_stack.append("Ron")
characters_stack.append("Severus")
characters_stack.pop()
characters_stack.pop()
characters_stack.append("Voldemort")
print(characters_stack)

After the first characters_stack.pop the last element of the stack is cancelled, after the second the previous one.
After the characters_stack.pop operation the string "Voldemort" is added.

@lisasiurina
Copy link

HPCharacters_stack = deque ( )
HPCharacters_stack = (["Draco","Harry","Hermione","Ron","Severus"])
#stack creation

HPCharacters_stack.pop( )
#it removes the element on top of the stack which is Severus

HPCharacters_stack.pop( )
#it removes the element on top of the stack which is Ron"

HPCharacters_stack.append("Voldemort")
#it adds a new name

print (HPCharacters_stack)
output = ['Draco', 'Harry', 'Hermione', 'Voldemort']

@tceron
Copy link

tceron commented Nov 27, 2018

my_hp_stack = deque()
my_hp_stack.append("Draco")
my_hp_stack.append("Harry")
my_hp_stack.append("Hermione")
my_hp_stack.append("Ron")
my_hp_stack.append("Severus")

print (my_hp_stack)

Output ["Draco", "Harry", "Hermione", "Voldemort"]

my_hp_stack.pop() # removes the element on top (stacks are seen from bottom to top)
my_hp_stack = (["Draco", "Harry", "Hermione", "Ron"])
my_hp_stack.pop() # removes the element on top
my_hp_stack = (["Draco", "Harry", "Hermione"])
my_hp_stack.append("Voldemort") # add one element to the top

print (my_hp_stack)
["Draco", "Harry", "Hermione", "Voldemort"]

@andreamust
Copy link

from collections import deque
my_stack = deque(["Draco", "Harry", "Hermione", "Ron", "Severus"]) # this passage creates a stack containing 5 names
print(my_stack)

my_stack.pop() # removes the last element "Severus"
print(my_stack)

my_stack.pop() # removes the last element "Ron"
print(my_stack)

my_stack.append("Voldemort") #adds a new element "Voldemord" on the top of the stack
print(my_stack)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Exercise The exercises that are introduced in the lectures.
Projects
None yet
Development

No branches or pull requests