  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # A different type of iterative function  » python » A different type of iterative function

By : Dries
Date : November 22 2020, 10:48 AM
it helps some times your error is with the for loop.
It is not indented properly, and (1, n-1) builds a tuple with only two elements, 1 & n-1 code :
``````def logistic(x0, r, n):
"""function for doing logistic regression n times"""
for i in range(n):
xi = r * x0 * (1-x0)
print(xi)
# here if you reassign xi = x0 each time, then you'll get the same
# result in each iteration. I'm assuming that is an error in your logic
# and that you meant x0 = xi
x0 = xi
`````` ## Converting a recursive function to an iterative function which represents the output of my algorithm

By : silverdahlia
Date : March 29 2020, 07:55 AM
this one helps. Edit: I am not looking for an algorithm, but actually a math equation similar to f(i) = sum i=0 to n-1 (C * A[i]), but with respect to n (length of the array) rather than i (iteration #). , I would say the function you describe is simply
code :
``````f(n) = sum (0 <= i < n: C^i * A[i])
``````
``````int c = 1;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += c * A[i];
c *= C;
}
`````` ## NLTK PunktSequenceTokenizer return type or a way to use it faster in an iterative function?

By : jonf
Date : March 29 2020, 07:55 AM
With these it helps Why not pickle it?
code :
``````import pickle
... # other imports and stuff
custom_sent_tokenizer = PunktSentenceTokenizer(train_text)
pickle.dump(custom_sent_tokenizer, open( "save.p", "wb" ))
``````
``````>>> import pickle
>>> pickle.load(open( "save.p", "rb" ) )
<nltk.tokenize.punkt.PunktSentenceTokenizer object at 0x00000000023B9EB8>
`````` ## Making code that is both iterative and recursive purely iterative

By : Roberto Rimondi
Date : March 29 2020, 07:55 AM
I hope this helps . Just use a queue or stack to track which neighbours to process next; the following function does exactly the same work as your recursive function, iteratively:
code :
``````from collections import deque

def clusterise(cell, cluster, tile_index, image, n, windows, image_clusters):
to_process = deque([(cell, tile_index)])
while to_process:
cell, tile_index = to_process.pop()
neighbouring_windows, neighbouring_indices = get_neighbouring_windows(tile_index, tile_index, n, windows)
neighbours = get_and_remove_all_neighbours(cell, image, tile_index, neighbouring_windows, neighbouring_indices)
if neighbours:
for neighbour, (n_i, n_j) in neighbours:
to_process.append((neighbour, (n_i, n_j))
`````` ## How to define a iterative function involving for loop where the iterator i,j,l.. are also used in the defined iterative

By : sky_e
Date : March 29 2020, 07:55 AM
it helps some times No particular need for a recursive function.
You can use a single loop for every number of indices if you use itertools.product; you can simplify the computation too, using sum and zip
code :
``````from itertools import product
...
spin_products = []
n = int(input('how many indices? '))
...
for indices in product([-1, 1], repeat=n):
spin_products.append(sum(i*j for i, j in zip(indices, indices[1:])))
`````` ## When following a referral for an A record in an DNS iterative query, should the next question be an A type or NS type?

By : user3166401
Date : March 29 2020, 07:55 AM
may help you . You send always the same question, because irrespective of the QTYPE the authoritative nameserver will give you a referral to the proper nameservers, so you get "automatically" the NS records in the AUTHORITY section.
See the generic algorithm in RFC1034 section 4.3.2. It is not clear from where you hint "but I keep hearing that you need to check NS records."
code :
``````;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47905
;; flags: qr rd; QUERY: 1, ANSWER: 0, AUTHORITY: 6, ADDITIONAL: 13

;; AUTHORITY SECTION:
org.            2d IN NS d0.org.afilias-nst.org.
org.            2d IN NS a0.org.afilias-nst.info.
org.            2d IN NS c0.org.afilias-nst.info.
org.            2d IN NS a2.org.afilias-nst.info.
org.            2d IN NS b0.org.afilias-nst.org.
org.            2d IN NS b2.org.afilias-nst.org.
``````
``````; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 39247
;; flags: qr rd; QUERY: 1, ANSWER: 0, AUTHORITY: 2, ADDITIONAL: 1

;; AUTHORITY SECTION:
ardainc.org.        1d IN NS ns.josh.com.
ardainc.org.        1d IN NS ns2.josh.com.
``````
``````;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 23030
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1 