Output index for reformat component in abinitio - reformat

I have a acct_id and I need to split them to various output files using Segregate the data based on the acct_id mod 1000 value again
Please anyone suggest me achieving this.

Here is one way to achieve this
In your reformat component, put the below code in the "output-index" section
output_index_out :: output_index(in) =
begin
output_index_out :: if (in.data % 1000 == 0) 0
else if (in.data % 1000 == 1) 1
else if (in.data % 1000 == 2) 2
else 3;
end;
The above sample code supports 4 out-ports, you can expand it to the required number of out-ports

Related

DRY integer float converter ruby on rails/ruby

I have NoDataBase calculator app. It takes digit parameters from view, made calculations in controller with some methods and return answer. The issue is to show correct answer in view. I need to show exact float or integer.
I made some convertation, but it seems to looks ugly.
I wondering, how to implement DRY converter.
Links:
interest_calculator/index.html.erb
interest_calculator_controller.rb
number_to_number spec tests
persent_from_number spec tests
Rounding of float to 10 characters
# If accepted parameter is integer, then it shows in view as 5, when it
# is float, it shows as 5.1
#first_0 = params[:a_0].to_f % 1 != 0 ? params[:a_0].to_f : params[:a_0].to_i
#second_0 = params[:b_0].to_f % 1 != 0 ? params[:b_0].to_f : params[:b_0].to_i
#first_1 = params[:a_1].to_f % 1 != 0 ? params[:a_1].to_f : params[:a_1].to_i
#second_1 = params[:b_1].to_f % 1 != 0 ? params[:b_1].to_f : params[:b_1].to_i
integer_decimal_converter(#first_0, #second_0, #first_1, #second_1)
If you don't need global variables you can do something like this:
result = [:a_0, :b_0, :a_1, :b_1].map do |key|
value = params[key].to_f
value % 1 == 0 ? value.to_i : value
end
integer_decimal_converter(*result)

Export the data to excel & formatting that as requirements

I am able to give color, width for columns also & now I want to format a number that which we are sending to excel(i.e, I am sending 0 it should appear as 0.00 in excel): How is it possible?
gone through Define cell format on AlaSQL/JS-XLSX Excel Export,
I know it would be too late to answer this but would help for people who are looking for formatting number with fixed decimal as 2.
You can define your custom function by adding to alasql.fn object as below
alasql.fn.precise_round = function (num, dec) {
if ((typeof num !== 'number') || (typeof dec !== 'number'))
return false;
console.log(num);
var num_sign = num >= 0 ? 1 : -1;
return (Math.round((num * Math.pow(10, dec)) + (num_sign * 0.0001)) / Math.pow(10, dec)).toFixed(dec);
}
alasql.fn.numberWithCommas = function (num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
You can now use this function inside your alasql select query
var data = alasql("SELECT INTO XLSX precise_round(column_name) from ?", [objData]);
alasql("SELECT INTO XLSX('Test.xlsx', { headers: true}) FROM ?", [data]);
Assuming one of your column in objData holds numbers including 0 as well which is output as 0.00 as per your requirement

Total sum from a set (logic)

I have a logic problem for an iOS app but I don't want to solve it using brute-force.
I have a set of integers, the values are not unique:
[3,4,1,7,1,2,5,6,3,4........]
How can I get a subset from it with these 3 conditions:
I can only pick a defined amount of values.
The sum of the picked elements are equal to a value.
The selection must be random, so if there's more than one solution to the value, it will not always return the same.
Thanks in advance!
This is the subset sum problem, it is a known NP-Complete problem, and thus there is no known efficient (polynomial) solution to it.
However, if you are dealing with only relatively low integers - there is a pseudo polynomial time solution using Dynamic Programming.
The idea is to build a matrix bottom-up that follows the next recursive formulas:
D(x,i) = false x<0
D(0,i) = true
D(x,0) = false x != 0
D(x,i) = D(x,i-1) OR D(x-arr[i],i-1)
The idea is to mimic an exhaustive search - at each point you "guess" if the element is chosen or not.
To get the actual subset, you need to trace back your matrix. You iterate from D(SUM,n), (assuming the value is true) - you do the following (after the matrix is already filled up):
if D(x-arr[i-1],i-1) == true:
add arr[i] to the set
modify x <- x - arr[i-1]
modify i <- i-1
else // that means D(x,i-1) must be true
just modify i <- i-1
To get a random subset at each time, if both D(x-arr[i-1],i-1) == true AND D(x,i-1) == true choose randomly which course of action to take.
Python Code (If you don't know python read it as pseudo-code, it is very easy to follow).
arr = [1,2,4,5]
n = len(arr)
SUM = 6
#pre processing:
D = [[True] * (n+1)]
for x in range(1,SUM+1):
D.append([False]*(n+1))
#DP solution to populate D:
for x in range(1,SUM+1):
for i in range(1,n+1):
D[x][i] = D[x][i-1]
if x >= arr[i-1]:
D[x][i] = D[x][i] or D[x-arr[i-1]][i-1]
print D
#get a random solution:
if D[SUM][n] == False:
print 'no solution'
else:
sol = []
x = SUM
i = n
while x != 0:
possibleVals = []
if D[x][i-1] == True:
possibleVals.append(x)
if x >= arr[i-1] and D[x-arr[i-1]][i-1] == True:
possibleVals.append(x-arr[i-1])
#by here possibleVals contains 1/2 solutions, depending on how many choices we have.
#chose randomly one of them
from random import randint
r = possibleVals[randint(0,len(possibleVals)-1)]
#if decided to add element:
if r != x:
sol.append(x-r)
#modify i and x accordingly
x = r
i = i-1
print sol
P.S.
The above give you random choice, but NOT with uniform distribution of the permutations.
To achieve uniform distribution, you need to count the number of possible choices to build each number.
The formulas will be:
D(x,i) = 0 x<0
D(0,i) = 1
D(x,0) = 0 x != 0
D(x,i) = D(x,i-1) + D(x-arr[i],i-1)
And when generating the permutation, you do the same logic, but you decide to add the element i in probability D(x-arr[i],i-1) / D(x,i)

how to randomly select from list of variables (bitmaps) with incremental names using Iterator and/or loop ? (java, blackberry, bb)

I have a big list (say about thousand) of .png bitmaps with incremental names:
_image1 = Bitmap.getBitmapResource("a1.png");
_image2 = Bitmap.getBitmapResource("a2.png");
_image3 = Bitmap.getBitmapResource("a3.png");
...
_image999 = Bitmap.getBitmapResource("a999.png");
_image1000 = Bitmap.getBitmapResource("a1000.png");
I need a code to select one bitmap and attach it to BitmapField myBitmapField, when integer myCounter obtains random value from 1 to 1000. I could do it by checking value of myCounter thousand times by using if and else :
if (myCounter == 1)
myBitmapField.setBitmap(_image1);
else if (myCounter == 2)
myBitmapField.setBitmap(_image2);
else if (myCounter == 3)
myBitmapField.setBitmap(_image3);
...
else if (myCounter == 1000)
myBitmapField.setBitmap(_image1000);
But that would be very long code. Is there a way of doing it using Loop and/or Iterator ? Something like this:
int i = 0;
while (i < 1000)
{
i = i + 1;
if (myCounter == i)
myBitmapField.setBitmap(_image[i]);
}
Is there an easy and short way of doing it ? Thank you very much for help! (Java for blackberry)
What about dynamically generating the name, like
myBitmapField.setBitmap(Bitmap.getBitmapResource("a" + myCounter + ".png"));
If you're concerned about resource duplication, you can check a cache first.

How can I get Velocity to output a greater than / less than without escaping it?

I'm trying to get Velocity to output the following Javascript code:
if ((whichOne+1) <= numCallouts ) {
whichOne = whichOne + 1; } else {
whichOne = 1;
}
Whenever I try to get Velocity to print a > or a <, it represents it as a & gt; or & lt;, which doesn't help me since I'm trying to get it to produce Javascript. I've tried:
#set ( $gt = ">" )
But even that ends up as a & gt;
Thanks in advance.
It is not a default behavior, the only reason I can think of why this is happening is if you have event ReferenceInsertionEventHandler configured with EscapeHtmlReference either in your velocity.config or in the Velocity initialization code.
Here is more info about events
I've had the same issue with Velocity - however, the problem is that I was using Velocity as an third party embedded language, and didn't have access to change the Velocity settings.
Unfortunately the only solution I was able to find was to rewrite the code without using greater than/less than explicitly, which admittedly is awful, but it's all about getting it to work...
Here is an example workaround for conditionals where you are trying to see if one number is larger than another:
if (n1 > n2) //Doesn't work because velocity turns this into if (n1 > n2)
if (n1 != n2)
{
diff = n1 - n2;
abs = abs(n1 - n2);
if (diff / abs == 1) //Greater than
else //if == -1 then less than
}
else //Equal
Maybe you are able to use the alternate symbols as described here :
http://velocity.apache.org/engine/devel/vtl-reference-guide.html#aifelseifelse_-_Output_conditional_on_truth_of_statements
So try to use if (n1 gt n2).

Resources