minMaxRange(***REVISE This one is tricky but very easy***)
/**
* min max range
Given 3 lists of positive integers. From the first list get the
minimum value m1 and second list get the maximum value m2. From the third
list get all the values that lies between m1 and m2 including m1 and m2.
If m1 <= m2 then we should consider all the values x from third list which satisifies m1 <= x <= m2
If m1 > m2 then we should consider all the values x from third list which satisifies m1 >= x >= m2
You have write 3 functions.
Takes list as input and returns a minimum value.
Takes list as input and returns a maximum value.
Takes a list, m1 and m2 and returns list of intergers which lies between m1 and m2.
If no such numbers exist return a list with -1 i.e [-1]
You have to return the list of numbers in the same order they are present.
You will be provided with function template, you have to fill those functions.
Input
3 5 4 5 7
7 6 4 4 23 2
6 5 1 3 8 9 2
Output
6
5
3
8
9
Explanation
The minimum value from the first input list given is 3 and maximum value
from the second input list is 23 . We have to return the list of numbers
which satisfies the condition 3<=number<=23 from the third input list.
So the resulting output is [6,5,3,8,9]
*/
let fs = require("fs");
let data = fs.readFileSync(0, 'utf-8');
let idx = 0;
data = data.split('\n');
function readLine() {
idx++;
return data[idx - 1];
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// ## This function should return single integer. The integer should be maximum value of input list
function maximumValue(inputNumbers) {
let maxVal = inputNumbers[0]
for (i = 1; i < inputNumbers.length; i++) {
if (maxVal < inputNumbers[i]) {
maxVal = inputNumbers[i]
}
}
return maxVal
}
// ## This function should return single integer.
// ## The integer should be minimum value of input list
function minimumValue(inputNumbers) {
// Please write below this
let minVal = inputNumbers[0]
for (let i = 1; i < inputNumbers.length; i++) {
if (minVal > inputNumbers[i]) {
minVal = inputNumbers[i]
}
}
return minVal
}
// ## This function should return list of integer which lies between m1 and m2.
// ## if m1 <= m2 return list of numbers between m1 and m2
// ## if m2 <= m1 return list of numbers between m2 and m1
// ## If no such numbers exist in the input list between m1 and m2 return a list with -1 i.e [-1]
function getNumbersInRange(inputNumbers, m1, m2) { // [6,5,1,3,8,9,2],m1= 3 ,m2= 23
let arr = []// we will push the elements in this array
let x1 = (m1 < m2) ? m1 : m2 // (m1=3 < m2=23 true i.e x1= 3)
let x2 = (m1 > m2) ? m1 : m2// (m1=3 > m2=23 false i.e x2=23)
for (i = 0; i < inputNumbers.length; i++) {
//0)i=0
//1)i=1
//2)i=2
//3)i=3
//4)i=4
//5)i=5
//6)i=6
if (inputNumbers[i] >= x1 && inputNumbers[i] <= x2) {
//0) inputNumbers[0] is 6 and 6>=3 && 6<=23 , true push 6 in arr
//1) inputNumbers[1] is 5 and 5>=3 && 5<=23 , true push 5 in arr
//2) inputNumbers[2] is 1 and 1 is not >=3 , so don't push 1 in arr
//3) inputNumbers[3] is 3 and 3>=3 && 3<=23 , true push 3 in arr
//4) inputNumbers[4] is 8 and 8>=3 && 8<=23 , true push 8 in arr
//5) inputNumbers[5] is 9 and 9>=3 && 9<=23 , true push 9 in arr
//6) inputNumbers[6] is 2 and 2<=3 false, don't push in arr
arr.push(inputNumbers[i]) //[ 6, 5, 3, 8, 9 ]
}
}
return arr.length > 0 ? arr : [-1]
// This reads as ,is arr.length greater than 0? if yes
// return arr or return [-1]
}
// Please do not change anything below this line
// This part is only input taking
function ConvertToNumber(list) {
for (let each in list) {
list[each] = Number(list[each]);
}
return list;
}
let list1 = readLine().split(" "); // ['3', '5', '4', '5', '7']
let list2 = readLine().split(" "); // ['7', '6', '4', '4', '23', '2']
let list3 = readLine().split(" "); // ['6', '5', '1', '3', '8', '9', '2']
ConvertToNumber(list1); // Invoking the function ConvertToNumber
ConvertToNumber(list2);
ConvertToNumber(list3);
let m1 = minimumValue(list1); // Invoking the function mininmumValue
let m2 = maximumValue(list2); // Invoking the function maximumValue
let minMaxRange = getNumbersInRange(list3, m1, m2);// Invoking the function getNumbersInRange
console.log(...minMaxRange); // ... will remove the [] and ,
//////////////////////////////////////////////////////////////////////////////////////////////
TERMINAL:
Comments
Post a Comment