nested-loops

Creating a nested dictionary comprehension in Python 2.7

一个人想着一个人 提交于 2019-12-11 18:26:53
问题 I have a nested tuple returned from a MySQL cursor.fetchall() containing some results in the form (datetime.date, float) . I need to separate these out in to a nested dictionary of the form [month/year][day of month] - so I would like to have a dictionary (say) readings which I would reference like readings['12/2011'][13] to get the reading for 13th day of the month '12/2011'. This is with a view to producing graphs showing the daily readings for multiple months overlaid. My difficulty is

How to print a triangle of integers with number of lines specified by the user? [closed]

淺唱寂寞╮ 提交于 2019-12-11 18:22:51
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I would like to further exercise myself on the use of ArrayLists and Nested Loops, so I thought of the following interesting problem. The number of lines is specialized by the user and something would come out like this: Enter a number: 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Can anybody give me some

Is my pseudo code for changing time format correct? [duplicate]

拈花ヽ惹草 提交于 2019-12-11 14:55:29
问题 This question already has answers here : Convert 12-hour date/time to 24-hour date/time (8 answers) Closed 5 years ago . I need to create a pseudo code that will prompt for time in 24 hours format, convert it to 12-hour format and also whether it is AM or PM. It will have to repeat until sentinel time of 9999 is entered. Below is what I had come out with, is it correct? Prompt for time Get time Enter this loop: DOWHILE (time != 9999) IF (time < 0000 OR time > 2400 OR (time%100 >=60)) Display

Generating all the values of N nested for loops

…衆ロ難τιáo~ 提交于 2019-12-11 13:58:02
问题 I would like to write a function to do the following, given two arguments to the function int K and int nest_level generate all possible points that result from creating nest_level nested loops where each loop ranges from -K to K . For example if k = 5 and nest_level = 3 the function prints the sequences of numbers that result from the following: for(int i = -k; i <= k; ++i) for(int j = -k; j <= k; ++j) for(int m = -k; m <= k; ++m) print(i, j, k) This should work for any K and nest_level From

golfscript nested while loops

家住魔仙堡 提交于 2019-12-11 12:50:05
问题 Are nested while loops broken in golfscript or do I not know how to use them? I want to iterate Q from 5 to 0, and for each iteration, iterate Z from 10 to 0. The single loops work well separately, and they seem self-contained (not relying on the stack between operations): 5:Q; {"Q:"Q+ p Q} { Q 1- :Q; }while 10:Z;{"Z:"Z+ p Z}{Z 1- :Z;}while Output: "Q:5" "Q:4" "Q:3" "Q:2" "Q:1" "Q:0" "Z:10" "Z:9" "Z:8" "Z:7" "Z:6" "Z:5" "Z:4" "Z:3" "Z:2" "Z:1" "Z:0" But if I put the Z loop inside the Q loop,

Nested cursor in a cursor

故事扮演 提交于 2019-12-11 12:43:53
问题 I have a cursor which is CURSOR B_CUR IS select DISTINCT big_id from TEMP_TABLE; This would return multiple values. Earlier it was being used as FOR b_id IN B_CUR LOOP select s.col1, s.col2 INTO var1, var2 from sometable s where s.col3 = b_id.col1; END LOOP; Earlier it was certain that the inner select query would always return 1 row. Now this query can return multiple rows. How can I change this logic? I was thinking to create a nested cursor which will fetch into an array of record type

time complexity for this algorithm with two for loops

99封情书 提交于 2019-12-11 12:16:28
问题 Can you explain me how to find time complexity for this sum1=0; for(k=1;k<=n;k*=2) for(j=1;j<n;j++) sum++ I am taking consideration of N as base, and tried calculating how many iteration happens when n=1,2,3...n But i am not able to genralize it. PLease some one explain me, thoroughly. I have even checked other queries of similar type like: saying it follows 1,2,4,8...n so it will be 2^n nlogn But i dint understand. I dont know how they say 2^n=logn . Please any properly explain how to

Python modifying wrong list?

99封情书 提交于 2019-12-11 11:51:10
问题 I'm trying to generate a list of primes using the this method. I need to loop through every number 2...n and check it for multiples of 2...n. For some reason, the wrong list seems to be getting modified. import sys import argparse import math parser = argparse.ArgumentParser(description='find the largest prime factor of a number') parser.add_argument('n', type=int, help='number') args = parser.parse_args() sieve = [] for i in range(2,args.n+1): sieve.append(i) # tried int(i) copy1 = sieve #

Python iterating through nested dictionaries

拈花ヽ惹草 提交于 2019-12-11 11:16:04
问题 First of all, here is the question and the code written: def family_lineage(familytree, lineage): '''(dict, list of strs) -> boolean Return True if lineage specifies a list of names who are directly related in a chain of parent-child relationships, and NOT child-parent, parent-grandchild..etc, beginning from the first generation listed. >>> trace_lineage({'Gina': {'Sam': {'Tina': {}}, 'Li': {}}, 'Guy': {}}, ['Gina']) True >>> trace_lineage({'Gina': {'Sam': {'Tina': {}}, 'Li': {}}, 'Guy': {}},

R nested loop returning only the last iteration

孤人 提交于 2019-12-11 09:56:49
问题 I am trying to replicate 81-place cryoboxes used in lab storage system using a nested for loop . The following code illustrates the problem using 3-place boxes: urine_random_df <- as.data.frame(c(seq(from = 10, to = 12, by = 1))) boxcells <- vector() cell_placeholder <- as.data.frame(c(seq(from = 1, to = 3, by = 1))) for (i in 1: 3){ #boxcells <- paste0("NEW", sprintf("%04d", as.numeric(urine_random_df[i,]))) for (j in 1: nrow(cell_placeholder)){ boxcells <- c(boxcells, paste(paste0("NEW",