Question

In: Computer Science

Python programming 1. Use 0 and 1 only to complete the function that outputs how many...

Python programming

1. Use 0 and 1 only to complete the function that outputs how many N-long sequence are in total.

Condition
1) Starts with zero and ends with zero
2) Zero does not exist twice in a row.
3) 1 does not exist three times in a row.
4) N is a natural number.

- A sequence that satisfies the conditions.
ex) 010, 0110, 01010

- A sequence that does not meet the conditions.

ex) 0100, 01110, 01100110

Need two answer. One is with recursion function and the other is Dynamic programming

thank you

Solutions

Expert Solution

Python recursion code :

# using recursion...

def n_long_sequence(n,li,str):
if(n == 1):
if str[-1] == '1':
li.append(str+'0')
return li


if(n>1 and str[-1] == '1' and (len(str)>=2 and str[-2] == '1')):
n_long_sequence(n-1,li,str+'0')
return li
elif(n>1 and str[-1] == '1'):
n_long_sequence(n-1,li,str+'0')
n_long_sequence(n-1,li,str+'1')
return li
else:
n_long_sequence(n-1,li,str+'1')
return li
  
  
  

if __name__ == '__main__':
n = int(input())
output = n_long_sequence(n-1,[],'0')
print(*output,sep='\n')
print("Number of %d"%n+" bit long sequences is : %d"%len(output))


Related Solutions

Python programming 1. Use 0 and 1 only to complete the function that outputs how many...
Python programming 1. Use 0 and 1 only to complete the function that outputs how many N-long sequence are in total. Condition 1) Starts with zero and ends with zero 2) Zero does not exist twice in a row. 3) 1 does not exist three times in a row. 4) N is a natural number. - A sequence that satisfies the conditions. ex) 010, 0110, 01010 - A sequence that does not meet the conditions. ex) 0100, 01110, 01100110 Need...
(Programming Language: Python) Complete the function remove number such that given a list of integers and...
(Programming Language: Python) Complete the function remove number such that given a list of integers and an integer n, the function removes every instance of n from the list. Remember that this function needs to modify the list, not return a new list. # DO NOT ADD ANY OTHER IMPORTS from typing import List def remove_number(lst: List[int], number: int) -> None: """ Remove every instance of number in lst. Do this *in-place*, i.e. *modify* the list. Do NOT return a...
Use the Python programming language to complete below (I need the New Answer for this, and...
Use the Python programming language to complete below (I need the New Answer for this, and please provide the screenshot of the source code. Thank you!): A website requests an user to input his account password. Write a program to examize the validity of the password. The valid password must consists of: At least 1 letter in [a-z] At least 1 number in [0-9] At least a letter in [A-Z] (capital) At least one special letter in [$#@] At least...
Use the Python programming language to complete below (I need the New Answer for this, and...
Use the Python programming language to complete below (I need the New Answer for this, and please provide the screenshot of the source code. Thank you!): Write a function to seek for all even numbers and odd numbers in the middle of two number A and B. Print even and odd numbers in 1 and 2020 (including both these two numbers)
If a function assigns 0 or 1 to each switching function of n variables, how many...
If a function assigns 0 or 1 to each switching function of n variables, how many such functions are there?
I did the complete programming in python , just want to use tkinter for GUI. please...
I did the complete programming in python , just want to use tkinter for GUI. please look at the 2nd part . i did some part of this program using tkinter but could not finis it. Thank you. import random image = 'w' # modified functions which accepts two numbers each and returns the respective # output def add(a, b): return a + b def subtract(a, b): return a - b def multiply(a, b): return a * b def kidCalc():...
complete in python The function sumEven should return the sum of only the even numbers contained...
complete in python The function sumEven should return the sum of only the even numbers contained in the list, lst. Example list_of_nums = [1, 5, 4, 8, 5, 3, 2] x = sum_evens(list_of_nums) print(x) #prints 14 Start your code with def evens(lst):
Using python Create a function that inputs a list of numbers and outputs the median of...
Using python Create a function that inputs a list of numbers and outputs the median of the numbers. sort them and them show the output.
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name...
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name it addToDictionary(s,r) that take a string and add it to a dictionary if the string exist increment its frequenc 2) function named freq(s,r) that take a string and a record if the string not exist in the dictinary it return 0 if it exist it should return its frequancy.
This lab problem demonstrates the use of import module. The Python programming language has many strengths,...
This lab problem demonstrates the use of import module. The Python programming language has many strengths, but one of its best is the availability to use many existing modules for various tasks, and you do not need to be an experienced computer programmer to start using these modules. We have given you some incomplete code; note that the very first line of that code contains an import statement as follows: import math This statement enables your program to use a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT