Aiman
BAN USER
function flat(list) {
if (!Array.isArray(list)) {
return [list];
}
const [first, ...rest] = list;
return rest.length? flat(first).concat(flat(rest)): flat(first) ;
}
function flat2(list){
let result = [];
list.forEach(item => {
if (!Array.isArray(item)){
result.push(item);
}else {
result = result.concat(flat2(item));
}
});
return result;
}
/*
Given an array and a value, find if there is a triplet in array whose sum is equal to the given value.
If there is such a triplet present in array, then print the triplet and return true.
Else return false.
For example, if the given array is {12, 3, 4, 1, 6, 9} and given sum is 24,
then there is a triplet (12, 3 and 9) present in array whose sum is 24.
*/
console.log(findTriplet3([1,2,3,4,6,9,20], 18));
function findTriplet(list, target) {
for (let i = 0; i < list.length; i++) {
const first = list[i];
for (let j = i + 1; j < list.length; j++) {
const second = list[j];
for (let h = j + 1; h < list.length; h++) {
const third = list[h];
if (target - third - second - first === 0) {
console.log([first, second, third]);
return true;
}
}
}
}
return false;
}
function findTriplet2(list, target) {
const numbers = new Set(list);
for (let i = 0; i < list.length; i++) {
const first = list[i];
for (let j = i + 1; j < list.length; j++) {
const second = list[j];
if (numbers.has(target - second - first)) {
console.log([first, second, target - second - first]);
return true;
}
}
}
return false;
}
function findTriplet3(list, target) {
list.sort((a, b) => a - b);
let left, right;
for (let i = 0; i < list.length; i++) {
const first = list[i];
left = i + 1;
right = list.length - 1;
while (left < right) {
const second = list[left];
const third = list[right];
const sum = first + second + third;
if (sum === target) {
console.log([first, second, target - second - first]);
return true;
}else if (sum > target) {
right--;
}else if (sum < target) {
left ++;
}
}
}
return false;
}
document.write(colorLetters("Lorem ipsum dolor sit amet",["red", "blue", "green", "yellow"]));
function colorLetters(sentence, colors){
let count =0;
let result = '';
for (let i=0;i< sentence.length; i++){
if (sentence[i] !== '') {
result += `<span style="color: ${colors[count]}">${sentence[i]}</span>`;
count++;
if (count>colors.length-1){
count =0;
}
}else{
result +=sentence[i];
}
}
return result;
}
console.log(findSubstring('TRABCSC', 'ABC'));
function findSubstring(text, substring) {
let index=0;
for(let i=0;i<text.length;i++){
if (substring[index]==text[i]) {
if(index == substring.length-1)
{
return trimString(text, i-substring.length+1, substring.length);
}
index++;
}else{
index = 0;
}
}
return -1;
}
function trimString(text, start, length){
let s='';
for(let i=0;i<start;i++){
s+= text[i];
}
for(let i=start+length;i<text.length;i++){
s+= text[i];
}
return s;
}
First Brute-force solution using JS
function sortByIndex(list, indexes) {
if (list.length !== indexes.length ){
throw new Error("the list length must match the indexes length");
}
for (let i=0;i<list.length;i++){
const index= indexes[i];
const tmp = list[index];
list[index] = list[i];
list[i] = tmp;
const tmp2 = indexes[i];
indexes[i] = indexes[index];
indexes[index] = tmp2;
}
return list;
}
Repclarasbarr, Korean Air Change Flight at Adap.tv
I am ClaraBarr from California USA. Writes and records various different genres for television, film and other artists.Wrote several ...
Repjessicajbunting, Aghori Mahakal Tantrik at ABC TECH SUPPORT
Hi I am Jessica, from Houston USA. I am working as a manager in Incredible Universe company. I enjoy additional ...
RepGlennPCannon, Applications Developer at Techlogix
Hi everyone, I am a professor in Houston, USA. I like to explore new things about Hire Someone To Break ...
Repabbyherza, Animator at ASAPInfosystemsPvtLtd
I am Viola .I am A public relations consultant, a communications specialist who works as an intermediary between the public ...
Repthubmorfin, Android Engineer at ABC TECH SUPPORT
I am currently working as a safety-focused Service Technician from Red Bank. I execute routine maintenance work while advising clients ...
Repterricjohnson8547, Animator at 247quickbookshelp
Hello, I am a dancer. I have completed my studies from New York. And today I am a dance teacher ...
RepGenesisCruz, Integration Software Engineer at NetApp
I am a highly professional and experienced board director with many years of experience leading non-profit as well as for-profit ...
RepBriannaWright, Analyst at A9
I am a skilled project manager with years of exemplary service in diverse IT roles. I am passionate about utilizing ...
RepGinaSanchez, Computer Scientist at Autoportal.com
Ginna from New York.Successfully managing a high volume of work assignments without compromising quality to exceed client expectations.Apart ...
- Aiman October 11, 2019