nested-loops

efficient algorithm instead of looping

荒凉一梦 提交于 2019-12-04 20:27:25
问题 I have a data set where each samples has a structure similar to this X=[ [[],[],[],[]], [[],[]] , [[],[],[]] ,[[][]]] for example: X=np.array([ [ [1,2,3], [2,4,5] ,[2,3,4] ] , [ [5,6], [6,6] ] , [[2,3,1],[2,3,10],[23,1,2],[1,4,5]] ] ,"object") Y=np.array([ [ [12,14,15] ,[12,13,14] ] , [ [15,16], [16,16] ] , [[22,23,21],[32,33,11],[12,44,55]] ] ,"object") so for every sample I need to calculate the dot product between every element of x with corresponding element of y of same index and sum the

Java nested loops

十年热恋 提交于 2019-12-04 07:15:29
问题 Program Description: Write a program to print 21 rows of X's in the shape of a large X as illustrated below. Be sure so the two rows intersect at the "11" row. Here is what I want as an output: Here is what I have so far. public class Program168h { public static void main (String [] args) { String d= "X"; for (int a = 1; a < 23; a++) { for (int b = a; b >= 1; b--) { System.out.print(" "); } System.out.print(d); for (int x = a; x < 22; x++) { System.out.print(" "); } System.out.print(d);

JavaScript get value from nested object [duplicate]

≯℡__Kan透↙ 提交于 2019-12-04 07:13:18
问题 This question already has an answer here : JavaScript - retrieve value from nested object, using array of keys (1 answer) Closed 2 years ago . If this is my object: var obj = { bakery1: { small: { name: "Small cookie", price: 0.75; } large: { name: "Large cookie", price: 3.00; } } bakery2: { small: { name: "Small cookie", price: 1.00; } large: { name: "Large cookie", price: 4.00; } } }; How would I go about making a loop that prints every price to the console? And when this value is printed,

Nested WHILE loops in Python

岁酱吖の 提交于 2019-12-04 07:06:15
I am a beginner with Python and trying few programs. I have something like the following WHILE loop construct in Python (not exact). IDLE 2.6.4 >>> a=0 >>> b=0 >>> while a < 4: a=a+1 while b < 4: b=b+1 print a, b 1 1 1 2 1 3 1 4 I am expecting the outer loop to loop through 1,2,3 and 4. And I know I can do this with FOR loop like this >>> for a in range(1,5): for b in range(1,5): print a,b 1 1 1 2 .. .. .. .. // Other lines omitted for brevity 4 4 But, what is wrong with WHILE loop? I guess I am missing some thing obvious, but could not make out. Answer: The corrected WHILE loop.. >>> a=0 >>>

Scheme removing nested duplicates

霸气de小男生 提交于 2019-12-04 06:36:17
问题 So I'm programming in scheme and made a function that removes duplicated but it doesn't work for nested. I can't really figure out a good way to do this, is there a way to modify the current code I have and simply make it work with nested? lists? Here's my code (define (duplicates L) (cond ((null? L) '()) ((member (car L) (cdr L)) (duplicates (cdr L))) (else (cons (car L) (duplicates (cdr L)))))) 回答1: So your procedure jumps over elements that exist in the rest of the list so that (duplicates

Return from a nested loop in Common Lisp

放肆的年华 提交于 2019-12-04 04:29:44
问题 I'm trying to convert this Python code into Common Lisp: for a in xrange(1,1000): for b in xrange(a,1000): c = (a**2 + b**2) ** 0.5 s = a + b + c if s == 1000: return a * b * c My first attempt was: (loop for a from 1 to 999 do (loop for b from a to 999 for c = (sqrt (+ (expt a 2) (expt b 2))) for s = (+ a b c) until (= s 1000) finally return (* a b c)))) This doesn't work. My task is: when s hits 1000 make the whole expression above return (* a b c) . How to return some value from a nested

In Apache Spark, can I easily repeat/nest a SparkContext.parallelize?

為{幸葍}努か 提交于 2019-12-04 02:27:40
问题 I am trying to model a genetics problem we are trying to solve, building up to it in steps. I can successfully run the PiAverage examples from Spark Examples. That example "throws darts" at a circle (10^6 in our case) and counts the number that "land in the circle" to estimate PI Let's say I want to repeat that process 1000 times (in parallel) and average all those estimates. I am trying to see the best approach, seems like there's going to be two calls to parallelize? Nested calls? Is there

Pythonic shortcut for doubly nested for loops?

♀尐吖头ヾ 提交于 2019-12-03 23:31:15
Consider if I had a function that took a tuple argument (x,y), where x was in the range(X), and y in the range(Y), the normal way of doing it would be: for x in range(X): for y in range(Y): function(x,y) is there a way to do for xy in something_like_range(X,Y): function(xy) such that xy was a tuple (x,y)? You can use product from itertools >>> from itertools import product >>> >>> for x,y in product(range(3), range(4)): ... print (x,y) ... (0, 0) (0, 1) (0, 2) (0, 3) (1, 0) (1, 1) (1, 2) (1, 3) ... and so on Your code would look like: for x,y in product(range(X), range(Y)): function(x,y) You

PHP Looping Template Engine - From Scratch

浪尽此生 提交于 2019-12-03 18:39:58
问题 For a group project I am trying to create a template engine for PHP for the people less experienced with the language can use tags like {name} in their HTML and the PHP will replace that tag with a predefined variable from an array. As well as supporting loops. This is well beyond the expectations of the project, but as I have experience with PHP I thought it would be a good challenge to keep me busy! My main questions are, how do I do the loop part of the parser and is this the best way to

bash shell nested for loop

流过昼夜 提交于 2019-12-03 04:50:01
问题 I want to write a nested for loop that has to work in the bash shell prompt. nested for loop in Single line command. For example, for i in a b; do echo $i; done a b In the above example, for loop is executed in a single line command right. Like this I have tried the nested for loop in the shell prompt. Its not working. How to do this. Please update me on this. 回答1: This is not a nested loop, just a single loop. And the nested version works, too: # for i in a b; do for j in a b; do echo $j;