SEI-ATL / UNIT_2

This repo is for all things Unit 2
0 stars 3 forks source link

[unit 2] Challenge Problems | 11/16/2020 #18

Closed romebell closed 3 years ago

romebell commented 4 years ago

Challenge 1

Write a function mindPsAndQs(str) that accepts a string of uppercase letters. The function should return the length of the longest consecutive streak of the letters 'P' and 'Q'.

Hint: Use two variables. One to track the length of the current streak and another to track the length of the longest streak so far. Think of using a strategy similar to maxValue. This can also be solved using a single loop!

Nested loops not needed!

Examples:

mindPsAndQs('BOOTCAMP'); // => 1
mindPsAndQs('APCDQQPPC'); // => 4
mindPsAndQs('PQPQ'); // => 4
mindPsAndQs('PPPXQPPPQ'); // => 5
function mindPsAndQs(str) {

}
romebell commented 4 years ago

Challenge 2

Write a function commonFactors(num1, num2) that returns an array that contains the common factors between both numbers. A factor is a number that divides another number with no remainder.

Examples:

commonFactors(12, 50); // => [ 1, 2 ]
commonFactors(6, 24); // => [ 1, 2, 3, 6 ]
commonFactors(11, 22); // => [ 1, 11 ]
commonFactors(45, 60); // => [ 1, 3, 5, 15 ]
function commonFactors(num1, num2) {

}
richardleung1 commented 4 years ago
//challenge 1
function mindPsAndQs(str) {
    let longestStreak = 0;
    let currentStreak = 0;
    for (let i = 0; i < str.length; i++) {
        const char = str[i];
        if (char === 'P' || char === 'Q') {
            currentStreak++;
            if (currentStreak > longestStreak) {
                longestStreak = currentStreak
            }
        } else {
            currentStreak = 0;
        }
    }
    return longestStreak;
}
console.log(mindPsAndQs('BOOTCAMP')); // => 1
console.log(mindPsAndQs('APCDQQPPC')); // => 4
console.log(mindPsAndQs('PQPQ')); // => 4
console.log(mindPsAndQs('PPPXQPPPQ')); // => 5

//challenge 2
function commonFactors(num1, num2) {
    let factors = [];
    for (let i = 1; i <= num1; i++) {
        if (num2 % i === 0 && num1 % i === 0) {
            factors.push(i);
        } else {
            continue;
        }
    }
    return factors;
}

console.log(commonFactors(12, 50))
console.log(commonFactors(6, 24))
console.log(commonFactors(11, 22))
console.log(commonFactors(45, 60))
CoreyWilson319 commented 4 years ago
function mindPsAndQs(str) {
    array = str.split('')
    let numPQ = 0
    let max = 0
    for (let i = 0; i < array.length; i ++) {
        if (array[i] === 'P' || 'Q') {
            numPQ ++}
            if (numPQ > max) {
            max = numPQ} else {
                numPQ = 0
            }

    }
    return max
}
tylerdance commented 4 years ago
function mindPsandQs(str) {
    let arr = str.split('');
    let letters = [];
    let count = 1
    for (let i = 0; i < arr.length; i ++) {
        if arr[i] === arr[i+1] {
            count++
        }
    }
    return arr
}
console.log(arr);

lol smh

tcgilbert commented 4 years ago

First one stumped me so didnt get to second

//Challenge 1
function mindPsAndQs(str) {
let chars = str.split('');
let currentStreak = 0;
let maxStreak = 0;
for (let i = 0; i < chars.length; i++) {
    if (chars[i] === 'Q' || chars[i] === 'P') {
        currentStreak += 1;
        if (chars[i + 1] === 'Q' || chars[i + 1] === 'P') {
            currentStreak
        }
    }
}
return chars;
}

