Skip to main content

Posts

Showing posts with the label Arrays

Find partial string text in array object

Example: If I use the code below it works for the whole word "gorilla". How do I get it to work with "gor" or "illa" or "orill"...? ...(equivalent to a str like "%gor%" in mysql for example) const jungle = [ { name: "frog", threat: 0 }, { name: "monkey", threat: 5 }, { name: "gorilla", threat: 8 }, { name: "lion", threat: 10 } ]; const names = jungle.map(el => el.name); // returns true document.write(names.includes("gorilla")); by Ruy Hoffmann in StackOverflow on July 05, 2022 . Answer You can use find (or filter ) const jungle = [ { name: "frog", threat: 0 }, { name: "monkey", threat: 5 }, { name: "gorilla", threat: 8 }, { name: "lion", threat: 10 } ]; const names = (partial) => jungle.find(el => el.name.includes(partial)).name; //You can also use ...

Remove duplicate index in 2D array

I have this 2D numpy array here: arr = np.array([[1,2], [2,2], [3,2], [4,2], [5,3]]) I would like to delete all duplicates corresponding to the previous index at index 1 and get an output like so: np.array([[1,2], [5,3]]) However, when I try my code it errors. Here is my code: for x in range(0, len(arr)): if arr[x][1] == arr[x-1][1]: arr = np.delete(arr, x, 0) >>> IndexError: index 3 is out of bounds for axis 0 with size 2 by testerboy22 in StackOverflow on July 02, 2022 . Answer Rather than trying to delete from the array, you can use np.unique to find the indices of first occurrences of the unique values in the second columns and use that to pull those values out: import numpy as np arr = np.array([[1,2], [2,2], [3,2], [4,2], [5,3]]) u, i = np.unique(arr[:,1], ret...

How to read the content from the second line onwards on a .txt file on C

I need help to read the numbers of a .txt file and put them in an array. But only from the second line onwards. I'm stuck and don't know where to go from the code that i built. Example of the .txt file: 10 20 45000000 48000000 56000000 #define MAX 50 int main (void){ FILE *file; int primNum; int secNum; int listOfNumers[50]; int numberOfLines = MAX; int i = 0; file = fopen("file.txt", "rt"); if (file == NULL) { printf("Error\n"); return 1; } fscanf(file, "%d %d\n", &primNum, &secNum); printf("\n1st Number: %d",primNum); printf("\n2nd Number: %d",secNum); printf("List of Numbers"); for(i=0;i<numberOfLines;i++){ //Count the number from the second line onwards } fclose(file); return 0; } by LearningLinux in StackOverflow on July 03, 2022 . Answer ...

Splitting Array and adding splitted part to end

I want to split the array at a point s and add it to the end of the array. But my output is my input list. def split_and_add(arr, s): n = len(arr) if n >= s or s <=0: return arr else: end = [] for i in range(0,s): end = end + [arr[i]] start = [] for i in range(s,n): start = start + [arr[i]] return start + end print(split_and_add([1,2,3,4,5,6],3)) The output is still [1,2,3,4,5,6]. Could somebody help? by MarcelausZell in StackOverflow on June 05, 2022 . Answer Only issue in your code is in line if n >= s or s <=0: Here, you are checking if length of array n is greater than the break point s and if yes, you are returning the originl array. But what you need to do is check if s is greater than or equal to length of array and if yes, return original array. So, all you need to do is replace n >= s with s >= n . So your condit...

Returning value corresponding to a searched key in a deeply nested Javascript object

I have an object like below in Javascript, const obj = { "group1": { "sub_group1_1": { "inner_sub_group1_1_1": { "name": "abc" }, "inner_sub_group1_1_2": { "name": "def" } }, "sub_group1_2": { "inner_sub_group1_2_1": { "name": "ghi" }, "inner_sub_group1_2_2": { "name": "jkl" } } }, "group2": { "sub_group2_1": { "inner_sub_group2_1_1": { "name": "mno" }, "inner_sub_group2_1_2": { "name": "pqr" } }, "sub_group2_2": { "inner_sub_group2_2_1": { "name...

Unexpected output - Choosing the lowest number from an array

I want to find the smallest number from the numbers in an array entered by the user. The output I am getting for the program is a vague number -858993460. This is generally because of a type mismatch but they seem alright here. Not able to think of a solution. Thanks for the help! ``` // #include<stdio.h> int main() { int a; int i; int num[25]; printf("Enter the 25 numbers:"); for (i = 0; i <= 24; i++) { scanf("%d", &num[i]); } for (i = 0; i <= 24; i++) { a = num[i]; if (a < num[i+1]) { a = a; } else a = num[i+1]; } printf("Lowest number is %d\n", a); return 0; } by JP0605 in StackOverflow on 2022-06-05 . Answer Hope this will work for you. Jus...

How to filter nested Map<String, Object> in java?

I have Map<String, Object> currencyRate that contains "license": "https:/etc", "timestamp": 1654, "base": "USD", "rates": { "AED": 3.6, "AFN": 88.9, "ALL": 112, etc } And e.g I want to get "AED" value which is 3.6. I've tried this: Map<String, Object> filtered = currencyRate.entrySet() .stream() .filter(map->map.getValue().toString().contains("AED")) .collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue())); But it returned this: "rates": { "AED": 3.6, "AFN": 88.9, "ALL": 112, etc } So "rates" is key and it has values which also has keys and values. How can I get keys and values in "rates"? by Nam in StackOverflow on 2022-06-05 . Answer Since you're using a map where each value can b...