(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.

Advanced Engineering Mathematics
10th Edition
ISBN:9780470458365
Author:Erwin Kreyszig
Publisher:Erwin Kreyszig
Chapter2: Second-order Linear Odes
Section: Chapter Questions
Problem 1RQ
icon
Related questions
Question

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

(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 my list.
Start with listofrulers = []. 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 the listofrulers. After the loop has completed, return
listofrulers.
Transcribed Image Text:(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 my list. Start with listofrulers = []. 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 the listofrulers. After the loop has completed, return listofrulers.
Expert Solution
steps

Step by step

Solved in 5 steps with 22 images

Blurred answer
Similar questions
Recommended textbooks for you
Advanced Engineering Mathematics
Advanced Engineering Mathematics
Advanced Math
ISBN:
9780470458365
Author:
Erwin Kreyszig
Publisher:
Wiley, John & Sons, Incorporated
Numerical Methods for Engineers
Numerical Methods for Engineers
Advanced Math
ISBN:
9780073397924
Author:
Steven C. Chapra Dr., Raymond P. Canale
Publisher:
McGraw-Hill Education
Introductory Mathematics for Engineering Applicat…
Introductory Mathematics for Engineering Applicat…
Advanced Math
ISBN:
9781118141809
Author:
Nathan Klingbeil
Publisher:
WILEY
Mathematics For Machine Technology
Mathematics For Machine Technology
Advanced Math
ISBN:
9781337798310
Author:
Peterson, John.
Publisher:
Cengage Learning,
Basic Technical Mathematics
Basic Technical Mathematics
Advanced Math
ISBN:
9780134437705
Author:
Washington
Publisher:
PEARSON
Topology
Topology
Advanced Math
ISBN:
9780134689517
Author:
Munkres, James R.
Publisher:
Pearson,