I wanted to create a MutableList of alphabets and then shuffle them and store it in another MutableList.
I used shuffle() function but it resulted in the original list being shuffled as well which I didn't wanted to happen as I will be using the original list to map it with new shuffled one.
fun main(){
val alphabets = ('A'..'Z').toMutableList()
var shuffAlp = alphabets
shuffAlp.shuffle()
println(alphabets)
println(shuffAlp)
}
So I had to create two mutable list and then shuffle one of them
val alphabets = ('A'..'Z').toMutableList()
var shuffAlp = ('A'..'Z').toMutableList()
shuffAlp.shuffle()
This might be a trivial question but is there any other way where I do not have to create two same list?
shuffle does shuffle into original list, shuffled do and return new list.
And same behavior is for sort & sorted, sortBy & sortedBy, reverse & asReversed:
fun main(){
val alphabets = ('A'..'Z').toMutableList()
val shuffAlp = alphabets.shuffled()
println(alphabets)
println(shuffAlp)
}
Result:
[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
[U, B, A, N, H, R, O, K, X, C, W, E, Q, P, J, Z, L, Y, S, M, I, D, V, F, G, T]
how to zip a Flow with the next value as zipWithNext operator for collections?
zipWithNext behaves like:
val letters = ('a'..'f').toList()
val pairs = letters.zipWithNext()
println(letters) // [a, b, c, d, e, f]
println(pairs) // [(a, b), (b, c), (c, d), (d, e), (e, f)]
but in my case letters would be:
val letters = flowOf('a'..'f')
bonus points:
i tried flowOf(1, 2, 3).scan(emptyList<Int>()) { acc, value -> acc + value }.toList() on https://play.kotlinlang.org/ but doesn't find flowOf what import or else i'm missing there?
One possible solution is:
val letters = ('a'..'f').toList().toTypedArray()
val lettersFlow = flowOf(*letters)
val result = lettersFlow.scan(Pair<Char, Char>('a','a')) { acc, value -> Pair(acc.second, value) }.drop(2).toList()
println(result)
Does anyone know what I am doing wrong here? I am trying to generate a matrix from the linear system defined and then solve the matrix using it's inverse. For some reason it won't create the matrix.
sys := {a+.9*h+.8*c+.4*d+.1*e+0*f = 1, .1*a+.2*h+.4*c+.6*d+.5*e+.6*f = .6,
.4*a+.5*h+.7*c+d+.6*e+.3*f = .7, .6*a+.1*h+.2*c+.3*d+.5*e+f = .5,
.8*a+.8*h+c+.7*d+.4*e+.2*f = .8, .9*a+h+.8*c+.5*d+.2*e+.1*f = .9}:
solve(sys, {a, c, d, e, f, h});
Z := genmatrix(sys, [a, h, c, d, e, f], 'b');
Indented values are Maple's response. Third line of code should have generated a matrix, but instead is giving back my input.
You need to load the linear-algebra package with(linalg).
restart:with(linalg):
Z := genmatrix(sys, [a, h, c, d, e, f], 'b');
file 1
a,b, c, d,session-111, e, f
p,f, y, j,session-222, e, o
p,e, c, j,session-333, e, r
t,y, u, j,session-444, r, r
t,y, u, j,session-555, e, w
e,g, m, j,session-555, e, m
e,e, m, j,session-555, e, m
file 2
session-111, data-123, 123, erwt
session-222, data-234, 345, fghjf
session-333, data-345, 456, aasdf
session-555, data-567, 789, aasdf
session-555, data-890, 121, aasdf
session-666, data-678, 121, aasdf
Output
a,b, c, d,session-111, e, f, data-123, 123
p,f, y, j,session-222, e, o, data-234, 345
p,e, c, j,session-333, e, r, data-345, 456
t,y, u, j,session-444, e, r, NODATA
t,y, u, j,session-555, e, r, date-567, 789
t,y, u, j,session-555, e, r, date-890, 121
e,e, m, j,session-555, e, m, NODATA
All data from file1 should be printed - no matter there is reference found in file2 or not
if reference found in file 2, then specific fields (field 2 and 3) will get concatinated in output file
If I understand you correctly, you want to sequentially match fields 5 and 1 in file1 to file2 respectively, and if there is no match a "NODATA" field should be used instead. The following comes close to what you want, I think your listed output has some errors, see the comments made by sudo_O:
parse.awk
BEGIN { FS = OFS = "," }
FNR == NR {
lines[$1][++count[$1]] = $2 FS $3
next
}
count[$5] == 0 { print $0, " NODATA" }
count[$5] > 0 {
count[$5]--
print $0, lines[$5][++prn[$5]]
}
Run it like this:
awk -f parse.awk file2 file1
Output:
a,b, c, d,session-111, e, f, data-123, 123
p,f, y, j,session-222, e, o, data-234, 345
p,e, c, j,session-333, e, r, data-345, 456
t,y, u, j,session-444, r, r, NODATA
t,y, u, j,session-555, e, w, data-567, 789
e,g, m, j,session-555, e, m, data-890, 121
e,e, m, j,session-555, e, m, NODATA
try this one-liner:
awk -F, 'NR==FNR{k[$1]=$2 OFS $3;next} {if($5 in k)print $0,k[$5];else print $0," NODATA"}' OFS="," file2 file1
a,b, c, d,session-111, e, f, data-123, 123
p,f, y, j,session-222, e, o, data-234, 345
p,e, c, j,session-333, e, r, data-345, 456
t,y, u, j,session-444, r, r, NODATA
t,y, u, j,session-555, e, w, data-890, 121
e,g, m, j,session-555, e, m, data-890, 121
e,e, m, j,session-555, e, m, data-890, 121