How to find the sum of a multidimensional array [duplicate] - arrays

This question already has answers here:
Ruby method to sum all values in a multidimensional array
(5 answers)
Closed 6 years ago.
I've been looking for the answer to this. Maybe I haven't looked hard enough. This question has stumped me.
stairs = [[sunday],[monday],[tuesday],[wednesday],[thursday],[friday],[saturday]]
sunday = [6737, 7244, 5776, 9826, 7057, 9247, 5842, 5484, 6543, 5153, 6832, 8274, 7148, 6152, 5940, 8040, 9174, 7555, 7682, 5252, 8793, 8837, 7320, 8478, 6063, 5751, 9716, 5085, 7315, 7859, 6628, 5425, 6331, 7097, 6249, 8381, 5936, 8496, 6934, 8347, 7036, 6421, 6510, 5821, 8602, 5312, 7836, 8032, 9871, 5990, 6309, 7825]
each day has their own set of values. How do I find the sum of all the values within the array? I apologize if this has already been asked. I am a beginner and I'm finding it difficult just searching for answers to my questions.

Could be as simple as this:
stairs.flatten.inject(0, &:+)

Related

Python numpy: Selecting array entries based on input array [duplicate]

This question already has answers here:
Getting the indices of several elements in a NumPy array at once
(5 answers)
Closed 2 years ago.
Assume I have an array:
a = np.array([1,2,3,4,5])
Now I want to find the indices of elements in this array corresponding to the values given by another array input:
input = np.array([2,4,5])
The expected result should be:
result = [1,3,4]
A boolean mask, which is true for element indices 1,3,4 would also be fine.
I do not want to use looping to solve this. I assume that a possible solution has to do with the numpy where() function, but using this one, I am only able to compare the entries of array a with one element of array input at a time. Because the length of input might differ, I cannot really use this approach. Do you have any other ideas?
Thanks in advance.
np.where(np.in1d(a, inp))[0]
or:
np.isin(a, inp).nonzero()[0]
or as suggested here:
sorter = np.argsort(a)
sorter[np.searchsorted(a, inp, sorter=sorter)]
output:
[1 3 4]
np.where(np.in1d(a, inp))[0] np.where(np.in1d(a, inp))[0]

how to generate a new array in perl each time a loop runs [duplicate]

This question already has answers here:
Creating arrays dynamically in Perl
(4 answers)
Closed 6 years ago.
I am running a loop and each time i need to store some information in a new array. How to generate a new array each time the loop runs like #array1, #array2 , #array3 and so on?
What you want is an array of arrays, i.e. a multi-dimensional array.
my #arrays;
for my $i(0..10)
{
$arrays[$i] = ['data1', 'data2'];
}
print $arrays[0][0];
print $arrays[0][1];

Swift - Formatting an Array of Phone Number Strings [duplicate]

This question already has answers here:
string replace phone number iOS swift
(4 answers)
Closed 7 years ago.
I have an array of phonenumbers from my phone's contacts list that are are in different formats ranging from +1 (123) 123-1234, 1231231234, +11231231234, and 123-123-1234. I want them to all be in the format of 1231231234, but I don't much about string formatting. Can anyone point me in the right direction?
Try something like this:
let digits = NSCharacterSet.decimalDigitCharacterSet()
var phoneNumberDigits = ""
for character in phoneNumber.unicodeScalars {
if digits.longCharacterIsMember(character.value) {
phoneNumberDigits += String(character)
}
}

Delete all occurrences of value in Swift Array [duplicate]

This question already has answers here:
Remove matched item from array of objects?
(5 answers)
Closed 7 years ago.
So I have an array of Int's and I am trying to create a function to delete a certain value from the array. However, the removeAtIndex() function only deletes the first occurrence and the removeLast() only removes the last. I tried enumerating through the array but I end up with an Array index out of range error, possible due to the reshifting that occurs when deleting an item from the array.
for (index, value) in connectionTypeIDs.enumerate() {
if (value == connectionTypeToDelete){
connectionTypeIDs.removeAtIndex(index)
}
}
Any ideas on how to accomplish this?
The quickest way is to use filter. I don't know is that answer your question but you can have a look on this:
// remove 1 from array
arr = arr.filter{$0 != 1}

Java 8 frequency Object in array [duplicate]

This question already has answers here:
Word frequency count Java 8
(11 answers)
Closed 7 years ago.
I have an Object[] array
I need to create map Map<Obejct, Integer>, where Integer value contains frequency of key Object in array.
How can i do it in java 8 style, using Collectors?
You can do (I hope I don't have any typos) :
Map<Object,Long> map = Stream.of(array)
.collect(Collectors.groupingBy(o -> o,
Collectors.counting()));
This should group the elements of the array by equality and count the number of Objects in each group.

Resources