Hackerrank solutions: Python 3 and Perl 6 (part 2)

As a continuation of the previous part of this series, I will be continuing to work through some Hackerrank challenges for Python 3, and compare the solutions to how I would solve them in a language I'm more proficient in, Perl 6. In this post, I will work through some of the Python 3 string challenges from Hackerrank.

Raiph posted a comment on Reddit suggesting a slightly different layout, which I will be using for this post. Additional comments are always welcome as I try to improve the format.

Disclaimer

Once again I'd like to make clear I'm trying to stick to the original Hackerrank challenges by not using any imports not specifically used in the original challenge. If you have suggestions for Python 3 or Perl 6 modules to make a given task easier, I still appreciate them, but I won't update my solutions to use a module.

Challenges

String Split and Join

This challenge involves a string containing spaces, where the spaces are to be replaced with dashes (-) instead.

def split_and_join(line):
    return "-".join(line.split(" "))

I personally am not too fond that join takes a list of words to join together, whereas split takes a word to split with. It feels a little inconsistent. It also doesn't allow me to read the code logically from left to right.

sub split-and-join ($line) {
    $line.split(" ").join("-")
}

The Perl 6 solution to the challenge does the same as the Python variant. Even the function names are the same! The biggest difference is that I can chain the functions from left to right, leading to clearer code.

What's Your Name?

The next challenge is a simply string formatting task. You get two inputs, a first name and a last name, and have to put it in a string which will be printed to STDOUT.

def print_full_name(a, b):
    print("Hello %s %s! You just delved into python." % (a, b))

Before you begin, I know this can be done using f"" strings, and that was my first attempt to use as well. However, Hackerrank did not accept this, complaining about invalid syntax, so I assume they're running an older Python 3 than I do.

That said, this is a simple printf formatted string, which then accepts a tuple of arguments to put into the string. printf formatted string are very powerful in their possibilities, and it's clear to read.

sub print-full-name($a, $b) {
    say "Hello $a $b! You just delved into Perl 6."
}

Perl 6 has double-quote semantics that many people may be familiar with from other languages. When you insert a variable in a double-quoted string, it's .Str value will be used. That is to say, the value will be converted to a Str if required, and then put into the string.

If you need it or want it for clarity, you can also use "Hello {$a}" in Perl 6, allowing you to use it similarly to Python 3's f"" strings.

Mutations

You are given a string string, an integer position and a character character. In string, replace the character at position to the given character. The position is counted from starting point 0, so I don't have to think about differences between what a human or computer considers to be position 1 in a string.

def mutate_string(string, position, character):
    chars = list(string)
    chars[position] = character

    return "".join(chars)

This is basically what the example showed as well that came with the challenge, so wasn't too hard to solve. My only complaint was that I couldn't call my list "list", because that's a reserved keyword.

sub mutate-string ($string, $position, $character) {
    my @list = $string.comb;
    @list[$position] = $character;

    @list.join;
}

The Perl 6 variant does the same things as the Python variant. comb without arguments converts a string to a list of characters, and join without arguments joins a list together to a string.

Find a String

In the following challenge you are given a string string, and a substring sub_string. The challenge is to find how often a substring occurs in the string. The substrings may overlap one another, so the string "ABCDCDC" contains the substring "CDC" twice.

def count_substring(string, sub_string):
    count = 0

    for i in range(0, len(string)):
        if string[i:i + len(sub_string)] == sub_string:
            count += 1

    return count

As solution to this challenge I loop through the entire string, and check whether it contains the sub_string at that point. If it does, I increment count by 1. Now, I learned that Python also has the inline if, just like Perl 6 does, however, it also needs an else block. That put me off from using it in this situation. I think it puts me off from using it in most situations, actually. With an else coming after it, it just becomes messy to read, in my opinion.

sub count-substring ($string, $sub-string) {
    elems $string ~~ m:overlap/$sub-string/
}

The Perl 6 version makes use of some regex magic, and the elems subroutine. elems returns the number of elements in a list, which in this case would be the number of matches found by the regex. The m:overlap// makes a regex to match, with overlapping strings.

String Validators

In the following challenge, the program is given a string s, and have to validate a number of properties on this string. These are, in order, whether they contain

  • alphanumeric characters (a-z, A-Z or 0-9),
  • alphabetic characters (a-z or A-Z),
  • digits (0-9),
  • lowercase characters (a-z),
  • uppercase characters (A-Z).

If any character in the given string passes a validation, it must print "True", otherwise it must print "False".

