convert Row[] into two dimensional array in Java8 Spark - apache-spark

I want to convert Row[] list into two dimension Array String[][] using Java8 with Spark
Input DataFrame
+-------------------+----+-----+
| attribute|city|cntry|
+-------------------+----+-----+
|LOC1,LOC2,LOC3,LOC4| chn| AU|
| LOC1,LOC4| mdu| PE|
| LOC9,LOC7| sdu| US|
| LOC5,LOC6| fdu| CAN|
+-------------------+----+-----+
Please help me to get expected output.
Unable to get the expected output and getting only last row data is stored.
Using Java8 with Spark
Dataset<Row> df1 = ss.read().option("inferSchema", true).format("json").load("src/main/resources/input.json");
String[][] outputList = new String[100][100];
Row[] colList = (Row[]) df1.collect();
int rowCount = (int) df1.count();
for (Row rw : colList) {
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < rw.size(); j++) {
outputList[i][j] = rw.get(j).toString();
}}}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
System.out.println("outputList[" + i + "][" + j + "]" + outputList[i][j]);
}}
Expected Output should be as below
outputList[0][0]:LOC1,LOC2,LOC3,LOC4
outputList[0][1]:chn
outputList[0][2]:AU
outputList[1][0]:LOC1,LOC4
outputList[1][1]:mdu
outputList[1][2]:PE
outputList[2][0]:LOC9,LOC7
outputList[2][1]:sdu
outputList[2][2]:US
outputList[3][0]:LOC5,LOC6
outputList[3][1]:fdu
outputList[3][2]:CAN

try it
Row[] rows = (Row[]) df.collect();
int cSize = rows[0].size();
int rSize = rows.length;
String[][] outputList = new String[rSize][cSize];
for (int i = 0; i < rSize; i++) {
Row row = rows[i];
for (int j = 0; j < cSize; j++) {
String element = row.get(j).toString();
outputList[i][j] = element;
}
}

Related

Digit Dynamic Programming Problem For Sum of Numbers

I want to find the sum of all the positive integers in the range [1, N] with a given digit sum d. For example, if n = 100 and d = 7, the answer will be 7 + 16 + 25 + 34 + 43 + 52 + 61 + 70 = 308.
Following code can be used to count the numbers in the range [1, N] with a given digit sum d.
cnt[i][0][s] denotes count of suffixes that can be formed starting from index i, whose digits add up to s.
cnt[i][1][s] count of suffixes that can be formed starting from index i, whose digits add up to s such that the formed suffix is not greater than corresponding suffix in input string
#include <bits/stdc++.h>
using namespace std;
typedef long long int i64;
i64 cnt[20][2][200];
void digit_sum_dp(string ss) {
int n = ss.size();
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 200; k++) {
cnt[i][j][k] = 0;
}
}
}
cnt[n][0][0] = 1;
cnt[n][1][0] = 1;
for (int i = n - 1; i >= 0; i--) {
for (int tight = 0; tight < 2; tight++) {
for (int sum = 0; sum < 200; sum++) {
if (tight) {
for (int d = 0; d <= ss[i] - '0'; d++) {
if (d == ss[i] - '0') {
cnt[i][1][sum] += cnt[i + 1][1][sum - d];
} else {
cnt[i][1][sum] += cnt[i + 1][0][sum - d];
}
}
} else {
for (int d = 0; d < 10; d++) {
cnt[i][0][sum] += cnt[i + 1][0][sum - d];
}
}
}
}
}
return cnt[0][1][d];
}
int main() {
string str = "100";
int d = 7;
cout << digit_sum_dp(str, d) << "\n";
return 0;
}
I have tried to extend the code to find out the sum of numbers instead of the count of numbers. Following is a code snippet.
cnt[i][1][sum] += cnt[i + 1][1][sum - d];
tot[i][1][sum] += (d * cnt[i + 1][1][sum - d] + tot[i + 1][1][sum - d] * pow(10, i));
I am getting incorrect results for some of the inputs. I shall be grateful if someone can help me.

Which items were selected during Unbounded Knapsack algorithm?

I am using 1D array to get the final answer, but I also need to get selected items. How to achieve that?
private static int UnboundedKnapsack(int capacity, int n, int[] itemValue, int[] itemWeight)
{
int[] dp = new int[capacity + 1];
for (int i = 0; i <= capacity; i++)
{
for (int j = 0; j < n; j++)
{
if (itemWeight[j] <= i)
{
dp[i] = Math.Max(dp[i], dp[i - itemWeight[j]] + itemValue[j]);
}
}
}
return dp[capacity];
}
Let's introduce a new path function that gives the optimal selcetions of items using the previously calculated dp array.
private static void path(int capacity, int n, int[] itemValue, int[] itemWeight, int[] dp){
if(capacity == 0) return; // here you handle when the function will end. I assume capacity should be empty at the last
int ans = 0, chosenItem;
for(int j = 0; j < n; j++){
int newAns = dp[capacity - itemWeight[j]] + itemValue[j];
if(newAns > ans){
ans = newAns;
chosenItem = j;
}
}
printf("%d ",chosenItem); // here you get the current item you need to select;
path(capacity - itemWeight[chosenItem], n, itemValue, itemWeight, dp);
}

Getting timeout error for min_sub_array_sum?

I am getting this kind of weird error, I wrote a function to find the minimum sub array sum. But this doesn't work when the values of array start from 1 to size for value 1 2 3 4. I get timeout, but same code for 1 2 3 4 5 gives the correct answer. If I put a statement print statement above min_sub_array it gives the right answer for all values. It also works fine when the values start from 0 to size - 1
int min_sub_array_sum(int d[], int size)
{
for(int i = 1; i <= size; i++)
d[i] = -d[i];
int max_end_i = d[1], max_so_far = d[1];
for(int i = 2; i <= size; i++)
{
max_end_i += d[i];
if(max_so_far < max_end_i)
max_so_far = max_end_i;
if(max_end_i < 0)
max_end_i = 0;
}
return (-max_so_far);
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int d[n];
for(int i = 1; i <= n; i++)
cin>>d[i];
cout<<min_sub_array_sum(d, n)<<'\n';
}
}
I should get the value of 1 for 1 2 3 4. But I am not getting.
You are accessing the nth index of array d[n] which does not exist.

Why nested for matrix iteration with explicit indices (javascript-like) in Python causes IndexError?

I need the equivalent of this javascript code:
var a = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]];
var b = [[6,8,3],[0,9,6],[1,8,4],[5,1,3]];
var r = [[0,0,0],[0,0,0],[0,0,0],[0,0,0]];
var rows=4;
var cols=3;
for(var i = 0; i < rows; i++){
for(var j = 0; j < cols; j++){
r[i][j] += a[i][j] * b[j][i];
}
}
So I have created this Python code:
a = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
b = [[6,8,3],[0,9,6],[1,8,4],[5,1,3]]
r = [[0,0,0],[0,0,0],[0,0,0],[0,0,0]]
rows=4
cols=3
for i in range(rows):
for j in range(cols):
r[i][j] += a[i][j] * b[j][i]
but unfortunately in Python seems this approach doesn't work and my code crashes with IndexError: list index out of range
in the line
r[i][j] += a[i][j] * b[j][i]
Why this happens?
I think, it happens because you write b[j][i], but correct b[i][j].

C++\CLI datagridview export to excel .xls file

The errors I have: don't create excel file just add another Microsoft excel in background process and saveFileDialog crash when I try to change file location:
saveFileDialog1->InitialDirectory = "C:";
saveFileDialog1->Title = "Save as Excel File";
saveFileDialog1->FileName = "";
saveFileDialog1->Filter = "Excel Files(2003)|*.xls|Excel Files(2007)|*.xlsx";
if(saveFileDialog1>ShowDialog()==System::Windows::Forms::DialogResult::OK){
Microsoft::Office::Interop::Excel::Application^ ExcelApp = gcnew Microsoft::Office::Interop::Excel::ApplicationClass();
ExcelApp->Workbooks->Add(Type::Missing);
for (int i = 1; i < datagridview1->Columns->Count + 1;i++)
{
ExcelApp->Cells[1, i] = datagridview1->Columns[i - 1]->HeaderText;
}
for (int i = 0; i < datagridview1->Rows->Count; i++)
{
for (int j = 0; j < datagridview1->Columns->Count; j++)
{
ExcelApp->Cells[i+2,j+1] = datagridview1->Rows[i]->Cells[j]->Value->ToString();
}
}
ExcelApp->ActiveWorkbook->SaveCopyAs(saveFileDialog1->FileName->ToString());
ExcelApp->ActiveWorkbook->Saved=true;
ExcelApp->Quit();
I had a similar problem once, the problem is in rows and cells writhing your datagridview1 into file. Code should look like this:
saveFileDialog1->Title = "Save as Excel File";
saveFileDialog1->FileName = "";
saveFileDialog1->Filter = "Excel Files(2003)|*.xls|Excel Files(2007)|*.xlsx";
if(saveFileDialog1>ShowDialog()==System::Windows::Forms::DialogResult::OK){
Microsoft::Office::Interop::Excel::Application^ ExcelApp = gcnew Microsoft::Office::Interop::Excel::ApplicationClass();
ExcelApp->Workbooks->Add(Type::Missing);
for (int i = 1; i < datagridview1->Columns->Count + 1;i++)
{
ExcelApp->Cells[1, i] = datagridview1->Columns[i - 1]->HeaderText;
}
for (int i = 0; i < datagridview1->Rows->Count; i++)
{
for (int j = 0; j < datagridview1->Columns->Count; j++)
{
ExcelApp->Cells[i + 2, j + 1] = datagridview1->Rows[i]->Cells[j]->Value;
safe_cast<Range^>(ExcelApp->Cells[i + 2, j + 1]); }
}
ExcelApp->ActiveWorkbook->SaveCopyAs(saveFileDialog1->FileName->ToString());
ExcelApp->ActiveWorkbook->Saved=true;
ExcelApp->Quit();

Resources