## Iterative search using Prolog

search.pl 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 »

## Insert a predefined array onto large array and shift the position of the smaller array iteratively

I would like to insert a array of size 2*2 filled with zeros onto a larger array. Further, I would like to shift the position of the zero array left to right, top to bottom iteratively. zero_array =[0 0 0 0] large_array =[ 1 2 3 4 ...
more »

## Generate Correct JSON from Java Object - Jackson

I have following result for the query select * from student where courseName = 'Science'; Results: student_id | name | points | course_name | course_id | +----------+--------+--------+---------------+-----------+ 1107| Matt | 3000 |...
more »

## 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 »

## python How to replace the first f(x) values of each x in array(x,n)

I would like to replace the first x values in every row of my array a with ones and to keep all the other values NaN. The first x values however changes in every row and is determined by a list b. Since I'm not very familiar with arrays I thought I ...
more »

## 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 »

## Create JS iterating array with specific number values 0001 to 9999

I have a grid of images to create out of images with such names: 0001.jpg that will expand to 9999.jpg (auto populate with foreach). I will use jQuery if needed, but basically I need to get this syntax: <img class="grid-item" src="0001.jpg" />...
more »

## Iterate through two parallel collections and output a third one

I have two folders containing two the same list of files with almost identical numbers. **Folder_XX:** - xx_file1.xml - xx_file2.xml ... - xx_fileN.xml **Folder_YY:** - yy_file1_bkabka.xml - yy_file2_blabla.xml ... - yy_fileN_zedzed.xml For eac...
more »

## What's the quickest way to assign a variables based on an array?

What's the quickest way, from a readability/typing standpoint, to assign a value to a specific variable based on a related variable? var abbrev; if(state=='Pennsylvania'){ abbrev='PA'; }else if(state=='New Jersey'){ abbrev='NJ'; }else if(st...
more »

## How to iterate through every object in an ElasticSearch index?

This should be easy. I've inserted a number of records into ElasticSearch and have been scouring their documentation but I can't seem to find a way to simply take an index and iterate through it in Node.js I don't have much experience with Elastic,...
more »

## iterate over array to find int over 10 and add those two digits together

Trying to iterate over and array and for any digit 10 or higher, split those digits and add them together for instance: 10 > "1" "0" > 1. I am able to iterate through the array and achieve that. however, it returns nil instead of the digits &...
more »

## 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 »

## 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 »

## Why `__iter__` does not work when defined as an instance variable?

If I define the __iter__ method as follows, it won't work: class A: def __init__(self): self.__iter__ = lambda: iter('text') for i in A().__iter__(): print(i) iter(A()) Result: t e x t Traceback (most recent call last): File...
more »

## Convert Recursive Method Group in LINQ Select to iterative method

I've got a class that looks like this: public class SourceObject { public string Id { get; set; } public List<SourceObject> Children { get; set; } public SourceObject() { Children = new List<SourceObject>(); ...
more »

## Why the Ruby each iterator goes first in the execution?

I've came across a weird thing doing simple tasks in Ruby. I just want to iterate the alphabet with the each method but the iteration goes first in the execution: alfawit = ("a".."z") puts "That's an alphabet: \n\n #{ alfawit.each { |litera| puts li...
more »

## 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 »

## Using datetime to determine highest frequency event within a time interval

I have a dataframe that includes a number of statistics on crime, including the date and time of the crime as well as the category. 0 5/13/2015 8:55 VEHICLE THEFT 1 5/13/2015 8:41 OTHER OFFENSES 2 5/13/2015...
more »

## How do you iterate over a 2d array in increments and increase the increment with ruby?

I'm trying to grab two subarrays at a time within an array - any ideas on how to do this? example: deck = [[2,spades],[3,hearts],[6,diamonds],[10,clubs],[8,hearts],[9,clubs]] Is there any way to grab two cards at a time with a loop, so the index ...
more »

## Need help saving a parameter to a variable each iteration

In the code shown, I want to save one parameter (fval) per iteration in one variable, but not sure how to do it. Can someone advise? clear; close all; clc; for i = 0 : 100 ii = i * 0.01; options = optimset('Display','iter-detailed', ... ...
more »

## 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 »

## 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 »

## How to iterate with for loop over numbers starting with 0 in PHP?

I'm trying to iterate with for loop through 8 digit numbers starting with 0. For example, the first number is: 00000000 and I would like to display next 5 numbers. So far I managed to accomplish sth. like that: <?php \$start = 00000000; ...
more »

## Speed to iterate multiple times over a generator compared to a list

I expected that in the case of multiple loops list iteration will be much faster than using a generator, and my code suggests this is false. My understanding is (by operation I mean any expression defining an element): a list requires n operation...
more »

## 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 »

## 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 »

## Accessing an array some elements at a time and wrapping round to the beginning

I have a simple question that should have a simple answer but I can't think of it yet. I want to process an array, a certain number of elements at a time, and wrapping round to the beginning. Here's a diagram showing when n is 10 and three elements ...
more »

## 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 »

## Many very similar functions, spaghetti code fix?

I have approx 11 functions that look like this: def pending_acceptance(order_fulfillments) order_fulfillments.each do |order_fulfillment| next unless order_fulfillment.fulfillment_time_calculator. pending_acceptance?; collect_fulfillme...
more »

## 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 = sp.search(q=query, type='playlist') id_ = results['playlists']['items'][0]['owner']['id'] retur...
more »

## Python for loop appending to every key in dictionary

I'm iterating over a list of tuples and a list of strings. The strings are identifiers for the items in the list. I have a dictionary that has the strings identifiers as keys and has an initially empty list for each value. I want to append something ...
more »

## handling large integers in for loop optimized in python

I'm reading a large csv file (500 000 rows) and adding every row in a dict. One example row is: 6AH8,F,B,0,60541765,60541765,90.52,1 index 4 - 60541765 and index 5 - 60541765in this case is the same, but it is not always the case. These integers a...
more »

## Select all but current in array

a = [1,4,1] total = [] a.each do |num| total << a.select {|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 »

## How to decide between lambda iteration and normal loop?

Since he introduction of Java 8 I got really hooked to lambdas and started using them whenever possible, mostly to start getting accustomed to them. One of the most common usage is when we want to iterate and act upon a collection of objects in which...
more »