## Data Structure

Browsing category ## Solar Doomsday

Yesterday I was bored at work and decided to see if I could get back into the Google Foo Bar challenge.  I once again tried all the known combinations that had gotten me in before, and surprise surprise… I got back in.  This time it was the solar doomsday challenge, which I have gotten before.  I intially solved this challenge in Java, and for this attempt I decided to try and solve it in Python.  Both answers are pretty similar, and I’m sure there is a more eloquent solution, but it’s what I came up with.  The Java version was done first, and then Python one follows the same algorithm, just with the Python syntax.  Both use recursion to solve the problem.  The main difference is in the data structure used to hold the final list.  In Java I used a deque, which is a faster version of a linked list when used a queue.  In Python I just went with a standard list and reversed the list before returning the final answer.  Both work, but Python’s seems easier and uses less space than the Java version.

## Python Version

```

'''
Created on Feb 15, 2017

@author: Tony Harrell
'''
import math;

fList = [];

solarPanelCalc(area, fList);
fList.reverse();
return fList;

def solarPanelCalc(area, fList):
pArea = 0;

if area >= 1 and area <= 1000000:
tmp = int(math.sqrt(area));
print tmp;
if tmp <= 0:
return pArea;
else:
pArea = int(math.pow(tmp, 2));
nArea = area - pArea;
solarPanelCalc(nArea, fList);
fList.append(pArea);
return pArea;

```

## Java Version

```
import java.util.ArrayDeque;
import java.util.Arrays;

public class SolarChallenge {

int[] finalList = {};
ArrayDeque&amp;lt;Integer&amp;gt; areaQueue = new ArrayDeque&amp;lt;&amp;gt;();

solarPanelCalc(area, areaQueue);

finalList = new int[areaQueue.size()];
for(int i=areaQueue.size()-1; i&amp;gt;=0; i--){
finalList[i] = areaQueue.pop();
}

return finalList;
}

public static int solarPanelCalc(int area, ArrayDeque&amp;lt;Integer&amp;gt; aq){
int panelArea = 0;

if(area &amp;gt;= 1 &amp;amp;&amp;amp; area &amp;lt;= 1000000){
int temp = (int) Math.sqrt(area);
if(temp &amp;lt;= 0){
return panelArea;
}else{
panelArea = (int) Math.pow(temp, 2);
int nextArea = area - panelArea;
solarPanelCalc(nextArea, aq);
}
}
return panelArea;
}

public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println(Arrays.toString(ans1));

}

}

``` 