summaryrefslogtreecommitdiff
path: root/src/_posts/2018-10-11-hackerrank-solutions-python3-and-perl6-part-2.html
blob: 60a9a75a81767f0857995e7a26fef8668416de73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
---
title: "Hackerrank solutions: Python 3 and Perl 6 (part 2)"
layout: language-war
tags: Hackerrank Perl6 Python Python3 Programming Raku
description: >
  A number of solutions to Hackerrank challenges in both the Python 3 and the
  Perl 6 programming languages. This is the second part of the series, and will
  work through the subdomain of Strings.
---

{% markdown %}
# Hackerrank solutions: Python 3 and Perl 6 (part 2)
{% endmarkdown %}

{% markdown %}
As a continuation of the [previous
part](/post/2018/09/13/hackerrank-solutions-python3-and-perl6-part-1/) 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](https://www.reddit.com/r/perl6/comments/9ffc2p/hackerrank_solutions_python_3_and_perl_6_part_1/e5xml3m)
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.
{% endmarkdown %}

{% admonition_md 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.
{% endadmonition_md %}

{% markdown %}
## Challenges
{% endmarkdown %}

<div class="language-war">
    <div class="language-announcer">

{% markdown %}
### String Split and Join

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

    </div>
    <div class="language-arena">
        <div class="language-challenger">
            <div class="language-code">

{% highlight python3 tio=https://tio.run/##K6gsycjPM/7/PyU1TaG4ICezJD4xLyU@Kz8zTyMnMy9V04pLAQiKUktKi/IUlHSV9OBSemDlGkoKSpqa/wuKMvNKNNAMyMwrKC3R0ARKp@XnKyQlFgFxFQA %}
def split_and_join(line):
    return "-".join(line.split(" "))
{% endhighlight %}

            </div>
            <div class="language-commentary">

{% markdown %}
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.
{% endmarkdown %}

            </div>
        </div>
        <div class="language-defender">
            <div class="language-code">

{% highlight perl6 tio=https://tio.run/##K0gtyjH7/7@4NEmhuCAns0Q3MS9FNys/M09BQyUnMy9VU6GaSwEIwBw9sBINJQUlTT2QGg0lXSVNrtr/KlqefnrFOaVFBXpqqKboFSdW/k/Lz1dISiwC4ioA %}
sub split-and-join ($line) {
    $line.split(" ").join("-")
}
{% endhighlight %}

            </div>
            <div class="language-commentary">

{% markdown %}
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.
{% endmarkdown %}

            </div>
        </div>
    </div>
    <div class="language-announcer">

{% markdown %}
### 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`.
{% endmarkdown %}

    </div>
    <div class="language-arena">
        <div class="language-challenger">
            <div class="language-code">

{% highlight python3 tio=https://tio.run/##XYzBCsIwEETv/YqxUEigePHmD@jZm6cSyRYj22ww2UK/PobqyWEOAzPz0laeEk@1epqR3iGWaVbmKbqFjBvxsOcOTXtl@isxC4bcfMBdFC/NBZ54JY@2EKQdeOwx4Pu39R8bYtJi7IhfsPWiwUu3uoib5KzLBw %}
def print_full_name(a, b):
    print("Hello %s %s! You just delved into python." % (a, b))
{% endhighlight %}

            </div>
            <div class="language-commentary">

{% markdown %}
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.
{% endmarkdown %}

            </div>
        </div>
        <div class="language-defender">
            <div class="language-code">

{% highlight perl6 tio=https://tio.run/##K0gtyjH7/7@4NEmhoCgzr0Q3rTQnRzcvMTdVQyVRR0ElSVOhmksBCIoTKxWUPFJzcvIVVBKB4ooKkfmlClmlxSUKKak5ZakpCkDd@QoBQPMUzPSUuGr/Y5in5emnl55aAjQVytL875NYVFTJFZ6YkwMA %}
sub print-full-name($a, $b) {
    say "Hello $a $b! You just delved into Perl 6."
}
{% endhighlight %}

            </div>
            <div class="language-commentary">

{% markdown %}
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.
{% endmarkdown %}

            </div>
        </div>
    </div>
    <div class="language-announcer">

{% markdown %}
### 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.
{% endmarkdown %}

    </div>
    <div class="language-arena">
        <div class="language-challenger">
            <div class="language-code">

{% highlight python3 tio=https://tio.run/##VY5BC8IwDIXv/RVhpxaKIN4Ef4mIjNm5iCYlTQ/79bXrpmIuyeN975E468R0KOUWRnhl7TVckwrS3a7LQ@SEikwehqmXftAg7migzqITnOCJSTfc/YzzJ3ipyDdqGiBBsxB03e7BSLbxrsTaoPb/C6SY1ToPi7WJptbLlZHZ7I28AQ %}
def mutate_string(string, position, character):
    chars = list(string)
    chars[position] = character

    return "".join(chars)
{% endhighlight %}

            </div>
            <div class="language-commentary">

{% markdown %}
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.
{% endmarkdown %}

            </div>
        </div>
        <div class="language-defender">
            <div class="language-code">

{% highlight perl6 tio=https://tio.run/##K0gtyjH7/7@4NEkht7QksSRVt7ikKDMvXUFDBcLQUVApyC/OLMnMzwMykzMSixKTS1KLNBWquRSAILdSwSEns7hEwVYBqkEvOT83yRosCZaJhuuPBSmCm2DNhVCjl5WfmWfNVfsfxQ0aKlqefnrpqSVAizFYmnrFiZX/0/LzuQy5igA %}
sub mutate-string ($string, $position, $character) {
    my @list = $string.comb;
    @list[$position] = $character;

    @list.join;
}
{% endhighlight %}

            </div>
            <div class="language-commentary">

{% markdown %}
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.
{% endmarkdown %}

            </div>
        </div>
    </div>
    <div class="language-announcer">

{% markdown %}
### 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.
{% endmarkdown %}

    </div>
    <div class="language-arena">
        <div class="language-challenger">
            <div class="language-code">

{% highlight python3 tio=https://tio.run/##XU67CsMwDNz9FTfaxENKt4CHPv4ilNCHkwqKbBx76Ne7bhxIqYSQdNzp5N/x6Xif88OOuLvEcZjTbY6BeJK1aRRkqLPqBEosRBi0YllHF0AgRrjyZGWr8bK8qtUq@QaNqGBPHaGprO32Bcb8eG26zbIx2FXPYGMKXOHsCz/K//eJfYpSaayDUvlwPJ1LilIf %}
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
{% endhighlight %}

            </div>
            <div class="language-commentary">

{% markdown %}
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.
{% endmarkdown %}

            </div>
        </div>
        <div class="language-defender">
            <div class="language-code">

{% highlight perl6
tio=https://tio.run/##K0gtyjH7/7@4NEkhOb80r0QXyCouKcrMS1fQUIEwdBRUgIK6EI6mQjUXZ2pOam6xAlRaoa5OIdcqvwxoUGKBPpJSfa7a/2hmaqhoefrppaeWAM2EsjT1ihMr/zs6ObsAIRcQAwA %}
sub count-substring ($string, $sub-string) {
    elems $string ~~ m:overlap/$sub-string/
}
{% endhighlight %}

            </div>
            <div class="language-commentary">

{% markdown %}
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
*m*atch, with *overlap*ping strings.
{% endmarkdown %}

            </div>
        </div>
    </div>
    <div class="language-announcer">

{% markdown %}
### 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"`.

{% endmarkdown %}

    </div>
    <div class="language-arena">
        <div class="language-challenger">
            <div class="language-code">

{% highlight python3 tio=https://tio.run/##hZLNioMwEIDvPsXQPdTAIrS9CR72sk@wt1JCVtMaqklIIqWUPrubn6p1MZib38x8zmQi76YW/ND37AwYc9JSjKEoYItxSxjHeJsnYI@GAhiXnUlR4kFZ0/Lq6MN/urMhDe/aTQ7fpNH0853Lmizwil2YWeCNuFG1wDspJ@7xMzRzFso2RJRtERqmTapRPpbZybgwr4aPryZPQHjlazKmPUrfSqYBp/wCflRHk7jWzfhPa1FcG/JXtOGKZlqPYtohf0Ubbnim9SimHfJXtGFBM61HMe2QH7QQvFd6d@/K7zEkZg6lCI3hTAs1vkS3fAvd7l1s@pNUjA@Kow2dUN9//ZbVbn/4@AM %}
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])
{% endhighlight %}

            </div>
            <div class="language-commentary">

{% markdown %}
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.
{% endmarkdown %}

            </div>
        </div>
        <div class="language-defender">
            <div class="language-code">

{% highlight perl6
tio=https://tio.run/##K0gtyjH7/7@4NEnB19HTT6GaSwEIcisVVIoVbBVUtDz99NJTS6y5wMLFiZUKxfkgqbo6BX2bxJy80lw7fWvscgUZiTjkUjLTM0twyOXkl6cW4ZArLSiAyNX@/@@YlJxiaGSsDAA %}
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>/;
}
{% endhighlight %}

            </div>
            <div class="language-commentary">

{% markdown %}
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][classes] in Perl 6
regexes. These check for exactly what we need in the challenges, so were a good
pick to solve them.

[classes]: https://docs.perl6.org/language/regexes.html#Predefined_character_classes
{% endmarkdown %}

            </div>
        </div>
    </div>
    <div class="language-announcer">

{% markdown %}
### 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_.
{% endmarkdown %}

    </div>
    <div class="language-arena">
        <div class="language-challenger">
            <div class="language-code">

{% highlight python3 tio=https://tio.run/##K6gsycjPM/7/PzO3IL@oRKEktaKkvCixgIsrJTVNAcTSKC4pysxL11HITayIL89MKcnQtOJSAIKi1JLSojwFpZg8Jb2s/Mw8DZhePRzaNP8XAIVKNMDSmXkFpSUamjoKIBEoBwj@Ozo5u7i6uXt4enn7@Pr5BwQGBYeEhoVHREZxmQIA %}
import textwrap

def wrap(string, max_width):
    return "\n".join(textwrap.wrap(string, max_width))
{% endhighlight %}

            </div>
            <div class="language-commentary">

{% markdown %}
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.
{% endmarkdown %}

            </div>
        </div>
        <div class="language-defender">
            <div class="language-code">

{% highlight perl6 tio=https://tio.run/##K0gtyjH7/7@4NEmhvCixQEFDpbikKDMvXUdBpTwzpSRDU6GaSwEIoMJ6yfm5SRpQKb2s/Mw8DaWYPCVNrtr/IO0aKlqefnrpqSVA7VCWnmdeiaZecWLlf0cnZxdXN3cPTy9vH18//4DAoOCQ0LDwiMgoLlMA %}
sub wrap ($string, $width) {
    $string.comb($width).join("\n")
}
{% endhighlight %}

            </div>
            <div class="language-commentary">

{% markdown %}
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`][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.

[string::fold]: https://modules.perl6.org/dist/String::Fold:cpan:TYIL
{% endmarkdown %}

            </div>
        </div>
    </div>
    <div class="language-announcer">

{% markdown %}
### 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`.
{% endmarkdown %}

{% highlight text %}
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
{% endhighlight %}

{% markdown %}
In the second example, the input is `11 33`.
{% endmarkdown %}

{% highlight text %}
---------------.|.---------------
------------.|..|..|.------------
---------.|..|..|..|..|.---------
------.|..|..|..|..|..|..|.------
---.|..|..|..|..|..|..|..|..|.---
-------------WELCOME-------------
---.|..|..|..|..|..|..|..|..|.---
------.|..|..|..|..|..|..|.------
---------.|..|..|..|..|.---------
------------.|..|..|.------------
---------------.|.---------------
{% endhighlight %}

    </div>
    <div class="language-arena">
        <div class="language-challenger">
            <div class="language-code">

{% highlight python3 tio=https://tio.run/##zZDNS8MwGMbv@SseMwbJlrbrehCEnWS3DS@CBxGpNFsCJQ1p5xD832s@Nh2I7GpyCHmf3/u8H/ZjUJ2pxnFyg@LQu@JNm0Kad9gkEKKk3qsBK2gzMKaNPQyM571ttX/58@KFk6NuBuWJEzpDRVTd7s45p3CGkqPAkhMywWNnERiy6xxabaRH4Wqzl6wUUcHc83cE/pjOvDZ1r2TvLRmL@MwbRctIfKuxYOonA7tI9E3xc/mQYV1A6bQPl2IKRjPqqYQL0PwzD/8fCx@7IHgcY6ubppVxAPLbMRZKSSxoqa8idV5xkfSn9eb@Ybum4gpOUslNd5Tur92FsMBCIPvfuxvHWyzLLw %}
#! /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))
{% endhighlight %}

            </div>
            <div class="language-commentary">

{% markdown %}
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.
{% endmarkdown %}

            </div>
        </div>
        <div class="language-defender">
            <div class="language-code">

{% highlight perl6 tio=https://tio.run/##zZBBTsMwEEX3PsXgdpEgxVFSqSwiVqiLSi1skFiiIE@xpeBEdtpQhZyDo3CAHix0UpKGSuzxypp5/8/XL9Bm87adXEG4dTZ80SZEs4OCxoy97WGqUL@qEm5her28Fy7b2kJUuZVOKEylWJoy6bhKy1IR9iM4fMLstFFptgkGG68HAoh8OHxBnDA2gce8AALZJrcQCfFLVTM4PvIyuQlk6hS6zuqZzsQ@eSUDM9qfQgXH30hIyfzhNKlcugde84DDey9vai4@RDc4S5sLhiesofBrLWWGkGmDbOzl9QnoFqWc@c3TYnX3sF7U8DfCu0ZWeYX23Il3UYovLO7QOvzX7bTtDcTRNw %}
#! /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}";
}
{% endhighlight %}

            </div>
            <div class="language-commentary">

{% markdown %}
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.
{% endmarkdown %}

            </div>
        </div>
    </div>
    <div class="language-announcer">

{% markdown %}
### 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.
{% endmarkdown %}

    </div>
    <div class="language-arena">
        <div class="language-challenger">
            <div class="language-code">

{% highlight python3 tio=https://tio.run/##jZFNCoMwEIX3nmIQhIQG0XYn9CwSTawpJoYYqVJ6dht/W7uQDlll3vveTKJ7W9bqMgyMF6CNUDYtaiOptZwh1cqMG5x44ErSLn0IZku4QsUV8p9Rkr38cJavWuxNYncJAoQCQ9WNo5jA3IcTxAtvLMZzIWnliI72YQkcmnvbWLRl4s1S53Y1JPV/lpJ3dJeUdDtjqzU36ACQCUVNv3iz49DNND0m8oMG1uNDAGgZhMyLkO/hyBKE8fD7EyNKKN1ahF33HL0B %}
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))
{% endhighlight %}

            </div>
            <div class="language-commentary">

{% markdown %}
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.
{% endmarkdown %}

            </div>
        </div>
        <div class="language-defender">
            <div class="language-code">

{% highlight perl6 tio=https://tio.run/##XY9NCsIwEIX3PcUQIiRiB1tEBPEA3bhxK0iq0VZMWpIUK1KvXmNb/HurGWbeN29KaS7ztrVVCqXJtQuPhVHCOXkARnWlUmk43APwUjegStThNT@4DFYwjDEVVrKY48YZ3GfC2OV7vYeF1nn0yVsYGd0/jMYSqGuYcTwXuWYECIcHkK0my6BDeDtEiMOhIcZLv2Dskh8Z3U2A7vo8C/6po/lXE3Pe52uCpv17mdFxssaTdJhox9t4@gQ %}
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));
    }
}
{% endhighlight %}

            </div>
            <div class="language-commentary">

{% markdown %}
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.
{% endmarkdown %}

            </div>
        </div>
    </div>
</div>

{% markdown %}
## 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.
{% endmarkdown %}