C
C#2y ago
Ossie

Shifting a List(int) to become 1,2,3,4,5...

Hi, If I have a List like this: 2,3,4,5 And I would like for it to become: 1,2,3,4,4 This should be done by taking from the higher numbers in the list shifting them to the left so they come in order like above, how would I do that?
54 Replies
ACiDCA7
ACiDCA72y ago
is the second 4 in what you would like a typo? this doesnt look like a shift but more like you are just decreasing everything by one
Ossie
OssieOP2y ago
No, its not a typo I need to reformat the List to be from 1->2->3 etc. but if they have the same value at the end it won't be a problem The 5 in the original list, removes 1 from itself, and becomes the 1, and then there is 4 left in the end, Idk if it makes sense
canton7
canton72y ago
Can you share an actual example, which is correct?
Ossie
OssieOP2y ago
Not sure what you mean? That is the example I don't understand, if its not a typo, and I wan't it, why would I remove it?
canton7
canton72y ago
Scrap that, I can't read I still don't understand how 2,3,4,5 became 1,2,3,4,4 though
Ossie
OssieOP2y ago
2,3,4,5: 2 stays the same 3 stays the same 4 stays the same 5 lends 1 to the beginning of the list, so it becomes 4, and the begging now has a 1 1,2,3,4,4
canton7
canton72y ago
So you want to take the last item in a list and subtract 1 from it? And you also want to add 1 to the start of the list? Which of those two bits are you having trouble with?
Ossie
OssieOP2y ago
Its situational I wanna input N (number of items in the list) N, N, N... N amount of items in the list that needs to be sorted this way
canton7
canton72y ago
Can you give a full example? I don't follow what N, N, N... means
Ossie
OssieOP2y ago
4 2,4,6,8 Another example ^^ N is just a whole number
canton7
canton72y ago
OK, so that becomes 1,2,4,6,7? (Presumably not, because that's too simple, but you're not giving me enough information to have a better guess at what's going on...)
Ossie
OssieOP2y ago
That sohuld become 1,2,3,4,5,5
canton7
canton72y ago
How did 2,4,6,8 become 1,2,3,4,5,5?
Ossie
OssieOP2y ago
Thats the thing im trying to figure out
canton7
canton72y ago
Uh...
ACiDCA7
ACiDCA72y ago
what are the rules? everything above 5 get added to beginning?
Ossie
OssieOP2y ago
Its in danish, I can't explain it well
ero
ero2y ago
??translate en Dette er en eksempelsætning. bot dead?
Ossie
OssieOP2y ago
It wont work like that Let me try and translate it, so it makes sense Just to quickly clarify: n = whole number i = whole number a = whole number Some beavers wanna build a new damn, but they wanna sort their sticks before starting to build, they have n amount of stickpiles, the beavers have counted the piles and written it down in the order that the i'th pile has a<i> amount of sticks. The beaver engineer says it will be easier if they sort the piles so one pile has 1 stick, the next has 2, the next has 3 and so on. The beaver engineer needs help to know how many sticks that maximally can be used. Example: If the beavers have the following piles with sticks {2,5,4,3}, the beavers can move them around and get the following piles: {1,2,3,4,4}, in this example the answer would then be 1+2+3+4=10, since these are the sticks that makes up the piles that the beaver engineer have asked for. Input: 1:4 2:2 4 5 3 Output expected: 1: 10
canton7
canton72y ago
Right, so you want to sum all of the values in your input array, and then construct an array 1,2,3,4,... such that the sum of the second array is as large as it can be, while also being <= the sum of the input array
Ossie
OssieOP2y ago
Yes I have the following for now, but im not sure where to go from now:
int BunkerAmount = int.Parse(Console.ReadLine());
var GreneInput = Console.ReadLine().Split(' ');

List<int> GreneAmount = new List<int>();

foreach (string Gren in GreneInput) {
GreneAmount.Add(int.Parse(Gren)); //Tilføj til liste
}

GreneAmount.Sort(); // Sorter så de kommer i numerisk rækkefølge

for (int i = 0; i < GreneAmount.Count; i++) {
GreneAmount[i] = GreneAmount[i] - 1;
}

Console.WriteLine(string.Join(",", GreneAmount));
int BunkerAmount = int.Parse(Console.ReadLine());
var GreneInput = Console.ReadLine().Split(' ');

List<int> GreneAmount = new List<int>();

foreach (string Gren in GreneInput) {
GreneAmount.Add(int.Parse(Gren)); //Tilføj til liste
}

GreneAmount.Sort(); // Sorter så de kommer i numerisk rækkefølge

for (int i = 0; i < GreneAmount.Count; i++) {
GreneAmount[i] = GreneAmount[i] - 1;
}

Console.WriteLine(string.Join(",", GreneAmount));
This works for the 2,4,5,3 example, but not for the 2,4,6,8
canton7
canton72y ago
I think you're making this more complex than it is
Ossie
OssieOP2y ago
I might be lol
canton7
canton72y ago
The question talks about "sort the piles" but I think all you need to do is to put all of the sticks in single big pile, and then draw sticks from the big pile to form a pile of 1 stick, then a pile of 2 sticks, etc Until your big pile runs out of sticks
Ossie
OssieOP2y ago
So I would just add all the sticks from the original togehter, and then just start adding them until it runs out
canton7
canton72y ago
Yeah
Ossie
OssieOP2y ago
Or it becomes longer than the original input
ACiDCA7
ACiDCA72y ago
no it can get quite much longer than original input eg 1 2 9 which would be 1 2 3 4 (2) solution would be 10 again tho if i understood that correctly
Ossie
OssieOP2y ago
Is there an easy wat to sum all of the value of a list? I mean the first input
canton7
canton72y ago
.Sum(), or just use a loop and sum += element inside the loop
Ossie
OssieOP2y ago
error CS1061: 'List<int>' does not contain a definition for 'Sum' and no accessible extension method 'Sum' accepting a first argument of type 'List<int>' could be found (are you missing a using directive or an assembly reference?) I'm using using System.Collections.Generic; Nvm i needed Linq Hm, it completes the first task, but it errors on someo f the other
ACiDCA7
ACiDCA72y ago
code + input + actual output + expected output would be helpful
Ossie
OssieOP2y ago
I don't know, its hidden Thats the thing
Ossie
OssieOP2y ago
Ossie
OssieOP2y ago
Forkert = wrong Lykkedes = completed Test data er skjult = Test data is hidden The first one in the top is the example with 2 5 4 3
ACiDCA7
ACiDCA72y ago
well show some code
canton7
canton72y ago
All we know is that some code you've written, which we can't see, is wrong
Ossie
OssieOP2y ago
int BunkerAmount = int.Parse(Console.ReadLine());
var GreneInput = Console.ReadLine().Split(' ');
List<int> GreneAmount = new List<int>();
List<int> Sorted = new List<int>();
int TotalGreneAmount = 0;
foreach (string Gren in GreneInput) {
GreneAmount.Add(int.Parse(Gren));
}
TotalGreneAmount = GreneAmount.Sum();
for (int i = 0; i < BunkerAmount; i ++) {
Sorted.Add(i + 1);
TotalGreneAmount -= i + 1;
}
Console.WriteLine(Sorted.Sum().ToString());
int BunkerAmount = int.Parse(Console.ReadLine());
var GreneInput = Console.ReadLine().Split(' ');
List<int> GreneAmount = new List<int>();
List<int> Sorted = new List<int>();
int TotalGreneAmount = 0;
foreach (string Gren in GreneInput) {
GreneAmount.Add(int.Parse(Gren));
}
TotalGreneAmount = GreneAmount.Sum();
for (int i = 0; i < BunkerAmount; i ++) {
Sorted.Add(i + 1);
TotalGreneAmount -= i + 1;
}
Console.WriteLine(Sorted.Sum().ToString());
canton7
canton72y ago
for (int i = 0; i < BunkerAmount; i ++) -- why stop at BunkerAmount?
Ossie
OssieOP2y ago
Since it's what it does in the example I think
canton7
canton72y ago
Where in the example? The question says to sort all of you sticks into piles, until you can't create any more piles
Ossie
OssieOP2y ago
1,2,3,4,4 -> 1+2+3+4 = 10 - wherei sth elast 4?
canton7
canton72y ago
Not to stop when you reach some arbitrary number of piles
ACiDCA7
ACiDCA72y ago
it cant fill the last stack so gets discarded
canton7
canton72y ago
You don't have enough sticks to make a pile with 5 sticks in, so you stop
Ossie
OssieOP2y ago
Oh But what would I then stop at if i < ??
canton7
canton72y ago
...when you can't create any more piles
ACiDCA7
ACiDCA72y ago
when rest of stick smaller then size of pile should be
canton7
canton72y ago
So if the next pile needs 5 sticks, and you have < 5 sticks in TotalGreneAmount, then you stop
Ossie
OssieOP2y ago
Not sure how I would do that
canton7
canton72y ago
You're not sure how to compare two numbers?
Ossie
OssieOP2y ago
No lol What should i have in my while loop
canton7
canton72y ago
I'm not going to tell you the answers I think if you start i at 1 rather than 0, you won't have all of those i+1's, and that'll make things easier
Ossie
OssieOP2y ago
I figured it out, thanks! And completed all tasks ty
Want results from more Discord servers?
Add your server