Is this a better solution for Where I belong challenge?

Is this a better solution for Where I belong challenge?
0

#1

I have coded like below. I want some feedback.

function getIndexToIns(arr, num){
	var sorted = arr.sort(sorting);
	console.log(arr);
	
	var position = 0;
	
	function sorting(a, b){
		return a-b;
	}
	
	for(var i=0;i<sorted.length;i++){
		if(num <= sorted[i]){
			position = i;
			break;
		}else if(i === sorted.length-1){
			position = i+1;
			break;
		}
	}
  	return position;
}

getIndexToIns([2, 5, 10], 15);

#3

This is so simple but brilliant. I’m doing a course on algorithms on Coursera, and the lecturer mentions how even the simplest problems (his example was integer multiplication) can have a far more efficient, clever solutions. This is a pretty good example of that as well.


#5

I’m afraid it’s rather basic but I wanted formal education on algorithm analysis so I went with this one: https://www.coursera.org/learn/algorithm-design-analysis/home/welcome

It’s mostly based on divide and conquer algorithms, but it’s really about the analysis and applications of algorithms for beginners with a base in basic math and programming.

I’ve finished a week, so I can’t comment on the overall quality of the course, but I’m certainly enjoying the video lectures and the (admittedly easy so far) math and analysis. You also have the freedom to code in whichever language you want, which is fantastic since I get to use Python and just focus on the algorithms not syntax. Plus it seems to be completely free.

There’s also a second part to this course, which is tougher and more focused on a variety of algorithms.

EDIT: You found the link and know the course, but I guess I’ll just leave this here anyway :sweat_smile:
Also I was torn between this and the Princeton one, but I definitely needed a stronger base to do the latter, so there wasn’t really much of a choice.


#7

I sometime want only solutions and I haven’t studied algorithms deeply, so that’s the reason. I liked your solution. A real programmer is who minimizes the effort. Your code reflects algorithmic thinking.


#8

Here is my solution, No idea if it is faster in computing, but it is another approach surly:

    function getIndexToIns(arr, num) {
      // Find my place in this sorted array.
           var myVar = arr.toString() +" , "+ (num);
           myVar = JSON.parse("[" + myVar + "]");
           myVar = myVar.sort(function(a, b)
             {
              return a - b;
              });
      return myVar.indexOf(num);

#9

here’s mine:

function getIndexToIns(arr, num) {
   var i = 0;
   arr.sort((a,b) => a-b);
   while (num > arr[i]) {
      i++;
   } return i;
}

it’s easy to read, and reflects the newbyness


#10

here is mine, can it be shortened anymore or how to remove the newArr declaration ?

function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  var newArr = arr.push(num);
  return arr.sort(function(a,b) {return a-b;}).indexOf(num);  
   }

getIndexToIns([20, 10, 30 , 60, 50, 40], 35);

#11

@Ediur if you use the concat method, you can get rid of the newArr declaration and chain the sort and indexOf methods since concat returns an array

function getIndexToIns(arr, num) {
  return arr.concat(num).sort(function(a, b) { return a - b; }).indexOf(num);
}

#12

tekhaus, brilliant solution!
My solution is here. I think I did some extra steps.
Shorter is better!
function getIndexToIns(arr, num) {
var args = arr.slice.call(arguments);
var flattened = args.reduce(function(a,b) {
return a.concat(b);
},[]);

flattened.sort(function(a,b){
return a-b;
});
return flattened.indexOf(num);
}

getIndexToIns([20, 3, 5], 1.9);


#13

This was my solution:

function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  arr = arr.sort( function( a, b ){
    return a - b;
  } );
  for( var i = 0; i < arr.length; i++ ) {
    if( arr[i] >= num ) { // found first higher element put num here
      return i;
    }
  }
  return arr.length;
}

getIndexToIns([40, 60], 50);

I didn’t add the num to the array becuase the challenge said all it wanted was the index of where it would go if it was to be added. I love reading everyone else’s solutions it really makes me think about my code and why i did what i did compared to other people. Great job to everyone that solved it.


#14

this is my solution if it helps… I always take extreme cases and separate them out of the main loop…
`function getIndexToIns(arr, num) {
//alert(arr.sort(function(a,b){return a-b;}));
var newArr = arr.sort(function(a,b){
return a -b;
});
if( num > newArr[newArr.length - 1])
return newArr.length;
for(var i = 0; i<newArr.length; i++){
if(newArr[i] === num)
return i;
else if(num > newArr[i] && num <newArr[i+1]){
return i+1;
}

  }
}

getIndexToIns([40, 60], 50);`