Question 4c Implement a function, generateSequence which has an integer parameter length. The function generates and...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Question 4c Implement a function, generateSequence which has an integer parameter length. The function generates and returns a string consisting of length unique integers and dashes, separated by commas (). The string starts with an integer, not necessarily 1. If length is 0 or negative, the function returns an empty string. If length is 1, the function returns a string of one integer. An example string is '2,3,-,-,7,8,9,-,11' when length is 9. Note that the string also satisfies these conditions: Integers are ordered in increasing order and neighbouring numbers are consecutive, e.g., 2 and 3 are consecutive and 7, 8 and 9 are consecutive. Integers immediately after a dash are at least one but not two more than the sum of the last integer before the dash and the number of dashes, e.g., the number after the first 2 dashes can be 6 or 7, and 7 is generated and included, instead of 6. Also, the number after the last dash can be 11 or 12, and 11 is generated and included, instead of 12. (10 marks) Question 4d Write a function, main which repeatedly prompts for the length of sequence to generate, calls the functions generate Sequence and checkSequence, and prints the output of both function calls until the user enters when prompted for the length. If the user enters negative numbers, display an error message and re-prompt the user. Sample run: to end: 10 Enter length of sequence to generate and to end: -2 Length of sequence must be a positive number Enter length of sequence to generate and 89,90,91,92,93,-,96,97,-,-: False Enter length of sequence to generate and 83, 84, 85, -,87,-,89,90,-,- : True to end: 10 to end: 9 Enter length of sequence to generate and 5,6,7,8,9,-,11,12,- : True Enter length of sequence to generate and to end: 0 (5 marks) Question 4c Implement a function, generateSequence which has an integer parameter length. The function generates and returns a string consisting of length unique integers and dashes, separated by commas (). The string starts with an integer, not necessarily 1. If length is 0 or negative, the function returns an empty string. If length is 1, the function returns a string of one integer. An example string is '2,3,-,-,7,8,9,-,11' when length is 9. Note that the string also satisfies these conditions: Integers are ordered in increasing order and neighbouring numbers are consecutive, e.g., 2 and 3 are consecutive and 7, 8 and 9 are consecutive. Integers immediately after a dash are at least one but not two more than the sum of the last integer before the dash and the number of dashes, e.g., the number after the first 2 dashes can be 6 or 7, and 7 is generated and included, instead of 6. Also, the number after the last dash can be 11 or 12, and 11 is generated and included, instead of 12. (10 marks) Question 4d Write a function, main which repeatedly prompts for the length of sequence to generate, calls the functions generate Sequence and checkSequence, and prints the output of both function calls until the user enters when prompted for the length. If the user enters negative numbers, display an error message and re-prompt the user. Sample run: to end: 10 Enter length of sequence to generate and to end: -2 Length of sequence must be a positive number Enter length of sequence to generate and 89,90,91,92,93,-,96,97,-,-: False Enter length of sequence to generate and 83, 84, 85, -,87,-,89,90,-,- : True to end: 10 to end: 9 Enter length of sequence to generate and 5,6,7,8,9,-,11,12,- : True Enter length of sequence to generate and to end: 0 (5 marks)
Expert Answer:
Answer rating: 100% (QA)
For 4c heres the implementation python def generateSequencelength if len... View the full answer
Related Book For
Data Structures and Algorithms in Java
ISBN: 978-1118771334
6th edition
Authors: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Posted Date:
Students also viewed these programming questions
-
can someone solve this Modern workstations typically have memory systems that incorporate two or three levels of caching. Explain why they are designed like this. [4 marks] In order to investigate...
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
Implement and maintain internal control procedures This task will require you to roleplay a meeting with your supervisor, Chris Kohler. You will discuss the reporting requirements and timetables...
-
During the year, Summit produces 50,000 snow shovels and sells 45,000 snow shovels. Summit Manufacturing produces snow shovels. The selling price per snow shovel is $30. There is no beginning...
-
Fit a robust model for Task 1. Task 1 A few years back I was stalked. Youd think they could have found someone a bit more interesting to stalk, but apparently times were hard. It wasnt particularly...
-
The following MINITAB output presents a 95% confidence interval for the mean ozone level on days when the relative humidity is 60%, and a 95% prediction interval for the ozone level on a particular...
-
China is a signatory country to the Madrid Protocol on the international registration of trademarks. Starbucks opened its first cafe in China in 1999 and has added outlets in numerous locations...
-
Han Ji-San Company produces a device that can analyze a person's handwriting. To help monitor costs, it employs a standard costing system and uses a flexible budget to predict overhead costs at...
-
Identify bugs and inadequacies in the following code which purports to implement a clock custom HTML element. class ClockElement extends HTMLElement { constructor() { this.time = Date.now(); }...
-
Examine the principles of role-based access control (RBAC) in an operating system. How does it differ from discretionary access control (DAC) ?
-
Define the four necessary conditions for a deadlock to occur. Discuss strategies for deadlock prevention, avoidance, and detection .
-
For a fully discrete whole life insurance of 1000: (i) The gross premium reserve at the end of the 10th year is 220. (ii) The gross premium reserve at the end of the 11th year is 230. (iii) Expenses...
-
Sam, married filing separately without dependents, has $ 3 2 5 , 0 0 0 salary and $ 7 5 , 0 0 0 qualified dividends. Her taxable income is $ 3 8 0 , 0 0 0 . Compute Sam's total tax liability for the...
-
Each response should be at least 2-4 full sentences each, with proper citation for each response. Cite your textbook when applicable. Do not copy and paste from the textbook. Use critical thinking...
-
3. Express Heat equation in form of (a)2D Cartesian (b) 2D Polar (c) 3D Cartesian (d) 3D Spherical (e) 3D Hyperbolic and solve it using product solution 4. Express Schrodinger equation in form of...
-
When you weigh yourself on good old terra firma (solid ground), your weight is 142 lb. In an elevator your apparent weight is 121 lb. What are the direction and magnitude of the elevator's...
-
If the parameter to the makePayment method of the CreditCard class (see Code Fragment 1.5) were a negative number, that would have the effect of raising the balance on the account. Revise the...
-
Write a short Java method that takes an array of int values and determines if there is a pair of distinct elements of the array whose product is even.
-
Show that if d(n) is O( f (n)), then ad(n) is O( f (n)), for any constant a > 0.
-
Describe the ways in which the scope of many audits in the early 1900 s was significantly different from what it is today.
-
What is the difference between statutory law and common law?
-
Name and define four standards of proof.
Study smarter with the SolutionInn App