How to create a Mutable List of Alphabets in Kotlin? - kotlin

I want to create a MutableList of alphabets form A to Z but so far I can only think of the following method as the shortest one without writing each and every alphabet.
fun main()
{
var alphabets: MutableList<Char> = mutableListOf()
for (a in 'A'..'Z')
{
alphabets.add(a)
}
print(alphabets)
}
So I wanted to know if there is any Lambda implementation or shorter method for the same?

You can use CharRange() or using .. operator to create a range.
For example:
val alphabets = ('A'..'Z').toMutableList()
print(alphabets)
// [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]
or
val alphabets = CharRange('A','Z').toMutableList()
print(alphabets)
// [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]

Alternatively, you can use ('a'..'z').joinToString("").
val abc = ('a'..'z').joinToString("")
println(abc) //abcdefghijklmnopqrstuvwxyz
You can perform the same functions on a string in Kotlin as you can perform on a list of characters
For example:
for (i in abc) { println(i) }
This lists each alphabet individually on a new line just as it would if you converted it into a list.

You can use rangeTo extension function as below
private val alphabets = ('A').rangeTo('Z').toMutableList()
println(alphabets)
[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]

Related

How to shuffle elements of Mutable list in Kotlin?

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]

zip with next value with kotlin Flow

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)

Maple genmatrix command

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');

Efficiently compute Knuth's up-arrow notation modulus

I'm already using memoization as a dictionary. Is there anything else I can do? I suspect the for loop might be able to be optimized. For reference I am computing knuth_arrow(2, 3, 9, 14**8)
memo = {}
def knuth_arrow(a, n, b, m):
if (a, n, b) in memo:
return memo[(a, n, b)]
if n == 0:
return (a*b) % m
if n == 1:
s = pow(a, b, m)
memo[(a, n, b)] = s
return s
if n > 1:
s = a
for i in range(b-1):
s = knuth_arrow(a, n-1, s, m)
memo[(a, n, b)] = s
return s

Merging content of 2 files based on some common field

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