console.log(mindPsAndQs('APCDQQPPC'));
tuhoalyhuynh commented 4 years ago
function mindPsAndQs (str) {
    let charArray = []
    charArray = str.split('');
    let pStreak = 0;
    let longStreak = 0;
    for (let i = 0; i < charArray.length; i++) {
        if charArray[i] = 'P' {

        }
    }
}
function commonFactors(num1, num2) {
    let num1Factors = []
    let num2Factors = []
    let commons = []
    for (let i = 1; i < num1; i++) {
        if (num1 % i = 0) {
            num1Factors.push(i)
        }
    }
    for (let i = 1; i < num2; i++) {
        if (num2 % i = 0) {
            num2Factors.push(i)
        }
    }
    for (let i = 0; i < num1Factors.length; i++) {
        for (let j = 0; j < num2Factors. length; j++) {
            if (num1Factors[i] = num2Factors[j]) {
                commons.push(num1Factors[i])
            }
        }
    }
    return commons
}

console.log(commonFactors(45, 60));
wjlock commented 4 years ago
mindPsandQs(str) {
    let array = str.split('');
    let currentStreak = 0
    let longestStreak = 0

    array.forEach(element => {
        let lastElement = array[i] -1
        let currentElement = array[i]
        if (element = "P" || "Q") currentStreak++
        if (lastElement = currentElement) longestStreak++

    });
    console.log(currentStreak);
}
NikkiHmltn commented 4 years ago
function mindPsAndQs(str) {
    let streak = 0;
    let longStreak = 0;
    str.split(" ")
    //split the string into letters, then iterate thru to find the amount of P and Q
    for(let i=0; i<str.length;i++) {
        if 
    }
}

function commonFactors(num1, num2) {
    let common = [];

}
edgerees commented 4 years ago

I got Question 2 but had a difficult time with Question 1 so i moved passed it.


function commonFactors(num1, num2) {
  if (num1 < num2) {
    var max = num1;
  } else {
    var max = num2
  }

  var factors = [];

  for (var i = 1; i <= max; i += 1) {
    if (num1 % i === 0 && num2 % i === 0) {
      factors.push(i);
    }
  }

  return factors;
}

commonFactors(12, 50); // => [ 1, 2 ]
commonFactors(6, 24); // => [ 1, 2, 3, 6 ]
commonFactors(11, 22); // => [ 1, 11 ]
commonFactors(45, 60); // => [ 1, 3, 5, 15 ]
AJStrizzy commented 4 years ago

function mindPsAndQs(str) { let streak = [] let longest = [] let arr = str.split(" ")

for( let i = 0; i < arr.length; i++) {
    if (arr[i] === 'q' || 'p' && arr[i+1] === 'q' || 'p') {
        streak.push(arr[i])
    } else if (arr[i-1] === 'q' || 'p' &&  arr[i] === 'q' || 'p') {
        streak.push(arr[i])
    }
    if(streak.length > longest.length) {
        longest.push(streak)
    }
}
return longest.length

}

console.log(mindPsAndQs('BOOTCAMP')); // => 1 console.log(mindPsAndQs('APCDQQPPC')); // => 4 console.log(mindPsAndQs('PQPQ')); // => 4 console.log(mindPsAndQs('PPPXQPPPQ')); // => 5

zfinnan commented 4 years ago

function mindPsAndQs(str) {
        var i,
            temp,
            streak,
            length = arr.length,
            highestStreak = 0;

        for(i = 0; i < length; i++) {
            if(temp != '' && temp == arr[i]) 
                streak++;
            } else {
                streak = 1;
            }
            temp = arr[i];
            if(streak > highestStreak) {
                highestStreak = streak;
            }
        }

        return highestStreak;
    }
codebypaul commented 4 years ago

Challenge 1

const mindPsAndQs = (str) =>{
    let streak = 0
    let longestStreak = 0
    let letter = ''
    const array = str.split('')
    for (let i = 0; i < array.length; i++){
        if ((array[i] === ('Q'||'P') )&&(array[i++]=== ('Q'||'P'))) streak++
        if (streak > longestStreak) longestStreak = streak
    }
    console.log(longestStreak)
}

Wasn't able to complete challenge 2

