Can help make array with only odd numbers from array in Kotlin - kotlin

I need help. I trying make array only with odd numbers but I don't want use arraylist because I only want array.
Input array like this: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
I am trying to get odd only array like : [1, 3, 5, 7, 9]
val array = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val arraylist = arrayListOf<Int>()
for(i in 0..array.size - 1) {
if(array[i] % 2 != 0)
arraylist.add(array[i])
}
val oddarray = arraylist.toArray()

Why not just use filter:
import java.util.Arrays;
fun main(args: Array<String>) {
val numbersArray = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val oddArray = numbersArray.filter{ it % 2 != 0 }.toTypedArray()
print(Arrays.toString(oddArray)) // [1, 3, 5, 7, 9]
}

Related

Numpy array value change via two index sets

I am trying to achieve the following:
# Before
raw = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# Set values to 10
indice_set1 = np.array([0, 2, 4])
indice_set2 = np.array([0, 1])
raw[indice_set1][indice_set2] = 10
# Result
print(raw)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
But the raw values remain exactly the same.
Expecting this:
# After
raw = np.array([10, 1, 10, 3, 4, 5, 6, 7, 8, 9])
After doing raw[indice_set1] you get a new array, which is the one you modify with the second slicing, not raw.
Instead, slice the slicer:
raw[indice_set1[indice_set2]] = 10
Modified raw:
array([10, 1, 10, 3, 4, 5, 6, 7, 8, 9])

Creating a matrix with values in Kotlin in convenience

I want to create the following one with a fancy notation:
arrayOf(intArrayOf(4, 5), intArrayOf(5, 8), intArrayOf(1, 9), intArrayOf(8, 10), intArrayOf(1, 6))
at least, cannot I achieve something that looks as follows:
arrayOf<IntArray>((4, 5), (5, 8), (1, 9), (8, 10), (1, 6))
because it is pretty awkward to rewrite intArrayOf for each row to put in.
Note that I do not ask for the following syntax I'm aware of which is used to initialize an empty matrix with values that are either same or following a common pattern.
val array = Array(row) { IntArray(column) }
val result = listOf(4, 5, 5, 8, 1, 9, 8, 10, 1, 6)
.chunked(2)
.map { it.toIntArray() }
.toTypedArray()
Edit 1:
The calculation could go into an extension function:
fun List<Int>.toArrayOfIntArrays() = this.chunked(2).map { it.toIntArray() }.toTypedArray()
val result = listOf(4, 5, 5, 8, 1, 9, 8, 10, 1, 6).toArrayOfIntArrays()
Edit 2:
Another option – assuming that the inner IntArrays all will consist of exactly two elements – would be to use a user-defined infix function:
infix fun Int.with(i1: Int) = intArrayOf(this, i1)
val result = arrayOf(4 with 5, 5 with 8, 1 with 9, 8 with 10, 1 with 6)
Any word not in conflict with the Kotlin keywords could be used, 'with' is just an example.
If you simply don't like the wordiness of using intArrayOf you could define a shorter name to do the same, for example
fun i(vararg e: Int) = intArrayOf(*e)
And then do
arrayOf(i(4, 5), i(5, 8), i(1, 9), i(8, 10), i(1, 6))
Here is my solution, abusing operator overloading ;)
object Matrix {
operator fun get(vararg rows: IntArray) = rows
operator fun get(vararg values: Int): IntArray = intArrayOf(*values)
}
fun main() {
val result = Matrix.let {
it[
it[1, 2, 3],
it[4, 5, 6],
it[7, 8, 9],
]
}
println(result.toList().map { it.toList() }) // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
}
Please don't use it

How do you keep the every kth element of a list in Ramda?

