Skip to content

Dcorkran/Off-week

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Off-week

Update 04/13/2017

I moved all of the exercises into different directories. Each is about a month and a half of time. part1 exercises were completed the longest ago.

Old Info

Thanksgiving break code wars assessments. ... This has branched out to more than just Thanksgiving. I will try to keep as updated as possible. ... Merry Christmas! Happy Holidays! Will move different solutions to different files asap

songDecoder

Polycarpus works as a DJ in the best Berland nightclub, and he often uses dubstep music in his performance. Recently, he has decided to take a couple of old songs and make dubstep remixes from them.

Let's assume that a song consists of some number of words. To make the dubstep remix of this song, Polycarpus inserts a certain number of words "WUB" before the first word of the song (the number may be zero), after the last word (the number may be zero), and between words (at least one between any pair of neighbouring words), and then the boy glues together all the words, including "WUB", in one string and plays the song at the club.

For example, a song with words "I AM X" can transform into a dubstep remix as "WUBWUBIWUBAMWUBWUBX" and cannot transform into "WUBWUBIAMWUBX".

Recently, Jonny has heard Polycarpus's new dubstep track, but since he isn't into modern music, he decided to find out what was the initial song that Polycarpus remixed. Help Jonny restore the original song.

Input

The input consists of a single non-empty string, consisting only of uppercase English letters, the string's length doesn't exceed 200 characters

Output

Return the words of the initial song that Polycarpus used to make a dubsteb remix. Separate the words with a space.

Best Practice Answer (found after completion)

function songDecoder(song){
  return song.replace(/(WUB)+/g," ").trim()
}

goodVsEvil

Middle Earth is about to go to war. The forces of good will have many battles with the forces of evil. Different races will certainly be involved. Each race has a certain 'worth' when battling against others. On the side of good we have the following races, with their associated worth:

Hobbits - 1 Men - 2 Elves - 3 Dwarves - 3 Eagles - 4 Wizards - 10 On the side of evil we have:

Orcs - 1 Men - 2 Wargs - 2 Goblins - 2 Uruk Hai - 3 Trolls - 5 Wizards - 10 Although weather, location, supplies and valor play a part in any battle, if you add up the worth of the side of good and compare it with the worth of the side of evil, the side with the larger worth will tend to win.

Thus, given the count of each of the races on the side of good, followed by the count of each of the races on the side of evil, determine which side wins.

Best Practice Answer (found after completion)

function goodVsEvil(good, evil) {  
  var getWorth = function( side, worth ) {
    return side.split(' ').reduce( function(result, value, index) {
      return result + (worth[index] * value);
    }, 0);
  }

  var result = getWorth( good, [1,2,3,3,4,10] ) - getWorth( evil, [1,2,2,2,3,5,10] );

  return result > 0 ? "Battle Result: Good triumphs over Evil" :
         result < 0 ? "Battle Result: Evil eradicates all trace of Good" :
                      "Battle Result: No victor on this battle field";
}

listNames

Given: an array containing hashes of names

Return: a string formatted as a list of names separated by commas except for the last two names, which should be separated by an ampersand.

Example:

list([ {name: 'Bart'}, {name: 'Lisa'}, {name: 'Maggie'} ])
// returns 'Bart, Lisa & Maggie'

Best Practice Answer (found after completion)

function list(names){
  return names.reduce(function(prev, current, index, array){
    if (index === 0){
      return current.name;
    }
    else if (index === array.length - 1){
      return prev + ' & ' + current.name;
    }
    else {
      return prev + ', ' + current.name;
    }
  }, '');
 }

array_diff

Your goal in this kata is to implement an difference function, which subtracts one list from another.

It should remove all values from list a, which are present in list b.

difference([1,2],[1]) == [2]

If a value is present in b, all of its occurrences must be removed from the other:

difference([1,2,2,2,3],[2]) == [1,3]

Best Practice (found after completion)

function array_diff(a, b) {
  return a.filter(function (v) { return b.indexOf(v) === -1 });
}

Clay Pigeon Shooting

Pete and his mate Phil are out in the countryside shooting clay pigeons with a shotgun - amazing fun.

They decide to have a competition. 3 rounds, 2 shots each. Winner is the one with the most hits.

Some of the clays have something attached to create lots of smoke when hit, guarenteed by the packaging to generate 'real excitement!' (genuinely this happened). None of the explosive things actually worked, but for this kata lets say they did.

For each round you will receive the following format:

[{P1:'XX', P2:'XO'}, true]

That is an array containing an object and a boolean. Pl represents Pete, P2 represents Phil. X represents a hit and O represents a miss. If the boolean is true, any hit is worth 2. If it is false, any hit is worth 1.

Find out who won. If it's Pete, return 'Pete Wins!'. If it is Phil, return 'Phil Wins!'. If the scores are equal, return 'Draw!'.

Note that as there are three rounds, the actual input (x) will look something like this:

[[{P1:'XX', P2:'XO'}, true], [{P1:'OX', P2:'OO'}, false], [{P1:'XX', P2:'OX'}, true]]

Clever Answer (found after solving)

shoot=x=>(y=x.reduce((a,b)=>a+(b[0].P1.split("O").length-b[0].P2.split("O").length)*(b[1]+1),0))>0?'Phil Wins!':y<0?'Pete Wins!':'Draw!'

Triangle Type

In this kata, you should calculate type of triangle with three given sides a, b and c (given in any order).

If all angles are less than 90°, this triangle is acute and function should return 1.

If one angle is strictly 90°, this triangle is right and function should return 2.

If one angle more than 90°, this triangle is obtuse and function should return 3.

If three sides cannot form triangle, or one angle is 180° (which turns triangle into segment) - function should return 0.

Input parameters are sides of given triangle. All input values are non-negative floating point or integer numbers (or both).

Best Practice (found after answering. Mine was kind of similar for once!)

function triangleType(a, b, c){
  var result = triangleType.ACUTE,
      sides = [a,b,c].sort(function(a,b){return a-b});

  a = sides[0], b = sides[1], c = sides[2];

  if(a <= c - b) {
    result = triangleType.INVALID;

  } else if(c*c === a*a + b*b) {
    result = triangleType.RIGHT;

  } else if(c*c > a*a + b*b) {
    result = triangleType.OBTUSE;
  }

  return result;
}

triangleType.INVALID = 0;
triangleType.ACUTE = 1;
triangleType.RIGHT = 2;
triangleType.OBTUSE = 3;

Create Phone Number

Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of those numbers in the form of a phone number.

Best Practice (found after answering)

function createPhoneNumber(numbers){
  numbers = numbers.join('');
  return '(' + numbers.substring(0, 3) + ') '
      + numbers.substring(3, 6)
      + '-'
      + numbers.substring(6);
}

Find the odd int

Given an array, find the int that appears an odd number of times.

There will always be only one integer that appears an odd number of times.

Whoops

I thought the problem was asking me to find the number that appeared an odd amount of times as in a different amount of times than other numbers. For example, if the number 3 showed up 5 times, the number 4 showed up 5 times, and the number 2 showed up four times, it should return 4. I kept failing the random tests and could not figure out why. I left my original answer in there because I think it is a cool function, but lesson learned! RTFD. (To my credit the instructions could possibly be worded better)

Best Practice (found after answering)

function findOdd(A) {
  var obj = {};
  A.forEach(function(el){
    obj[el] ? obj[el]++ : obj[el] = 1;
  });

  for(prop in obj) {
    if(obj[prop] % 2 !== 0) return Number(prop);
  }
}

Number of trailing zeros of N!

Write a program that will calculate the number of trailing zeros in a factorial of a given number.

N! = 1 * 2 * 3 * 4 ... N

zeros(12) = 2 # 1 * 2 * 3 .. 12 = 479001600
that has 2 trailing zeros 4790016(00)

Be careful 1000! has length of 2568 digital numbers.

DC Note

This took me a really long time to get right. I wrestled with the large numbers for a pretty long time. At first, I tried converting the solved factorial into a string and counting from the last digit until I hit something that was not zero. However, most of the tests for this exercise involved large numbers that turned my strings into scientific notation. After struggling for a while, I googled around and found a formula that helped here. Never be afraid to burn something if it isn't working! https://en.wikipedia.org/wiki/Trailing_zero

The Hashtag Generator

The marketing team are spending way too much time typing in hashtags. Let's help them with out own Hashtag Generator!

Here's the deal:

If the final result is longer than 140 chars it must return false. If the input is a empty string it must return false. It must start with a hashtag (#). All words must have their first letter capitalized. Example Input to Output:

" Hello there thanks for trying my Kata" => "#HelloThereThanksForTryingMyKata"

" Hello World " => "#HelloWorld"

Best Practice (Found after answering)

function generateHashtag (str) {
  return str.length > 140 || str === '' ? false :
    '#' + str.split(' ').map(capitalize).join('');
}

function capitalize(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
}

Largest 5 digit number in a series

In the following 6 digit number:

283910

91 is the greatest sequence of 2 digits.

Complete the solution so that it returns the largest five digit number found within the number given.. The number will be passed in as a string of only digits. It should return a five digit integer. The number passed may be as large as 1000 digits.

Adapted from ProjectEuler.net

Best Practice (Found after answering)

function solution(digits){
  var r = 0;
  for (i = 0; i < digits.length - 4; i++) {
    r = Math.max(r, parseInt(digits.substr(i,5),10));
  }
  return r;
}

Snail sort

Given an n x n array, return the array elements arranged from outermost elements to the middle element, traveling clockwise.

array = [[1,2,3], [4,5,6], [7,8,9]] snail(array) #=> [1,2,3,6,9,8,7,4,5] For better understanding, please follow the numbers of the next array consecutively:

array = [[1,2,3], [8,9,4], [7,6,5]] snail(array) #=> [1,2,3,4,5,6,7,8,9]

NOTE: The idea is not sort the elements from the lowest value to the highest; the idea is to traverse the 2-d array in a clockwise snailshell pattern.

NOTE 2: The 0x0 (empty matrix) is represented as [[]]

Best Practice (Found after answering)

snail = function(array) {
  var result;
  while (array.length) {
    // Steal the first row.
    result = (result ? result.concat(array.shift()) : array.shift());
    // Steal the right items.
    for (var i = 0; i < array.length; i++)
      result.push(array[i].pop());
    // Steal the bottom row.
    result = result.concat((array.pop() || []).reverse());
    // Steal the left items.
    for (var i = array.length - 1; i >= 0; i--)
      result.push(array[i].shift());
  }
  return result;
}

DC Note

My answer workeds, but it could definitely use some refactoring. I could create a function that takes in the direction and acts accordingly which would make the sort function much more readable.

Next bigger number with the same digits

You have to create a function that takes a positive integer number and returns the next bigger number formed by the same digits:

nextBigger(12)==21 nextBigger(513)==531 nextBigger(2017)==2071

If no bigger number can be composed using those digits, return -1:

nextBigger(9)==-1 nextBigger(111)==-1 nextBigger(531)==-1

About

Contains codewars assessment solutions

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published