(c) Write a function sparsenkrulers(n,k) which takes as inputs positive integers n, k where n+ 1 ≥ k ≥ 2 and returns a list of all sparse rulers of reach n and order k. Hint: the itertools library has a function combinations. After importing it, the command combinations (mylist, r) will generate all sublists of length r from mylist. Start with listof rulers = []. Note that each ruler must contain the entries 0 and n with k-2 others. Using the technique above, set up a loop through all combinations of k-2 numbers from range (1,n). At each stage, create a ruler which contains the entries and n along with the current combination, sorted into ascending order. Append it to the listofrulers. After the loop has completed, return listofrulers.
An ordinary ruler is a straight piece of wood where distances 0, 1, 2 . . . , N are marked, for some N ≥ 1. A sparse ruler (or simply a ruler ) is an ordinary ruler from which some of the numbers 1, . . . , N −1 may have been deleted. The number of marks on the ruler is its order and the value N is its reach. Here, we will represent a ruler as a Python list of strictly increasing integers starting with 0. For instance [0,1,3,7] is a ruler of order 4 and reach 7.
A sparse ruler of reach N is complete if it is possible to measure all distances between 1 and N by taking the dierences between two marks. For instance [0,1,3] is complete because the pairs (0, 1), (1, 3), and (0, 3) yield distances of 1, 2, and 3 respectively. (Note that the pair of marks do not need to be consecutive.) On the other hand, [0,1,4] is not complete as there is no way to measure a distance of 2.
could you please provide the code for this in python , the parts in bold, are to help give some info about the concepts in the question
Step by step
Solved in 5 steps with 22 images