What is the best/most correct way to iterate through the characters of a string in ESQL? - ibm-integration-bus

What is the best/most correct way to iterate through the characters of a string in ESQL?
In Java I would do like this but how do I do it in esql?
String s = "...stuff...";
for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
//Process char }

DECLARE text CHAR 'asdf';
DECLARE TOTAL INT;
DECLARE count INT 1;
SET TOTAL = LENGTH(text);
WHILE count <= TOTAL DO
DECLARE c CHAR;
SET c = SUBSTRING(text FROM count FOR 1);
SET count = count + 1;
END WHILE;

Related

Sort method c# string to char

I'm trying to do Sort method, however i get this error:
IndexOutOfRangeException, on the line if(chars[i] > chars1[y]). Amount is equal to 25
string temp1;
for (int i = 0; i < amount; i++)
{
for (int y = i + 1; y < amount - 1; y++)
{
var chars = Duomenys[i].Pozicija.ToCharArray();
var chars1 = Duomenys[y].Pozicija.ToCharArray();
if (chars[i] > chars1[y])
{............}
You are using the same indices (i and y) to identify locations within the array Duomenys and chars/chars1, which seem like very different things. Can't tell what you should be doing instead, given the lack of information provided.

Run (C#) algorithm assessment

I have been doing programming algorithms lately, just to practice :).
i got this question from a site. The problem is:
Write a function that finds the zero-based index of the longest run in a string. A run is a consecutive sequence of the same character. If there is more than one run with the same length, return the index of the first one.
For example, IndexOfLongestRun("abbcccddddcccbba") should return 6 as the longest run is dddd and it first appears on index 6.
So what i came up is this :
public static int IndexOfLongestRun(string str)
{
string retval = string.Empty;
string firstOccurence = string.Empty;
string maxOccurence = string.Empty;
string val1 = string.Empty;
string val2 = string.Empty;
int counter = 1;
int occurenceCounter = 1;
int maxOccur = 0;
for (int i = 0; i < str.Length; i++)
{
val1 = str[i].ToString();
if (i < str.Length - 1)
{
val2 = str[i + 1].ToString();
}
else
{
val2 = str[i].ToString();
}
if (val1 == val2)
{
firstOccurence = val1;
occurenceCounter = occurenceCounter + counter;
if (occurenceCounter > counter && occurenceCounter > maxOccur)
{
maxOccur = occurenceCounter;
maxOccurence = firstOccurence;
}
continue;
}
else
{
occurenceCounter = 1;
}
}
return str.IndexOf(maxOccurence, 0);
}
This past the me on the main purpose of the test. however it fails me on performance benchmark. Could anyone shed some light on how this code of mine can be optimized? Thanks.
You can optimize it in a few ways:
Firstly, occurenceCounter > counter condition in if is redundant.
Also, don't use str.IndexOf(). Instead, try to store index in a variable as soon as its occurrenceCounter gets greater than maxOccur. That should be easy. (Hint: You know current index and count)
Other than that, I don't see if the code can be optimized. Also, I'm not too familiar with C#, so, I don't know if you have a char type there (must be, surely). If you have to compare a single character, I'd advice not using strings to compare. (Something like toChar should be faster than toString).
Also, I don't think your code will pass all test cases. Check on: kaaabbb. Your code would return 4 (I think) (Correct o/p is 1).
For that, loop until string.length - 2 (not string.length - 1, as you are doing) and remove if condition on assignment of val2.
Why do you use transformation .ToString? It takes time. I suspect that C# has char type for string elements. And you'd better to work with indexes, not with run-strings. Pseudocode:
MaxLen = 0
MaxIndex = 0
StartIndex = 0
StartChar = s[0]
for i = 1 to s.Length - 1 do
if s[i] != StartChar then
Len = i - StartIndex
if MaxLen < Len then
MaxLen = Len
MaxIndex = StartIndex
StartIndex = i
StartChar = s[i]
Len = s.Length - StartIndex
if MaxLen < Len then
MaxLen = Len
MaxIndex = StartIndex

How to set a number string into a real string formation?

I'm now using NPOI to cope with Excel export, and here's my codes (part in .NET):
int rowIndex = 1;
for (int i = 0; i < dt.Rows.Count; i++)
{
IRow dataRow = sheet.CreateRow(rowIndex);
for (int j = 0; j < cellCount; j++)
{
cell = dataRow.CreateCell(j,CellType.String);
cell.SetCellValue(new HSSFRichTextString(dt.Rows[i][j].ToString()));
}
rowIndex++;
}
What makes me feel surprised is there's a list whose number string is "20150525", and it will be analyzed as "2015……E+10" formation (scientific number formation). However I wanna keep it as a string value. So How?
Thanks!
In fact we have to set a CellStyle, snippet of sample codes is below:
IRow row = book[0].CreateRow(rowIndex + 1);
ICell rowCell = null;
rowCell = row.CreateCell(colIndex);
rowCell.SetCellValue(realCellValue);
ICellStyle cellStyle = book.CreateCellStyle();
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#");
rowCell.CellStyle = cellStyle;

Iterate all possible variants of a sequence

I have a sequence of n letters: e.g. A B C A D , or A B F A
What I want is to get every possible variant with a comma between the letters.
i.e. for A B F A
A B F A
A,B F A
A B,F A
A B F,A
A,B,F A
A B,F,A
A,B F,A
A,B,F,A
Can anyone recommend a good algorithm for doing this? Language not important.
Simple solution to use binary array to represent if there is a comma or not.
A B F A contains three positions where comma may be (AB, BF, FA)
That means if you create 3-element array and try every possible combination of 0 and 1 you'll get the desired result. 000, 001, 010, 011, 100, 101, 110, 111
Simple program in java prints all binary permutation for n bits:
String s = "ABFA";
int bits = s.length() - 1;
int lastNumber = (int)Math.pow(2, bits);
System.out.println(lastNumber);
for (int i = 0; i < lastNumber; i++) {
System.out.println(completeZeros(Integer.toString(i, 2), bits));
}
static String completeZeros(String s, int bits) {
String result = s;
for (int i = 0; i < bits - s.length(); i++) {
result = "0" + result;
}
return result;
}
To apply binary permutation "010" to string "ABFA" use next function:
static String applyBinary(String s, String binary) {
String result = "" + s.charAt(0);
for (int i = 0; i < binary.length(); i++) {
if (binary.charAt(i) == '1') result += ", ";
result += s.charAt(i + 1);
}
return result;
}
The output is:
ABFA
ABF, A
AB, FA
AB, F, A
A, BFA
A, BF, A
A, B, FA
A, B, F, A
Use binary system for this task.
1 means comma is present, 0 means comma is not present. Each position in number informs about presence of another comma. For example for AFA:
00 : A F A
01 : A F,A
10 : A,F A
11 : A,F,A
Numbers must be taken from range [0 .. (n-1)^2-1]
Here's a simple JavaScript demonstration.
var str = "ABFA";
function algo(str) {
var result = [];
var n = str.length;
var total = Math.pow(n-1, 2) - 1;
for(var mask = 0; mask < total; mask++) {
var bits = mask;
var newstr = "";
for(var i=0; i<str.length - 1; i++, bits>>>=1) {
var hasComma = (bits & 1) == 1;
newstr += str.charAt(i);
newstr += (hasComma ? "," : " ");
}
newstr += str.charAt(str.length - 1);
result.push(newstr);
}
return result;
}
algo(str);
You calculate the total number of combinations "total"
You count up to that number "mask"
You use the binary bits of your counter "bits" to add commas
There are two approaches to this problem.
1.Recursive ( Start = "printAllComb ( s , "" , 0 );" )
printAllComb(string s, string const, int i)
{
if ( i == s.length() )
print const
printAllComb(s,const+string.at(i)+',',i+1);
printAllComb(s,const+string.at(i),i+1);
}
2.Dynamic Programming
char binaryS[s.length]="0000";
//Basically no. of zeros = no. of Alphabets in the string
//define a function AddOne() which adds 1 to the character representation
//AddOne() modifies the character array such that it stays in the bit representation
//Characters because to save space
while ( All the bits are not one )
{
for ( int i=0; i<s.length(); i++ )
{
print s.at(i)
if ( binaryS.at(i) == '1' )
print ","
}
print "\n"
AddOne();
}

Sort letters in huge string

I have a 16 MB text document containing a single huge string of letters and numbers without any separators. Excerpt: "as81jsa8sm1o1kmka9s93m1l"
Is there a simple way to alphabetize all of the characters, without having to write a script? I'm afraid JS will crash under the weight of the file.
Thanks.
If you know the string only contains letters and numbers, you can use a bucket sort and achieve good performance.
I am not sure what language you are using, so I'll assume you can read the string character by character. my solution is psuedocode
int[] buckets = int[36]; // 26 for letters, 10 for numbers; assuming only lowercase characters
while(string.hasNext()) {
char x = next character in string;
if(x.isAlpha()) {
buckets[x-'a']++;
}else {
buckets[26 + x - '0']++
}
}
To print out the sorted string:
string s = ""; // at the end of the loops, s will contain the sorted string
for(int i =0 ; i < 26; ++i) {
int y = buckets[i];
for(int j = 0; j < y; ++j) {
s+=(y+'a');
}
}
for(int i =0 ; i < 10; ++i) {
int y = buckets[i+26];
for(int j = 0; j < y; ++j) {
s+=(y+'0');
}
}

Resources