Oct-12-2020, 04:33 PM
(This post was last modified: Oct-12-2020, 04:36 PM by prasanthbab1234.)
Hi Team,
I have following query but littlie confusion to fix it.
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.
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.