Scala array splitting - arrays

For a given Array[Byte] such as
val in = Array(104, 101, 108, 108, 111, 10, 119, 111, 114, 108, 100, 10)
how to split it by value 10 so that
val out = in.arr_split(10)
would deliver
Array( Array(104, 101, 108, 108, 111),
Array(119, 111, 114, 108, 100))
Assume in general many occurrences of splitting elements, for instance many 10.
If possible, a parallel solution is desired.
Many Thanks.

Something like this should work:
def split(l: Array[Int], i:Int):Array[Array[Int]] = {
l match {
case Array() => Array()
case _ =>
val (h, t) = l.span(a => a != i)
Array(h) ++ split(t.drop(1), i)
}
}
val in = Array(104, 101, 108, 108, 111, 10, 119, 111, 114, 108, 100, 10)
val out = split(in, 10)
// res: Array[Array[Int]] = Array(Array(104, 101, 108, 108, 111), Array(119, 111, 114, 108, 100))

scalaz-stream solution. Instead of Array I use Vector here.
val in = Vector(104, 101, 108, 108, 111, 10, 119, 111, 114, 108, 100, 10)
val P = scalaz.stream.Process
implicit val eq = scalaz.Equal.equal[Int]((l, r) => l == r)
println(P.emitSeq[Task, Int](in).splitOn(10).filter(!_.isEmpty).runLog.run)
Output:
Vector(Vector(104, 101, 108, 108, 111), Vector(119, 111, 114, 108, 100))

Pimped version
implicit def toDivide[A, B <% TraversableLike[A, B]](a : B) = new {
private def divide(x : B, condition: (A) => Boolean) : Iterable[B] = {
if (x.size > 0)
x.span(condition) match {
case (e, f) => if (e.size > 0) Iterable(e) ++ divide(f.drop(1),condition) else Iterable(f)
}
else
Iterable()
}
def divide(condition: (A) => Boolean): Iterable[B] = divide(a, condition)
}

Related

How can I visualize neurosity brainwave api data in react js

How to visualize the brainwave API data in d3.js and react.js.
Here is the brainwave API:
const mind = new Notion();
mind.brainwaves("psd").subscribe((brainwaves) => {
console.log(brainwaves);
});
The code above will output new epochs 4 times a second. Every frequency label (e.g. alpha) contains the computed FFT (Fast Fourier transform) value per channel (see the psd property), as well as the frequency ranges (see the freqs property).
Here's an example of 1 event that I want to visualize in d3 and react js:
{
label: 'psd',
freqs: [
0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22,
24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46,
48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70,
72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94,
96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118,
120, 122, 124, 126
],
info: {
notchFrequency: '60Hz',
samplingRate: 256,
startTime: 1628197298732
},
psd: [
[
98.63572869924877, 278.0499119981597, 396.9075453246308,
330.93307599602673, 154.4437300666471, 127.381718447909,
156.28589064508202, 90.27952532968459, 74.02596881179568,
102.68745491461037, 77.40464372151173, 65.97947493071318,
93.61333998578448, 70.03755439407374, 47.965913961348285,
72.11457749610696, 60.14793608854482, 36.43407809513316,
52.5321191045999, 45.400500672083176, 24.168757651016627,
37.1839936941784, 35.105296424441036, 14.991272333196237,
17.013079679743214, 22.931615422127962, 9.64319909169338,
6.95610789706202, 10.48806813349181, 8.77101666889275,
8.08687117576467, 7.88454615426007, 7.00857990702008,
9.129752553805993, 7.500414008219254, 6.4966183674128635,
7.833399187762861, 7.283708613586358, 5.616493707372124,
7.336663052350952, 6.859592851990316, 6.153804860755752,
6.618696201331677, 6.837180878389385, 5.7838083130648945,
6.562155335152424, 6.093398492507891, 6.073406841367065,
5.9593899491763205, 6.14611107916922, 5.674535238756583,
6.0774035077156645, 5.656938723201682, 5.892346415487732,
5.61605742554047, 5.842031463718972, 5.514410378848478,
5.803658958523979, 5.47172273287992, 5.745739449800702,
5.452574435173335, 5.724439426371041, 5.4273919360609035,
5.707772456903569
],
[
705.0449351206108, 1355.4773207863375, 1795.4768676020658,
1480.8269991044856, 879.7073135412625, 734.4677613113015,
691.6145778964477, 482.9726329188916, 463.9458627254311,
448.9185196251005, 325.3989179173811, 356.7357077059943,
366.94089924861487, 288.75232488327777, 304.2605284381872,
301.8930577524112, 237.4042509842181, 248.189270828571,
244.01379638689255, 177.6237336667693, 172.43627544841166,
176.69895192953885, 125.52722189861495, 105.15448954029884,
106.56146701054848, 63.477588704077554, 33.251603187541896,
42.84498288204394, 23.928294234593277, 9.767390485089537,
15.03794181419898, 13.965161093202841, 20.844294981525614,
12.007423569211078, 11.126393885153014, 20.104729878667776,
12.319226156469027, 10.486815016962693, 17.143209506256614,
11.132954923524995, 10.62728760663002, 14.463591856614492,
10.925935011739528, 10.576245202399233, 12.869498809209984,
10.551373735436435, 10.90154409382562, 11.496161902596342,
10.59771747532043, 10.626533456022605, 10.982565808529692,
10.292226858572462, 10.587506870484761, 10.420838952336604,
10.33846013622055, 10.228524593265222, 10.333151489515492,
10.081149399888313, 10.23400481786508, 10.046416371678554,
10.14064797386651, 9.979626942208188, 10.115418833026341,
9.962197147976129
],
[
929.0377610383296, 1793.6811181430987, 2377.6119679334947,
1958.9102655269323, 1162.3055283149445, 979.7382233236812,
921.065883147485, 640.2289218688652, 619.3532710184182,
597.9752360209405, 433.84218492741303, 480.63827859906377,
494.8759497888118, 388.5592867189369, 408.72806358233913,
403.8696475504568, 318.0820897599802, 335.6971387951459,
330.1749076377176, 240.2816149573954, 234.1828700249589,
238.8172342465352, 168.40453177012395, 141.41297208524767,
143.3763643586936, 84.85781325822384, 44.693260335642535,
57.99822015732011, 32.12541610045182, 13.475265334606835,
20.599681672533375, 19.01837044906831, 28.246044041267428,
16.189180127175323, 15.41587209212851, 27.05517471975363,
16.903913745426895, 14.33546383874818, 23.026090510272617,
14.87036823280212, 14.6068129622348, 19.471383549994453,
14.96633838574153, 14.387933483886725, 17.466586501671532,
14.355984995364704, 14.919336874633427, 15.536030663642576,
14.543171342633388, 14.399423945911408, 15.00275665739408,
13.982397994287624, 14.474361692225106, 14.126311107434065,
14.160828645624179, 13.86227555141294, 14.139642435285486,
13.674442534649062, 14.000882290360456, 13.623523705584073,
13.881719450096554, 13.533315732597867, 13.84382520692153,
13.508775392377734
],
[
461.1411944745596, 865.879891899699, 1150.3297939965412,
967.6717977233337, 599.5067484775997, 487.7449557189379,
449.7148527252277, 324.75340665195546, 307.3960653823736,
289.99356448521917, 218.9307327550319, 241.757776766985,
249.28709256762335, 206.95217758165205, 213.8552238566172,
208.04287443336904, 172.16085191285578, 177.8042520513104,
170.76433578042244, 131.2290615556113, 127.75140879293434,
125.8563352501824, 94.44550500099892, 81.32600662751962,
76.33377477822643, 47.53219019300205, 29.403234515228505,
29.734512582314988, 14.48430634759893, 4.338569856695335,
7.956256668786119, 7.925904164095972, 12.763456529014546,
5.823156703304557, 7.213304914646235, 12.53665043042392,
6.064277734596193, 6.0142267398677225, 10.591216540020291,
5.491335175417487, 5.995538415704912, 8.41366666249266,
5.354337464315892, 5.684078918046329, 7.289586947844527,
5.258040775750918, 5.927892633808341, 6.209024439918837,
5.5308778688068525, 5.658218846438647, 5.937393602233365,
5.337787715362042, 5.723456582324143, 5.491309810378187,
5.517788579034077, 5.3995359451843115, 5.544753793342432,
5.291402564159946, 5.499716204904763, 5.281561955171903,
5.4342620388212115, 5.243854533655554, 5.426831995465968,
5.23668469315059
],
[
485.28953220351815, 913.8215446531855, 1212.6893063853145,
1017.6653954348992, 629.0590135927589, 513.8401411331691,
473.94607162953474, 340.7794194629709, 323.4068209463424,
304.6140613386581, 227.9530765749002, 253.44275369319936,
261.96382482250846, 215.95821471824453, 223.2060790303756,
217.5887331092368, 180.05264499052626, 186.6722683242584,
178.86415942933493, 135.75616983861607, 132.7145908145038,
131.7205674261096, 98.13621951582651, 84.22587059556682,
79.69348482329639, 49.27415323250583, 29.885948066276374,
31.018207012950032, 15.464201551787149, 4.696597650070098,
8.680077668220271, 8.409519490488169, 13.587393841532371,
6.264695355862866, 7.572139679407593, 13.17605643990251,
6.652754348269858, 6.3465028991975325, 11.306345734652368,
5.926439990819285, 6.470051702062516, 8.904949649675096,
5.90150761172456, 6.060485555618185, 7.854198979433359,
5.663406547398727, 6.4306283909466435, 6.623721018560318,
6.041438452881903, 6.0442294631002795, 6.444444318919457,
5.721040377425073, 6.213246144964029, 5.8753004744243755,
6.002696003640614, 5.774903831465746, 6.026353504659674,
5.663944879598529, 5.975725903093066, 5.652394044025673,
5.907643067149823, 5.612177778683849, 5.898257253854689,
5.604260960707902
],
[
703.9620591951088, 1348.1617601998341, 1787.817378338989,
1480.9682977349662, 887.8732586924484, 741.7489045127593,
696.4862482257432, 486.69267953812624, 464.30488178918847,
448.10745320129496, 329.6131727268781, 364.1234842222161,
375.17667115955084, 296.30580382389024, 307.6916385785675,
303.4585158601969, 241.4840430193035, 253.3373457325428,
248.14875370587004, 181.8456760420915, 176.16078095306457,
178.58969714768043, 126.8938114163353, 106.7131960446341,
106.86715498126117, 63.565099293832944, 33.94124074989405,
42.78261882478681, 23.418093057211088, 9.598376452708248,
14.8564635663729, 13.73158527388318, 20.632309203759725,
11.571312623082235, 11.16199944105178, 19.71697966916169,
12.151289348370563, 10.27332116826051, 16.93453042721219,
10.734196078665759, 10.578983816474802, 14.17031151713728,
10.741999390916682, 10.370828224990875, 12.669038407738478,
10.290437631963869, 10.794986602960588, 11.204752776686476,
10.481500894235385, 10.373479646590457, 10.845762801813153,
10.04623630410688, 10.466118545780976, 10.16324823810254,
10.225788013632457, 9.975974411529377, 10.21577779934349,
9.834755780463283, 10.117741157508208, 9.799067226573825,
10.029038431063377, 9.733625015451048, 10.002552468346979,
9.71630020598175
],
[
753.3573854351718, 1451.3463711535637, 1925.4394750722938,
1592.2479396735228, 950.1320711729846, 793.4163539564408,
744.990261771651, 519.4611344925438, 498.6282329256233,
480.4651411728872, 349.9224396629356, 388.08464608248545,
400.41433150212987, 314.28699618071386, 328.3871848882801,
325.21671199729667, 257.08898576447365, 270.0756790958063,
265.56055695000117, 193.58422699465976, 187.3975181698473,
190.5723885919431, 134.7850068920377, 113.55417235960783,
114.65352872167782, 67.97249319078067, 36.07066166066659,
46.193455592634194, 25.446519462818365, 10.645647869461468,
16.33292205396577, 15.082694752225358, 22.479081487850554,
12.660457753439347, 12.331655605615103, 21.469122245638992,
13.259611882976637, 11.366093905761021, 18.4614508154199,
11.778790555694387, 11.710458297806564, 15.485077358432786,
11.805213571500564, 11.446226210170733, 13.887884653568616,
11.334007371207884, 11.881804314077982, 12.30654501069072,
11.528873716721828, 11.419099102496702, 11.910897138255397,
11.059631088502826, 11.505557872713792, 11.178885987141047,
11.242816922347224, 10.97697117415906, 11.227884136208706,
10.822693801769324, 11.121177549665633, 10.782078947654583,
11.024368739435461, 10.711151846144949, 10.995151378799578,
10.691866595209362
],
[
367.4136193009799, 826.7329948628463, 1118.3539023221265,
890.162220791093, 436.1682590608995, 391.4382314784865,
417.04217210936963, 251.56740893464422, 238.5284921292077,
267.6000138141995, 168.617128049186, 165.98870799455165,
200.56943140232212, 129.37112302840023, 126.00839013852573,
162.39587433692205, 109.20332945126022, 87.00867663058928,
117.95711115144483, 83.29961985396704, 58.57022651921219,
86.68284556964056, 63.23057486573713, 22.615043247176825,
45.79353231282386, 39.94573246684187, 6.411920387449734,
21.964845928081306, 21.959370088243116, 16.277025835788837,
16.547064843486048, 15.715335138181468, 16.607457789253704,
17.537478155658583, 13.336615276197591, 15.835823046176726,
15.461292461652397, 13.730332854951738, 13.27865408978899,
14.709605078820157, 12.9045807988706, 13.692853045756497,
13.258453124525246, 13.287331440282053, 12.481768554519784,
13.223425784019863, 12.187280042833416, 12.738723198131671,
12.048047848217715, 12.49122466572343, 11.744286244430379,
12.342663893673903, 11.552471648965968, 12.117055516659004,
11.432033986591367, 11.965037193629023, 11.288502743059457,
11.863912386218576, 11.190384199321217, 11.771734261131785,
11.131137815008097, 11.71660417394918, 11.08761147894627,
11.686948260719255
]
]
}
Please Help me in this regard...

