What is the difference between loop and iteration? [closed] - algorithm

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
What kinds of data structure are used to implement loop and iteration?

Loop refers to the code...
iteration refers to the process in which the content of loop is executed once..
so 1 iteration refers to 1 time execution of loop..
so basically one loop can undergo many iterations..

loop is used for fixed no. of element or certain condition occurs while iterator is used for itearing element dynamically that have different no. of elements at runtime

Only such of kind 1.
Some have tried using data structures of kind 42 for this purpose, but failed miserably.

Iteration is simply the number of time/times a loop can be executed, while loop is the code which generate or causes expressions to be iterated iteration when the loop is executing.
An example of lines of code in c++:
for(i=1; i<=10; i++)
{
cout<<"this is printed 10 times";
}
The above code is a for_loop in which the execution of the statement "this is printed 10 times" will be iterated/repeated 10 times.

Related

While ⇋ For convertibility and feasibility

We know that while and for are the most frequently used loop syntax for many programming and scripting languages. Here I would like to ask some questions regarding convertibility and feasibility of using while vs for loop.
Is for to while and vice versa transformation or conversion always possible? I mean suppose one used while loop for some functionality and I want to replace while with for or say vice-versa, then Is while ⇋ for transformation/conversion always possible (also interested in knowing the feasibility)? It would be helpful of I can refer If any research regarding this carried out.
I'm also interested in getting the general guidance for using while vs for. Also want to know if while has some advantages over for and vice versa.
Note: I've this question for log time, I thought -- being a great programming site, this question can be useful here. If the question is not suitable here. I'm unsure if this question is acceptable here, so requesting to consider it liberal; you can ask me to remove if such question hurts the quality of site :)
I will answer using Java as a reference, though this answer should also be completely valid for C, C#, C++ and many others. If we consider the following for loop:
for (int i=0; i < 10; ++i) {
// do something, maybe involving i
}
We can see that the loop has 3 components:
int i=0; initialization of loop counter
i < 10 criteria for loop to execute
++i increment to loop counter
The following while loop is functionally equivalent to the above for loop:
int i=0;
while (i < 10) {
// do something, maybe involving i
++i;
}
We can see that the main difference between this while loop and the for loop are that the declaration and initialization of the loop counter is outside the loop in the former case. Also, we increment the loop counter inside the actual while loop. The check for the loop continuing is still done inside the loop structure, as with for loops.
So a for loop can be thought of an enhanced while loop of sorts. It frees us from having to create a loop counter outside the loop, and also we can increment/change the loop counter within the loop structure, rather than mixing such logic with the code of the loop body.

lock free programming - c++ atomic

I am trying to develop the following lock free code (c++11):
int val_max;
std::array<std::atomic<int>, 255> vector;
if (vector[i] > val_max) {
val_max =vector[i];
}
The problem is that when there are many threads (128 threads), the result is not correct, because if for example val_max=1, and three threads ( with vector[i] = 5, 15 and 20) will execute the code in the next line, and it will be a data race..
So, I don't know the best way to solve the problem with the available functions in c++11 (I cannot use mutex, locks), protecting the whole code.
Any suggestions? Thanks in advance!
You need to describe the bigger problem you need to solve, and why you want multiple threads for this.
If you have a lot of data and want to find the maximum, and you want to split that problem, you're doing it wrong. If all threads try to access a shared maximum, not only is it hard to get right, but by the time you have it right, you have fully serialized accesses, thus making the entire thing an exercise in adding complexity and thread overhead to a serial program.
The correct way to make it parallel is to give every thread a chunk of the array to work on (and the array members are not atomics), for which the thread calculates a local maximum, then when all threads are done have one thread find the maximum of the individual results.
Do an atomic fetch of val_max.
If the value fetched is greater than or equal to vector[i], stop, you are done.
Do an atomic compare exchange -- compare val_max to the value you read in step 1 and exchange it for the value of vector[i] if it compares.
If the compare succeeded, stop, you are done.
Go to step 1, you raced with another thread that made forward progress.

