Array of indexes to array of ranges
(New and improved. Stays fresh in your refrigerator for up to two weeks!):
a = [1, 2, 3, 10, 11, 20, 20, 4]
ranges = a.sort.uniq.inject([]) do |spans, n|
if spans.empty? || spans.last.last != n - 1
spans + [n..n]
else
spans[0..-2] + [spans.last.first..n]
end
end
p ranges # [1..4, 10..11, 20..20]
return an index range of elements in an array
You can use slice
var array = ["1", "2", "3"]
let indexRange = (arr, start, end) => {
return arr.slice(start, end)
}
console.log(indexRange(array, 0, 3));
How can I get specific range of array just in specific range index by PHP?
I found a simple solution after several tests:
foreach($my_arr as $indx=>$val )
{
if(is_int($indx) && ($indx>=0 && $indx<=4))
{
$my_arr[$indx]= $val;
}
else
{ continue; }
}
I want to select specific range of indexes from an array
Numpy slicing allows you to input a list of indices to an array so that you can slice to the exact values you want.
For example:
import numpy as np
a = np.random.randn(10)
a[[2,4,6,8]]
This will return the 2nd, 4th, 6th, and 8th array elements (keeping in mind that python indices start from 0). So, if you want every 2nd element starting from an index x, you can simply populate a list with those elements and then feed that list into the array to get the elements you want, e.g:
idx = list(range(2,10,2))
a[idx]
This again returns the desired elements (index 2,4,6,8).
How to return a specific range of indexes from within in a JSON Array?
If you can use ES2019+:
const getPastCasesByCountry = (response, country, pastDays) => Object.fromEntries(
Object.entries(response[country].dates)
.sort(([date1], [date2]) => date1.localeCompare(date2))
.slice(-pastDays)
);
const last7DaysAN = getPastCasesByCountry(response, 'AN', 7);
This uses
- the handy
Object.entries
+Object.fromEntries
combo which gives you the ability to work with an object essentialy the same way as with an array - the fact that
Array.slice
can take a negative start index, which is an offset from the end – as we needed here
Convert to data set
To convert to DataSet
as described in your question, I'd change the function a bit:
const getPastCasesByCountry = (response, country, pastDays) =>
Object.entries(response[country].dates)
.sort(([date1], [date2]) => date1.localeCompare(date2))
.slice(-pastDays)
const DataSet = (casesByDate) => casesByDate.map(([dateString, cases]) => ({
x: new Date(dateString),
y: cases.total.confirmed
}))
const data = DataSet(getPastCasesByCountry(response, 'AN', 7));
Indexing different sized ranges in a 2D numpy array using a Pythonic vectorized code
We can use broadcasting
to generate an appropriate mask and then masking
does the job -
In [150]: a
Out[150]:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])
In [151]: b
Out[151]: [4, 3, 1]
In [152]: mask = np.arange(len(a))[:,None] < b
In [153]: a.T[mask.T]
Out[153]: array([0, 3, 6, 9, 1, 4, 7, 2])
Another way to mask would be -
In [156]: a.T[np.greater.outer(b, np.arange(len(a)))]
Out[156]: array([0, 3, 6, 9, 1, 4, 7, 2])
Bonus : Slice per row
If we are required to slice per row based on chunk sizes, we would need to modify few things -
In [51]: a
Out[51]:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
# slice lengths per row
In [52]: b
Out[52]: [4, 3, 1]
# Usual loop based solution :
In [53]: np.concatenate([a[i,:b_i] for i,b_i in enumerate(b)])
Out[53]: array([ 0, 1, 2, 3, 5, 6, 7, 10])
# Vectorized mask based solution :
In [54]: a[np.greater.outer(b, np.arange(a.shape[1]))]
Out[54]: array([ 0, 1, 2, 3, 5, 6, 7, 10])
How do I declare a range within an array using an enumeration in Ada?
There are some different means you can go about doing this. The simplest is to declare a constant array using Donuts as the index and Bakers_Dozen as the Element type for the array. Then you just use the array to convert the indexes:
package Indexes is
type Bakers_Dozen is range 1 .. 13; -- Indices from 1 to 6 are donuts
type Donut is
(Plain,
Chocolate,
Strawberry,
Glazed,
Blueberry,
Cannabis);
To_Index : constant array (Donut'Range) of Bakers_Dozen :=
(Plain => 1,
Chocolate => 2,
Strawberry => 3,
Glazed => 4,
Blueberry => 5,
Cannabis => 6);
end Indexes;
Then you can do:
P1 : array(Indexes.Bakers_Dozen'Range) of Integer;
and
P1(Indexes.To_Index(Indexes.Plain)) := 2;
An alternative to this method is to ensure that Donuts is the same size as Bakers_Dozen and then use Ada.Unchecked_Conversion to convert a Donut object to a Bakers_Dozen object
package Conversions is
type Bakers_Dozen is range 1 .. 13; -- Indices from 1 to 6 are donuts
type Donut is
(Plain,
Chocolate,
Strawberry,
Glazed,
Blueberry,
Cannabis)
with Size => Bakers_Dozen'Size;
for Donut use
(Plain => 1,
Chocolate => 2,
Strawberry => 3,
Glazed => 4,
Blueberry => 5,
Cannabis => 6);
function To_Index is new Ada.Unchecked_Conversion(Donut,Bakers_Dozen);
end Conversions;
Then you can do:
P2 : array(Conversions.Bakers_Dozen'Range) of Integer;
and
P2(Conversions.To_Index(Conversions.Chocolate)) := 3;
If you want to get fancy you can also wrap one of those methods into a private type and use more advanced concepts such as "Reference_Type" objects and the aspects Variable_Indexing and Constant_Indexing to make your private type act like an array
package Wrappers is
type Bakers_Dozen is range 1 .. 13; -- Indices from 1 to 6 are donuts
type Donut is
(Plain,
Chocolate,
Strawberry,
Glazed,
Blueberry,
Cannabis);
type Bakers_Array is tagged private
with
Variable_Indexing => Element;
type Element_Holder(Element : access Integer) is null record
with Implicit_Dereference => Element;
function Element
(Self : aliased in out Bakers_Array;
Index : Donut)
return Element_Holder;
function Element
(Self : aliased in out Bakers_Array;
Index : Bakers_Dozen)
return Element_Holder;
private
type Element_Array is array (Bakers_Dozen'Range) of aliased Integer;
type Bakers_Array is tagged record
Elements : Element_Array;
end record;
To_Index : constant array (Donut'Range) of Bakers_Dozen :=
(Plain => 1,
Chocolate => 2,
Strawberry => 3,
Glazed => 4,
Blueberry => 5,
Cannabis => 6);
function Element
(Self : aliased in out Bakers_Array;
Index : Donut)
return Element_Holder
is (Element => Self.Elements(To_Index(Index))'Access);
function Element
(Self : aliased in out Bakers_Array;
Index : Bakers_Dozen)
return Element_Holder
is (Element => Self.Elements(Index)'Access);
end Wrappers;
Then you can do:
P3 : Wrappers.Bakers_Array;
and
P3(Wrappers.Strawberry) := 4;
NOTE: I didn't add the Constant_Indexing aspect to save on typing, but you might want that too. You can look at Ada.Containers.Vectors to see how this is done.
There are probably other methods, but these are just off the top of my head.
Note also that I am just using Integers as the array elements for simplicity. You can always make these arrays of other types.
Return the range within an array Excel
Use INDEX and SEQUENCE:
=SUM(INDEX(FILTER(Numbers,Numbers<>0),SEQUENCE(8)+3))
Related Topics
What's the Difference Between These Ruby Namespace Conventions
What Does the Equal ('=') Symbol Do When Put After the Method Name in a Method Definition
How to Remove Installed Ri and Rdoc
How to Make Rails 3.1 Use SASS (Over SCSS) as the Default
Create Custom HTML Helpers in Ruby on Rails
Undefined Method with "_Path" While Using Rails Form_For
What Does "Shadowing" Mean in Ruby
Rails 3.1 Absolute Url to an Image
Returning Data from Forked Processes
How to Solve Insecure World Writable Dir /Usr in Path,Mode 040777 Warning on Ruby
How to Compile Ruby to Byte Code as with Python
Query Records Through Its Belongs_To Relation in Rails