Skip to content

Commit fb30f25

Browse files
author
Deepak Malik
committed
Hopping Iterator
1 parent b8542c6 commit fb30f25

File tree

3 files changed

+218
-1
lines changed

3 files changed

+218
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Implementation of Algorithms in Java, Most of these names are picked up from Gee
3131
- [X] [Standard Iterator](../master/src/com/deepak/algorithms/Iterators/StandardIterator.java)
3232
- [ ] Deep Iterator
3333
- [ ] Filtering Iterator
34-
- [ ] Hopping Iterator
34+
- [X] [Hopping Iterator](../master/src/com/deepak/algorithms/Iterators/HoppingIterator.java)
3535
- [ ] InOrder Iterator
3636
- [X] [LevelOrder Iterator](../master/src/com/deepak/algorithms/Iterators/LevelOrderIterator.java)
3737
- [ ] MaxIterator Iterator
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* Algorithms-In-Java
3+
* HoppingIterator.java
4+
*/
5+
package com.deepak.algorithms.Iterators;
6+
7+
import java.util.Iterator;
8+
import java.util.NoSuchElementException;
9+
10+
/**
11+
* <br> Problem Statement :
12+
*
13+
* Implement an iterator that hops specified number of times and then returns the next
14+
* element after the hop. Note: the iterator always returns the first element as
15+
* it is, and starts hopping only after the first element.
16+
*
17+
* Examples:
18+
*
19+
* If the original iterator returns: [1, 2, 3, 4, 5] in order, then the hopping
20+
* iterator will return [1, 3, 5] in order when the hop value is 1.
21+
*
22+
* If the original iterator returns: [1, 2, 3, 4, 5] in order, then the hopping
23+
* iterator will return [1, 4] in order when the hop value is 2.
24+
*
25+
* If the original iterator returns: [1, 2, 3, 4, 5] in order, then the hopping
26+
* iterator will return [1, 5] in order when the hop value is 3.
27+
*
28+
* Methods expected to be implemented:
29+
*
30+
* public class HoppingIterator<T> implements Iterator<T> {
31+
* public HoppingIterator(Iterator<T> iterator, int numHops) {...}
32+
* public boolean hasNext() {...}
33+
* public T next() {...}
34+
* }
35+
*
36+
* </br>
37+
*
38+
* @author Deepak
39+
*/
40+
public class HoppingIterator<T> implements Iterator<T> {
41+
42+
/* Iterator */
43+
private final Iterator<T> iterator;
44+
/* Variable to keep track of next item */
45+
private T nextItem;
46+
/* Number of hops needed */
47+
private final int numOfHops;
48+
/* Flag to check if it is first element */
49+
private boolean first;
50+
51+
/**
52+
* Constructor
53+
*
54+
* @param iterator
55+
* @param numOfHops
56+
*/
57+
public HoppingIterator(Iterator<T> iterator, int numOfHops) {
58+
if (numOfHops < 0) {
59+
throw new IllegalArgumentException("Invalid value for number of hops!!");
60+
}
61+
this.iterator = iterator;
62+
this.numOfHops = numOfHops;
63+
nextItem = null;
64+
first = true;
65+
}
66+
67+
/**
68+
* Method to check of next element exists
69+
*/
70+
@Override
71+
public boolean hasNext() {
72+
/* If we already have next item, return true */
73+
if (nextItem != null) {
74+
return true;
75+
}
76+
/* If it is not first element, move till the next hop */
77+
if (!first) {
78+
for (int hop = 0; hop < numOfHops && iterator.hasNext(); hop++) {
79+
iterator.next();
80+
}
81+
}
82+
/* Now, if next element exits. move there and update first flag */
83+
if (iterator.hasNext()) {
84+
nextItem = iterator.next();
85+
first = false;
86+
}
87+
return nextItem != null;
88+
}
89+
90+
/**
91+
* Method to find next element in collection
92+
*/
93+
@Override
94+
public T next() {
95+
/* If no next element exits, collection has finished */
96+
if (!hasNext()) {
97+
throw new NoSuchElementException("No Element left in collection!!");
98+
}
99+
/* Else, find the next item, return it and set next item to null */
100+
T itemToReturn = nextItem;
101+
nextItem = null;
102+
return itemToReturn;
103+
}
104+
105+
/**
106+
* Method to remove the element
107+
*/
108+
@Override
109+
public void remove() {
110+
throw new UnsupportedOperationException("Remove not supported!!");
111+
}
112+
113+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* Algorithms-in-Java
3+
* HoppingIterator_Test.java
4+
*/
5+
package com.deepak.algorithms.Iterators;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import org.junit.Assert;
11+
import org.junit.Test;
12+
13+
/**
14+
* Test cases for Hopping Iterator
15+
*
16+
* @author Deepak
17+
*/
18+
public class HoppingIterator_Test {
19+
20+
/**
21+
* Test case for collection of integers
22+
*/
23+
@Test
24+
public void testHoppingIteratorForIntegers() {
25+
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5, 6);
26+
27+
/* Iterator with 0 hops, behaves the same way */
28+
HoppingIterator<Integer> iteratorWith0Hops = new HoppingIterator<>(integerList.iterator(), 0);
29+
Assert.assertTrue(iteratorWith0Hops.hasNext());
30+
Assert.assertTrue(iteratorWith0Hops.next() == 1);
31+
Assert.assertTrue(iteratorWith0Hops.next() == 2);
32+
Assert.assertTrue(iteratorWith0Hops.next() == 3);
33+
Assert.assertTrue(iteratorWith0Hops.hasNext());
34+
Assert.assertTrue(iteratorWith0Hops.next() == 4);
35+
Assert.assertTrue(iteratorWith0Hops.hasNext());
36+
Assert.assertTrue(iteratorWith0Hops.next() == 5);
37+
Assert.assertTrue(iteratorWith0Hops.next() == 6);
38+
39+
/* Iterator with 1 hop, jumps 1 */
40+
HoppingIterator<Integer> iteratorWith1Hops = new HoppingIterator<>(integerList.iterator(), 1);
41+
Assert.assertTrue(iteratorWith1Hops.hasNext());
42+
Assert.assertTrue(iteratorWith1Hops.next() == 1);
43+
Assert.assertTrue(iteratorWith1Hops.next() == 3);
44+
Assert.assertTrue(iteratorWith1Hops.hasNext());
45+
Assert.assertTrue(iteratorWith1Hops.next() == 5);
46+
47+
/* Iterator with 2 hops, jumps 2 */
48+
HoppingIterator<Integer> iteratorWith2Hops = new HoppingIterator<>(integerList.iterator(), 2);
49+
Assert.assertTrue(iteratorWith2Hops.hasNext());
50+
Assert.assertTrue(iteratorWith2Hops.next() == 1);
51+
Assert.assertTrue(iteratorWith2Hops.next() == 4);
52+
Assert.assertFalse(iteratorWith2Hops.hasNext());
53+
54+
/* Iterator with 3 hops, jumps 3 */
55+
HoppingIterator<Integer> iteratorWith3Hops = new HoppingIterator<>(integerList.iterator(), 3);
56+
Assert.assertTrue(iteratorWith3Hops.hasNext());
57+
Assert.assertTrue(iteratorWith3Hops.next() == 1);
58+
Assert.assertTrue(iteratorWith3Hops.next() == 5);
59+
Assert.assertFalse(iteratorWith3Hops.hasNext());
60+
}
61+
62+
/**
63+
* Test case for collection of strings
64+
*/
65+
@Test
66+
public void testHoppingIteratorForStrings() {
67+
List<String> stringList = Arrays.asList("A", "B", "C", "D", "E", "F");
68+
69+
/* Iterator with 0 hops, behaves the same way */
70+
HoppingIterator<String> iteratorWith0Hops = new HoppingIterator<>(stringList.iterator(), 0);
71+
Assert.assertTrue(iteratorWith0Hops.hasNext());
72+
Assert.assertTrue(iteratorWith0Hops.next() == "A");
73+
Assert.assertTrue(iteratorWith0Hops.next() == "B");
74+
Assert.assertTrue(iteratorWith0Hops.next() == "C");
75+
Assert.assertTrue(iteratorWith0Hops.hasNext());
76+
Assert.assertTrue(iteratorWith0Hops.next() == "D");
77+
Assert.assertTrue(iteratorWith0Hops.hasNext());
78+
Assert.assertTrue(iteratorWith0Hops.next() == "E");
79+
Assert.assertTrue(iteratorWith0Hops.next() == "F");
80+
81+
/* Iterator with 1 hop, jumps 1 */
82+
HoppingIterator<String> iteratorWith1Hops = new HoppingIterator<>(stringList.iterator(), 1);
83+
Assert.assertTrue(iteratorWith1Hops.hasNext());
84+
Assert.assertTrue(iteratorWith1Hops.next() == "A");
85+
Assert.assertTrue(iteratorWith1Hops.next() == "C");
86+
Assert.assertTrue(iteratorWith1Hops.hasNext());
87+
Assert.assertTrue(iteratorWith1Hops.next() == "E");
88+
89+
/* Iterator with 2 hops, jumps 2 */
90+
HoppingIterator<String> iteratorWith2Hops = new HoppingIterator<>(stringList.iterator(), 2);
91+
Assert.assertTrue(iteratorWith2Hops.hasNext());
92+
Assert.assertTrue(iteratorWith2Hops.next() == "A");
93+
Assert.assertTrue(iteratorWith2Hops.next() == "D");
94+
Assert.assertFalse(iteratorWith2Hops.hasNext());
95+
96+
/* Iterator with 3 hops, jumps 3 */
97+
HoppingIterator<String> iteratorWith3Hops = new HoppingIterator<>(stringList.iterator(), 3);
98+
Assert.assertTrue(iteratorWith3Hops.hasNext());
99+
Assert.assertTrue(iteratorWith3Hops.next() == "A");
100+
Assert.assertTrue(iteratorWith3Hops.next() == "E");
101+
Assert.assertFalse(iteratorWith3Hops.hasNext());
102+
}
103+
104+
}

0 commit comments

Comments
 (0)