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
I have the following code:
var result = AllSequences(1, 10, 3);
public static IEnumerable<List<int>> AllSequences(int start, int end, int size)
{
if (size == 0)
return Enumerable.Repeat<List<int>>(new List<int>(), 1);
return from i in Enumerable.Range(start, end - size - start + 2)
from seq in AllSequences(i, end, size - 1)
select new List<int> { i }.Concat(seq).ToList();
}
Result:
1,1,1
1,1,2
1,1,3
1,1,4
....
2,2,2
2,2,3
2,2,4
But before coming to this sequence, wish you were like this:
2,1,1
2,1,2
2,1,3
2,1,4
.....
I'm having trouble generating sequence is, I'm using LINQ to gain performance in this loop
I'm a bit confused by your question but if you want the sequence to run through each digit from 1 to 10, for all permutations try something like this.
I'm not advocating the use of LINQ here, just trying to make as few changes to your code as possible.
public static IEnumerable<List<int>> AllSequences(int start, int end, int size)
{
if (size == 0)
return Enumerable.Repeat<List<int>>(new List<int>(), 1);
return from i in Enumerable.Range(start, end)
from seq in AllSequences(start, end, size - 1)
select new List<int> { i }.Concat(seq).ToList();
}
Results will be
1, 1, 1
1, 1, 2
... etc
1, 1, 10
1, 2, 1
1, 2, 2
... etc
1, 10, 8
1, 10, 9
1, 10, 10
2, 1, 1
2, 1, 2
.... etc
10, 10, 10
Related
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 4 months ago.
Improve this question
I am new here and in C# and I dont't know how to figure out that example..Printing numbers 1 to 100 that way "100, 1, 2, 99, 98, 3, 4, 97,....52, 49, 50, 51". I have a course work and should write it befoure 25.10..Thank you all!
for (int i = 1; i < 51; i+=2)
{
Console.WriteLine(101-i);
Console.WriteLine(i);
Console.WriteLine(1+i);
Console.WriteLine(100-i);
}
EDIT: just i want to add that i use (1+i) // (100-i) because if I use i-- or i++ inside the writeline, it will execute the ++ or -- after the print message
By using a lower and a upper variable and a while-loop, you can do it stright forward without any calculations:
int lower = 1, upper = 100;
while (lower < upper) {
Console.WriteLine(upper--);
Console.WriteLine(lower++);
Console.WriteLine(lower++);
Console.WriteLine(upper--);
}
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
/*
Given an array of ints length 3, return an array with the elements "rotated left" so {1, 2, 3}
yields {2, 3, 1}.
RotateLeft3([1, 2, 3]) → [2, 3, 1]
RotateLeft3([5, 11, 9]) → [11, 9, 5]
RotateLeft3([7, 0, 0]) → [0, 0, 7]
*/
public int[] RotateLeft3(int[] nums)
{
int temp = nums[0];
for (int i = 0; i < nums.Length -1; i++)
{
nums[i] = nums[i + 1];
}
nums[nums.Length - 1] = temp;
return nums;
}
This is an exercise question that I'm having trouble translating into plain English, I'm not following how this ends up rotating* the array after the loop finishes.
This is not reversing the array. It is shifting all the entries to the left one, and wrapping the first element back to the end of the array.
The first operation saves the first item.
The for loop takes each item and copies the next item over the top of it.
The last operation puts the saved first item over top the last item.
The code as posted appears to be rotating the values through the array.
Translating it into words,
Remember the first item
Move the second item into the first position, 3rd into 2nd etc (repeat for all remaining items)
Put the remembered item in the last position.
It doesn't appear to 'reverse' the array.
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.
Improve this question
How do I make the last result equal the first, second, third, and fourth result combined?
If all results are 10 points except the last one, then the last one should be 40 points. The last result is the total result.
int[] result = new int[5] { 0, 0, 0, 0, 0 };
Here you go
int[] result = { 1, 2, 3, 4, 0 };
result[result.Length - 1] = result.Take(result.Length - 1).Sum(); // 1 + 2 + 3 + 4 = 10
//result = { 1, 2, 3, 4, 10 };
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 7 years ago.
Improve this question
I am new to LinQ and I want to get all the data from a range of numbers when I pass a parameter. For an example, the range is 1-100 and I pass in 40, it should get all the data from 1-100 but not only 40. Any ideas?
Thanks
You can use Enumerable.Range which will give you range of numbers. You can use Except method to remove the numbers that you dont want.
private static int[] GetRange(int start,int inclusiveEnd,int[] except)
{
return Enumerable.Range(start, inclusiveEnd - start + 1).Except(except).ToArray();
}
Then call it in this way.
var x = GetRange(1, 100, new[] {40});
Use filtering (i.e. Where statement):
var result = Enumerable.Range(1,100).Where(x=>x != 40).ToList()
List<int> list = new List<int>();
var result = list.Where(i=> i >= 1 && i <= 100 && i != 40);
Assuming, that there is a function, that transforms a number from given range into some data:
Enumerable.Range(1, 100).Where(_ => _ != 40).Select(_ => GetData(_));
Use Except extension method From MSDN
List<int> oldList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int parameter = 2;
List<int> newList = oldList.Except(new int[] { parameter }).ToList<int>();
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
I have two database tables (connected with relation 1:m):
Location (locId, locName)
1, USA
2, Germany
3, Spain
Sublocations (subLocId, subLocName, locId)
1, Denver, 1
2, Detroit, 1
3, New York, 1
4, Hamburg, 2
5, Berlin, 2
6, Munich, 2
7, Madrid, 3
8, Barcelona, 3
9, Valencia, 3
With using Linq to Sql, I need to fill the LocationDto, like this:
LocationDto (locId, subLocId, name)
1, null, USA
1, 1, Denver
1, 2, Detroit
1, 3, New York
2,null, Germany
2, 4, Hamburg
2, 5, Berlin
2, 6, Munich
3,null, Spain
3, 7, Madrid
3, 8, Barcelona
3, 9, Valencia
Create a new class to hold the joined objects:
public class JoinedLocations
{
public int locId{get;set;}
public int? subLocId{get;set;}
public string Description{get;set;}
}
and then run this query
var query =
Location
.GroupJoin
(
Sublocations.DefaultIfEmpty(),
l=>l.locId,
s=>s.locId,
(l,s)=>new {l,s}
)
.SelectMany
(
x=>
x.s.DefaultIfEmpty
(
new Sublocations
{
subLocId=-1,
subLocName="",
locId=-1
}
),
(l,s)=>
new JoinedLocations
{
locId=l.l.locId,
subLocId=s.subLocId,
Description = (s.subLocId==-1?l.l.locName:s.subLocName)
}
)
.Union
(
loc
.Select
(
x=>
new JoinedLocations
{
locId=x.locId,
subLocId=null,
Description = x.locName
}
)
)
.OrderBy(x=>x.locId)
.ThenBy (x => x.subLocId)
This will give you the results you want.