How to draw text using loadStrings(); and text();? - string

basic loadstrings() code:
String[] txt = loadStrings("/Users/rjth/Desktop/data.txt");
println("there are " + txt.length + " lines");
println(txt);
when I add text(); or RG.getText(); I am given an error code:
String[] txt = loadStrings("/Users/rjth/Desktop/data.txt");
println("there are " + txt.length + " lines");
println(txt);
text(txt, 1, 1);
error: The method text(char[], int, int float, float) in the type PApplet is not applicable for the arguments (String[], int, int)
where is the problem?

well that's because txt as you set it is an array (that's what loadstrings gives out).. You have to iterate over it and set the text to each one of its elements like this:
for(int i = 0; i < txt.length; i++) text(txt[i],1,1);
or:
for(String s: txt) text(s,1,1);

Related

How to rearrange the string in C#

\In my csv file have two row data :
\a;b;c;d;e;
\1;2;3;4;5;
\How do I make it as below in c#:
\a=1;enter code here
\b=2;enter code here
\c=3;enter code here
\d=4;enter code here
\e=5;enter code here
tldr; you split the text.
got string from csv //e.g. string originalTxt = #"\a;b;c;d;e; \1;2;3;4;5";
split row
rearrange/edit for each word in each row
combine
string resultTxt = "";
string mycodehere ="enter code here ";
string originalTxt = ...; //TODO :: your code for get text from csv
string[2] rows = originalTxt.Split('\'); // split the \ got 2 rows
string[] row1 = rows[0].Split(';'); //split each word
string[] row2 = rows[1].Split(';'); //split each word
for(int i= 0 ; i< row1.Count ; i++){
resultTxt += #"\" + row1[i] + "=" + row2[i] + ";" + mycodehere + " " ;
}
//TODO :: use resultTxt

Remove duplicate string elements C++

The problem consists of finding all permutations using k out of n digits. I'm able to find all the permutations, but I'm struggling trying to erase duplicates. I can successfully compare and find the duplicates, but erasing them is what I'm struggling to do. I have a feeling I'm missing something simple but I don't know what it is.
Any help would be greatly appreciated. I've been staring at this for a week.
Here is the code that I've got right now.
void getPermutations(int n, int k)
{
string str = "";
//fill string with numbers <= n
for(int i = 0; i < n; i++)
{
str += to_string(i); //convert numbers to string
}
string tempStr = "";
string outputStr = "";
do {
tempStr = str.substr(0, k);
int compareResult = tempStr.compare(0, k, outputStr, 0, k);
if (compareResult == 0)
{
cout << "| same | ";
outputStr.erase(k,k);
}
outputStr = tempStr;
cout << outputStr << " ";
} while (next_permutation(str.begin(), str.end()));
}
I think what you meant to do was to erase the contents of tempStr, not outputStr.
The call to erase is not exactly right. Its first argument marks the starting position of your erasing, and the second argument tells how many characters to erase. So if you want to erase the whole string, the first argument should be...
You actually don't have to erase anything. After you get it working your way, try to do it without erasing!
Good Luck!

CSV file parsing in C#

Wanted to check if there is any null value in column field in the csv file and also there shouldn't be null value after / in number column, if it is null the entire row should not be written to output file.
name,number,gender,country
iva 1/001 f Antartica
aaju 2/002 m russia
lax 3/ m brazil
ana 4/004 f Thailand
vis 5/005 m
for e.g. 3rd and 5th row should not be written to output file.
using (StreamWriter file = new StreamWriter(filepathop)) {
for (int i = 0; i < csv.Length; i++) {
{
if (i == 0) {
file.WriteLine(header + "," + "num" + "," + "serial" + "," + "date");
}
else {
var newline = new StringBuilder();
string[] words = csv[i].Split(',');
string[] no = words[1].Split('/');
string number = no[0];
string serial = no[1];
newline.Append(number + "," + serial + "," + tokens[0]);
file.WriteLine(csv[i] + "," + newline);
}
}
}
}
}
}
}
You can test for null columns with string.IsNullOrEmpty(column) or column.Length == 0 like so:
if (!string.IsNullOrEmpty(serial) && !string.IsNullOrEmpty(country))
file.WriteLine(csv[i] + "," + newline);
You might want to check and remove white space, too. Depends on your input.

How to calculate average from multiple values in Spark

I have a mapper that emit key/value pairs(composite keys and composite values separated by comma).
e.g
key: a,b,c,d Value: 1,2,3,4,5
key: a1,b1,c1,d1 Value: 5,4,3,2,1
...
...
key: a,b,c,d Value: 5,4,3,2,1
I could easily SUM these values using reduceByKey.
e.g
reduceByKey(new Function2<String, String, String>() {
#Override
public String call(String value1, String value2) {
String oldValue[] = value1.toString().split(",");
String newValue[] = value2.toString().split(",");
int iFirst = Integer.parseInt(oldValue[0]) + Integer.parseInt(newValue[0]);
int iSecond = Integer.parseInt(oldValue[1]) + Integer.parseInt(newValue[1]);
int iThird = Integer.parseInt(oldValue[2]) + Integer.parseInt(newValue[2]);
int iFourth = Integer.parseInt(oldValue[3]) + Integer.parseInt(newValue[3]);
int iFifth = Integer.parseInt(oldValue[4]) + Integer.parseInt(newValue[4]);
return iFirst + "," + iSecond + ","
+ iThird+ "," + iFourth+ "," + iFifth;
}
});
But the problem is how do I find average of just one of these values. Lets assume I want to SUM iFirst, iSecond, iThird and iFourth but I want to find Average of iFifth. How do i do it? With a simple key/value pairs I could use mapValues function but not sure how I could do it with my example. Please advice.
I've used foldByKey function to resolve this issue.

String Format Issue

I've got the following method:
public static string ReturnFormat(string input, int maxLength, int decimalPrecision, char formatChar)
{
string[] format = new string[2];
string[] inputs = new string[2];
inputs = input.Split(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0]);
if (input.Length > maxLength)
{
int offset = 0;
int counter = 0;
if (inputs[0].Length > maxLength - (1 + decimalPrecision))
{
offset = maxLength - (1 + decimalPrecision);
}
else
offset = inputs[0].Length;
for (int i = 0; i < offset; i++)
{
format[0] += formatChar;
if (counter < decimalPrecision)
{
format[1] += '0';
counter++;
}
}
System.Windows.Forms.MessageBox.Show("{0:" + format[0] + "." + format[1] + "}");
return String.Format(CultureInfo.CurrentCulture, "{0:" + format[0] + "." + format[1] + "}", input);
}
else
return input;
}
Which say I'm using as:
ReturnFormat("12.3456789011243", 10, 2, '#') // format is {0:##.00} // output 12.3456789011243
ReturnFormat("12345678901.1243", 10, 2, '#') // format is {0:#######.00} // output 12345678901.1243
Now my issue is that the input string is not formatted well, still the format strig appears to be ok.
Any ideas of what I'm doing wrong?
Your input is a String not a Double, so it gets formatted like a string: the formatting does not know about decimal places in that case.
You could use Double.Parse() to transform the string into a Double value, but take care of using the right culture.
Another thing, is there a specific reason for not using the more natural format {0:0.00} in both cases? If you really mean to use a placeholder for digits then # is ok, otherwise 0 is best.
Tested solution (beware it truncates and does not round) I needed some time to understand what was actually wanted:
public static string ReturnFormat(string input, int maxLength, int decimalPrecision)
{
if (input.Length <= maxLength)
return input;
Char separator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
string[] inputs = input.Split(separator);
// NB: truncating rather than rounding
if (inputs[1].Length > decimalPrecision)
inputs[1] = inputs[1].Substring(0, decimalPrecision);
int digits = (maxLength - decimalPrecision - 1);
// NB: truncating rather than rounding, adding ~ to signalize the
// presence of missing significant digits
if (inputs[0].Length > digits)
inputs[0] = inputs[0].Substring(0, digits-1) + "~";
return inputs[0] + separator + inputs[1];
}

Resources