Question

In: Computer Science

Part A - Palindromic Bitlists Write a function palindrome binary(n) which returns a list of bitlists...

Part A - Palindromic Bitlists Write a function palindrome binary(n) which returns a list of bitlists of length n, where every bitlist is a palindrome. The returned list of bitlists must be in lexicographical order (think about the order of your options). You must use backtracking to solve this problem (i.e. do not use brute force).

Part B - All Paths Write a function all paths(M, u, v) which takes as input an adjacency matrix of a graph, M, and two vertices u, v and returns a list of all paths from u to v in M. A path is a list of vertices. Note that a path cannot use the same vertex twice, but two different paths can use some of the same vertices. The returned list of paths must be in lexicographical order. You must use backtracking to solve this problem (i.e. do not use brute force).

PYTHON CODE

Solutions

Expert Solution

Program code screenshot:

Part A:

Sample output:

Program code to copy:

# Create a function with the name of

# palindrome_binary with argument of n

def palindromes_binary(n):

# store the resul in the rlist

bitlist = []

# divide the given bits from the mid

# and then append the list

position = int(n/2)

# loop to access the element

for i in range(n+2):

    # Compute the first helf of the bit

    # to bitlist in the palindrome

    firsthalf = bin(i)[2:].zfill(position)

    # modulous the given bitlist to check

    # n is even

    if(n % 2 == 0):

    # Reverse the first half to add the second half in the bitlist

      lexicographical = firsthalf + firsthalf[::-1]

      # reverse the list in the lexicographical order

      bitlist.append(lexicographical)

    # otherwise

    else:

       

     # when n is odd, middle can be 0/1 which is 2 possibilities

      # Check the condition when n is odd

      # two cases are avialable

      # case 1 first half and second half mid by zero

      case1 = firsthalf + '0' + firsthalf[::-1]

      # case 2 first half and second half mid by one

      case2 = firsthalf + '1' + firsthalf[::-1]

      # append the case 1 in reverse order

      bitlist.append(case1)

      # append the case 2 in reverse order

      bitlist.append(case2)

     

    # return the bit list

return bitlist

# Display the palindrome list for

# 5 and 9

print(palindromes_binary(5))

print(palindromes_binary(9))

Program code screenshot:

Part B:

Program code to copy:

# Create a function with the name of

# create_all_paths

def create_all_paths(M,u,v,CN,p):

# Check the current node vertices

   if(CN==v):

       #Display the path when reach the end vertex

       print(p,end=" ")

    # for loop check the path is a list of vertices

   for node,pathExist in enumerate(M[CN]):

       # Check the path cannot use the

       # same vertex twice,

       # but two different paths can

       # use some of the same vertices

       if(pathExist == 1 and (node not in p)):

           # append the node at the end of the list

           p.append(node)

           # Use backtracking to solve all paths

           create_all_paths(M,u,v,node,p)

         

           # pop out the path

           p.pop()

          

# create a function with the name of all_paths

# which takes as input an adjacency

# matrix of a graph, M, and two vertices u, v

def all_paths(M,u,v):

# Display the paths

   print("Paths: ",end=" ")

   # call a list of all paths from u to v in M

   create_all_paths(M,u,v,u,[u])

# Define the required variables

# u, v and M

u=0

v=3

# Adjacency matrix of a graph, M,

M=[

   [0,1,1,0],

   [1,0,1,1],

   [1,1,0,1],

   [0,1,1,0]

   ]

# call all_paths function

all_paths(M,u,v)


Related Solutions

Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree...
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree in level-order. That is, the method should return the root, then the nodes at depth 1, followed by the nodes at depth 2, and so on. Your algorithm should begin by putting the tree root on an initially empty queue. Then dequeue a node, add it to the output, and enqueue its left and right children (if they exist). Repeat until the queue is...
Python language: Write a function dice(n) that returns a list with the results of n dice...
Python language: Write a function dice(n) that returns a list with the results of n dice rolls conducted with a six sided dice. Use Numpy's random number functions to do this (Hint: use randint() within numpy's random module). i.e. dice(3) returns something like [2,3,5] to indicate the first dice obtained a 2, the second a 3 etc
Write a Scheme function that takes a positive integer n and returns the list of all...
Write a Scheme function that takes a positive integer n and returns the list of all first n positive integers in decreasing order: (decreasing-numbers 10) (10 9 8 7 6 5 4 3 2 1) Please explain every step
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
1.Write a function div7(lst) which takes in a list of integers, and returns a list of...
1.Write a function div7(lst) which takes in a list of integers, and returns a list of booleans of the same length, such that for each integer in the original list, the boolean in the output list is True if that integer was divisible by 7, or False if not. Use list comprehensions in python, the function only could be at most two lines long. Here is some examples: >>> div7([14, 5, 7, 3, 29, 28, 10]) [True, False, True, False,...
Task 2: Recursive Binary Search Part A - Target in list? Write a function simple recursive...
Task 2: Recursive Binary Search Part A - Target in list? Write a function simple recursive binary search(lst, target) which returns True if target is in lst; otherwise False. You must use recursion to solve this problem (i.e. do not use any loops). Part B - Where is target? Write a function advanced recursive binary search(lst, target, lo, hi) which returns the index of an instance of target in lst between lo and hi, or None if target is not...
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a...
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a list containing only the items in list-a that are not also in list-b. E.g., (remove-all '(a b b c c d) '(a c a)) -> '(b b d)
Write a function that takes a list of integers as input and returns a list with...
Write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2] Do not use any special or built in functions like append, reverse etc.
Write a LISP function COUNTX which takes an atom and a list and returns the number...
Write a LISP function COUNTX which takes an atom and a list and returns the number of top-level occurrences of the atom in the list. For example: (COUNTX ‘A ‘(A (A B) B A B A (B A)) Returns the value 3, the other two A’s are not at the top level
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT