Iterative search using Prolog solve(P) :- start(Start), search(Start,[Start],Q), reverse(Q,P). search(S,P,P) :- goal(S), !. /* done */ search(S,Visited,P) :- next_state(S,Nxt), /* generate next state */ ...
more »

2017-09-05 20:09 (0) Answers

How to iterate over this n-dimensional dataset?

I have a dataset which has 4 dimensions (for now...) and I need to iterate over it. To access a value in the dataset, I do this: value = dataset[i,j,k,l] Now, I can get the shape for the dataset: shape = [4,5,2,6] The values in shape represent...
more »

2017-08-17 16:08 (1) Answers

Finding a true integer square root from an array

I'm sure this is such a simple solution and for that I'm sorry but it's driving me mad. I'm trying to iterate over an array and find a true integer square root and if there isn't one then square the number instead. I don't know how to do Math.sqrt...
more »

2017-07-16 21:07 (5) Answers

Searching in python dictionary of dictionaries

I have a dictionary of dictionary called data_dict. Following is how it looks: {'UMANOFF ADAM S': {'total_stock_value': 'NaN', 'loans': 'NaN', 'salary': 288589}, 'YEAP SOON': {'total_stock_value': 192758, 'loans': 'NaN', 'salary': 'NaN'}, 'PIPER GRE...
more »

2017-05-04 13:05 (2) Answers

Efficiently sum items by type

I have a list of items with properties "Type" and "Time" that I want to quickly sum the time for each "Type" and append to another list. The list looks like this: Items = [{'Name': A, 'Type': 'Run', 'Time': 5}, {'Name': B, 'Type': 'Walk'...
more »

2017-04-12 20:04 (8) Answers

Non-iterative algorithm for 1D game of life

Consider a Boolean array a[n], where each element is a cell. A cell becomes alive (set to true) in the next generation if one and only one adjacent cell is alive, otherwise it becomes dead (set to false). The first and last cell are considered neighb...
more »

2017-02-19 03:02 (3) Answers

C++: Fast structure with two related keys

I have a situation where I have a container which needs to hold an ID (IDs are unique) and a data value. I also need to keep these IDs in an order. The tuple of these variables will be looked up by the ID, but then processed in order up to the found ...
more »

2016-12-06 15:12 (3) Answers

Recursion to loop

I've been given an assignment of writing both recurring and iterating programs of function, defined as: T(n,0)=n, n>=0 T(0,m)=m, m>=0 T(n,m)=T(n-1,m)+2*T(n, m-1) I am allowed to use only basic operations (so +, -, *, /, %) and not allowed...
more »

2016-12-06 03:12 (1) Answers

Iterating array over array without duplicates

I am writing a simple game with a simple collision detection system. I currently have this code for iterating the array over the same array so i can check if the objects are close to each other and if they will collide: var objects = []; //assu...
more »

2016-11-01 17:11 (2) Answers

How to convert recursion to iteration?

This question is asked few times but I still find it quite difficult to convert easily readable and intuitive code into iterative code. For example I was practicing a coding question and I am given 26 integers which indicate how many times each chara...
more »

2016-10-13 19:10 (2) Answers

Max value within array of objects

I'm new in ruby. I'm trying to do the following but haven't succeeded. I've got an array of objects, let's call it objs. Each object has multiple properties, one of those is a variable that holds a number, let's call it val1. I wanna iterate through...
more »

2016-09-17 03:09 (2) Answers

python :: iterate through nested JSON results

iterating through JSON results can get quite confusing at times. say I have a functionlike so: def get_playlist_owner_ids(query): results =, type='playlist') id_ = results['playlists']['items'][0]['owner']['id'] retur...
more »

2016-08-20 01:08 (4) Answers

Select all but current in array

a = [1,4,1] total = [] a.each do |num| total << {|x| x != num} end p total => [[4], [1, 1], [4]] I am hoping to select all the other elements inside an array except the current element. The above works fine when there's no dup...
more »

2016-06-30 22:06 (1) Answers