Implement the functions marked with ??? package iterators: /** * Returns an iterator that generates the prime numbers * 1,2,3,5,7,11,13,17,19,23,... * (Strictly speaking, 1 is not considered prime in most of the modern definitions as including it causes problems in some other definitions but let's consider it to be a prime in this exercise.) */ def primesIterator: Iterator[Int] = new Iterator[Int]: /* You'll need some private var or val to store the internal state. * Put it/them here */ /** * There is always the next prime number. * In reality, we should return false when the next prime would be larger than that can * be represented with an Int but, for the sake of focusing the exercise on the essential parts, * this consideration is not required for getting the points. */ def hasNext = true /** * Get the next prime number. * The first call should return 1, the next 2, then 3, 5, 7, 11 and so on. * * These numbers must be computed by the code here, one is not allowed to pre-compute and * store them in an Array or similar data structure. */ def next(): Int = ??? end next end primesIterator /** * Return a new iterator whose "next" method returns randomly an element that * - is in the non-empty constructor argument set s, and * - has not been returned during the blockingTime last calls to "next". * That is, after being selected and returned by "next", an element is blocked in the * the "blockingTime" subsequent calls to "next". * The argument seed is the seed given to the pseudo-random number generator * (fix it to have repeatable runs and set to System.nanoTime etc to have true pseudo-random sequences). */ def blockingRandomIterator[T](s: Set[T], blockingTime: Int, seed: Int = 2105): Iterator[T] = new Iterator[T]: require(0 <= blockingTime && blockingTime < s.size) /* Pseudo-random number generator. * See http://www.scala-lang.org/api/current/index.html#scala.util.Random */ val r = new scala.util.Random(seed) /* You'll need some var or val to store the internal state. * * Perhaps a queue to keep track of the blocked elements? * See http://en.wikipedia.org/wiki/Queue_%28abstract_data_type%29 and * http://docs.scala-lang.org/overviews/collections/concrete-mutable-collection-classes.html * * And perhaps a mutable Buffer to store the elements that are not blocked? * By the way, a fast method to remove an element at index i from an unordered buffer * is to just replace that element with the last element of the buffer and then shrink * the buffer by dropping the last element (which is now a copy of the one in the i:th position) * with dropRightInPlace(1) * * Put your state vars/vals here. */ /** There is always the next element */ def hasNext = true /** Returns a random, non-blocked element */ def next(): T = ??? end next end blockingRandomIterator
Implement the functions marked with ??? package iterators: /** * Returns an iterator that generates the prime numbers * 1,2,3,5,7,11,13,17,19,23,... * (Strictly speaking, 1 is not considered prime in most of the modern definitions as including it causes problems in some other definitions but let's consider it to be a prime in this exercise.) */ def primesIterator: Iterator[Int] = new Iterator[Int]: /* You'll need some private var or val to store the internal state. * Put it/them here */ /** * There is always the next prime number. * In reality, we should return false when the next prime would be larger than that can * be represented with an Int but, for the sake of focusing the exercise on the essential parts, * this consideration is not required for getting the points. */ def hasNext = true /** * Get the next prime number. * The first call should return 1, the next 2, then 3, 5, 7, 11 and so on. * * These numbers must be computed by the code here, one is not allowed to pre-compute and * store them in an Array or similar data structure. */ def next(): Int = ??? end next end primesIterator /** * Return a new iterator whose "next" method returns randomly an element that * - is in the non-empty constructor argument set s, and * - has not been returned during the blockingTime last calls to "next". * That is, after being selected and returned by "next", an element is blocked in the * the "blockingTime" subsequent calls to "next". * The argument seed is the seed given to the pseudo-random number generator * (fix it to have repeatable runs and set to System.nanoTime etc to have true pseudo-random sequences). */ def blockingRandomIterator[T](s: Set[T], blockingTime: Int, seed: Int = 2105): Iterator[T] = new Iterator[T]: require(0 <= blockingTime && blockingTime < s.size) /* Pseudo-random number generator. * See http://www.scala-lang.org/api/current/index.html#scala.util.Random */ val r = new scala.util.Random(seed) /* You'll need some var or val to store the internal state. * * Perhaps a queue to keep track of the blocked elements? * See http://en.wikipedia.org/wiki/Queue_%28abstract_data_type%29 and * http://docs.scala-lang.org/overviews/collections/concrete-mutable-collection-classes.html * * And perhaps a mutable Buffer to store the elements that are not blocked? * By the way, a fast method to remove an element at index i from an unordered buffer * is to just replace that element with the last element of the buffer and then shrink * the buffer by dropping the last element (which is now a copy of the one in the i:th position) * with dropRightInPlace(1) * * Put your state vars/vals here. */ /** There is always the next element */ def hasNext = true /** Returns a random, non-blocked element */ def next(): T = ??? end next end blockingRandomIterator
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
Implement the functions marked with ???
package iterators:
/**
* Returns an iterator that generates the prime numbers
* 1,2,3,5,7,11,13,17,19,23,...
* (Strictly speaking, 1 is not considered prime in most of the modern definitions as including it causes problems in some other definitions but let's consider it to be a prime in this exercise.)
*/
def primesIterator: Iterator[Int] = new Iterator[Int]:
/* You'll need some private var or val to store the internal state.
* Put it/them here */
/**
* There is always the next prime number.
* In reality, we should return false when the next prime would be larger than that can
* be represented with an Int but, for the sake of focusing the exercise on the essential parts,
* this consideration is not required for getting the points.
*/
def hasNext = true
/**
* Get the next prime number.
* The first call should return 1, the next 2, then 3, 5, 7, 11 and so on.
*
* These numbers must be computed by the code here, one is not allowed to pre-compute and
* store them in an Array or similar data structure.
*/
def next(): Int =
???
end next
end primesIterator
/**
* Return a new iterator whose "next" method returns randomly an element that
* - is in the non-empty constructor argument set s, and
* - has not been returned during the blockingTime last calls to "next".
* That is, after being selected and returned by "next", an element is blocked in the
* the "blockingTime" subsequent calls to "next".
* The argument seed is the seed given to the pseudo-random number generator
* (fix it to have repeatable runs and set to System.nanoTime etc to have true pseudo-random sequences).
*/
def blockingRandomIterator[T](s: Set[T], blockingTime: Int, seed: Int = 2105): Iterator[T] = new Iterator[T]:
require(0 <= blockingTime && blockingTime < s.size)
/* Pseudo-random number generator.
* See http://www.scala-lang.org/api/current/index.html#scala.util.Random */
val r = new scala.util.Random(seed)
/* You'll need some var or val to store the internal state.
*
* Perhaps a queue to keep track of the blocked elements?
* See http://en.wikipedia.org/wiki/Queue_%28abstract_data_type%29 and
* http://docs.scala-lang.org/overviews/collections/concrete-mutable-collection-classes.html
*
* And perhaps a mutable Buffer to store the elements that are not blocked?
* By the way, a fast method to remove an element at index i from an unordered buffer
* is to just replace that element with the last element of the buffer and then shrink
* the buffer by dropping the last element (which is now a copy of the one in the i:th position)
* with dropRightInPlace(1)
*
* Put your state vars/vals here. */
/** There is always the next element */
def hasNext = true
/** Returns a random, non-blocked element */
def next(): T =
???
end next
end blockingRandomIterator
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 3 steps
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education