Stumped - How do I code this loop...
-
Given a List of Integers:
List positions = new List{1,2,3,4,5};
How would I code a loop to get the following results for a given number of iterations {n}:
n P1 P2
1 1 5
2 2 1
3 3 2
4 4 3
5 5 4
6 1 5
7 2 1
8 3 2
9 4 3
10 5 4
...This should be simple but I have brain freeze.
I don't speak Idiot - please talk slowly and clearly "I have sexdaily. I mean dyslexia. Fcuk!" Driven to the arms of Heineken by the wife
-
Given a List of Integers:
List positions = new List{1,2,3,4,5};
How would I code a loop to get the following results for a given number of iterations {n}:
n P1 P2
1 1 5
2 2 1
3 3 2
4 4 3
5 5 4
6 1 5
7 2 1
8 3 2
9 4 3
10 5 4
...This should be simple but I have brain freeze.
I don't speak Idiot - please talk slowly and clearly "I have sexdaily. I mean dyslexia. Fcuk!" Driven to the arms of Heineken by the wife
p1 almost looks like n mod 5 + (5 iif prev result is 0) p2 almost looks like p1 - 1 + (5 iif prev result is 0) 'g'
-
Given a List of Integers:
List positions = new List{1,2,3,4,5};
How would I code a loop to get the following results for a given number of iterations {n}:
n P1 P2
1 1 5
2 2 1
3 3 2
4 4 3
5 5 4
6 1 5
7 2 1
8 3 2
9 4 3
10 5 4
...This should be simple but I have brain freeze.
I don't speak Idiot - please talk slowly and clearly "I have sexdaily. I mean dyslexia. Fcuk!" Driven to the arms of Heineken by the wife
List<int> positions = new List<int> { 1, 2, 3, 4, 5 };
for (int n = 1, p1 = 0, p2 = 4; n <= 10; ++n, ++p1, ++p2)
{
p1 %= positions.Count;
p2 %= positions.Count;
Console.WriteLine("{0,2} {1} {2}", n, positions[p1], positions[p2]);
}THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
List<int> positions = new List<int> { 1, 2, 3, 4, 5 };
for (int n = 1, p1 = 0, p2 = 4; n <= 10; ++n, ++p1, ++p2)
{
p1 %= positions.Count;
p2 %= positions.Count;
Console.WriteLine("{0,2} {1} {2}", n, positions[p1], positions[p2]);
}THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
That works well and creates the list for n iterations. How about finding the value of p1 and p2 when n is a defined number?
int n = 23;
p1 = ?
p2 = ?
...I don't speak Idiot - please talk slowly and clearly "I have sexdaily. I mean dyslexia. Fcuk!" Driven to the arms of Heineken by the wife
-
That works well and creates the list for n iterations. How about finding the value of p1 and p2 when n is a defined number?
int n = 23;
p1 = ?
p2 = ?
...I don't speak Idiot - please talk slowly and clearly "I have sexdaily. I mean dyslexia. Fcuk!" Driven to the arms of Heineken by the wife
If
n
starts with 1, thenp1 = (n-1) % positions.Count;
p2 = (positions.Count + n - 2) % positions.Count;THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
List<int> positions = new List<int> { 1, 2, 3, 4, 5 };
for (int n = 1, p1 = 0, p2 = 4; n <= 10; ++n, ++p1, ++p2)
{
p1 %= positions.Count;
p2 %= positions.Count;
Console.WriteLine("{0,2} {1} {2}", n, positions[p1], positions[p2]);
}THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
+5 for this wonderful code "haiku" :)
“I have diligently numbered the days of pure and genuine happiness which have fallen to my lot: They amount to 14.” Abd-Ar Rahman III, Caliph of Cordoba, circa 950CE.
-
+5 for this wonderful code "haiku" :)
“I have diligently numbered the days of pure and genuine happiness which have fallen to my lot: They amount to 14.” Abd-Ar Rahman III, Caliph of Cordoba, circa 950CE.
Thank you.
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
List<int> positions = new List<int> { 1, 2, 3, 4, 5 };
for (int n = 1, p1 = 0, p2 = 4; n <= 10; ++n, ++p1, ++p2)
{
p1 %= positions.Count;
p2 %= positions.Count;
Console.WriteLine("{0,2} {1} {2}", n, positions[p1], positions[p2]);
}THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
I hope this doesn't sound picky-picky, but I kept studying the line of code that writes to the Console: Console.WriteLine("{0,2} {1} {2}", n, positions[p1], positions[p2]); Thinking that {0,2} did some exotic thing I had never seen before, but it appears it actually does nothing, and changing it to {0} produces no change in the output. Or, am I missing something ? Once again, thanks for the great code example: you've expanded my understanding of what a C# 'for loop can do ! thanks, Bill
“I have diligently numbered the days of pure and genuine happiness which have fallen to my lot: They amount to 14.” Abd-Ar Rahman III, Caliph of Cordoba, circa 950CE.
-
I hope this doesn't sound picky-picky, but I kept studying the line of code that writes to the Console: Console.WriteLine("{0,2} {1} {2}", n, positions[p1], positions[p2]); Thinking that {0,2} did some exotic thing I had never seen before, but it appears it actually does nothing, and changing it to {0} produces no change in the output. Or, am I missing something ? Once again, thanks for the great code example: you've expanded my understanding of what a C# 'for loop can do ! thanks, Bill
“I have diligently numbered the days of pure and genuine happiness which have fallen to my lot: They amount to 14.” Abd-Ar Rahman III, Caliph of Cordoba, circa 950CE.
It's putting the space before the single digit numbers. Try changing it {0,4} to see the effect in greater detail.
-
I hope this doesn't sound picky-picky, but I kept studying the line of code that writes to the Console: Console.WriteLine("{0,2} {1} {2}", n, positions[p1], positions[p2]); Thinking that {0,2} did some exotic thing I had never seen before, but it appears it actually does nothing, and changing it to {0} produces no change in the output. Or, am I missing something ? Once again, thanks for the great code example: you've expanded my understanding of what a C# 'for loop can do ! thanks, Bill
“I have diligently numbered the days of pure and genuine happiness which have fallen to my lot: They amount to 14.” Abd-Ar Rahman III, Caliph of Cordoba, circa 950CE.
The MSDN documentation is reasonably clear (for once!):
http://msdn.microsoft.com/en-us/library/system.string.format%28v=vs.110%29.aspx#FormatItem[^]
A format item has this syntax:
{index[,alignment][:formatString]}
... alignment Optional. A signed integer that indicates the total length of the field into which the argument is inserted and whether it is right-aligned (a positive integer) or left-aligned (a negative integer). If you omit alignment, the string representation of the corresponding argument is inserted in a field with no leading or trailing spaces.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
It's putting the space before the single digit numbers. Try changing it {0,4} to see the effect in greater detail.
Exactly.
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
The MSDN documentation is reasonably clear (for once!):
http://msdn.microsoft.com/en-us/library/system.string.format%28v=vs.110%29.aspx#FormatItem[^]
A format item has this syntax:
{index[,alignment][:formatString]}
... alignment Optional. A signed integer that indicates the total length of the field into which the argument is inserted and whether it is right-aligned (a positive integer) or left-aligned (a negative integer). If you omit alignment, the string representation of the corresponding argument is inserted in a field with no leading or trailing spaces.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Richard Deeming wrote:
he MSDN documentation is reasonably clear (for once!):
:-)
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
It's putting the space before the single digit numbers. Try changing it {0,4} to see the effect in greater detail.
Thanks Pete !
“I have diligently numbered the days of pure and genuine happiness which have fallen to my lot: They amount to 14.” Abd-Ar Rahman III, Caliph of Cordoba, circa 950CE.
-
The MSDN documentation is reasonably clear (for once!):
http://msdn.microsoft.com/en-us/library/system.string.format%28v=vs.110%29.aspx#FormatItem[^]
A format item has this syntax:
{index[,alignment][:formatString]}
... alignment Optional. A signed integer that indicates the total length of the field into which the argument is inserted and whether it is right-aligned (a positive integer) or left-aligned (a negative integer). If you omit alignment, the string representation of the corresponding argument is inserted in a field with no leading or trailing spaces.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks, Richard !
“I have diligently numbered the days of pure and genuine happiness which have fallen to my lot: They amount to 14.” Abd-Ar Rahman III, Caliph of Cordoba, circa 950CE.
-
If
n
starts with 1, thenp1 = (n-1) % positions.Count;
p2 = (positions.Count + n - 2) % positions.Count;THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
Here what I ended up using:
public List GetPositions(int n, int posCount)
{
int pos1 = n % posCount;
int pos2 = pos1 == 0 ? posCount - 1 : pos1 - 1;
return new List{pos1, pos2};
}
...
int n = 0;
List positions = new List{0,1,2,3,4};
while( n < 10)
{
List res = GetPositions(n, positions.Count)
Console.WriteLine("Count{0}: Pos1 {1} - Pos2 {2}", n, res[0], res[1]);
n++;
}Thanks to you and G for your help clearing the fog!
I don't speak Idiot - please talk slowly and clearly "I have sexdaily. I mean dyslexia. Fcuk!" Driven to the arms of Heineken by the wife
-
Here what I ended up using:
public List GetPositions(int n, int posCount)
{
int pos1 = n % posCount;
int pos2 = pos1 == 0 ? posCount - 1 : pos1 - 1;
return new List{pos1, pos2};
}
...
int n = 0;
List positions = new List{0,1,2,3,4};
while( n < 10)
{
List res = GetPositions(n, positions.Count)
Console.WriteLine("Count{0}: Pos1 {1} - Pos2 {2}", n, res[0], res[1]);
n++;
}Thanks to you and G for your help clearing the fog!
I don't speak Idiot - please talk slowly and clearly "I have sexdaily. I mean dyslexia. Fcuk!" Driven to the arms of Heineken by the wife
You are welcome.
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite