[All Languages] Recursion Challenge 03-27-2014, 10:09 PM
#1
Problem: A telescope scans a rectangular area of the night sky and collects the data into a 1-dimensional array. Each data value scanned is a number representing the amount of light detected by the telescope. The telescope scans back and forth across the sky (alternating between left-to-right and right-to-left) in the pattern indicated below. This is telescope order.
{0.3, 0.7, 0.8, 0.4, 1.4, 1.1, 0.2, 0.5, 0.1, 1.6, 0.6, 0.9}
becomes
{0.3, 0.7, 0.8)
(1.1, 1.4, 0.4}
{0.2, 0.5, 0.1}
{0.9, 0.6, 1.6}
Essentially, you are recursively transforming a 1D array in to a 2D array. Here is how the transformation should be called:
Here is the class to build off of (in Java):
Here is an example answer in Java:
Post your answers as the source of the SkyView class!
{0.3, 0.7, 0.8, 0.4, 1.4, 1.1, 0.2, 0.5, 0.1, 1.6, 0.6, 0.9}
becomes
{0.3, 0.7, 0.8)
(1.1, 1.4, 0.4}
{0.2, 0.5, 0.1}
{0.9, 0.6, 1.6}
Essentially, you are recursively transforming a 1D array in to a 2D array. Here is how the transformation should be called:
Code:
static double [] arr = {0.3, 0.7, 0.8, 0.4, 1.4, 1.1, 0.2, 0.5, 0.1, 1.6, 0.6, 0.9};
public static void main(String[] args) {
SkyView SV = new SkyView(4, 3, arr);
System.out.println(SV.toString());
}Here is the class to build off of (in Java):
Code:
public class SkyView
{
private int rows;
private int cols;
private double [][] view;
private double [] scan;
public SkyView(int h, int w, double [] s) {
rows = h;
cols = w;
view = new double [h][w];
scan = s;
addToView();
}
// -- WHAT YOU NEED TO WRITE (ANY PARAMS YOU WANT) -- //
private void addToView() {
}
// -- END OF WHAT YOU NEED TO WRITE -- //
public String toString() {
StringBuffer sb = new StringBuffer();
for (double [] d : view) {
for (double e : d) {
sb.append(e + ", ");
}
sb.append("\r\n");
}
return sb.toString();
}
}Here is an example answer in Java:
Code:
public class SkyView
{
private int rows;
private int cols;
private double [][] view;
private double [] scan;
public SkyView(int h, int w, double [] s) {
rows = h;
cols = w;
view = new double [h][w];
scan = s;
addToView(0, 0, 0, 0);
}
// -- WHAT YOU NEED TO WRITE -- //
private void addToView(int v, int rpos, int cpos, int sw) {
if (rpos < rows) {
if (sw == 0) {
view[rpos][cpos] = scan[v];
if (cpos < (cols - 1)) {
addToView(v + 1, rpos, cpos + 1, sw);
} else {
addToView(v + cols, rpos + 1, 0, 1);
}
} else {
view[rpos][cpos] = scan[v];
if (cpos < (cols - 1)) {
addToView(v - 1, rpos, cpos + 1, sw);
} else {
addToView(v + cols, rpos + 1, 0, 0);
}
}
}
return;
}
// -- END OF WHAT YOU NEED TO WRITE -- //
public String toString() {
StringBuffer sb = new StringBuffer();
for (double [] d : view) {
for (double e : d) {
sb.append(e + ", ");
}
sb.append("\r\n");
}
return sb.toString();
}
}Post your answers as the source of the SkyView class!
![[Image: CDUAq9d.png]](http://i.imgur.com/CDUAq9d.png)





![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)
