You don't need the arbitrary join(':') after reverse() since sort() coerces the nested arrays to strings for you. That means you can avoid having to split again later.
19 yo student *and* a full-time developer.
Building with Rails currently, exploring new frontiers.
I enjoy functional programming, Linux, FOSS, refactoring and mentoring :)
Not much of a challenge, I'm afraid. Pretty straight forward split, split, join, sort and join again. A one liner in most modern languages, I suspect.
In c# it would look like this:
#!/usr/bin/perlusewarnings;usestrict;usefeatureqw{ say };my$in='Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill';sayjoin"",map"($_->[1], $_->[0])",sort{$a->[1]cmp$b->[1]||$a->[0]cmp$b->[0]}map[split/:/],split/;/,uc$in;
Lets do it:
It's pretty straightforward:
split(';')gets us allName:Surname.map(n=>n.split(':').reverse().join(':'))reverses them to beSurname:Name.sort()sorts them alphabetically.map(n=>(${n.split(':')[0]}, ${n.split(':')[1]}).toUpperCase())reformats every name (uppercase it, place parentheses and commas).join(''), which transforms the name array to an output stringHere is the output:
Same but different:
You don't need the arbitrary
join(':')afterreverse()sincesort()coerces the nested arrays to strings for you. That means you can avoid having to split again later.But Amin is right:
That's way shorter! Thanks a lot for the input
Rust:
Not much of a challenge, I'm afraid. Pretty straight forward split, split, join, sort and join again. A one liner in most modern languages, I suspect.
In c# it would look like this:
Perl solution:
Anyone who says rex is too clever clearly hasn't bumped into bit-shifting code-golf lol
Rex is a very appropriate amount of cleverness 😉