100 Days of code Day 8

100 Days of code Day 8

Codewars

1st challenge

Exclamation marks series #1: Remove an exclamation mark from the end of string

my solution

function remove (string) {
  let newString = ''
if(string.charAt(string.length - 1) === '!'){
   newString = string.slice(0,-1)
  return newString
}
  return string
}

most clever

function remove(s){
  return s[s.length - 1] == '!' ? s.slice(0, -1) : s;
}

2nd challenge

Complete the method that takes a boolean value and return a "Yes" string for true, or a "No" string for false.

my solution

function boolToWord( bool ){
  return bool === true ? 'Yes' : 'No'
}

most clever

function boolToWord( bool ){
  return bool ? 'Yes':'No';
}

3rd challenge

Your classmates asked you to copy some paperwork for them. You know that there are 'n' classmates and the paperwork has 'm' pages.

Your task is to calculate how many blank pages do you need. If n < 0 or m < 0 return 0.

my solution

function paperwork(n, m) {
  if(m < 0 || n < 0){
    return 0
  }
  return n * m 
}

most clever

function paperwork(n, m) {
    return n < 0 || m < 0 ? 0 : n * m;
}

4th challenge

Given an array of integers as strings and numbers, return the sum of the array values as if all were numbers.

Return your answer as a number.

my solution

function sumMix(x){
  let sum = 0
for(let char of x){
  sum += Number(char)
}
  return sum
}

most clever below is not a good solution because of space complexity

function sumMix(x){
  return x.map(a => +a).reduce((a, b) => a + b);
}

5th challenge

Given a set of numbers, return the additive inverse of each. Each positive becomes negatives, and the negatives become positives.

my solution

function invert(array) {
  if(array.length === 0){
    return array
  }
  let newArray = []

  for(let value of array){
   newArray.push(value *= -1)
  }

   return newArray;
}

most clever

function invert(array) {
   return array.map( x => x === 0 ? x : -x);
}

Build a function that returns an array of integers from n to 1 where n>0.

Example : n=5 --> [5,4,3,2,1]

const reverseSeq = n => {
  let arr = []
  for(let i = n; n > 0; n-- ){
    arr.push(n)
  }
  return arr;
};

6th challenge

Deoxyribonucleic acid (DNA) is a chemical found in the nucleus of cells and carries the "instructions" for the development and functioning of living organisms.

If you want to know more: en.wikipedia.org/wiki/DNA

In DNA strings, symbols "A" and "T" are complements of each other, as "C" and "G". You function receives one side of the DNA (string, except for Haskell); you need to return the other complementary side. DNA strand is never empty or there is no DNA at all (again, except for Haskell).

More similar exercise are found here: rosalind.info/problems/list-view (source)

Example: (input --> output)

my solution

function DNAStrand(dna){
let replaceObj = {A:'T', T:'A', G:'C', C:'G'}
return dna.split('').map(x=>replaceObj[x]).join('')

}

most clever

function DNAStrand(dna) {
  return dna.replace(/./g, function(c) {
    return DNAStrand.pairs[c]
  })
}

DNAStrand.pairs = {
  A: 'T',
  T: 'A',
  C: 'G',
  G: 'C',
}

Colt Steel's DS & A Lectures

Continuing on with the coding challenges in section six, return true if the arr has a pair of nums that equal the average. (num1 + num2 / 2). This is an example of multiple pointers. If avg is less than num, increase the starting index, otherwise decrease the ending index.

function averagePair(arr, num){
  let start = 0
  let end = arr.length-1;
  while(start < end){
    let avg = (arr[start]+arr[end]) / 2 
    if(avg === num) return true;
    else if(avg < num) start++
    else end--
  }
  return false;
}

Screen Shot 2022-02-15 at 8.36.15 PM.png

function isSubsequence(str1, str2) {
  var i = 0;
  var j = 0;
 while(j < str2.length){
     if(str2[j] === str1[i]) i++;
     if(i === str1.length) return true;
     j++
 }
 return false
}

Screen Shot 2022-02-15 at 10.14.02 PM.png

function maxSubarraySum(arr, num){
    if (arr.length < num) return null;

    let total = 0;
    for (let i=0; i<num; i++){
       total += arr[i];
    }
    let currentTotal = total;
    for (let i = num; i < arr.length; i++) {
       currentTotal += arr[i] - arr[i-num];
       total = Math.max(total, currentTotal);
    }
    return total;
}