Is Foreach loop a definite or indefinite? [closed] - loops

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 8 years ago.
Improve this question
Is Foreach loop a definite or indefinite one? Meaning do I know prior to the execution of the loop the number of iterations it is going to do?

No. foreach in most languages (c#, java) doesn't know beforehand how many iteration it will have to do. You could even use foreach to go through an infinite sequence.
That's why for exists

In most languages a foreach loop will iterate over a collection of elements until the collection ends. In most languages I can think of a collection can be a definitive set of elements contained in memory OR a calculated set of elements that could in theory be of an infinite length. This would make the foreach loop "indefinite" in most modern languages.

Related

How to sort an array in a way that if a condition is met the contents get sorted first? [closed]

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 last month.
Improve this question
I need to sort an array of orders so that the ones already on delivery get put at the end of the list so to put the unfinished orders on the top.
The variable state is what i wanted to sort on. If the variable is 0 the contents should have a higher priority than where the state is 1.
Here is a simplification of what i need:
input array:
{{state=1,...},{state=0,...},{state=0,...},{state=0,...},{state=1,...}}
=>
output array:
{{state=0,...},{state=0,...},{state=0,...},{state=1,...},{state=1,...}}
I tried table.sort and some other methods but it didn't fit what i needed or didn't do what i wanted it to do.
table.sort(arr, function(a, b) return a.state < b.state end) works fine to sort arr in non-descending order based on the state field of each entry. How did you try to use table.sort?

How to find time complexity of a simultaneous for loop? [closed]

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 2 years ago.
Improve this question
I am trying to find the time complexity of simultaneous loops, and I am stuck up on this question.
Find the time complexity of this simultaneous loop
for(int i=1,int j=0; i*i<=n && j<=n ;j=j*2 ,i++);
Can someone explain how to approach these types of questions?
Variable "i" is increased by 1 and stops at sqrt(n). Variable "j" is increased by two multiplied every step, but its initial value is zero, so it can not break the loop. So "i" variable reached to the end when n limits to a big number.
For complexity calculations, we focus the power of n or logarithmic of n values. Scalar values are not important. We discard 1, 2 or 10 steps. So the complexity of your statement is O(sqrt(n)). Initial values do not affect the complexity of statements.

finding the biggest value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
PLEASE NOTE: I am not looking for code, but for a way how to solve this problem.
My input is world that looks like this:
The problem is, i have to find the biggest number, without using OWN variables I could declare myself, and I'm only allowed to use turnLeft(), turnRight(), move(), isLeft/Right/FrontClear(), getNumber() and putNumber() functions to move < around the world.
Could you please give me a 'verbal solution' or a hint how to do such thing?
While you cannot use any variable, note that you do have available memory (getNumber() and putNumber()). For instance, you could think about leaving a mark in positions you have already been to implement some kind of flood fill.
Further, you can fill the floor with the biggest number you have seen yet. Basically, encoding your own state in the floor.
Important questions:
Is the configuration of the maze always fixed?
Is the range of possible numbers in the floor fixed to a reasonable range (e.g. digits 1-9)?

C How can I calculate an average of a list without loops? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So I created a linked List in C using structs and it stores ints. My mission is to calculate the average of the values in the list without using recursion or loops.
I already have the list's item count I just need the sum.
Any ideas?
Simply have two varibles - count, total; Update them in Add and Delete.
When u want avg, just return total/count.
The list is not length bounded, but i found a solution.
I create a variable in the list's ateuct to save the sum of the list, and i change the sum of the list each time I add or remove a cell. When I want to calculate the sums I'll just divide the sum by the count.
Thank you anyway for your help :)

Sort words and then the sentence including digits and characters in Shell scripting or perl scripting [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a string
"heya64 this is21 a good89 day91"
I have to sort in such way that , first each word have to sorted like,
46ahey and then these words need to be sorted with other words. So the result has to be like the one below,
"12is 19ady 46aehy 89dgoo a hist"
Can you please tell me how to do this mostly in scripting,bash or perl . If not atleast good algorithm in c language
The algorithm to sort this problem is simple, just like you said in your question description, sort characters in each word first, then sort these sorted-word again.
Like this:
$ echo heya64 this is21 a good89 day91 | perl -anE 'say(join " ", sort(map { join "", sort split // } #F))'
12is 19ady 46aehy 89dgoo a hist

Resources