What are your favourite CodeWars Kata's?

What are your favourite CodeWars Kata's?
0

#1

Hi guys,

I love CodeWars as a supplement to the FCC algorithm challenges. With CodeWars you can find easier / harder problems to solve so that you can reinforce your learning along with the FCC challenges. So I decided to do a monthly curation of my favourite code kata’s on my FCC portfolio website:

I’d love to hear what your favourite Code Kata’s are!! Add me as a friend in CodeWars to see my answers

Jack


#2

I had signup for CoderWars a while ago before joining FCC. Thanks for reminding me about the site as it does compliment many of the challenges on FCC as well as to expose me to new ones. It is a great way to broaden my approach to solving problems. I will update this post with my favourite code kata as I resume CodeWars and hone my coding skills.


#3

Awesome, yeah it is the perfect tool to learn new approaches and work on easier problems, which really gives you a chance to tackle the FCC problems. I found the FCC challenges to be really hard, and so I just stuck with 8 KYU codewars challenges for a looooong time.


#4

Going to start posting my answers if you don’t mind.

  1. n length array of num:
function(num, n){
    return Array.from(Array(n)).map( () => num);
}

\2. string sequence by array of indexes

function(str, arrOfIndexes){
  return arrOfIndexes.map(index => str[index]).join(""); 
}

\3.

function mealCount(attendess){
    arrOfMeals = attendees.map(person => person.meal);
    arrayedObjOfMealCount = arrOfMeals.filter((meal, index) => index === arrOfMeals.indexOf(meal)).
          map(meal => ({[meal]: arrOfMeals.filter(food => food === meal).length}));
    return Object.assign({}, ...arrayedObjOfMealCount);
}
function fuelPrice(liters, price){
    if (liters > 5){
         return Math.round((Math.round((price - (liters * 0.05 * price)) * 100) / 100) * liters* 100) / 100;
    } else {
        return Math.round((Math.round((price - (liters * 0.25)) * 100) / 100) * liters* 100) / 100;
    }
}

\5. And I still don’t know how to make classes in Js…


I’d say my favorite (read hardest one I’ve done) is creating a function that takes as input a number is seconds and returns a string of how many minutes, hours, days and years it is. in format “3 hours, 1 minute and 30 seconds” or “2 years and 5 days” Notice the output adjusting for a singular number. It’s a Kata 4


#5

Why don’t you join codewars? We can see your answers there.


#6

My favourite was Rank Poker Hands https://www.codewars.com/kata/5739174624fc28e188000465
which took a few days to get right.


#7

AWESOME!! Such good work, well done.
And for Number 5 you don’t need to know Classes, I’m not so crash hot at Object Oriented JS, but still managed… Do you want me to post my answers here??

Here’s my version of number 5 – it can surely be refactored!!!

function declareWinner(fighterA, fighterB, firstAttacker) {
  const args = Array.from(arguments);
  var fighters =  args.filter(arg => !arg[2]);  
  firstAttacker = fighters.find(fighter => fighter.name === firstAttacker);
  var secondAttacker = fighters.find(fighter => fighter.name != firstAttacker);

  do {
    secondAttacker.health = secondAttacker.health - firstAttacker.damagePerAttack;
    if (secondAttacker.health <= 0) {
      return firstAttacker.name;
    } else {
      firstAttacker.health = firstAttacker.health - secondAttacker.damagePerAttack;
    }
    if (firstAttacker.health <= 0) {
      return secondAttacker.name;
    }
  }
  while (secondAttacker.health >= 0 || firstAttacker.health >= 0 ); 
}

Thanks for contributing !!!


#8

Awesome thanks I wanna try that too! Oh man it looks hard though!


#9

I’m currently stuck on Snail. Seems like one of the coolest Kata I’ve attempted nonetheless. If anyone’s completed it and wants to drop a tip (but not a spoiler!) it would be much appreciated!


#10

The hard bit is working out all the edge cases where you have the same hand but you have to go to the highest card.


#11

I solved it, testing everything from a 3x3 to a 6x6 (1 - 36). I traced a given input, doing a loop in my head and realized if the array was even, it would be for example (array is 8 x 8, you’d go through it 12 times. If it’s 4 you’d go through it 6 times). If it was odd, for example 9 you’d still go through it 13 times. If it’s 5 you’d go through it 7 times.

So I did a greater loop that looked like

for (var entire = 0; entire < array.length + Math.floor(array.length / 2); entire++)

Within the entire loop I first pushed the entire first nested array into a variable result (which I created outside of the loop obviously). I then splice it from the array. Then I added two sepparate loops within entire, one called down and the other called up. In between I push the entire last nested array, reversed, into result. Then splice it out.

Down starts at 1 (array[0] has been pushed in its entirety and spliced away at this point) and goes up while it’s less than array.length - 1. For each index it pushes, then splices the last index of each nested array.

Then I push the final nested array, reversed into result and splice it away.

Then up starts at array.length - 1 and goes to 0. For each index, it pushes the first index of the nested array and splices it away.

I then concat result together with a reduce function.
In some cases (when the array was large and even), there ended up being an ‘undefined’ at the end, so I did an if statement testing whether ‘undefined’ was included in result. If so, I pop it away. Then return result.

Can anyone give me some TDD tests to put in?


#12

I saw poker hands the other day and it seemed pretty intense.


#13

I tried it using class Fighter, which can maybe show you how classes work (I’m at the part in Eloquent Js where it’s teaching some OOP)

function Fighter(name, health, damagePerAttack) {
        this.name = name;
        this.health = health;
        this.damagePerAttack = damagePerAttack;
}

function declareWinner(fighter1, fighter2, firstAttacker){
	var secondAttacker = firstAttacker === fighter1.name? fighter2 : fighter1;
	firstAttacker = firstAttacker === fighter1.name ? fighter1 : fighter2;
	while(firstAttacker.health > 0 && secondAttacker.health > 0){
		secondAttacker.health = secondAttacker.health - firstAttacker.damagePerAttack;
		if (secondAttacker.health <= 0){break;}
		firstAttacker.health = firstAttacker.health - secondAttacker.damagePerAttack;
	}
	return firstAttacker.health > 0 ? firstAttacker.name : secondAttacker.name;
}

#14

Yeah cool, I suppose my logic wasn’t far off yours at all, but using a Fighter Class makes more sense. Thanks for adding this :slight_smile:


#15

If you like playing poker, like I do, you will enjoy it. :slight_smile: :wink:
Plus I wanted to try one of the higher Kyu challenges.


#16

Hmm, quite a lot of detail - thanks but I think I’d have to call that a spoiler! I solved it before reading your answer in though so I’m claiming my points. From what you write it seems we did something similar but not the same.
I (spoiler alert!) created 4 loops to cover the 4 sides of the matrix, first extracting the values to a new array and then cutting the extracted values out; this way the matrix gets smaller and smaller and I just keep repeating the same 4 steps (extracting from the sides) until array.length == 0, at which point return.


#17

Yeah it sounds like a sweet problem, but I think 4kyu is a bit out of my league… I’m trying to get solid at 6kyu at the moment ! Have just finished my front end cert!!


#18

I enjoyed solving katas from kyu2 and 3. Katas from kyu > 3 were quite boring. If I feel like solving a simple challenge, I’d rather go for a clash at codingame.


#19

well done :slight_smile: that’s quite an achievement!


#20

Hey, I’d never heard of CodeWars but it’s pretty cool. Thanks for the link!