TRENDING NEWS

POPULAR NEWS

Stack Data Structure In Python

How do you reverse a string, using the stack data structure in Python?

def rev_string_using_stack(string):
rev_str_st = Stack()
# push all characters into stack.
for c in string:
rev_str_st.push(c)

# Build a new reverse string and return it
rev_str = ""
while rev_str_st.size():
rev_str += rev_str_st.pop()
return rev_str

print (rev_string_using_stack("ult pult"))

Is string a data structure in Python?

After reading the official documentation and Python Programming/Data Types,It is safe to say that str: String; represented as a sequence of 8-bit characters in Python 2.x, but as a sequence of Unicode characters (in the range of U+0000 - U+10FFFF) in Python 3.xis hence a data structure.

Which data structure is used to fill a stack?

By stack you mean stack, and stack is a stack.Okay, that was a bad one!!Here's what these Stack mean:It's the stack in question.It means the memory stack.This stack is memory stack.And finally stack data structures.But what does it mean?When i saw this question, i thought you were asking “How stack ds is filled” or “how items are inserted in a stack”. Then i realized that may be it was “memory stack”. So this answer is for memory stack.The stack memory segment is an area of memory allotted for automatic variables. Automatic variables are allocated and de-allocated automatically when program flow enters and leaves the variable's scope. A program consist of calls to different functions and reference to different variables. While the program is being executed, it may jump from one place to another, and usually it does! So to manage the program flow, we need something. Something which can store last call variables and jump to next one, and as soon as it is finished, jump back again to first one.You may get an idea, what I am trying to say:void foo1(){
...
...
//do something
}
void foo2(){
...
...
//do something
}
void foo3(){
...
...
//do something
}
int main(){
foo2();
foo1();
foo3();
return 0;
}
You can see from the above code, the execution start with main then goes to foo2(), comes​ back to main(). Goes to foo1() and so on.Now to process this, we will require memory in such a way that jumping to and fro can be done efficiently. So we use stack, the data structures.Hope it helps!Happy Coding

Which is better for data structure: Python or Java?