if __name__ == '__main__':
    s = input()

    checks = {
        "alnum": False,
        "alpha": False,
        "digit": False,
        "lower": False,
        "upper": False
    }

    for char in list(s):
        if not checks["alnum"] and char.isalnum():
            checks["alnum"] = True

        if not checks["alpha"] and char.isalpha():
            checks["alpha"] = True

        if not checks["digit"] and char.isdigit():
            checks["digit"] = True

        if not checks["lower"] and char.islower():
            checks["lower"] = True

        if not checks["upper"] and char.isupper():
            checks["upper"] = True 

    keys = list(checks.keys())
    keys.sort()

    for key in keys:
        print(checks[key])

As stated in the disclaimer, I don't want to make use of any import statements unless these are explicitly given in the original challenges. This means I can't use regexes, as these are stuffed away in the re packages in Python. Luckily, Python has the correct check available as a method on the string object, so I can still check them in a single line.

I first tried to call the methods on s directly, but this seemed to require the entire string to match the check, instead of just any character in the string. So I had to loop through the string by character, which I did. If any character is found to validate, the appropriate key in the checks dict will be set to True. Once I've walked through the entire string, I sort the keys from checks so I can be sure they're printed in the right order.

sub MAIN {
    my $s = $*IN.get;

    say so $s ~~ /<alnum>/;
    say so $s ~~ /<alpha>/;
    say so $s ~~ /<digit>/;
    say so $s ~~ /<lower>/;
    say so $s ~~ /<upper>/;
}

Perl 6 does have regexes available in the main namespace by default, so that made this challenge a lot easier to work with. $*IN in a special variable that refers to STDIN, and the .slurp method reads all remaining data from the buffer.

The next 5 lines all do a say, which acts like print in Python 3. The so function coerces a value to become a Bool. When a Bool is given to say, it will be coerced to a string representation again, and become either "True" or "False". The smartmatch operator ~~ has already been covered in the previous post, so I recommend you read that as well if you haven't yet.

In Perl 6, regexes are (usually) delimited by the / character. The <alnum>, <alpha> etcetera parts are predefined character classes in Perl 6 regexes. These check for exactly what we need in the challenges, so were a good pick to solve them.

Text Wrap

You are given a string s and a width w. The string should be split over multiple lines so it is never more wide than w.

import textwrap

def wrap(string, max_width):
    return "\n".join(textwrap.wrap(string, max_width))

This challenge introduces the first Python module: textwrap. This makes the challenge very easy to solve as well, using the wrap function exposed by the module. This function makes a list of strings, each no longer than the given width. I then join these together with newlines to get the desired output.

sub wrap ($string, $width) {
    $string.comb($width).join("\n")
}

For the Perl 6 solution, I have not used an additional module, as all the functionality are in the core namespace. I actually made a module in Perl 6 for a less primitive wrapping functionality, called String::Fold.

In this solution, I use comb with the $width argument. This returns a list of strings, each no longer than the given width, just like Python's textwrap.wrap. I can then join these together with newlines as well to get the same result.

Designer Door Mat

This challenge is more complex than previous challenges. The task at hand is to "draw" a certain "design" as the output. For the input, you are given both a height y and a width x, however x must always be y × 3, so you can ignore the second argument.

This one is much simpler to explain using two examples. The first example is the output if the input were 7 21.

---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------

In the second example, the input is 11 33.

---------------.|.---------------
------------.|..|..|.------------
---------.|..|..|..|..|.---------
------.|..|..|..|..|..|..|.------
---.|..|..|..|..|..|..|..|..|.---
-------------WELCOME-------------
---.|..|..|..|..|..|..|..|..|.---
------.|..|..|..|..|..|..|.------
---------.|..|..|..|..|.---------
------------.|..|..|.------------
---------------.|.---------------
#! /usr/bin/env python3

height = int((input().split())[0])
width = height * 3
half = int((height - 1) / 2)

# Top half
for line in range(1, half + 1):
    non_dashes = ((line * 2) - 1)
    dashes = int((width - (non_dashes * 3)) / 2)

    print("%s%s%s" % ("-" * dashes, ".|." * non_dashes, "-" * dashes))

# Middle line
print("%s%s%s" % (
    "-" * (int(width / 2) - 3),
    "WELCOME",
    "-" * (int(width / 2) - 3)
))

# Lower half
for line in range(half, 0, -1):
    non_dashes = ((line * 2) - 1)
    dashes = int((width - (non_dashes * 3)) / 2)

    print("%s%s%s" % ("-" * dashes, ".|." * non_dashes, "-" * dashes))

I split the code up in a top half, middle line and lower half, to make it easier to reason about. The for loops contain some logic to get the right output on every line. I found out that range supports a third argument, allowing me to count down with it as well, which was perfect for this situation.

#! /usr/bin/env perl6

my $height = $*IN.slurp.words.head.Int;
my $width = $height × 3;
my $half-height = ($height - 1) ÷ 2;

# Top half
for 1..$half-height {
    my $non-dashes = ($_ × 2) - 1;
    my $dashes = ($width - ($non-dashes × 3)) ÷ 2;

    say "{"-" x $dashes}{".|." x $non-dashes}{"-" x $dashes}";
}

# Middle line
say "{"-" x (($width ÷ 2) - 3)}WELCOME{ "-" x (($width ÷ 2) - 3)}";

# Lower half
for (1..$half-height).reverse {
    my $non-dashes = ($_ × 2) - 1;
    my $dashes = ($width - ($non-dashes × 3)) ÷ 2;

    say "{"-" x $dashes}{".|." x $non-dashes}{"-" x $dashes}";
}

As usual, the code is functionally the same. I must admit I like the functional style to get an Int from the first argument much more than the way I do it in Python, though.

A thing I learned is that the .. operator that generates a sequence does not have a way to make a sequence that counts down, so I had to use .reverse on a sequence that counts up. I had expected this to Just Work as I expected and count down if the left hand side would be larger than the right hand side.

You may notice some fancy Unicode characters in the source, namely × for multiplication, and ÷ for division. Perl 6 allows Unicode characters in the source files, which can oftentimes lead to prettier code. In this particular instance, there's no big difference in code readability, though. And for those who don't yet have a modern editor that can make Unicode characters, do not worry, as the ASCII equivalents (* and / respectively) still work as well.

String Formatting

In this challenge, you are to produce a table with four columns. The columns should contain the decimal, octal, hexadecimal and binary values of the row numbers. The function receives an int number. The table should contain that many rows, starting with row number 1.

def print_formatted(number):
    max_width = len("{0:b}".format(number))

    for i in range(1, number + 1):
        decimal = "{0}".format(i).rjust(max_width)
        octal = "{0:o}".format(i).rjust(max_width)
        hexadecimal = "{0:x}".format(i).upper().rjust(max_width)
        binary = "{0:b}".format(i).rjust(max_width)

        print("%s %s %s %s" % (decimal, octal, hexadecimal, binary))

In the Python 3 solution I first calculate the max width I need to take into account. Then I loop from 1 until number to get the right amount of rows. Each iteration, I format the number correctly, and then print it out using a printf format string.

The hardest part of this challenge was to get formatting right the way Hackerrank wanted it. But I guess that was the entire point of the challenge.

sub print-formatted ($number) {
    my $max-width = $number.base(2).Str.chars;
    my $format-string = ("%{$max-width}s" xx 4).join(" ") ~ "\n";

    for 1..$number {
        $format-string.printf($_, $_.base(8), $_.base(16), $_.base(2));
    }
}

The Perl 6 solution starts of the same, in that it first calculates the max width I need to take into account. Next, however, I generate the format string using the $max-width to make the printf subroutine pad it for me. The xx operator makes a total of 4 such strings, and puts them into a list, which I can then join together with a space character, and add a \n at the end of it (the ~ operator is for string concatenation).

I'm assuming something similar is possible in Python 3 as well, and I would like to have an example so I can compare it more fairly.

In the Perl 6 solution I am also able to make use of the base method to convert the numbers into the right base, something I could not find for Python 3.

Wrap-up

This time I did not do all of the challenges, as the post would probably get too long. I still did 8 of them, and might do the rest of the string challenges in a later part anyway.

I still find Perl 6 to produce much cleaner code, which is shown best with the first challenge. In Perl 6 ($line.split(" ").join("-")), I can read from left to right to see what I'm doing: I have a $line, which I split, and then join. In the Python variant ("-".join(line.split(" "))), it is much less clear what the actual item I'm working on is, as it's hidden inbetween the join and split calls.

Of course, I'm still not an expert on Python 3 code, so I'm sure that there are many parts that could be written in a cleaner fashion. I'm still open for feedback to improve my Python 3 skills (hence I'm publishing these posts), so please let me know if you know better ways to solve some challenges.