How to decrypt this transposition cipher? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
My problem is that I am trying to decrypt following cipher text:
AWGNEYDTBSREAHGIDEIEEIHKIWABHPRCROIWSSBHTE
OCSNASLAIGTTOTYIOBNANNOOCHENEHSADUAIEOSNTH
TEOOAWUTOVTISNTVIFIHSNADVOGOREAPTBETSIT
DCSNASAAHOOULITUREICBNOERSNETTNOITTNTSDEOO
RERNINTOVTINTEFKGITPRCROEEIGREIGREHKFN
HOCWHPDTOSREGTOUREINDIUATHCHIEOSN
I know that it is English language. I know that it was encrypted only once by substitution, monoalphabetic or transposition.
I checked the Index of Coincidence and I received: 0.06833754056978002
I also checked frequency of letter and I received:
T 11.02, E 10.59, O 10.16, I 9.74, N 8.47, S 6.77, A 6.35, R 5.93, H 5.93, G 3.38, D 3.38, C 3.38, U 2.54, B 2.54, W 2.11, V 1.69, P 1.69, K 1.27, F 1.27, Y 0.84, L 0.84.
So I claim that it is transposition cipher. Am I right?
I checked all combinations of Rail Fence Transposition. I did not receive something sensible.
I checked all combinations of Columnar Transposition without changing order. I did not receive something sensible.
I checked a few combinations of Columnar Transposition with changing order. I did not receive something sensible.
Do you have idea how I can resolve this interesting task?

Password Algorithm [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Could anyone tell me what type of Algotithm/Hash Function the following passwords use?
password: scrambled text
hhhhhhhhhhhhhhh : .!_-*/P_.!_-*/P
iiiiiiiiiiiiiii : / ^,+.Q^/ ^,+.Q
jjjjjjjjjjjjjjj : ,#]/(-R],#]/(-R
kkkkkkkkkkkkkkk : -"\.),S\-"\.),S
lllllllllllllll : *%[).+T[*%[).+T
It just looks like
char[i] = someMap[i%6, char[i%6]]
and the someMap[i] submaps are sequentially taken from ascii.
You don't have to guess the exact formula to reproduce it : a char of ascii code c at index i%6 always give the same char, so, with 127 tests (or more if you want to handle more chars), you can build your own map of dimension 127 x 6.
Note that it's impossible with your small test data to be sure there is no cumulative function in which a permutation could be function of the precedent char.

Regex to ristrict to accept maximum 14 digits [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Last time I was asked for checking string for minimum 8 digits. And I got following regex:
/^(?=(.*\d){8,})[\d\(\)\s+-]{8,}$/
You can see the question here: Checking string with minimum 8 digits using regex
Now I want to restrict string to accept maximum 14 digits in same regex. And I tried this:
/^(?=(.*\d){8,14})[\d\(\)\s+-]{8,}$/
No luck. Please anyone help me in fixing this.
UPDATE
After getting 2 down votes I thought better to write my own. I constructed regex using previous regex. Following regex works for me:
/^(?=(.*\d){8})(?!(.*\d){15})[\d\(\)\s+-]{8,}$/
By your request, the regex should be as simple as:
/^\d{8,14}$/
From your answer, and your other question, it seems like you are encoding a whole bunch of different rules into one increasingly complicated regex:
the string must be at least 12 chars long
it can only contain digits, parentheses, + and - signs, and spaces
there must be between 8 and 14 digits
While it's possible to do this with a regex, is it worthwhile? I'd argue that such a complicated regex is impossible to read, and therefore difficult to maintain.
If you split up the different criteria, it'll be much clearer.
string.length >= 12
string =~ /^[\d()+-\s]+$/ - note that by using square brackets to create a character class, you don't need to escape things, which also makes it much simpler.
(8..14).include?(string.count("0-9")) - check out the docs for String#count
So, altogether,
def valid?(string)
string.length >= 12 &&
string =~ /^[\d()+-\s]+$/ &&
(8..14).include?(string.count("0-9"))
end
It's a bit longer but it's a heck of a lot more understandable.
Try:
/^(?=(.*\d){8,14}(?!.*\d))[\d\(\)\s+-]{8,}$/
If I got the placement of the negative look ahead right I should fail to match a strimg with more than 14 digits.
Try also adding the 14 after the second instance of "8," if the previous regex achieved what you wanted - but it really is more complex than just 8-14 digits!

Resources