How do you filter out every kth element of a list with Ramda?
input = [1, 2, 3, 4, 5, 6, 7, 8, 9]
output = keepKth(input, 3)
output = [1, 4, 7]
This seemed to work:
let k = 3;
let Kth = (value, index) => (index % k == 0)
let filterKth = R.addIndex(R.filter)(Kth);
let input = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let output = filterKth(input);
A variation based on Scott's comment:
const keepEvery = k => compose(pluck(0), splitEvery(k));
keepEvery(3)([1, 2, 3, 4, 5, 6, 7, 8, 9]);
//=> [1, 4, 7]
https://ramdajs.com/docs/#pluck
https://ramdajs.com/docs/#splitEvery
Probably not the cleanest, but point free.
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const whitelist = R.addIndex(R.reject)(R.flip(R.modulo(R.__, 3)));
console.log(
whitelist(data),
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.2/ramda.js" integrity="sha512-MEPRnhl9ArIiZuk6ikVrLzYxQm8ov1Ngkn4kIUO82hwpD7d+cwXQ7+isupqVgZ6HHtAEBDMff8eUhzixwEBSbA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
And here is another method based on R.unfold. The seed is the index (initial 0), and it's incremented by k on every iteration.
const { curry, unfold } = R
const fn = curry((k, arr) => unfold(n => n < arr.length && [arr[n], n + k], 0))
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const result = fn(3, data)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.2/ramda.js" integrity="sha512-MEPRnhl9ArIiZuk6ikVrLzYxQm8ov1Ngkn4kIUO82hwpD7d+cwXQ7+isupqVgZ6HHtAEBDMff8eUhzixwEBSbA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

How can I generate a random Verhoeff number in Apache Jmeter?

Need to pass a new Verhoeff Number every time I execute my script. The already used Verhoeff number is rejected by my application, as a business validation. Can someone help with the script for this?
The Java algorithm implementation is available at the Wikipedia page
In JMeter it's recommended to use Groovy for scripting so you will need to amend it to look like:
/**
* #see <ahref="http://en.wikipedia.org/wiki/Verhoeff_algorithm" > More Info</a>
* #see <ahref="http://en.wikipedia.org/wiki/Dihedral_group" > Dihedral Group</a>
* #see <ahref="http://mathworld.wolfram.com/DihedralGroupD5.html" > Dihedral Group Order 10</a>
* #author Colm Rice
*/
public class Verhoeff {
// The multiplication table
static int[][] d = new int[][]
{
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
};
// The permutation table
static int[][] p = new int[][]
{
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8]
};
// The inverse table
static int[] inv = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9];
/*
* For a given number generates a Verhoeff digit
*
*/
public static String generateVerhoeff(String num) {
int c = 0;
int[] myArray = stringToReversedIntArray(num);
for (int i = 0; i < myArray.length; i++) {
c = d[c][p[((i + 1) % 8)][myArray[i]]];
}
return Integer.toString(inv[c]);
}
/*
* Validates that an entered number is Verhoeff compliant.
* NB: Make sure the check digit is the last one.
*/
public static boolean validateVerhoeff(String num) {
int c = 0;
int[] myArray = stringToReversedIntArray(num);
for (int i = 0; i < myArray.length; i++) {
c = d[c][p[(i % 8)][myArray[i]]];
}
return (c == 0);
}
/*
* Converts a string to a reversed integer array.
*/
private static int[] stringToReversedIntArray(String num) {
int[] myArray = new int[num.length()];
for (int i = 0; i < num.length(); i++) {
myArray[i] = Integer.parseInt(num.substring(i, i + 1));
}
myArray = reverse(myArray);
return myArray;
}
/*
* Reverses an int array
*/
private static int[] reverse(int[] myArray) {
int[] reversed = new int[myArray.length];
for (int i = 0; i < myArray.length; i++) {
reversed[i] = myArray[myArray.length - (i + 1)];
}
return reversed;
}
}
and in order to call this and to store the result into a JMeter Variable you need to use vars shorthand to JMeterVariables class instance, something like:
vars.put('myVar', Verhoeff.generateVerhoeff("your-source-number-here"))
and then you will be able to refer the generated value as ${myVar} where required.

Remove all strings from list of strings and integers

You are given:
list1 = [2, "berry", "joe", 3, 5, 4, 10, "happy", "sad"]
want to return
[2, 3, 5, 4, 10]
Is it possible to remove just the strings from the list?
Using a list-comprehension you can construct another list with just the elements you want.
>>> list1 = [2, "berry", "joe", 3, 5, 4, 10, "happy", "sad"]
>>> [i for i in list1 if isinstance(i, int)]
[2, 3, 5, 4, 10]
Alternative in case for example you also have floats and wish to keep those, too:
>>> list1 = [2, "berry", "joe", 3, 5, 4, 10.0, "happy", "sad"]
>>> [i for i in list1 if not isinstance(i, str)]
[2, 3, 5, 4, 10.0]