I think maybe the question was quickly lumped up into the “language war” category and on that basis you were judged too harshly. For a beginner the choice of language makes all the difference, so the question isn’t totally uninitiated.When consdiering Python and Java specifically, Python comes out on top in terms of readability and preparedness to allow a junior programmer to create their own data structures by using the list data type or something (I haven’t used Python that much). While Java already provides Stack() and ArrayList() classes which makes the process require little to no effort.So looking at a stack implementation in Javapublic class MyStack {
private int maxSize;
private long[] stackArray;
private int top;

public MyStack(int s) {
maxSize = s;
stackArray = new long[maxSize];
top = -1;
}
public void push(long j) {
stackArray[++top] = j;
}
public long pop() {
return stackArray[top--];
}
And one in Pythonclass Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
I think that for someone who has just begun studying data structures (and programming), the push() and pop() methods for instance are more easily readable in Python, and perhaps more easily implemented as well.So it’s up to you to decide what you want. But if it were me deciding, I’d definitely go with Java as it can give you a much better understanding of memory management, data types, things like time complexity, and other fundamentals of programming you need a complete grip of if you want to understand how your data structures work. You’ll eventually use one in some type of algorithm, and even if you use the leverage your language of choice (Java, in this case) provides you with, you still need to know how your structure works. That’s something Python won’t help you with, unless you want to get down to business without the rigmarole.Example code above copied from Java Examples - Implementation of Stack and 3.5. Implementing a Stack in Python respectively.

What is an implementation of the stack data structure in C++?

Take any std::list and then write a push, a pop, top, and empty function and you have a stack, if you only use those 4 functions.In the STL, an ‘adaptor’ class is made from std::deque (double-ended queue)called std::stack, that restricts the API to the above functions.push push the item to the front with std::list::push_front(T)top get a copy of the item with T std::list::front() (does not remove)pop get the item with T std::list::front(), and then remove the front item with void std::list::pop_front().empty use bool std::list::empty(). It does the job for you.#include
#include

// alias
using Stack = std::list;


void push(Stack& st, const std::string& s)
{
st.push_front(s);
}

// get top but don't pop it
std::string top(Stack& st)
{
return st.front();
}

std::string pop(Stack& st)
{
std::string s = st.front();
st.pop_front();
return s;
}


bool empty(Stack& st)
{
return st.empty();
}

// some stack algorithms

// return a reversed stack (Why tho?)
Stack reverse(const Stack& st)
{
Stack ST{st};
Stack RV;
while( ! empty(ST)) push( RV, ST.pop() );
return RV;
}

// recursive sort of stack (ASCII)

void insert(Stack& st, std::string& s)
{
std::string tmp;
if ( empty(st) or (s.compare(top(st)) < 0) )
push(S, s);
else
{
tmp = pop(st);
insert(st, s);
push(st, tmp);
}


void sort(Stack& st)
{
std;:string tmp;
if (! empty(st) )
tmp = pop(st);
sort(st);
insert(st, tmp);
}
Stack S;push(S, "one");
push(S, "two");
push(S, "three");
push(S, "four");
push(S, "five");

Stack S2(S); // copy


while( ! empty(S) )
{
std::cout << pop(S); << std::endl;
}

five
four
three
two
one


sort(S2);
while( ! empty(S2) )
{
std::cout << pop(S2) << std::endl;
}

five
four
one
three
two

Where are some practical uses of data structures such as stack, queue, linked list, tree(s) etc.?

Lots of programs deal with collections of data: words on a page, items in inventory or a shopping cart, tasks to be accomplished, things to display, etc. etc.  Stacks, queues, lists, trees, etc all deal with collections of data.So why do we have so many structures?  Why not just one?  The reason is that each data structure has it's own benefits and costs in terms the space it takes and the speed with which it does various operations.If you need to insert lots of items in the middle of a collection then a linked list is good.  Want random access to any element? You need an array.  Processing items in first-in-first-out order? That's a queue.  Last-in-first-out? Use a stack. The list goes on and on.In my experience, knowing the benefits and costs of different data structures is one of the most important things to learn in computer science.

Horasantal stack python?

a={1:"one", 2:"TWO", 3:"three"}
b={1:"four", 2:"FIVE", 3:"six", 4:"seven"}
c={1:["cat","dog","rat"], 2:["MOM","DAD"]}
array=[a,b,c]

# find the largest key (ie column number) in the input
maxkey = 0
for d in array:
  maxkey = max(maxkey, max(d.keys()))

# transpose by repeatedly walking the array, collecting
# the next desired item from each of its dicts, and adding
# that item into a new dict. if the source dict doesn't
# contain an item with the desired key, store 'None' into
# that position of the new dict.
result = []
for desired in range(1, 1+maxkey):
  newdict = {}
  newdictkey = 1
  for d in array:
    if desired in d:
      val = d[desired]
    else:
      val = None
    newdict[newdictkey] = val
    newdictkey += 1
  result.append(newdict)

# done. Show the result.
print(result)

C++ Data Structures and Algorithms - I'm scared?

Well I'm slowly chipping away at a BS in Computer Science and I've kind of put off one of the courses, Data Structures and Algorithms.

I know this is sort of a cheesy question, since the answer depends on many things...but I'm wondering how difficult this class will be. I'm nervous about it because I took the two previous C++ classes over a year ago...so not sure how much I'll remember. I did pretty good in these classes but was starting to struggle a bit toward the end with arrays of classes and whatnot.

I also did fine in the other pre-req for this course, applied discrete math, but we really didn't do any programming in this course.

Description of the course:

Upon completion of this course, you should be capable of discussing and/or applying C++ programming methods in the following areas:

• Analysis of algorithms using Big-O notation
• Complexity analysis
• Linked lists
• Queues
• Stacks
• Recursion
• Trees
• Heaps
• Searching
• Sorting
• Hashing

any thoughts? thanks

What is the difference between algorithm and data structure and what are their types?

An algorithm is basically a program and a data structure is like a database record - it's a set of different types of data, all stored together in one place.

Not sure what their Types would be though...

TRENDING NEWS