Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

An array A of size 2n that contains each integer 1, 2, . . . , n twice is balanced if, for each integer k,

An array A of size 2n that contains each integer 1, 2, . . . , n twice is balanced if, for each integer k, the two occurrences of k in A are separated by k other integers. For example, the array A = [3, 1, 2, 1, 3, 2] is balanced because there are three integers between the two 3s, two integers between the two 2s and one integer between the two 1s. This question requires you to create a python function balanced that implements an algorithm (that you must devise) that counts, for an input n, the number of balanced arrays of size 2n.

finish the codeimage text in transcribed

You must create a function balanced whose input is a positive integer n that returns the number of different ways of forming a balanced array of size 2n. #edit this cell with your answer def balanced(n): "return the number of balanced arrays of size 2n". Note that if you use backtracking (or recursion in some other way) and so the function calls itself, you might want it to accept more inputs. For example, if you want to call the function and say how many balanced arrays of size 2n you have found so far, you might write def balanced(n, count): You must, however, provide default values so that the function works when called with only one argument (in particular, your function should pass the tests below). So you might write, for example, def balanced(n, count=0) def balancedtest(): assert balanced(1) == 0 assert balanced(2) == 0 assert balanced(3) == 2 assert balanced(4) == 2 assert balanced(5) == 0 assert balanced(6) == 0 assert balanced (7) == 52 assert balanced(8) == 300 print ("all tests passed")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

b. What is the banks return on equity? LOP8

Answered: 1 week ago

Question

=+5. Are emotional appeals ethical? Why or why not? [LO-6]

Answered: 1 week ago