AlexJBustillos commented 4 years ago
function mindPsAndQs(str) {
    let currentStreak = 0;
    let longestStreak = 0;
    for (i = 0; i < str.length; i++) {
        if (str[i] === 'P' || 'Q') {
           return currentStreak++;
        };
        longestStreak = currentStreak++
    };
    return longestStreak
};

did not finish challenge 2

canourrea23 commented 4 years ago
function mindPsAndQs(str) {
    for(let i = 0; i < str.length; i++) {

    }
console.log(mindPsAndQs('BOOTCAMP')); // => 1
console.log(mindPsAndQs('APCDQQPPC')); // => 4
console.log(mindPsAndQs('PQPQ')); // => 4
console.log(mindPsAndQs('PPPXQPPPQ')); // => 5

}
function commonFactor(num1, num2) {
    if(num2 === 0) {
        return num1;
    }

    return commonFacto(num2, num1%num2);
}

function commonFactor(num, arr)
{
    factors = arr[0];

    for ( let i = 1; i < num; i++) {
        factors = gcd(factors, arr[i])
    }

    return factors;
}
Swolepenguin commented 4 years ago
function mindPsAndQs(string1){
    max = 0;    
    for (i = 0; i < str.length(); i++) {
         count = 0;
        for (j = i; j < str.length(); j++) {
            if (str.charAt(i) == str.charAt(j)) {
                count++;
            } else break;
        }
        if (count > max) max = count;
    }
    return max;
}
JJURIZ commented 4 years ago

Nothing for problem 1

INCOMPLETE

const commonFactors = (num1, num2) => {
    num1Factors = [];
    num2Factors = [];
    commonNums = [];
    for (let i = 0; i <= num1; i++) {
        if(num1 % i === 0) {
            num1Factors.push(i)
        }
    }
    for (let j = 0; j <= num2; j++) {
        if(num2 % j === 0) {
            num2Factors.push(j)
        }
    }

 console.log(num1Factors);
 console.log(num2Factors);

}
commonFactors(12, 50);
ruvvet commented 4 years ago
function mindPsAndQs(str) {
  let streak = 0;
  let maxstreak = 0;
  let letter = ['P', 'Q'];

  for (let i = 0; i < str.length; i++) {
    if (str[i] == 'P' || str[i] == 'Q') {
      streak++;
      if (streak > maxstreak) {
        maxstreak = streak;
      }
    } else {
      streak = 0;
    }
  }
  return maxstreak;
}

function commonFactors(num1, num2) {
  let commonfac = [];
  for (let i = 1; i <= num1; i++) {
    if (num1 % i == 0 && num2 % i == 0) {
      commonfac.push(i);
    }
  }
  return commonfac;
}
ABarranco95 commented 4 years ago

Didn't get to finish the 1st much less start the 2nd

function mindPsAndQs(str) {
    let p = 0
    let q = 0
    for(let i = 0; i < str.length; i ++) {
        if(str[i] == 'P') {
            p++
        } else if(str[i] == 'Q') {
            q++
        }
    }
    console.log(p + q)
}

mindPsAndQs('BOOTCAMP'); // => 1
mindPsAndQs('APCDQQPPC'); // => 4
mindPsAndQs('PQPQ'); // => 4
mindPsAndQs('PPPXQPPPQ'); // => 5
alanavery commented 4 years ago
// Challenge 1

let mindPsAndQs = str => {
  let currentStreak = 0;
  let bestStreak = 0;
  for (let i = 0; i < str.length; i++) {
    if (str[i] === 'P' || str[i] === 'Q') {
      currentStreak += 1;
      bestStreak = currentStreak;
    } else {
      currentStreak = 0;
    }
  }
  return bestStreak;
};

// Challenge 2

