"""
Note: there is no such class in Python. We will perform normal operations on array
"""
arrayList = []
print "Initial size of arrayList: ", len(arrayList)
arrayList.append("A")
arrayList.append("B")
arrayList.append("C")
arrayList.append("D")
arrayList.insert(2, "E")
print "Changed contents of arraylist by adding element at the given index: ", arrayList
arrayList.pop(3)
arrayList.remove("A")
print "Changed contents of arraylist by removing element from the list: ", arrayList
"""
Note: There is no such collection in Python. We will dqueue instead, which is very much similar
"""
from collections import deque
d = deque()
class MyStack():
def push1(self, o):
d.appendleft(o)
def push2(self, o):
d.append(o)
def bottom(self):
return d.pop()
def pop(self):
return d.popleft()
class Car():
car1 = "Benz"
car2 = "Toyoto"
car3 = "Qualis"
car4 = "Santro"
class Bird():
bird1 = "parrot"
bird2 = "duck"
bird3 = "raven"
myCar = Car()
myBird = Bird()
s = MyStack()
s.push1(myCar.car1)
s.push2(myBird.bird3)
myCar = s.pop()
print "The first element in the list: ", myCar
print "The last element in the list: ", myBird.bird3
"""
Note: there is no collection hash set in Python. We will solve it in different manner
"""
hs = set()
hs.add("D")
hs.add("A")
hs.add("C")
hs.add("B")
hs.add("E")
#answer will differ due to difference in built-in techniques
print "The elements available in the hash set are: ", hs
#There is no TreeSet or similar collection in Python (can be done using normal list like 18.1) so skipping example 18.4
"""
Note: there is no vector in Python. Doing using normal lists.
"""
fruits = []
fruits.append("Apple")
fruits.append("Orange")
fruits.append("Grapes")
fruits.append("Pine")
for ele in fruits:
print ele
"""
In python we can use list itself as stack
"""
st = []
st.append("Java")
st.append("latest")
st.append("Edition")
st.append("-fifth")
print "The elements in the Stack: ", st
print "The element at the top: ", st[-1:]
print "The element poped out of the stack: ", st.pop()
print "The element in a stack after pop out element: ", st
try:
"The result of searching: ", st.index("r u")
except ValueError:
print "The result of searching: ", -1
"""
Note: we will use dictionaries
"""
ht ={}
ht['item1'] = "Apple"
ht['item2'] = "Orange"
ht['item3'] = "Grapes"
ht['item4'] = "Pine"
for key in ht.iterkeys():
print ht[key]
#Answers will differ due to difference in built-in techniques of the classes
from random import shuffle
l = []
l.append("java")
l.append("is")
l.append("platform")
l.append("independent")
l_r = list(reversed(l))
l_r = sorted(l_r)
print "List sorted in reverse order is: "
for ele in l_r:
print ele,
print "\nList shuffled: "
shuffle(l_r)
for ele in l_r:
print ele,
print "\nMinimum: ", min(l_r)
print "Maximum: ", max(l_r)