Questions tagged [nested-loops]

A logical structure used in computer programming where two or more repeating statements are placed in a "nested" form (i.e., one loop is situated within the body of another). The inner loop is repeated in full on every pass through the outer loop.

Filter by
Sorted by
Tagged with
1 vote
2 answers
60 views

Python Shopping Cart - How to use loops to limit certain inputs?

I'm creating a shopping cart that accepts discounts. How to limit discounts to only be applied once? print("Welcome to the Store!") print("Enter 1 to start registering your grocery"...
Noob's user avatar
  • 11
-1 votes
1 answer
82 views

Brainfuck Compiler in javascript [duplicate]

I wrote a BrainFuck compiler in JavaScript and it works fine with this input: ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++.....
Laczkó Lajos's user avatar
2 votes
2 answers
65 views

How can I implement a linear time implementation of a function that counts the number of occurrences of a non-consecutive subsequence?

My Programming I class assigned an exercise in which we have to implement a function that counts the number of occurrences of a non-consecutive subsequence in a string. This is what we're asked to do: ...
serialexperimentshaku's user avatar
0 votes
2 answers
95 views

Optimizing Nested Loop Performance for Large Dataset Processing in Python

I am currently working on a data analysis script in Python 3.8 where I need to process a large dataset consisting of over a million rows. My script uses nested loops to compare each row with multiple ...
Nadav Ishai's user avatar
2 votes
4 answers
129 views

Print numbers of a matrix in a wave pattern

I want the output to be 7 4 1 2 5 8 9 6 3 but it's coming out to be 1 4 7 2 5 8 3 6 9. How can I fix it, and what's the logic behind it so that I can print the reverse wave pattern, too? #include<...
Samarth Talreja's user avatar
0 votes
0 answers
13 views

How to match mulitple cells from Sheet1 to multiple cells from sheet2 in Excel including having multiple conditions met?

Sheet1 Columns: A1- Account Number B1- Invoice/Credit # C1- NDC D1- Item Description E1-Invoiced Quantity Sheet2 Columns: A1- Invoice Number B1- NDC C1- Qty In another column in sheet1, I would like ...
TWA1913's user avatar
0 votes
0 answers
18 views

How to use parallel for loop with scipy L-BFGS-B minimzer in python?

I am trying to do inversion using scipy L-BFGS-B optimizer which uses Pool inside for forward modelling function. In my forward modelling function I have a for loop of a function running over subset ...
user155709's user avatar
0 votes
1 answer
47 views

Pascal - Shape Drawing

So the task is to draw this shape : ****** ** ** * * ** ** ****** The number of rows is set by the user,and it is labeled as n.In this case it is n=3. So i tried splitting it in two , and i ...
Ognjen's user avatar
  • 3
0 votes
1 answer
71 views

Optimization of Python Nested loops with Two Arrays

In an assessment, I got the following question: "There are two teams of size N, and the skill values of each player on the team are stored inside an array, write an algorithm that counts all the ...
Bruno De Vivo's user avatar
0 votes
6 answers
108 views

How to print differences between 2 elements of an array in a form of another array

