|
|
Well, there is this incredibly inefficient solution:
int[] data = new int[] { 1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0 };
int y = 0;
var result = data.OrderBy(d => d == 0 ? int.MaxValue : y++); It works, but ... it's pretty horrible!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Oh you fancy kids and your LINQ statements.
It was for a frontend position, so I used JavaScript. There are some LINQ libs for JavaScript actually, but they are slow as dirt and nobody uses them.
Jeremy Falcon
|
|
|
|
|
Doing it, "in place":
List<int> a = new List<int>( new int[] { 1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0 } );
int count = a.RemoveAll( i => i== 0 );
a.AddRange( new int[ count ] );
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
This just occurred to me ...
JavaScript's Array.sort enables you to use a function to say how sorting is done. All that is needed is a simple function to treat 0 as bigger than non-0.
var data = [ 1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0 ];
var sorted = data.sort( (a, b) => a == 0 ? 1 : (b == 0 ? -1 : 0) );
var sorted = data.sort( (a, b) => a ? (b ? 0 : -1 ) : 1 );
alert(sorted);
modified 24-Sep-20 11:29am.
|
|
|
|
|
Yes, but ... it doesn't do it "in place" (part 2 of OP's "test")
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
OOPS! I'd failed to read the spec correctly. Definitely won't get the job.
|
|
|
|
|
A few moments though gave me this:
int[] data = new int[] { 1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0 };
int outp = 0;
for (int inp = 0; inp < data.Length; inp++)
{
int d = data[inp];
if (d != 0)
{
data[outp++] = d;
}
}
while (outp < data.Length)
{
data[outp++] = 0;
}
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Right... that's the first take, but I used two arrays to pull it off to avoid the second loop.
Jeremy Falcon
|
|
|
|
|
Seriously, I was thinking about this and there is a cheat to the original instructions.
At least in JavaScript. See they just said put all the zeroes at the end.
Well. you can do that in one for loop...
Each time through loop
1. splice off the 0 at its original location.
2. then push it onto the end
Voila!
var data = [ 1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0 ];
for (var x = 0;x<data.length;x++){
if (data[x] == 0){
data.splice(x,1);
data.push(0);
}
}
|
|
|
|
|
That's a good solution and better than my first one that used two arrays. Too bad I didn't think of it two days ago.
Jeremy Falcon
|
|
|
|
|
But internally, the splice() operation is performing an O(N) operation - moving all elements down by one position. Your solution is therefore O(N^2).
If I were the interviewer, I would ask if you can see a problem with this solution, and then ask you for a more efficient (O(N)) solution.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Daniel Pfeffer wrote: If I were the interviewer, I would ask if you can see a problem with this solution, and then ask you for a more efficient (O(N)) solution.
I'd throw my dry-erase marker at my interviewer's forehead and storm out!!
...
Later, they would offer me the job for sticking to my principles.
|
|
|
|
|
And while you're at, can you stop space and time and develop the first programming language used on Mars and feed that back to us?
Then on the actual job... your coworker asks you... what's a box model again? How do you configure Webpack? How do I actually do any of the things my job requires? Well, at least I studied algorithms for a few weeks while interviewing. Phew.
Jeremy Falcon
|
|
|
|
|
Jeremy Falcon wrote: And while you're at, can you stop space and time and develop the first programming language used on Mars and feed that back to us?
Well, we already have the first programming language to be used on Jupiter (JOVIAL)
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Touché!
Jeremy Falcon
|
|
|
|
|
don't you just love the way JS let's you express your thoughts?
|
|
|
|
|
Jeremy Falcon
|
|
|
|
|
Look at this stupid C# code I wrote that allows you to do only one for loop.
int [] data = {0,1,2,0,0,0,0,0,5};
List<int> allData = new List<int>(data);
var originalSize = allData.Count;
var counter=0;
for (int x =0;counter<=originalSize;x++){
if (allData[x]== 0){
Console.WriteLine($"got 0 : {x}");
allData.RemoveAt(x--);
allData.Add(0);
}
counter++;
}
data = allData.ToArray();
Console.WriteLine(data);
I've provided multiple data sets to test the data. It works.
EDIT
i was trying to reproduce what I could do in JavaScript. See above... The Lounge[^]
|
|
|
|
|
|
OriginalGriff wrote: That's probably even less efficient than my Linq code!
But it works!!
|
|
|
|
|
yep, I was once asked a question of if you can only have two variables how do you switch their values. I was in a peevy mood(due to idiot I was interviewing with) and didn't really think before I answered. But I essentially said never happens in real life and why the heck do you want too and dude it really isn't possible. Once I thought about it for a second(a week later) I understood it was just a theoretical question. But hell.
a = 15
b = 5
a= a+b
b = a-b
a = a-b
But would that ever happen in a real life work environment?
To err is human to really mess up you need a computer
|
|
|
|
|
rnbergren wrote: But would that ever happen in a real life work environment?
That's the point. If this was systems programming or device programming I could see that. But for your typical LOB app...
Jeremy Falcon
|
|
|
|
|
There is an old trick from days when memory sizes were counted in bytes. (No I did not figure it out and had to be taught it.)
Use three exclusive or operations. This will avoid the overflow issue of using add and subtract.
For example:
a = 15
b = 5
a = a^b
b = b^a
a = a^b
|
|
|
|
|
I've always liked this trick since I first saw it in an assembler manual for PLAN (there must be other OPers who remember PLAN). The only problem with it is that it only works with integers.
|
|
|
|
|