let commonFactors = (num1, num2) => {
  let findFactors = num => {
    let array = [];
    for (let i = 1; i <= num; i++) {
      array.push(i);
    }
    return array.filter(element => num % element === 0);
  };
  array1 = findFactors(num1);
  array2 = findFactors(num2);
  return array1.filter(element => array2.includes(element));
};
anonyymous1 commented 4 years ago
function mindPsAndQs(str) {
    let currentStreak = ''
    let longestStreak = ''
    str.split ='';
    for (let i = 0; i > str.length; i++) {
        if(currentStreak != '' && currentStreak == str[i]) {
            longestStreak++;
        } else {
            longestStreak = 1;
        }
        currentStreak = str[1];

        if (currentStreak > longestStreak) {
            longestStreak = currentStreak;
        }
    }
    return longestStreak;
}
console.log(mindPsAndQs('BOOTCAMP'));

Didn't get to the second one cause stuck on first one

tuanhrex commented 4 years ago
function commonFactors(num1, num2) {
    let num1Factor = []
    for (let i = 1; i <= num1; i++) {
        if(num1 % i == 0);
        num1Factor.push(i);
    }

    let commonArray = [],

}

Didn't finish

nitishdayal commented 4 years ago

Challenge 1

/**
 * 
 * @param {string} str
 */
const mindPsAndQs = (str) => {
  if (str.length <= 0) return;
  const arrOfChars = str.toUpperCase().split('')

  let longestStreak = 0,
    currentStreak = 0

  arrOfChars.forEach((val) => {
    if (val === 'P' || val === 'Q') {
      ++currentStreak
      longestStreak = currentStreak > longestStreak ? currentStreak : longestStreak
    } else {
      currentStreak = 0
    }
  })

  return longestStreak
}

Challenge 2

/**
 * 
 * @param  {...number} nums 
 */
const commonFactors = (...nums) => {
  const factors = [1]
  const smallestValue = Math.min(...nums)

  for (let i = 2; i <= smallestValue; i++) {
    if (nums.every(num => num % i === 0)) factors.push(i)
  }

  return factors
}

console.log(commonFactors(12, 50))
console.log(commonFactors(6, 24))
console.log(commonFactors(11, 22))
console.log(commonFactors(45, 60))
sschneeberg commented 4 years ago

function mindPsAndQs(str) {
    let pqlength = 0;
    let prevChar = '';
    for (let i = 0; i < str.length; i++) {
        if (str[i] === 'P' || str[i] === 'Q') {
            if (i != 0 && (prevChar === 'P' || prevChar === 'Q')) {
                pqlength++;
            } else {
                pqlength = 1;
            }
        }
        prevChar = str[i];

    }
    return pqlength;
}
function commonFactors(num1, num2) {
    let common = [];
    for (let i = 0; i < Math.max(num1, num2); i++) {
        if (num1 % i === 0 && num2 % i === 0) {
            common.push(i)
        }
    }
    return common.sort((a, b) => a - b);
}
fmuwanguzi commented 4 years ago

I started looking into using match to count the string and didn't get through the first one or get to the second one

function mindPsAndQs(str) {
    let pees = 0;
    let ques = 0;
    const myArray = str.split('')

    for(let i=0; 0 < myArray.length; i++){
        let char = myArray[i]
        if(char === 'P') pees++;
        else if(char === 'Q') ques++;
    }

    return str.match
    }
lev-choubine commented 4 years ago
let count = 0;
function mindPsAndQs(str) {
    if(str === str.toUpperCase()){
       let res=str.split("");
       for(let i=0; i < res.length; i++){
         if(res[i] ==="Q" || res[i] ==="P"){
             count = count + 1;
         }
    }
}console.log(count);
}

let arr1 =[];
let arr2 =[];
let res = [];

function commonFactors(num1, num2) {
    for(let i=0; i < num1.length; i++){
        if(num % i === 0){
            arr1.push(i);
        }

    }
    for(let i=0; i < num2.length; i++){
        if(num % i === 0){
            arr2.push(i);
        } 

    }
    for(let i=0; i < arr1.length; i++){
        for(let a=0; a < arr2.length; a++){
            if(i === a){
                res.push(a)
            }
        }
    }console.log(res)

}

mindPsAndQs('FHPPPPPGHQQQ');
commonFactors(12, 16);
JaxonNarramore commented 4 years ago

function mindPsAndQs(str) { let count = 0; let max = 0; for (i = 0; i < str.length(); i++) }

Sorry I wasn't able to finish I had a committing/pushing issue with deliverable that me and Pete were solving

nitishdayal commented 4 years ago
const evenCaps = (str) => {
  const cappedString = str.split('').map((val, i) => {
    if (i % 2 === 0) {
      val = val.toUpperCase()
    }
    return val
  })

  return cappedString.join('')
}

console.log(evenCaps("Tom got a small piece of pie"))
console.log(evenCaps("the book is in front of the table"))
sschneeberg commented 4 years ago
function evenCaps(str) {
    let strArray = str.split('');
    for (let i = 0; i < strArray.length; i++) {
        if (i % 2 === 0) {
            strArray[i] = strArray[i].toUpperCase();
        }
    }
    return strArray.join('');
}
tcgilbert commented 4 years ago
function evenCaps(str) {
    let strArray = str.split('');
    for (let i = 0; i < strArray.length; i++) {
        if (i % 2 === 0) {
            strArray[i] = strArray[i].toUpperCase();
        }
    }
    return strArray.join('');
}
alanavery commented 4 years ago
function evenCaps(str) {
  let strArray = str.split('');
  for (let i = 0; i < strArray.length; i++) {
    if (i % 2 === 0) {
      strArray[i] = strArray[i].toUpperCase();
    }
  }
  return strArray.join('');
}
CoreyWilson319 commented 4 years ago
let strArray = str.split('');
for (let i = 0; i < strArray.length; i++) {
if (i % 2 === 0) {
strArray[i] = strArray[i].toUpperCase();
}
}
return strArray.join('');
}
JaxonNarramore commented 4 years ago
function evenCaps(str) {
    let strArray = str.split('');
    for (let i = 0; i < strArray.length; i++) {
        if (i%2 === 0) {
            strArray[i] = strArray[i].toUpperCase();
        }
    }
    return strArray.join('');
}
ruvvet commented 4 years ago
function evenCaps(sentence){

    let newstr = '';

    for (let i=0; i<sentence.length; i++){
        if( i %2 ===0 ){
            newstr += sentence[i].toUpperCase();

        } else {
        newstr += sentence[i].toLowerCase();}

    }
    return newstr;
}

console.log(evenCaps('Hello MY name is aLKJLKJFdsda'));
tylerdance commented 4 years ago
function evenCaps(str) {
    let strArray = str.split('');
    for (let i = 0; i < strArray.length; i++) {
        if (i % 2 === 0) {
            strArray[i] = strArray[i].toUpperCase();
        }
    }
    return strArray.join('');
}
richardleung1 commented 4 years ago
function evenCaps(sentence) {
    let array = sentence.split('')
    for (let i = 0; i < array.length; i++) {
        if (i % 2 === 0) {
            array[i] = array[i].toUpperCase()
        }
    }
    return array.join('');
}
tuanhrex commented 4 years ago
function evenCaps(sentence){

    let newstr = '';
    for (let i=0; i<sentence.length; i++){
        if( i %2 ===0 ){
            newstr += sentence[i].toUpperCase();

        } else {
        newstr += sentence[i];}

    }
return newstr;
}
NykywanP commented 4 years ago

function evenCaps(sentence){

let newstr = '';
for (let i=0; i<sentence.length; i++){
    if( i %2 ===0 ){
        newstr += sentence[i].toUpperCase();

    } else {
    newstr += sentence[i];}

}

return newstr; }

zfinnan commented 4 years ago
function evenCaps(sentence) {
    let sentenceArray = sentence.split('');
    for(let i = 0; i < sentenceArray.length; i ++) {
        if (i % 2 === 0) {
            sentenceArray[i] = sentenceArray[i].toUpperCase();
        }
    }
    return sentenceArray.join('');
}
JJURIZ commented 4 years ago
const evenCaps = (str) => {
    let newStr = '';
    for (let i=0; i<str.length; i++) {
        if (i % 2 === 0) {
            newStr += str[i].toUpperCase();
        } else {
            newStr += str[i];
        }
    }
    return newStr;
}

console.log(evenCaps('this is a string'))
Swolepenguin commented 4 years ago
function evenCaps(sentence){

    let newstr = '';
    for (let i=0; i<sentence.length; i++){
        if( i %2 ===0 ){
            newstr += sentence[i].toUpperCase();

        } else {
        newstr += sentence[i];}

    }
return newstr;
}
wjlock commented 4 years ago

function evenCaps(sentence){

let newstr = '';
for (let i=0; i<sentence.length; i++){
    if( i %2 ===0 ){
        newstr += sentence[i].toUpperCase();

    } else {
    newstr += sentence[i];}

}

return newstr; }

lev-choubine commented 4 years ago
function evenCaps (sentence){
    let sentenceArray = sentence.split('');
    for (let i = 0; i < sentenceArray.length; i++){
        if (i%2 === 0){
            sentenceArray[i] = sentenceArray[i].toUpperCase();
        }
    }
    return sentenceArray.join('');
}

console.log(evenCaps("Tom got a small piece of pie"));
tuhoalyhuynh commented 4 years ago
function evenCaps (sentence) {
    let sentenceArray = [];
    sentenceArray = sentence.split('');
    let newArray = [];
    for (let i = 0; i < sentenceArray.length; i++) {
        if (i % 2 === 0) {
            newArray.push(sentenceArray[i].toUpperCase());
        } else {
            newArray.push(sentenceArray[i])
        }
    }
    let newSentence = newArray.join('');
    return newSentence
}
AlexJBustillos commented 4 years ago
evenCaps = (sentence) => {
    let string = sentence.split('');
    for (let i = 0; i < string.length; i++) {
        if (i % 2 === 0) {
            string[i] = string[i].toUpperCase();
        }
    } 
    return string.join('');
};
ABarranco95 commented 4 years ago
function evenCaps (sentence) {
    let sentenceArray = [];
    sentenceArray = sentence.split('');
    let newArray = [];
    for (let i = 0; i < sentenceArray.length; i++) {
        if (i % 2 === 0) {
            newArray.push(sentenceArray[i].toUpperCase());
        } else {
            newArray.push(sentenceArray[i])
        }
    }
    let newSentence = newArray.join('');
    return newSentence
}
maddevred commented 4 years ago
function evenCaps(sentence) {
let sentenceArray = sentence.split('');
for(let i = 0; i < sentenceArray.length; i ++) {
if (i % 2 === 0) {
sentenceArray[i] = sentenceArray[i].toUpperCase();
}
}
return sentenceArray.join('');
}
jtreeves commented 4 years ago
function evanCaps(sentence) {
    let array = sentence.split('');
    for (let i = 0; i < array.length; i++) {
        if (i % 2 === 0) {
            array[i] = array[i].toUpperCase()
        }
    }
    return array.join('')
}
canourrea23 commented 4 years ago

function evenCaps(sentence) { let string = sentence.split(''); for(let i = 0; i < string.length; i++) { if (i % 2 === 0) { string[i] = string[i].toUpperCase(); } } return string.join(''); } console.log(evenCaps("Tom got a small piece of pie"));

AJStrizzy commented 4 years ago

function evenCaps(sentence) { let string = sentence.split(''); for(let i = 0; i < string.length; i++) { if (i % 2 === 0) { string[i] = string[i].toUpperCase(); } } return string.join(''); } console.log(evenCaps("Tom got a small piece of pie"));

anonyymous1 commented 4 years ago
function evenCaps (sentence) {
    let sentenceArray = [];
    sentenceArray = sentence.split('');
    let newArray = [];
    for (let i = 0; i < sentenceArray.length; i++) {
        if (i % 2 === 0) {
            newArray.push(sentenceArray[i].toUpperCase());
        } else {
            newArray.push(sentenceArray[i])
        }
    }
    let newSentence = newArray.join('');
    return newSentence
}