Question

In: Computer Science

Write a Python function that receives a stack object s, where the items in the stack...

Write a Python function that receives a stack object s, where the items in the stack are only numbers in the set {0,1} and returns the number of 1's in the stack s. The function must satisfy the following restrictions:

  • the state of the stack must be preserved; ie., after calling this function the state of the stack s must be the same it was before the function was called.
  • The function cannot use any additional variables of any of the collection types such as lists, stacks, dictionaries, strings etc.
  • The function cannot use any global variables.
  • Since the implementation of Stack is not known you must use the API given in the Table 1.

For example, if the stack contains 0,0,1,1,0,1,0, the function must return 3 and the stack must still contain 0,0,1,1,0,1,0.

Solutions

Expert Solution

Raw_code:

def function(stack):
# temporary variable of integer type (non-collection type)
count = 0
# for loop for iterating through each element in the stack using membership in operator
for number in stack:
# if statement for checking whether the number is 1 or
if number == 1:
# if number is 1 incrementing the count of 1
count += 1
# after breaking of for loop returning count
else:
return count

stack1 = [0, 0, 1, 1, 0, 1, 0]
# calling the function with stack as parameter
print(function(stack1))
print(stack1)

stack2 = (0, 0, 1, 1, 0, 1, 1, 0)
print(function(stack2))
print(stack2)


Related Solutions

Write method reverseStack(Stack s) that receives a stack s and reverse the order of its elements....
Write method reverseStack(Stack s) that receives a stack s and reverse the order of its elements. the values inside the stack must be changed, that the top will be the last and so on. please use java code to slove. Thank you.
Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
In PYTHON: Write a function that receives a sentence and returns the last word of that...
In PYTHON: Write a function that receives a sentence and returns the last word of that sentence. You may assume that there is exactly one space between every two words, and that there are no other spaces at the sentence. To make the problem simpler, you may assume that the sentence contains no hyphens, and you may return the word together with punctuation at its end.
In PYTHON: Write a function that receives an encrypted string and decrypts it as follows. The...
In PYTHON: Write a function that receives an encrypted string and decrypts it as follows. The decryption does not change the lowercase letters ‘e’, ‘w’, and ‘l’ (lowercase L). For any other character, we change it to a number using ord(). We then take the number to a power of 19, and then find the remainder after diving by 201. Finally, we take the character corresponding to the number we obtained (using chr()). For example, assume that we receive the...
Write a function that receives a StaticArray with integers and returns a new StaticArray object with...
Write a function that receives a StaticArray with integers and returns a new StaticArray object with the content from the original array, modified as follows: 1) If the number in the original array is divisible by 3, the corresponding element in the new array should be a string ‘fizz’. 2) If the number in the original array is divisible by 5, the corresponding element in the new array should be a string ‘buzz’. 3) If the number in the original...
Write a Python program to (a) create a new empty stack. Then, (b) add items onto...
Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents:
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Python program.  from random import . Write a function called cleanLowerWord that receives a string as a...
Python program.  from random import . Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks...
Without using method size(), write recursive method stackSize(Stack > s1 ) that receives a stack and...
Without using method size(), write recursive method stackSize(Stack > s1 ) that receives a stack and returns number of elements in the stack. The elements in the stack should not be changed after calling this method. please do it in java
In Python Create a function called ????. The function receives a "string" that represents a year...
In Python Create a function called ????. The function receives a "string" that represents a year (the variable with this "String" will be called uve) and a list containing "strings" representing bank accounts (call this list ????). • Each account is represented by 8 characters. The format of each account number is "** - ** - **", where the asterisks are replaced by numeric characters. o For example, “59-04-23”. • The two central characters of the "string" of each account...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT