For generating the bingo ticket generator I need the shuffling array .
When I press a button I should retrive the values from array (ex. array(1,2,3,4,5,6,7,8,9)). I
If I retrive first five random value may be 2 5 7 4 8. If press the button again then it should retrive other than previously retrived value (ex. 1 3 9 6 7)
I don't know if you are allowed to modify your input but why not try something like this :
// passing your array as argument
// passing the total number you want to extract as argument
function getRandNumbers( a:Array, requested_numbers:Number ):Array
{
// verify we don't request to much numbers
if ( requested_numbers > a.length )
{
trace( "Not enought available numbers in array" );
return null;
}
results_array = new Array(); // create our output array
while( results_array.length < requested_numbers )
{
rnd = Math.floor( Math.random() * a.length );
results_array.push( a[rnd] );
a.splice( rnd, 1 ); // remove the random result
}
}
now you're sure your array will contain only non used numbers each time you will call getRandNumbers.
Related
My idea is to subtract each value from my list through the value of a variable, for example:
var subtraction = 250
var list = mutableListOf(300, 200, 100)
Then, using the 250 of the subtraction variable,
you can dynamically subtract each value of the item,
from the last to the first, so with that 250 the program should return: -> list(300, 50).
Where 250 is subtracted from item 100 (last item) and then "150" remains from the value "250",
and the remaining 150 is subtracted from 200 (second item) and remains 50,
thus zeroing out the value of "250" and the program stop.
Getting (300, 50) -> 50 which comes from 200 (second item).
As if I was going through my list of numbers, subtracting item by item through the value of a variable, from last to first.
Your question still needs further clarification:
What should be the output if subtraction = 700?
What should be the output if subtraction = 600?
What should be the output if subtraction = 100?
The following can be a starting point to solve your question:
fun subtraction() {
var subtraction = 250
var list = mutableListOf(300, 200, 100)
// Loop the list in Reverse order
for (number in list.indices.reversed()) {
subtraction -= list[number] // Subtract the last number
// If subtraction is greater than zero, continue and remove the last element
if (subtraction > 0)
list.removeAt(number)
else {
// It subtraction is less than or equal to zero,
// remove the last element, append the final subtraction result,
// and stop the loop
list.removeAt(number)
list.add(abs(subtraction))
break
}
}
// If the subtraction result is still positive after whole list has been processed,
// append it back to the list
// if (subtraction > 0)
// list.add(subtraction)
println(list)
}
Output
[300, 50]
The question isn't very clear, but as I understand it: OP wants to modify a list of numbers, subtracting a fixed amount from each, and removing those for which the result would be negative.
If so, it can be done very simply:
list.replaceAll{ it - subtraction }
list.removeIf{ it < 0 }
However, instead of mutating the existing list, it would probably be more common to create a new one:
val newList = list.map{ it - subtraction }.filter{ it >= 0 }
Or, if you need to avoid creating a temporary list:
val newList = list.mapNotNull{ (it - subtraction).takeIf{ it >= 0 } }
A solution with foldRight using a Pair as accumulator:
val list = listOf(300, 200, 100)
val subtraction = 250
val result = list
.foldRight(subtraction to emptyList<Int>()) { item, (diff, list) ->
when {
diff > item -> diff - item to emptyList()
else -> 0 to listOf(item - diff) + list
}
}
.second
println(result) // Output: [300, 50]
A =[0,0,0,0,0] if I select 1 element and increment it by 1, so the should that to become new array A=[0,0,1,0,0] and when I also select another element and increment it then the new array may become A=[0,1,1,0,0] and so on. the increment is done by button, the increment may not only be 1 times.
in short is to make a counter for each of the random selected element and update the array anytime
Your question is rather an JS question than an react-native.
Below is an sample:
let numbers = [0,0,0,0,0]
const max = numbers.length
function replaceArrayElement (values, index) {
return ([...values.slice(0, index), values[index]+1, ...values.slice(index+1)])
}
function getRandomInt() {
return Math.floor(Math.random() * max);
}
for(var i = 0; i<=10; i++){
numbers = replaceArrayElement(numbers, getRandomInt())
}
console.log('numbers: ',numbers)
I've got a csv file that contain more than 60 columns and 2 000 000 lines, I'm trying to count the number of null value per variable (per column) then to do the sum of that new row to get the number total of null value in the entire csv. For example if we got this file in input:
We expect this other file in output:
I know how to count the number of null value per line but, I didn't figure out how to count the number of null value per column.
There has to be a better way to do this, but I made a really nasty JavaScript which does the job.
It has some problems for different column types, as it doesn't set the column type. (It should set all columns to integer, but I don't know if that is possible from JavaScript.)
You have to run Identify last row in a stream first, and save it to the column last (or change the script).
var nulls;
var seen;
if (!seen) {
// Initialize array
seen = 1;
nulls = [];
for (var i = 0; i < getInputRowMeta().size(); i++) {
nulls[i] = 0;
}
}
for (var i = 0; i < getInputRowMeta().size(); i++) {
if (row[i] == null) {
nulls[i] += 1;
}
// Hack to find empty strings
else if (getInputRowMeta().getValueMeta(i).getType() == 2 && row[i].length() == 0) {
nulls[i] += 1;
}
}
// Don't store any values
trans_Status = SKIP_TRANSFORMATION;
// Only store the nulls at the last row
if (last == true) {
putRow(nulls);
}
Please drag and drop below steps in to canvas.
step1: Add constants: create one variable called constant and value = 1
step2: Filter Rows: you have filter null values of all columns.
step3: Group by: here group by field constant variable
aggregates section we have to specify remaining columns like ct_inc.And type is Number of Values (N)
If you have any doubts feel free to ask.
skype_id : panabakavenkatesh
I have a 2D array that absolutely will not return the values I need. I start off with this array:
var userdata:Array = new Array(new Array(1000),new Array(4))
Then I try to set all values to 0, with this:
this.onLoad()
{
for (i = 0; i < 1000; i++)
{
for (j = 0; j < 4; j++)
{
userdata[i][j] = 0
trace(userdata[i][j])
}
}
}
This trace returns 8 0s and then a giant amount of "undefined"s. I can't figure out why this would be. I try something like this as well:
userdata[5][0] = 0
trace(userdata[5][0])
It still returns "undefined". Can anyone help with this?
To understand why you got only 8 "zeros" and many undefined values, let's start by your array declaration :
var userdata:Array = new Array(new Array(1000),new Array(4));
Here you should understand that you have created an array with only 2 cells ( that's why userdata[5][0] is undefined ) : the 1st cell is an array of 1000 elements and the 2nd one is an array of 4 elements, and that's why you can only set 8 items ( 2 x 4 ) : the 4th first items from the 1000 of the the 1st cell + the the 4th first items from the 4 of the 2nd cell.
Let's return to your question, you want create a multidimensional array of 1000 rows and 4 columns. To start, we create an array of 1000 rows (cells) :
var a:Array = [1000]; // you can write it : new Array(1000);
Then, we create 4 columns for every row, and set values like this :
var i:Number, j:Number;
for (i = 0; i < 1000; i++)
{
// create the 4 columns
a[i] = [4]; // you can write it : a[i] = new Array(4);
for (j = 0; j < 4; j++)
{
a[i][j] = 0;
}
}
Then we can verify our array :
trace(a[0][0]); // gives : 0
trace(a[255][2]); // gives : 0
trace(a[255][5]); // gives : undefined, because we have only 4 columns
trace(a[1500][0]); // gives : undefined, because we have only 1000 rows
Hope that can help.
I currently have code in objective C that can pull out an integer's most significant digit value. My only question is if there is a better way to do it than with how I have provided below. It gets the job done, but it just feels like a cheap hack.
What the code does is that it takes a number passed in and loops through until that number has been successfully divided to a certain value. The reason I am doing this is for an educational app that splits a number up by it's value and shows the values added all together to produce the final output (1234 = 1000 + 200 + 30 + 4).
int test = 1;
int result = 0;
int value = 0;
do {
value = input / test;
result = test;
test = [[NSString stringWithFormat:#"%d0",test] intValue];
} while (value >= 10);
Any advice is always greatly appreciated.
Will this do the trick?
int sigDigit(int input)
{
int digits = (int) log10(input);
return input / pow(10, digits);
}
Basically it does the following:
Finds out the number of digits in input (log10(input)) and storing it in 'digits'.
divides input by 10 ^ digits.
You should now have the most significant number in digits.
EDIT: in case you need a function that get the integer value at a specific index, check this function out:
int digitAtIndex(int input, int index)
{
int trimmedLower = input / (pow(10, index)); // trim the lower half of the input
int trimmedUpper = trimmedLower % 10; // trim the upper half of the input
return trimmedUpper;
}