How to Search and Replace object property value if each key has an array of numbers? Javascript

Let's say we have an object with an array of numbers for each property:
const obj = {
line1: [2, 13, 7, 6, 14],
line2: [25, 16, 24, 27, 28],
line3: [41, 31, 32, 44, 42],
};
How can we Search for a specific number and Replace it with another number?
Let's say we would like to find number 44 and replace it with 88? In a modern JS way...
You can use loop to iterate on each key-value pair of the object, and then you can find and replace the number you want in the array. Keep in mind that if the number can be more than once in the same array you will have to loop on the array as well, otherwise you can use indexOf and assign the new value.
Here is an example:
const obj = {
line1: [2, 13, 7, 6, 14],
line2: [25, 16, 24, 27, 28],
line3: [41, 31, 32, 44, 42],
};
const changeNumber = (oldNum, newNum, obj) => {
for (const [key, arr] of Object.entries(obj)) {
const index = arr.indexOf(oldNum);
if (index > -1) {
arr[index] = newNum;
}
}
}
console.log(obj); // Before
changeNumber(44, 88, obj);
console.log(obj); // After
/* With duplicates entries */
const changeNumberDuplicate = (oldNum, newNum, obj) => {
for (const [key, arr] of Object.entries(obj)) {
let index = 0;
do {
index = arr.indexOf(oldNum, index);
if (index > -1) {
arr[index] = newNum;
}
} while (index > -1);
}
}
const objDup = {
line1: [2, 13, 44, 6, 44],
line2: [44, 16, 44, 27, 28],
line3: [41, 44, 32, 44, 42],
};
console.log(objDup); // Before
changeNumberDuplicate(44, 88, objDup);
console.log(objDup); // After

Merging multiple arrays and get an array of common elements as output

I'm trying to merge several arrays in javascript/typescript in angular, I need to output an array with the common values in all the arrays.
for eg:
arrayOne = [34, 23, 80, 93, 48]
arrayTwo = [48, 29, 10, 79, 23]
arrayThree = [23, 89, 48, 20, 63]
output: outputArr= [23, 48]
for getting common elements out of 2 arrays i'm filtering one array with the element from the next array.
return this.arrayOne.filter(el => this.arrayTwo.includes(el));
How do I do it effectively if I have to combine large number of arrays....?
Thanks in advance...
You could use multiple Set to quickly look up for each value among the different sets.
const intersection = <T>(arrays: T[][]): T[] => {
if (arrays.length == 0) return [];
if (arrays.length == 1) return arrays[0];
const sets = arrays.slice(1).map(array => new Set(array));
const result = [];
arrays[0].forEach(item => {
if (sets.every(set => set.has(item))) {
result.push(item);
}
});
return result;
};
const arrayOne = [34, 23, 80, 93, 48];
const arrayTwo = [48, 29, 10, 79, 23];
const arrayThree = [23, 89, 48, 20, 63];
const result = intersection([arrayOne, arrayTwo, arrayThree]);
console.log(result);
One approach is to create an initial set that contains the first array's elements. Then loop over the other arrays, converting them to sets and updating the initial set to retain the corresponding set intersection.
const commonElements = function(arrays) {
if (arrays.length == 0)
return [];
const intersection = new Set(arrays[0]);
for (const array of arrays) {
const set = new Set(array);
for (x of intersection) {
if (!set.has(x))
intersection.delete(x);
}
}
return Array.from(intersection);
};
Calling this function on the sample arrays in the question:
commonElements([
[34, 23, 80, 93, 48],
[48, 29, 10, 79, 23],
[23, 89, 48, 20, 63]])
Returns:
[23, 48]

