Login Register


[All Languages] Recursion Challenge filter_list
Author
Message
[All Languages] Recursion Challenge #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:
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]

Reply

RE: [All Languages] Recursion Challenge #2
I don't think you know what a 2D array is... This is a jagged array, not a 2D array:
Code:
private double [][] view;

Java actually doesn't even support *true* 2D arrays, so a jagged array is all you've got.

And if this is a multi-language challenge, you shouldn't restrict it to a class. C doesn't have classes for instance, and same with other languages as well.

Here's my solution in C:
Code:
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> float 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 }; void transform_array(float **dest, float *src, size_t r, size_t rows, size_t columns) { if (r >= rows) return; for (size_t i = 0; i < columns; ++i) { dest[r][i] = *(src + (r * columns) + (r & 1 ? columns - i - 1 : i)); } transform_array(dest, src, r + 1, rows, columns); } int main() { // initialization size_t rows = 4, columns = 3; size_t len = sizeof(arr[0]); float **ptr = malloc(rows * len); for (size_t i = 0; i < rows; ++i) ptr[i] = malloc(columns * len); transform_array(ptr, &arr[0], 0, 4, 3); for (size_t i = 0; i < rows; ++i) { for (size_t j = 0; j < columns; ++j) printf("%.1f ", ptr[i][j]); printf("\n"); free(ptr[i]); } }

Output:
Code:
0.3 0.7 0.8 1.1 1.4 0.4 0.2 0.5 0.1 0.9 0.6 1.6
(This post was last modified: 03-30-2014, 07:18 PM by 0xDEAD10CC.)

Reply

RE: [All Languages] Recursion Challenge #3
(03-30-2014, 06:43 PM)0xDEAD10CC Wrote: I don't think you know what a 2D array is... This is a jagged array, not a 2D array:
Code:
private double [][] view;

Java actually doesn't even support *true* 2D arrays, so a jagged array is all you've got.

And if this is a multi-language challenge, you shouldn't restrict it to a class. C doesn't have classes for instance, and same with other languages as well.

Here's my solution in C:
Code:
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> float 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 }; void transform_array(float **dest, float *src, size_t r, size_t rows, size_t columns) { if (r >= rows) return; for (size_t i = 0; i < columns; ++i) { dest[r][i] = *(src + (r * columns) + (r & 1 ? columns - i - 1 : i)); } transform_array(dest, src, r + 1, rows, columns); } int main() { // initialization size_t rows = 4, columns = 3; size_t len = sizeof(arr[0]); float **ptr = malloc(rows * len); for (size_t i = 0; i < rows; ++i) ptr[i] = malloc(columns * len); transform_array(ptr, &arr[0], 0, 4, 3); for (size_t i = 0; i < rows; ++i) { for (size_t j = 0; j < columns; ++j) printf("%.1f ", ptr[i][j]); printf("\n"); free(ptr[i]); } }

Output:
Code:
0.3 0.7 0.8 1.1 1.4 0.4 0.2 0.5 0.1 0.9 0.6 1.6

You make a fair point.

Blame the 2D array stuff vs. Jagged array bit on my Comp Sci teacher. Everything in our course is oversimplified so everyone can code, but it means most of my class sucks at theory.

As for classes, I didn't expect anyone to post a response in a language that doesn't have a class. I assumed 99% of people here were coding in language that has classes (C#, VB, Java, Python, etc.). Should have figured since you came with the HF merge you would be around still Wink2



For reference to someone reading this and wondering the difference between the two:

Code:
//this is an example of a 2d array int[,] x = new int[10,10]; //this is an example of a jagged array int[][] y = new int[10][]; //initialize the rest of the array here
[Image: CDUAq9d.png]

Reply

RE: [All Languages] Recursion Challenge #4
The difference simplified:

Jagged array: An array of arrays
2D array: A single array with internal partitions of arrays

And still, even a while after the merge, and after I mentioned it, all of my tutorial threads and challenges seem to be missing. They didn't know how to merge properly...

Reply

RE: [All Languages] Recursion Challenge #5
(03-30-2014, 08:41 PM)0xDEAD10CC Wrote: The difference simplified:

Jagged array: An array of arrays
2D array: A single array with internal partitions of arrays

And still, even a while after the merge, and after I mentioned it, all of my tutorial threads and challenges seem to be missing. They didn't know how to merge properly...

That's kind of odd how those got missed. All of my stuff made it over fine.
[Image: CDUAq9d.png]

Reply

RE: [All Languages] Recursion Challenge #6
He obviously didn't take the most up to date version of the SQL database or something. He just doesn't know what he's doing. I probably won't be on this forum for long, the only reason I'm here now was forcibly through the merge.

Reply

RE: [All Languages] Recursion Challenge #7
(03-30-2014, 06:43 PM)0xDEAD10CC Wrote: Here's my solution in C:
Code:
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> float 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 }; void transform_array(float **dest, float *src, size_t r, size_t rows, size_t columns) { if (r >= rows) return; for (size_t i = 0; i < columns; ++i) { dest[r][i] = *(src + (r * columns) + (r & 1 ? columns - i - 1 : i)); } transform_array(dest, src, r + 1, rows, columns); } int main() { // initialization size_t rows = 4, columns = 3; size_t len = sizeof(arr[0]); float **ptr = malloc(rows * len); for (size_t i = 0; i < rows; ++i) ptr[i] = malloc(columns * len); transform_array(ptr, &arr[0], 0, 4, 3); for (size_t i = 0; i < rows; ++i) { for (size_t j = 0; j < columns; ++j) printf("%.1f ", ptr[i][j]); printf("\n"); free(ptr[i]); } }

Output:
Code:
0.3 0.7 0.8 1.1 1.4 0.4 0.2 0.5 0.1 0.9 0.6 1.6

I really like your solution. It's concise. I was going to write one in C when I was done with a small project, but your own would have definitely looked more efficient Good job.

Reply

RE: [All Languages] Recursion Challenge #8
I might as well explain how it works since it doesn't look like too many more programmers here will submit an entry.

The array in the beginning is a standard 1D array (obvious). During the first parts of the main() function before calling transform_array(), I set the number of rows and columns to effectively multiply out to the total number of elements of the original linear array (4 * 3 = 12 elements). Here we have a pointer to pointers declared to be the length of the number of rows to store a pointer to each internal contiguous range of elements for each row in the resulting *table*, then we loop through and make sure to allocate enough memory for each of the elements of each row, which just needs to be equivalent to the number of columns multiplied by the size of the datatype in question.

When we pass the pointer to pointers to the function, along with a pointer to the first element of the original 1D array, and the properties for the current row, and number of rows and columns allocated in the destination... I look at the current row and determine whether we've directly assigned all of the rows to the destination by comparing it with the total number of rows allocated (= total number of pointers allocated for each row). If the row number is odd (we use some bitwise math to calculate this), then we initialize from end to start, instead of start to end.

All of the end code just outputs each integer from start address to end address for each pointer in the array of pointers to prove the results, along with free-ing up that memory, just for good causes.

Quote:
Code:
testing-testing-testing

What the f*ck? Why are dashes in quotes all messed up? They change to a very dark gray that nearly matches the background.
(This post was last modified: 03-31-2014, 02:44 AM by 0xDEAD10CC.)

Reply







Users browsing this thread: 1 Guest(s)