Hackerrank solutions: Python 3 and Perl 6 (part 1)
tyil.nl/post/2018/09/13/hackerrank-solutions-...Posted Sep 13, 2018, 5:34 AM UTC
Posted Sep 13, 2018, 5:34 AM UTC
I love this post. I am still trying wrap my head around the mini maxi. Keep 'em coming!
Naturally, I wanted to use a reduce function, but Python 3 does not support these.
from functools import reduce
For some reason, this was not needed in Python 2, and other functions like map are still available in the global namespace. A very odd choice from the Python team to do it this way, in my opinion.
map is not pythonic, use list comprehension instead.
You know you're talking to a Perl programmer, right?
I'm fairly sure that a list comprehension is just map using a different syntax.
To me that is the same as saying to not use either of these:
my @b = @a.map:{ $_² if $_ %% 3 }
my @b = map { $_² if $_ %% 3 }, @a;
But to use this instead:
my @b = ( $_² if $_ %% 3 for @a );
I would call all of the above map, as they are mapping over the values of @a.
The biggest difference in Perl 6 is that the last one will run forever on an infinite list.
For something closer to Python ordering of parts:
my @b = ($_² for @a .grep: * %% 3)
Or if you want something that will work on infinite sequences, use map as if it was an infix operator. (You can pretend any subroutine is an infix operator if you surround it with [].)
my @b = *² [&map] @a .grep: * %% 3
I will point out that in Perl 6, I would recommend using the method form as it is easier to add operations to
my $b = @a.grep(*%%3).map(*²).head(5).sum;
This sets up a sequence pipeline that doesn't do any work until the .sum call at the end.
I mean you could write that as a single map with a .sum
my $b = @a.map({
next unless $_ %% 3; # grep( * %% 3 )
last if ++$ > 5; # head( 5 )
$_²
}).sum
Or even combine in the .sum
my $b = do {
my $sum = 0;
@a.map:{
next unless $_ %% 3; # grep( * %% 3 )
last if ++$ > 5; # head( 5 )
$sum += $_²
}
$sum
}
Neither of which are compatible with .hyper or .race.
my $b = @a.hyper.grep(*%%3).map(*²).head(5).sum;
my $b = @a.hyper.map({$_² if $_ %% 3}).head(5).sum;
Yes, of course. I never said otherwise. I'm just saying list comprehension is the preferred way to use map in python. I.e. most of the community prefers list comprehension over writing map with a lambda.
For questions like "A very big sum"
There's no value in just adding the values... The whole point is to learn how to implement biginteger addition. Never understood people who do this. I see the same thing with questions like "Implement mergesort" or, do insertion sort on a linked list, where people just call library functions. Sure that solves the question, but that doesn't teach you anything
The point of a higher level language is that I do not need to keep thinking about such issues. If I did the challenges in C, I would have to keep it in mind.
That might be the point of the higher level languages, what I'm saying is that's not the point of the problem.
If you got that on an interview and solved it like this, you would be told to redo it the way it was intended to be done.
If you got that on an interview and solved it like this, you would be told to redo it the way it was intended to be done.
If a company wanted me to do extra effort for something a language fixes by design, I don't think it'd be a company where I would want to work, so I don't think that'll be an issue. There's no point in using a higher level language if you don't use any of its features.
But instead of telling me that I'm doing it absolutely wrong, I'd be more interested in you telling me how I should have done it instead.
By looking at the HackerRank question, they expect you to return an integer, so I guess your way is fine, usually this question would give you two numbers - either a LL representation, or a string representation, and would want you to return the sum of them in the respective representation.
I'm not quite sure what the point of the question on hackerrank is, as they explicitly tell you to use a type which is larger than an int to compute the result, and based on their input restrictions, it'll always be big enough to hold the result.
https://leetcode.com/problems/add-two-numbers/description/ for a LL example
https://leetcode.com/problems/add-binary/description/ for a string example
It's kind of the same thing as using itertools for generating combinations/permutations. Sure you can do it, but does that show that you understand what is going on under the hood? Most interviewers would probably ask you to implement the methods themselves.
I hope Perl6 gets some traction and sponsorship that allow the developer to speed things up and solve outstanding issues like performance and lack of libraries. Perl6 got so many things right that I would really love to use it, but in the end the speed is so low and the ecosystem so poor that I always end up using Python (yes, it’s way slower even then Python).
There are about ~1200 modules in the Perl 6 ecosystem https://modules.perl6.org/ The core language itself is p. big too. Out of curiosity what's missing that you would need/want right now?
The fact that Python has 152,366 modules (https://pypi.org/) and npm 700.000 (https://www.npmjs.com/). This means that once I learn javascript / python and I want to experiment with something new or I have to solve a problem, some times 90% of the problem solving is installing 2 or 3 obscure modules that fit my edge case. This is reassuring.
Perl 6 can call Perl 5 modules, Python modules and call C libs.
When using Perl 6, you're not limited to CPAN OR PyPI, you have both at the same time and also CPAN6 on top, and everything written in C too.