How to duplicate array within same array x times?

So, I have made function that return publicKey and I need to duplicate it x times for later use.
For example: publicKey: [76, 152, 93, 102, 145, 181] and I need to duplicate it 3 times, so the end result should be [76, 152, 93, 102, 145, 181, 76, 152, 93, 102, 145, 181, 76, 152, 93, 102, 145, 181].
I have tried using var list3 = publicKey*x, but as you can imagine, it didn't work..
fun getPublicKey(privateKey: ArrayList<BigInteger>,
birthDay: BigInteger,
primNumb: BigInteger): ArrayList<BigInteger> {
val inversMod = birthDay.modInverse(primNumb)
//println(inversMod)
val publicKey: ArrayList<BigInteger> = ArrayList()
for (keyValue in privateKey){
val pagaiduValue = (keyValue * inversMod).mod(primNumb)
//println("($keyValue * $inversMod) mod $primNumb = $pagaiduValue")
publicKey.add(pagaiduValue)
}
return publicKey
}
Here's a short and simple solution:
inline fun <T> Iterable<T>.times(count: Int) = (1..count).flatMap { this }
Can be used like this:
val numbers = listOf(1, 2, 3)
val result = numbers.times(5)
println(result) // [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
Perhaps the naming could use some work, times could also be assumed to have the effect of multiplying each element by a given number, repeat could be a better alternative. Also, you might wanna define it on Array<T> and the primitive array types (IntArray, etc) as well.

Ruby - Separate out the first two digits from integer of increasing length

I keep getting the error below when running my code.
My Code:
class Integer
def num_digits
Math.log10(self).to_i + 1
end
def p(t)
case t
when 3
return self * (self + 1) / 2
when 4
return self**2
when 5
return self * ((3 * self) - 1) / 2
when 6
return self * ((2 * self) - 1)
when 7
return self * ((5 * self) - 3) / 2
when 8
return self * ((3 * self) - 2)
end
end
end
$p3 = [], $p4 = [], $p5 = [], $p6 = [], $p7 = [], $p8 = []
$polygonals = [$p3, $p4, $p5, $p6, $p7, $p8]
(3..8).each_with_index do |t, index|
i = 1
until i == 150
$polygonals[index] << i.p(t)
i += 1
end
end
$polygonals.each { |array| array.reject! { |x| x.num_digits != 4 } }
Error:
`block (2 levels) in <main>': undefined method `num_digits' for []:Array (NoMethodError)
However, if I change the last line of code to:
$polygonals.each { |array| array.each { |x| x.reject! { |y| y.num_digits != 4 } } }
I get the error:
`block (2 levels) in <main>': undefined method `reject!' for 1:Fixnum (NoMethodError)
How can x be type array in the first case and type Fixnum in the second?
The commas on this line
$p3 = [], $p4 = [], $p5 = [], $p6 = [], $p7 = [], $p8 = []
Don't do what you think. In ruby
a = 1,2
sets a to [1,2], so this is setting $p3 to
[[], [], [], [], [], []]
If you put those assignments on separate lines or use ; rather than commas then those variables will be initialised to what you expect.
Edit
The reason for your failure is shown below:
irb(main):072:0> $polygonals = [$p3, $p4, $p5, $p6, $p7, $p8]
=> [[[], [], [], [], [], []], [], [], [], [], []]
Observe how does your first element look like. Not exactly as you would have expected, right?
Actually you have both. See how your $polygonals[0] look like:
irb(main):066:0> $polygonals[0]=> [[], [1, 4, 9, 16, 25, 36, 49, 64,
81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484,
529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225,
1296, 1369, 1444, 1521,1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209,
2304, 2401, 2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364,
3481, 3600, 3721, 3844, 3969, 4096, 4225, 4356, 4489, 4624, 4761,
4900, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241, 6400,
6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921,8100, 8281, 8464,
8649, 8836, 9025, 9216, 9409, 9604, 9801, 10000, 10201, 10404, 10609,
10816, 11025, 11236, 11449, 11664, 11881, 12100, 12321, 12544, 12769,
12996, 13225, 13456, 13689, 13924, 14161,14400, 14641, 14884, 15129,
15376, 15625, 15876, 16129, 16384, 16641, 16900, 17161, 17424, 17689,
17956, 18225, 18496, 18769, 19044, 19321, 19600, 19881, 20164, 20449,
20736, 21025, 21316, 21609, 21904, 22201], [1, 5, 12, 22, 35, 51, 70,
92, 117, 145, 176, 210, 247, 287, 330, 376, 425, 477, 532, 590, 651,
715, 782, 852, 925, 1001, 1080, 1162, 1247, 1335, 1426, 1520, 1617,
1717, 1820, 1926, 2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882,
3015, 3151, 3290, 3432, 3577, 3725, 3876, 4030, 4187, 4347,4510, 4676,
4845, 5017, 5192, 5370, 5551, 5735, 5922, 6112, 6305, 6501, 6700,
6902, 7107, 7315, 7526, 7740, 7957, 8177, 8400, 8626, 8855, 9087,
9322, 9560, 9801, 10045, 10292, 10542, 10795, 11051, 11310, 11572,
11837, 12105, 12376, 12650, 12927, 13207, 13490, 13776, 14065, 14357,
14652, 14950, 15251, 15555, 15862, 16172, 16485, 16801, 17120, 17442,
17767, 18095, 18426, 18760, 19097, 19437, 19780,20126, 20475, 20827,
21182, 21540, 21901, 22265, 22632, 23002, 23375, 23751, 24130, 24512,
24897, 25285, 25676, 26070, 26467, 26867, 27270, 27676, 28085, 28497,
28912, 29330, 29751, 30175, 30602, 31032, 31465, 31901, 32340, 32782,
33227], [1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 276, 325, 378,
435, 496, 561, 630, 703, 780, 861, 946, 1035, 1128, 1225, 1326, 1431,
1540, 1653, 1770, 1891, 2016, 2145, 2278, 2415, 2556, 2701, 2850,
3003, 3160, 3321, 3486, 3655, 3828, 4005, 4186, 4371, 4560, 4753,
4950, 5151, 5356, 5565, 5778, 5995, 6216, 6441, 6670, 6903, 7140,
7381, 7626, 7875, 8128, 8385,8646, 8911, 9180, 9453, 9730, 10011,
10296, 10585, 10878, 11175, 11476, 11781, 12090, 12403, 12720,13041,
13366, 13695, 14028, 14365, 14706, 15051, 15400, 15753, 16110, 16471,
16836, 17205, 17578, 17955, 18336, 18721, 19110, 19503, 19900, 20301,
20706, 21115, 21528, 21945, 22366, 22791, 23220, 23653, 24090, 24531,
24976, 25425, 25878, 26335, 26796, 27261, 27730, 28203, 28680, 29161,
29646, 30135,30628, 31125, 31626, 32131, 32640, 33153, 33670, 34191,
34716, 35245, 35778, 36315, 36856, 37401, 37950, 38503, 39060, 39621,
40186, 40755, 41328, 41905, 42486, 43071, 43660, 44253], [1, 7, 18,
34, 55, 81, 112, 148, 189, 235, 286, 342, 403, 469, 540, 616, 697,
783, 874, 970, 1071, 1177, 1288, 1404,1525, 1651, 1782, 1918, 2059,
2205, 2356, 2512, 2673, 2839, 3010, 3186, 3367, 3553, 3744, 3940,
4141, 4347, 4558, 4774, 4995, 5221, 5452, 5688, 5929, 6175, 6426,
6682, 6943, 7209, 7480, 7756, 8037, 8323, 8614, 8910, 9211, 9517,
9828, 10144, 10465, 10791, 11122, 11458, 11799, 12145, 12496, 12852,
13213, 13579, 13950, 14326, 14707, 15093, 15484, 15880, 16281, 16687,
17098, 17514, 17935, 18361, 18792, 19228, 19669, 20115, 20566, 21022,
21483, 21949, 22420, 22896, 23377, 23863, 24354, 24850, 25351,25857,
26368, 26884, 27405, 27931, 28462, 28998, 29539, 30085, 30636, 31192,
31753, 32319, 32890, 33466, 34047, 34633, 35224, 35820, 36421, 37027,
37638, 38254, 38875, 39501, 40132, 40768, 41409, 42055, 42706, 43362,
44023, 44689, 45360, 46036, 46717, 47403, 48094, 48790, 49491, 50197,
50908, 51624, 52345, 53071, 53802, 54538, 55279], [1, 8, 21, 40, 65,
96, 133, 176, 225, 280, 341, 408, 481, 560,645, 736, 833, 936, 1045,
1160, 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 2640,
2821, 3008, 3201, 3400, 3605, 3816, 4033, 4256, 4485, 4720, 4961,
5208, 5461, 5720, 5985, 6256, 6533, 6816,7105, 7400, 7701, 8008, 8321,
8640, 8965, 9296, 9633, 9976, 10325, 10680, 11041, 11408, 11781,
12160, 12545, 12936, 13333, 13736, 14145, 14560, 14981, 15408, 15841,
16280, 16725, 17176, 17633, 18096,18565, 19040, 19521, 20008, 20501,
21000, 21505, 22016, 22533, 23056, 23585, 24120, 24661, 25208, 25761,
26320, 26885, 27456, 28033, 28616, 29205, 29800, 30401, 31008, 31621,
32240, 32865, 33496, 34133, 34776, 35425, 36080, 36741, 37408, 38081,
38760, 39445, 40136, 40833, 41536, 42245, 42960, 43681, 44408, 45141,
45880, 46625, 47376, 48133, 48896, 49665, 50440, 51221, 52008, 52801,
53600, 54405,55216, 56033, 56856, 57685, 58520, 59361, 60208, 61061,
61920, 62785, 63656, 64533, 65416, 66305], 1, 3, 6, 10, 15, 21, 28,
36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253,
276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666,
703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225,
1275, 1326, 1378, 1431, 1485, 1540, 1596, 1653, 1711, 1770, 1830,
1891, 1953, 2016, 2080, 2145, 2211, 2278, 2346, 2415, 2485, 2556,
2628, 2701, 2775, 2850, 2926, 3003,3081, 3160, 3240, 3321, 3403, 3486,
3570, 3655, 3741, 3828, 3916, 4005, 4095, 4186, 4278, 4371, 4465,
4560, 4656, 4753, 4851, 4950, 5050, 5151, 5253, 5356, 5460, 5565,
5671, 5778, 5886, 5995, 6105, 6216, 6328, 6441, 6555, 6670, 6786,
6903, 7021, 7140, 7260, 7381, 7503, 7626, 7750, 7875, 8001, 8128,8256,
8385, 8515, 8646, 8778, 8911, 9045, 9180, 9316, 9453, 9591, 9730,
9870, 10011, 10153, 10296,10440, 10585, 10731, 10878, 11026, 11175]
And this is how your $polygonals[1] look like:
irb(main):067:0> $polygonals[1]=> [1, 4, 9, 16, 25, 36, 49, 64, 81,
100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529,
576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296,
1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209,
2304, 2401, 2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364,
3481, 3600, 3721, 3844, 3969, 4096, 4225, 4356, 4489, 4624, 4761,
4900, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241, 6400,
6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921, 8100, 8281,
8464, 8649, 8836, 9025, 9216, 9409, 9604, 9801, 10000, 10201, 10404,
10609, 10816, 11025, 11236, 11449, 11664, 11881, 12100, 12321, 12544,
12769, 12996, 13225, 13456, 13689, 13924, 14161, 14400, 14641, 14884,
15129, 15376, 15625, 15876, 16129, 16384, 16641, 16900, 17161, 17424,
17689, 17956,18225, 18496, 18769, 19044, 19321, 19600, 19881, 20164,
20449, 20736, 21025, 21316, 21609, 21904, 22201]
First off, it's generally considered bad practice to extend core classes such as Integer. You should only do this in special circumstances, where you are providing generic, useful behaviour that your application can use throughout.
The two methods you have written are both bad examples of when to do this:
class Integer
def num_digits
Math.log10(self).to_i + 1
end
end
Why bother with such a mathematical technique (123.num_digits == 3) when you could just do something like: 123.to_s.length == 3?
def p(t)
case t
when 3
return self * (self + 1) / 2
# ...
when 8
return self * ((3 * self) - 2)
end
end
end
What the heck is this?! What does p mean, why does the method only work for integers 3..8 (otherwise it returns nil)? I'm guessing it's to do with calculating polygonal numbers, in which case you should name it as such. This has no place in the Integer class; just make it a separate method, and name it better:
def rename_me(x, y) # What are x and y? What IS this method? Use real words!
case y
when 3
x * (x + 1) / 2
# ...
when 8
x * ((3 * x) - 2)
end
end
Next, never use global variables, unless you have a very good reason to. In ruby, variable names starting with $ are global. Instead of $polygonals, just define variables like polygonals.
Next, learn how to define variables in ruby. Writing code like x = 1, y = 2 is not doing what you think; that's actually setting x == [1, 2] and y == 2. The intention here is that a possible shorthand for defining variables is: x, y = 1, 2 which sets x == 1 and y == 2 - but I wouldn't generally recommend 1-line definitions like this; just put it on 2 lines.
With all of this in mind, here is how I would write your full code in a more "ruby way":
def rename_me(x, y) # What are x and y? What IS this method? Use real words!
case y
when 3
x * (x + 1) / 2
# ...
when 8
x * ((3 * x) - 2)
end
end
polygonals = ([(1..149)] * 6)
.map.with_index do |range, index|
range
.map { |x| rename_me(x, index + 3) }
.select { |x| x.to_s.length == 4 }
end
However (!!) you should use better variables names than I have above. Don't just call things x etc, unless they are just scratch variables; e.g. using i as an index can be OK sometimes.

Resources