As an example I have an array of 5 elements: Input array: ele = [2, 5, 9, 1, 4] I want to output an array that has difference between all the elements but not repeated difference (ie, I don't want to ...
newprogrammer's user avatar
-2 votes
1 answer
44 views

How to multiply the elements of two lists with each other in python? [duplicate]

You have to create an N-string. An N-string is a string created using two lists. The first list consists of strings. The second list consists of integer values. These two lists are of same length. ...
NAYEEM's user avatar
  • 13
0 votes
0 answers
57 views

do I need a nested foreach loop to accomplish this?

Looking at this chunk of code I managed to cobble together: Get-ChildItem -Path "$hashpath" -Recurse -File | Select @{N='FileHash'; E={(Get-FileHash -LiteralPath $_.FullName -algorithm MD5)....
HTWingNut's user avatar
  • 263
0 votes
0 answers
67 views

Changing a nested loop to print elements differently

I have this nested loop. for (Fencerss fencer1 : team1.getFencers()) { for (Fencerss fencer2 : team2.getFencers()) { PdfPCell cell1 = new PdfPCell(new Phrase(fencer1.getName() + " &...
Moustafa Amr's user avatar
2 votes
3 answers
92 views

Filling 2D array diagonally

I've been trying to get a 10x10 2D array filled diagonally as in the image below for a couple hours now, but after checking every possible way to use rows and columns I still can't seem to identify my ...
blackburrow112's user avatar
0 votes
2 answers
66 views

Find all unique combinations of removing a duplicate in groups from a data set

I am trying to figure out how to create code in R to find all combinations of how a duplicate can be removed from a dataset from different groups and create a list of all the dataset combinations. An ...
madmad123's user avatar
1 vote
3 answers
119 views

for loop in between if statement is not working in C

#include <stdio.h> int main() { int row, col, i, j; char ch; printf("Enter the Value of Row or Column: "); scanf("%d", &row); col = row; int mat[...
MJ Saif's user avatar
  • 11
1 vote
1 answer
21 views

Ansible build report from json

A PyATS Ansible playbook output is saved to a json file: - name: Export variable to file copy: content: "{{ output }}" dest: "report.json" The json content is as follows:...
everlearning777's user avatar
-1 votes
1 answer
38 views

Printing two related lists

I have an issue that I can't wrap my head around. I have two lists containing related information. The structure of the list is as follows: List_a = [(id_nr, date, event), (...)] List_b = [(id_nr, ...
VirreJoj's user avatar
-2 votes
2 answers
37 views

Perform multiple summation

I am trying to perform multiple summation in a code C++, but I don't know how to write a code to perform multiple for's . $$\sum_{S_1=\pm1}...\sum_{S_N=\pm1}(\Pi_{i=1}^{N-1}e^{S_iS_{i+1})}$$ As you ...
Pedro Ye's user avatar
0 votes
0 answers
48 views

what is the correct way to insert another nested do loop inside one nested do loop?

i have written a code like given below, my doubt is whether i am taking the loop in correct order to calculate the each pair of 'i' & 'j' which runs over iloop and jloop respectively. do i=1,n-1 ...
Sayantan Mondal's user avatar
0 votes
2 answers
39 views

Nested loop: interval loop brings results for the first iteration of the main loop

I am writing a laravel code in which I need a nested loop in the blade. Here is my controller public function show(string $id) { $transaction = Transaction::findOrFail($id); $subject_ids = ...
islambassiem's user avatar
0 votes
0 answers
34 views

Can I create a nested loop inside my function to return to the beginning of my code using (continue) [duplicate]

I want my terminateProgram to loop back to the beginning but is throwing an error saying its not in the loop. #this is the beginning of my code while True: #I want it to loop back here import os ...
Logan Brunner's user avatar
0 votes
1 answer
60 views

How can I implement a template-driven nested for-loop that is capable of returning a generator?

I am attempting to implement a generator that provides subscripts over a multi-dimensional array. Specifically, I am using the generator implementation by CPP Chrono. Since the number of dimensions is ...
Danny's user avatar
  • 356
1 vote
3 answers
50 views

error in comparing elements of a 2d array using if-else statements

i tried to make a game using 2d character arrays. but the if else statements are not working and the loop is not breaking. Also when i assign a character to the last element of my row , the next row's ...
Mayank's user avatar
  • 11
-1 votes
3 answers
78 views

3 nested for-each loops as Java Stream

On the project I am currently working on, we have this construct of three nested for loops: final Vector3i startPos = new Vector3i(-3, -2, -3); final Vector3i finishPos = new Vector3i(3, 4, 3); final ...
Ancient Darth's user avatar
1 vote
3 answers
70 views

How do I prevent checking of duplicate elements in a array?

I am checking frequency of each element in an array. The problem is the duplicate elements are counting themselves more than needed, I want to skip the counting procedure of the element which has been ...
NORAGAMI's user avatar
0 votes
1 answer
85 views

How can I optimize my algorithm by replacing if-then-else statements with case statements?

One may consider using case rather than if-then-else statements. If there are different cases, then the code goes to the right choice. This will save time without going through everything. If f_op is ...
husin alhaj ahmade's user avatar
0 votes
1 answer
45 views

VBA: Nested Loop Only Runs Once

srcPath = Dir("M:\UPLOAD\Feedback\*.xlsx") For Each File In myArray currentFile = CStr(File) '...
mRminer's user avatar
  • 11
1 vote
4 answers
70 views

Duplicate pairs of rows side by side in pandas given certain condition

I have the following code: import pandas as pd data = { 'Col1': ['John 1', 'John 2', 'John 3', 'Kyle 1', 'Kyle 3', 'Kyle 2'], 'Col2': ['B', 'C', 'E', 'F', 'F', 'S'], 'Col3': ['1', '1', '1'...
buckenup's user avatar
-1 votes
4 answers
70 views

Hourglass code logic error using nested for loops

I'm working on a assignment where I need to print our an hourglass using nested for loops. The input base number(number of initial quotation marks) is user inputed, and odd and even difference is that ...
Babette's user avatar
0 votes
3 answers
60 views

Terraform iterate over list in map

i'm working with terraform, and I have configuration like this: locals { clusters { "cluster1" = { instance_type = "t2.large" vms = ["vm1", "vm2&...
user3139566's user avatar
6 votes
1 answer
137 views

Transforming a C++ nested loop into a single one with an iterator

Let's say we have the following nested loop: MyClass myobject; for (const std::array<int,16>& arr : myobject) { for (int i : arr) { /* do something with i */ } } where MyClass can ...
edrezen's user avatar
  • 299
1 vote
1 answer
48 views

Populate the Diagonal Elements of An Array MATLAB

I have the following matrix C that I would like to recreate in MATLAB: In this case uppercase C is an N = 3 by N = 3 matrix, but I would like to generate C for any size of N by N. I randomly create ...
Jonathan Frutschy's user avatar
1 vote
1 answer
48 views

Populate A Function Handle "Array" in MATLAB

I have the following function handle uppercase K and array of lowercase k and gamma values. N = 3; k = randi([1,1000],N+1,1,"double"); gamma = randi([1,100],N+1,1,"double"); K = @...
Jonathan Frutschy's user avatar
1 vote
2 answers
76 views

How to use lapply to replace nested for loop to get correlations between two data frames?

I would like to get the estimate, statistic and p value from correlating two data frames together. I know how to do this using a nested for loop or foreach using parallelisation. However, I do not ...
pemb_bex6789's user avatar
-1 votes
1 answer
61 views

VBA to Python for array and nested iteration

I want to make this vba code into python, but i stuck in nested loop. Here is the vba code and result table equation : Sub iter_K() num_HW = 4 For i = 1 To num_HW For k = 1 To 0.01 Step -0.01 ...
Muhammad Kafiansyah's user avatar
0 votes
0 answers
41 views

Apps Script Slow Output [duplicate]

I am having problems creating an attendance tracker for my job. Specifically, I am trying to create some analysis metrics; the one I'm having a problem with is finding a total sum of days present for ...
B.A. Ceradsky's user avatar
-4 votes
1 answer
99 views

How do I make a boolean variable that is true if it is divisible by any odd number greater than 1 (including possibly itself), and false otherwise? [closed]

I need to create a boolean variable called hasOddFactor that is true if num is divisible by any odd number greater than 1 (including possibly itself), and false otherwise. The current code that I can ...
Sheebs's user avatar
  • 15
0 votes
2 answers
48 views

need to rotate a square matrix by 90degree clockwise. works for 1st loop but gives index out of range the moment it enters next cycle

class Solution: def rotate(self, matrix: List[List[int]]) -> None: for i in range(0, len(matrix)//2): x = i y = len(matrix)-i-1 for j in range(i,len(...
Sankalp Rawat's user avatar
-1 votes
1 answer
38 views

nested loop does not return any value - low level

alumnos = ["Pepe", "Juan", "Margarita", "Ariana"] notas = [99, 99, 99, 80] def pregunta_3(alumnos, notas): result = [] i = 0 while i < len(...
Amadis131's user avatar
-1 votes
3 answers
124 views

Simple roulette simulator where the bet is always on odd, but it is infinite

Trying to create a roulette simulator that only bets on odd. Here is a sample execution: Profit Goal: 150 Starting money: 100 Table minimum bet: 25 Table maximum bet: 300 Spin: 16. You lost $25 on the ...
Gwyn's user avatar
  • 1
0 votes
2 answers
114 views

Else statement logic is wacko

#include <cs50.h> #include <stdio.h> #include <string.h> #include <ctype.h> int main(void) { string key_upper = "YTNSHKVEFXRBAUQZCLWDMIPGJO"; string key_lower =...
Kalle's user avatar
  • 23
0 votes
2 answers
99 views

Why do I get a segmentation fault? The code works fine in CLion and VSC

I am currently using exercism, to improve my programming skills. The code works fine in my IDE, but causes an error message on the website. What did I do wrong and how would you improve my code? Thx ...
BenG's user avatar
  • 1
0 votes
0 answers
25 views

How can I save in a table the CIs of numerous bootstraps performed by a double loop in R?

I have made a double loop in R to make several bootstrap at the same time, but I do not know how to save just the CIs in a table. I need the double loop to perform Pearson correlation between several ...
Danielibero's user avatar
0 votes
2 answers
132 views

To print a pattern in python using nested for loops

Ok I am a beginner in Python. The pattern to be printed w/ nested loop: 1@ 1#3# 1@3@5@ 1#3#5#7# (For loop preferred) Thanks. This is the code I tried: n = int(input("Enter no:")) for i in ...
Anonymous 7's user avatar
0 votes
1 answer
50 views

My nested loop range is decreasing with every iteration

I have a nested for loop to iterate through my matrix where r and c are the rows and columns. However, somehow with every iteration, c decreased. e.g when r = 0, c will iterate from 0 to 9 when r = 1, ...
nic's user avatar
  • 3
0 votes
1 answer
19 views

Using purrr:map2 to transfer row names from one nested list to another

I can't seem to find a solution to the following problem: I have two nested lists (l1 and l2) and I want to transfer information in l2 as row names for the matrices in l1. To this end, I need to use ...
zionlion's user avatar
1 vote
1 answer
3k views

Nested Loop functions Python

Given num_rows and num_cols, output the label for each seat of a theater. Each seat is followed by a space, and each row is followed by a newline. Define the outer for loop, initialize curr_col_let ...
Whyte_Out's user avatar
0 votes
1 answer
47 views

How to nested loop infinite in Web Api 2.0 ApiController

I am stuck at the end to find code for Web Api 2.0 (Request and Response) in Get or Post Method BODY. Can someone help me to solve this problem? I want to make body JSON loop based on Request Body in ...
anto's user avatar
  • 5
-4 votes
2 answers
66 views

Stuck on pset 3 (Runoff) HELPP

Can someone tell me why my code doesn't work. here's its description https://cs50.harvard.edu/x/2020/psets/3/runoff/. the error is most probably outside of the main function. edit - the issue has been ...
Hashim's user avatar
  • 3

1
2 3 4 5
99