- Joined
- Jan 2, 2014
- Messages
- 1,787
- Reaction score
- 939
- First Language
- Chinese
- Primarily Uses
- N/A
This post aims to incite you to share some algorithm challenges, so we can learn from those challenges, seek for answers for difficult problems we're facing, and hopefully apply their answers in case we encounter similar issues 
Let's start by sharing mine:
Challenge 1: Finding Array Elements
It's given that:
- array is an Array
- num is a Number
- sign is always either 1 or -1
- Every element in array has a property num, which is a Number
Goal -
Returns null if array's empty
Returns the element in array with the smallest property num that's just larger than num if sign equals to 1.
Returns the element in array with the largest property num that's just smaller than num if sign equals to -1.
If there's more than 1 such elements, return any of them.
If there's no such elements, return the element in array with the smallest property num if sign equals to 1.
If there's no such elements, return the element in array with the largest property num if sign equals to -1.
No argument will be mutated.
Condition - Your codes must always be 100% correct in this situation.
Constraint - You can only add new code in find_min_max_num_prop(although you can add anything you want).
My Hint -
My Solution -
It achieves the goal, meets the condition and conform to the constraint.
Proof:
It doesn't mutate any passed argument as temp_array is the only mutated variable, which isn't linked to any passed ones.
It's always 100% correct because -
It returns null if array's empty.
temp_array's always sorted in ascending and descending order if sign's equal to 1 and -1 respectively.
If the ith element's large/smaller than num for sign being 1/-1, so do all the (i + j)th elements.
So it's sure that that ith element's the smallest/largest one that's just larger/smaller than num for sign begin 1/-1.
If no such element's found, the 1st element, which must be the smallest/largest element for sign being 1/-1, will be returned.
I'll continue to post more and more. Let's share yours, so we can all benefit from each other here
Edit: My solution made some serious mistakes. Now they're all fixed lol
Let's start by sharing mine:
Challenge 1: Finding Array Elements
Intended difficulty level - 2(Little Javascript coding proficiency)
Situation - Consider the below piece of code:
/* Returns null if array's empty Returns the element in array with the smallest property num that's just larger than num if sign equals to 1 Returns the element in array with the largest property num that's just smaller than num if sign equals to -1 If there's more than 1 such elements, return any of them If there's no such elements, return the element in array with the smallest property num if sign equals to 1 If there's no such elements, return the element in array with the largest property num if sign equals to -1 No argument will be mutated */function find_min_max_num_prop(array, num, sign) { // Writes your codes here only //}; // find_min_max_num_prop
Situation - Consider the below piece of code:
/* Returns null if array's empty Returns the element in array with the smallest property num that's just larger than num if sign equals to 1 Returns the element in array with the largest property num that's just smaller than num if sign equals to -1 If there's more than 1 such elements, return any of them If there's no such elements, return the element in array with the smallest property num if sign equals to 1 If there's no such elements, return the element in array with the largest property num if sign equals to -1 No argument will be mutated */function find_min_max_num_prop(array, num, sign) { // Writes your codes here only //}; // find_min_max_num_prop
- array is an Array
- num is a Number
- sign is always either 1 or -1
- Every element in array has a property num, which is a Number
Goal -
Returns null if array's empty
Returns the element in array with the smallest property num that's just larger than num if sign equals to 1.
Returns the element in array with the largest property num that's just smaller than num if sign equals to -1.
If there's more than 1 such elements, return any of them.
If there's no such elements, return the element in array with the smallest property num if sign equals to 1.
If there's no such elements, return the element in array with the largest property num if sign equals to -1.
No argument will be mutated.
Condition - Your codes must always be 100% correct in this situation.
Constraint - You can only add new code in find_min_max_num_prop(although you can add anything you want).
My Hint -
Sort array first.
/* Returns null if array's empty Returns the element in array with the smallest property num that's just larger than num if sign equals to 1 Returns the element in array with the largest property num that's just smaller than num if sign equals to -1 If there's more than 1 such elements, return any of them If there's no such elements, return the element in array with the smallest property num if sign equals to 1 If there's no such elements, return the element in array with the largest property num if sign equals to -1 No argument will be mutated */function find_min_max_num_prop(array, num, sign) { // Writes your codes here only if (array.length == 0) { return null; }; var temp_array = []; for (index = 0; index < array.length; index++) { temp_array.push(array[index]); } temp_array.sort(function(a, { return (a.num - b.num) * sign }); for (index = 0; index < array.length; index++) { if (temp_array[index].num * sign > num * sign) { return temp_array[index] } } return temp_array[0] //} // find_min_max_num_prop
Proof:
It doesn't mutate any passed argument as temp_array is the only mutated variable, which isn't linked to any passed ones.
It's always 100% correct because -
It returns null if array's empty.
temp_array's always sorted in ascending and descending order if sign's equal to 1 and -1 respectively.
If the ith element's large/smaller than num for sign being 1/-1, so do all the (i + j)th elements.
So it's sure that that ith element's the smallest/largest one that's just larger/smaller than num for sign begin 1/-1.
If no such element's found, the 1st element, which must be the smallest/largest element for sign being 1/-1, will be returned.
I'll continue to post more and more. Let's share yours, so we can all benefit from each other here
Edit: My solution made some serious mistakes. Now they're all fixed lol
Last edited by a moderator:

