Last active
October 21, 2018 19:07
-
-
Save oyilmaztekin/c058cdeb1bfcbc6e60ad490e80bfcd4b to your computer and use it in GitHub Desktop.
Problem Solving
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // You are in charge of the cake for your niece's birthday | |
| // and have decided the cake will have one candle for each year of her total age. | |
| // When she blows out the candles, she’ll only be able to blow out the tallest ones. | |
| // Your task is to find out how many candles she can successfully blow out. | |
| let ar = [3, 2, 1, 3, 2, 2, 2] // each element of array represents to size of candles | |
| function birthdayCakeCandles(ar) { | |
| let max = 0; | |
| let max_freq = 0; | |
| for(let i = 0; i < ar.length; i++){ | |
| if(ar[i] > max) { | |
| max = ar[i] | |
| max_freq = 1; | |
| } | |
| else if(ar[i] === max) { | |
| max_freq++ | |
| } | |
| } | |
| return max_freq; | |
| } | |
| birthdayCakeCandles(ar); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // find second highest value in array | |
| // 8 | |
| // O(n) | |
| // | |
| "use strict" | |
| let nums = [6,3,5,7,6,8,9,10,10,11,3,4,5,1,1,1] | |
| function findSecondHighestValue(nums){ | |
| let first = 0; | |
| let second = 0; | |
| for(let i = 0; i < nums.length; i++){ | |
| if(nums[i] > first){ | |
| second = first; | |
| first = nums[i] | |
| } | |
| if(nums[i] < first && nums[i] > second){ | |
| second = nums[i] | |
| } | |
| } | |
| return second; | |
| } | |
| findSecondHighestValue(nums) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://www.hackerrank.com/challenges/grading/problem | |
| function gradingStudents(grades) { | |
| return grades.map(n => { | |
| let diff = 5 - (n % 5) | |
| diff < 3 && n >= 38 | |
| ? n += diff | |
| : null | |
| return n | |
| }) | |
| } | |
| gradingStudents( [4, 73, 67, 38, 33 ] ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // minimum four number and minumum five number ex: 1+2+3+4 and 2+3+4+5 | |
| //input: array output: number | |
| function miniMaxSum(arr) { | |
| // sort numbers as an order | |
| // take first four numbers | |
| // take last four numbers | |
| // slice arr.length - 4, arr.length | |
| // reduce first and last arrays | |
| let newArr = arr.sort() | |
| let firstFour = newArr.slice(0,4) | |
| let lastFour = newArr.slice(newArr.length -4, newArr.length) | |
| let maxResult = 0; | |
| let minResult = 0; | |
| maxResult = lastFour.reduce((c,r) => c + r) | |
| minResult = firstFour.reduce((c,r) => c + r) | |
| console.log(minResult) | |
| console.log(maxResult) | |
| } | |
| miniMaxSum([4,1,3,5,2]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function nMinus1(arr){ | |
| let i = arr.length; | |
| while(i--) { | |
| if(i > 0) { | |
| if(arr[i].key === arr[i-1].key){ | |
| arr[i-1].type += " " + arr[i].type | |
| arr.splice(i,1) | |
| } | |
| } | |
| } | |
| return arr | |
| } | |
| nMinus1( | |
| [ | |
| { | |
| "key": "str", | |
| "type": "int" | |
| }, | |
| { | |
| "key": "str", | |
| "type": "bool" | |
| }, | |
| { | |
| "key": "str", | |
| "type": "array" | |
| }, | |
| { | |
| "key": "b", | |
| "type": "func" | |
| }, | |
| { | |
| "key": "c", | |
| "type": "func" | |
| }, | |
| { | |
| "key": "f", | |
| "type": "func" | |
| }, | |
| { | |
| "key": "f", | |
| "type": "object" | |
| }, | |
| { | |
| "key": "f", | |
| "type": "string" | |
| } | |
| ] | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Write a program that prints a staircase of size . | |
| // Consider a staircase of size : n = 4 | |
| // # | |
| // ## | |
| // ### | |
| // #### | |
| function staircase(n){ | |
| let stairLayer = [] | |
| for(let i = 0; i < n; i++){ | |
| // decreasing n number | |
| let stairBlock = [] | |
| let skipBlock = n - i - 1; // -1 because i starts with 0 | |
| for(let j = 0; j < n; j++){ | |
| debugger | |
| if(skipBlock > 0) stairBlock.push(' ') | |
| else stairBlock.push('#') | |
| skipBlock-- | |
| } | |
| stairLayer.push(stairBlock) | |
| } | |
| for(let s of stairLayer) { | |
| console.log(s.join('')) | |
| } | |
| } | |
| staircase(3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // input: 07:05:45PM | |
| // expected output: 19:05:45 | |
| function timeConversion(s){ | |
| s = s.split(':') | |
| if(s[0] < 12 && s[2].includes('PM')) { | |
| s[0] = parseInt(s[0],0) + 12 | |
| } | |
| if(s[0] == 12 && s[2].includes('AM')) { | |
| s[0] = '00' | |
| } | |
| s[2].includes('PM') | |
| ? s[2] = s[2].replace('PM','') | |
| : null | |
| s[2].includes('AM') | |
| ? s[2] = s[2].replace('AM','') | |
| : null | |
| s[0] = s[0].toString() | |
| console.log(`${s[0]}:${s[1]}:${s[2]}`) | |
| } | |
| timeConversion('03:05:45PM') | |
| timeConversion('12:40:22AM') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment