Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ADT related stacks
#1
Hi Team,

I have following query but littlie confusion to fix it.
Quote:##Basic input#####
def push(stack, new_item):
    stack.append(new_item)
def is_empty(stack):
    return stack == []
def size(stack):
    return len(stack)
#Do not change any code above this line.

def reverse_string(string):
    #We have defined the stack for you
    stack = []
    
    for char in string:
        def push():
            _________ #push to stack
    new_string = ""
    while not ________: #Check if the stack is empty
        new_string += "" + _______ #pop the last element
    return new_string

#Tests
#Do not change code below this line
assert reverse_string("programming") == "gnimmargorp"
print("Awesome job!")
print(reverse_string("programming"))

We have 3 blanks(__________) in this question so hints as follow.
1.At the first blank space within the for loop, use the previously created push()  function to add char  to the stack!Remember to give the 
push()  function the necessary arguments.

2.Since you need to reverse the whole string, and strings can have different lengths, the optimal way to solve this is with the 
while - loop and the  is_empty()  function. You can get the length of a string with the  len()  function.Use the is_empty()
 function on the stack to check if the stack is empty or not.

3.The last task for this mini-project is to use the pop()  function. Append the last element from the stack to the 
new_string  by using the pop()  function.

4. When you are done with previous exercises, and have completed the  reverse_string  function, run this check to perform the final check of the reverse_string   function.
Reply
#2
Create the enqueue() function.

1. Name the function enqueue
2.It should take 2 arguments; the first called queue (the queue you created) and the second called new_item (the item that is being added to the queue)
3.Use the append() method to add the new item to the queue
4.Please note that this function does not return anything. Please note too that an enqueue method doesn't have to have arguments called queue and new_item, and it's only for the purpose of checking your answer that we're asking you to make arguments with these names.
I tried below query but something is missing in this.
Quote:def enqueue(queue,new_item):
     enqueue.append(new_item)
     print(enqueue)
Reply
#3
Create the dequeue function that is used to remove an item from the queue.

1.Name the function dequeue
2.It should take only one argument: queue (which is the queue that you created)
3.Use the pop() method to remove the item. Please note: Inside the pop method, specify the index of the item that should be removed
4.Please note that this function does not return anything
I tried as below but not sure what did I miss here?
Quote:def dequeue(queue):
    dequeue.pop(queue) 
Reply
#4
Create a function called is_empty(). Its task is to check whether or not there are elements left inside the stack.

1.Create a function called is_empty()
2.It should take one argument: queue
3.return the boolean True if the argument is empty (one method is to compare len(queue) with 0).
Quote:def is_empty(queue):
    return
Reply
#5
How do you convert instruction 3 into Python.  Shouldn't be too hard since they already provide most of the code in the instruction.
Reply
#6
Create the size() function. This function should take a queue and return the number of elements it currently contains.

1.Define a function called size()
2.It should take one argument: queue
3.Return len of the queue - which is the argument for this function
Quote:def size(self):
    return len(self.queue)
Reply
#7
Read the directions carefully, particularly 3.
Reply
#8
What is self?  I don't see "self" in any of your other posts about this assignment.
Reply
#9
@prasanthbab1234  Please, don't start new thread for each function. This will not help. We need to see effort on your part. We are not going to do your homework for you.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Expression Evaluation Using Stacks walrus_overlord2 3 4,991 Apr-15-2020, 11:19 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020