codewars Highest and Lowest Vowel Count Ones and Zeros Disemvowel Trolls Find the odd int Who likes it? Bit Counting Playing with digits

codewars

Highest and Lowest

In this little assignment you are given a string of space separated numbers, and have to return the highest and lowest number.

Example:

1
2
3
Kata.HighAndLow("1 2 3 4 5"); // return "5 1"
Kata.HighAndLow("1 2 -3 4 5"); // return "5 -3"
Kata.HighAndLow("1 9 3 4 -5"); // return "9 -5"

Notes:

  • All numbers are valid Int32, no need to validate them.
  • There will always be at least one number in the input string.
  • Output string must be two numbers separated by a single space, and highest number is first.
1
2
3
4
5
sub high-and-low($str) {
with $str.words».Int {
"{.max} {.min}"
}
}

Vowel Count

Return the number (count) of vowels in the given string.

We will consider a, e, i, o, and u as vowels for this Kata.

The input string will only consist of lower case letters and/or spaces.

1
2
3
sub vowel-count($str) {
[+] gather .value.take for $str.comb.Bag.grep: {.key ∈ ['a','e','i','o','u']}
}

or

1
2
3
sub vowel-count($str) {
[+] $str.comb.Bag.{"a","e","i","o","u"}
}

or use trans method:

1
2
3
sub vowel-count($str) {
with $str { .chars - .trans(/<[aeiou]>/ => '').chars }
}

Ones and Zeros

Given an array of one’s and zero’s convert the equivalent binary value to an integer.

Eg: [0, 0, 0, 1] is treated as 0001 which is the binary representation of 1.

Examples:

1
2
3
4
5
6
7
8
Testing: [0, 0, 0, 1] ==> 1
Testing: [0, 0, 1, 0] ==> 2
Testing: [0, 1, 0, 1] ==> 5
Testing: [1, 0, 0, 1] ==> 9
Testing: [0, 0, 1, 0] ==> 2
Testing: [0, 1, 1, 0] ==> 6
Testing: [1, 1, 1, 1] ==> 15
Testing: [1, 0, 1, 1] ==> 11

However, the arrays can have varying lengths, not just limited to 4.

1
2
3
sub ones-and-zeros(@a) {
:2(@a.join())
}

or

1
2
3
4
sub ones-and-zeros(@a) {
my $str = @a.join;
:2(<<$str>>)
}

Disemvowel Trolls

Trolls are attacking your comment section!

A common way to deal with this situation is to remove all of the vowels from the trolls’ comments, neutralizing the threat.

Your task is to write a function that takes a string and return a new string with all vowels removed.

For example, the string “This website is for losers LOL!” would become “Ths wbst s fr lsrs LL!”.

Note: for this kata y isn’t considered a vowel.

1
2
3
sub remove-vowel($str) {
$str.trans: <[aeiouAEIOU]> => ''
}

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.

sub find-ood(@a) {

}

Who likes it?

You probably know the “like” system from Facebook and other pages. People can “like” blog posts, pictures or other items. We want to create the text that should be displayed next to such an item.

Implement a function likes :: [String] -> String, which must take in input array, containing the names of people who like an item. It must return the display text as shown in the examples:

1
2
3
4
5
likes [] // must be "no one likes this"
likes ["Peter"] // must be "Peter likes this"
likes ["Jacob", "Alex"] // must be "Jacob and Alex like this"
likes ["Max", "John", "Mark"] // must be "Max, John and Mark like this"
likes ["Alex", "Jacob", "Mark", "Max"] // must be "Alex, Jacob and 2 others like this"

For 4 or more names, the number in and 2 others simply increases.

1
2
3
4
5
6
7
8
sub who-likes-it(@a) {
given @a {
when .elems == 0 { "no one likes this" }
when 1 <= .elems <= 2 { @a.join(" and ") ~ "like this" }
when .elems == 3 { roundrobin(@a, [', ', ' and ']).flat.join() ~ ' like this' }
when .elems == 4 { @a[0] ~ ', ' ~ @a[1] ~ ' and 2 others like this' }
}
}

Bit Counting

Write a function that takes an integer as input, and returns the number of bits that are equal to one in the binary representation of that number. You can guarantee that input is non-negative.

Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case

1
2
3
sub bit-counting($n) {
$n.base(2).comb.Bag.{"1"}
}

Playing with digits

Some numbers have funny properties. For example:

89 –> 8¹ + 9² = 89 1
695 –> 6² + 9³ + 5⁴= 1390 = 695
2
46288 –> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51

Given a positive integer n written as abcd… (a, b, c, d… being digits) and a positive integer p we want to find a positive integer k, if it exists, such as the sum of the digits of n taken to the successive powers of p is equal to k * n. In other words:

Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + …) = n * k

If it is the case we will return k, if not return -1.

Note: n, p will always be given as strictly positive integers.

1
2
3
4
digPow(89, 1) should return 1 since 8¹ + 9² = 89 = 89 * 1
digPow(92, 1) should return -1 since there is no k such as 9¹ + 2² equals 92 * k
digPow(695, 2) should return 2 since 6² + 9³ + 5⁴= 1390 = 695 * 2
digPow(46288, 3) should return 51 since 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51