Code kata of the week

Here a collection of katas extracted from exercises and coding interviews.

Kata 01

Source: Qualified.io

Write a function that receives two strings and returns the number of characters we would need to rotate the first string forward to match the second.

For instance, take the strings “fatigue” and “tiguefa”. In this case, the first string can be rotated 5 characters forward to produce the second string, so 5 would be returned. Here are the step:

no rotations: “fatigue”

1st rotation: “efatigu”

2nd rotation: “uefatig”

3rd rotation: “guefati”

4th rotation: “iguefat”

5th rotation: “tiguefa”

If the second string isn’t a valid rotation of the first string, the method should return -1.

Specification

Challenge.shiftedDiff(first,second)

computes the number of rotations to make string first equal to string second, if possibile

Parameters

first: String - string to be rotated

second: String - target string to be matched by rotating first

Return Value

int - the number of rotations. If not possibile it returns -1.

Kata 02

Source: Qualified.io

You are provided a string containing a list of positive integers separated by a space ( " " ). Take each value and calculate the sum of its digits, which we call it’s “weight”. Then return the list in ascending order by weight, as a string joined by a space.

For example 99 will have “weight” 18, 100 will have “weight” 1 so in the output 100 will come before 99.

Specification

Challenge.orderWeight(input)
Parameters

input: String - a string containing a list of positive integers separated by a space ( " " ).

Return Value

String - a string with the values ordered by weight.

Constraints

All numbers in the list are positive integers and the list can be empty.

Examples
  • “56 65 74 100 99 68 86 180 90” ordered by numbers weights becomes:

    “100 180 90 56 65 74 68 86 99”

    When two numbers have the same “weight”, let’s consider them to be strings and not numbers: 100 is before 180 because its “weight” (1) is les than the one of 180 (9) an 180 is before 90 since, having the same “weight” (9) it comes before as a string.

Kata 03: Find routes

Source: Qualified.io

We are tracking down our rogue agent and she travels from place to place to avoid being tracked. Each of her travels are based on a list of itineraries in an unusual or incorrect order. The task is to determine the complete route she will take.

You are given an array of routes containing her travel itineraries. Convert this into a complete, in-order list of the places she will travel.

Specification

Challenge.findRoutes(routes)
Parameters

routes: List<String[]> - array of itineraries

Return Value

String - An ordered list or destinations

Constraints

All inputs have at least one valid, complete route

Examples
  • routes: [[“USA”,“BRA”],[“JPN”,“PHL”],[“BRA”,“UAE”],[“UAE”,“JPN”]]

    return: “USA, BRA, UAE, JPN, PHL”

Comments

comments powered by Disqus