In Scala:
Is there a way to directly split a string that contains 72 numeric values separated by ; into a 2-dimensional array of 9 rows and 8 columns with those numeric values -in numeric data type-?
val input = List.tabulate(72)(_.toString).mkString(";")
input.split(";").map(_.toInt).grouped(9).toArray
transforms
0;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;57;58;59;60;61;62;63;64;65;66;67;68;69;70;71
into
Array(
Array(0, 1, 2, 3, 4, 5, 6, 7, 8),
Array(9, 10, 11, 12, 13, 14, 15, 16, 17),
Array(18, 19, 20, 21, 22, 23, 24, 25, 26),
Array(27, 28, 29, 30, 31, 32, 33, 34, 35),
Array(36, 37, 38, 39, 40, 41, 42, 43, 44),
Array(45, 46, 47, 48, 49, 50, 51, 52, 53),
Array(54, 55, 56, 57, 58, 59, 60, 61, 62),
Array(63, 64, 65, 66, 67, 68, 69, 70, 71)
)
If you want to swap the dimensions of rows/columns, replace 9 by 8.
using Range and grouped functions
scala> val a = (0 to 71).map(_.toString).toArray.mkString(";")
a: String = 0;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;57;58;59;60;61;62;63;64;65;66;67;68;69;70;71
scala> a.split(";").map(_.toInt).sliding(9,9).toArray
res269: Array[Array[Int]] = Array(Array(0, 1, 2, 3, 4, 5, 6, 7, 8), Array(9, 10, 11, 12, 13, 14, 15, 16, 17), Array(18, 19, 20, 21, 22, 23, 24, 25, 26), Array(27, 28, 29, 30, 31, 32, 33, 34, 35), Array(36, 37, 38, 39, 40, 41, 42, 43, 44), Array(45, 46, 47, 48, 49, 50, 51, 52, 53), Array(54, 55, 56, 57, 58, 59, 60, 61, 62), Array(63, 64, 65, 66, 67, 68, 69, 70, 71))
scala>
Very new to Swift
I have a multidimensional array of some 500 records
[10, 2, 4, 10, 23, 56]
[0, 12, 14, 20, 28, 42]
[0, 2, 4, 10, 26, 54]
[1, 24, 34, 40, 47, 51]
[1, 23, 24, 30, 33, 50]
so that I would have
[0, 2, 4, 10, 26, 54]
[0, 12, 14, 20, 28, 42]
[1, 23, 24, 30, 33, 50]
[1, 24, 34, 40, 47, 51]
[10, 2, 4, 10, 23, 56]
I am fine for the individual record sort.
But when looking at the 500 records, to sort the records for the first column I used
arr.sort { $0[0] < $1[0] }.
which worked fine, I need to extend that to columns 2,3,4,5,6. I want to be able to sort on Column 1 then by 2, by 3, by 4, by 5, by 6.
Assuming that all subarrays contains 6 elements you can use a tuple (which conforms to Comparable to an arity of 6) to sort your array:
let array = [[10, 2, 4, 10, 23, 56],
[0, 12, 14, 20, 28, 42],
[0, 2, 4, 10, 26, 54],
[1, 24, 34, 40, 47, 51],
[1, 23, 24, 30, 33, 50]]
let sorted = array.sorted(by: {
($0[0],$0[1],$0[2],$0[3],$0[4],$0[5]) < ($1[0],$1[1],$1[2],$1[3],$1[4],$1[5])
})
print(sorted) // [[0, 2, 4, 10, 26, 54],
// [0, 12, 14, 20, 28, 42],
// [1, 23, 24, 30, 33, 50],
// [1, 24, 34, 40, 47, 51],
// [10, 2, 4, 10, 23, 56]]
I have an array of Arrays and an Array of Integers and want to create a dictionary with the Integers Array elements as keys to elements in the Array of Array.
I have tried a number of iteration methods without much luck. Any thoughts or ideas?
var populationArray = [[98, 8, 45, 34, 56], [9, 13, 65, 4, 90], [24, 5, 4, 56, 88], [3, 55, 22, 19, 10], [8, 33, 26, 93, 16], [31, 38, 92, 70, 36], [9, 39, 15, 14, 66]]
var IntegerKeys = [17, 41, 10, 34, 5, 85, 87]
var Dictionary : [Int: [Int]] = [:]
Try this:
var populationArray = [[98, 8, 45, 34, 56], [9, 13, 65, 4, 90], [24, 5, 4, 56, 88], [3, 55, 22, 19, 10], [8, 33, 26, 93, 16], [31, 38, 92, 70, 36], [9, 39, 15, 14, 66]]
var IntegerKeys = [17, 41, 10, 34, 5, 85, 87]
var Dictionary = [Int: [Int]]()
// Swift 2.0
for (index, key) in IntegerKeys.enumerate() {
Dictionary[key] = populationArray[index]
}
// Swift 1.2
for (index, key) in enumerate(IntegerKeys) {
Dictionary[key] = populationArray[index]
}
#ZoffDino's answer does work, however it would crash if the populationArray would contain less elements than integerKeys. I'm proposing a method that doesn't have this flaw:
var populationArray = [[98, 8, 45, 34, 56], [9, 13, 65, 4, 90], [24, 5, 4, 56, 88], [3, 55, 22, 19, 10], [8, 33, 26, 93, 16], [31, 38, 92, 70, 36], [9, 39, 15, 14, 66]]
var IntegerKeys = [17, 41, 10, 34, 5, 85, 87]
var dictionary : [Int: [Int]] = [:]
for (key, value) in zip(IntegerKeys, populationArray) {
dictionary[key] = value
}
i know this sounds silly, but can someone please post the arrays described by rfc2612:
Cm = 0x5A827999
Mm = 0x6ED9EBA1
Cr = 19
Mr = 17
for (i=0; i<24; i++)
{
for (j=0; j<8; j++)
{
Tmj_(i) = Cm
Cm = (Cm + Mm) mod 2**32
Trj_(i) = Cr
Cr = (Cr + Mr) mod 32
}
}
i think im doing is wrong for some reason
i get this for Tr
[[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2]]
Tr0 = { 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11 }
Tr1 = { 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28 }
Tr2 = { 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13 }
Tr3 = { 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30 }
Tr4 = { 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15 }
Tr5 = { 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0 }
Tr6 = { 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17 }
Tr7 = { 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2 }
..and Tm (in hex):
Tm0 = { 5a827999, d151d6a1, 482133a9, bef090b1, 35bfedb9, ac8f4ac1, 235ea7c9, 9a2e04d1, 10fd61d9, 87ccbee1, fe9c1be9, 756b78f1, ec3ad5f9, 630a3301, d9d99009, 50a8ed11, c7784a19, 3e47a721, b5170429, 2be66131, a2b5be39, 19851b41, 90547849, 723d551 }
Tm1 = { c95c653a, 402bc242, b6fb1f4a, 2dca7c52, a499d95a, 1b693662, 9238936a, 907f072, 7fd74d7a, f6a6aa82, 6d76078a, e4456492, 5b14c19a, d1e41ea2, 48b37baa, bf82d8b2, 365235ba, ad2192c2, 23f0efca, 9ac04cd2, 118fa9da, 885f06e2, ff2e63ea, 75fdc0f2 }
Tm2 = { 383650db, af05ade3, 25d50aeb, 9ca467f3, 1373c4fb, 8a432203, 1127f0b, 77e1dc13, eeb1391b, 65809623, dc4ff32b, 531f5033, c9eead3b, 40be0a43, b78d674b, 2e5cc453, a52c215b, 1bfb7e63, 92cadb6b, 99a3873, 8069957b, f738f283, 6e084f8b, e4d7ac93 }
Tm3 = { a7103c7c, 1ddf9984, 94aef68c, b7e5394, 824db09c, f91d0da4, 6fec6aac, e6bbc7b4, 5d8b24bc, d45a81c4, 4b29decc, c1f93bd4, 38c898dc, af97f5e4, 266752ec, 9d36aff4, 14060cfc, 8ad56a04, 1a4c70c, 78742414, ef43811c, 6612de24, dce23b2c, 53b19834 }
Tm4 = { 15ea281d, 8cb98525, 388e22d, 7a583f35, f1279c3d, 67f6f945, dec6564d, 5595b355, cc65105d, 43346d65, ba03ca6d, 30d32775, a7a2847d, 1e71e185, 95413e8d, c109b95, 82dff89d, f9af55a5, 707eb2ad, e74e0fb5, 5e1d6cbd, d4ecc9c5, 4bbc26cd, c28b83d5 }
Tm5 = { 84c413be, fb9370c6, 7262cdce, e9322ad6, 600187de, d6d0e4e6, 4da041ee, c46f9ef6, 3b3efbfe, b20e5906, 28ddb60e, 9fad1316, 167c701e, 8d4bcd26, 41b2a2e, 7aea8736, f1b9e43e, 68894146, df589e4e, 5627fb56, ccf7585e, 43c6b566, ba96126e, 31656f76 }
Tm6 = { f39dff5f, 6a6d5c67, e13cb96f, 580c1677, cedb737f, 45aad087, bc7a2d8f, 33498a97, aa18e79f, 20e844a7, 97b7a1af, e86feb7, 85565bbf, fc25b8c7, 72f515cf, e9c472d7, 6093cfdf, d7632ce7, 4e3289ef, c501e6f7, 3bd143ff, b2a0a107, 296ffe0f, a03f5b17 }
Tm7 = { 6277eb00, d9474808, 5016a510, c6e60218, 3db55f20, b484bc28, 2b541930, a2237638, 18f2d340, 8fc23048, 6918d50, 7d60ea58, f4304760, 6affa468, e1cf0170, 589e5e78, cf6dbb80, 463d1888, bd0c7590, 33dbd298, aaab2fa0, 217a8ca8, 9849e9b0, f1946b8 }
(I'm not sure why they didn't just include these as tables in the spec).