Skip to content

Instantly share code, notes, and snippets.

@gustafe
Last active February 5, 2021 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gustafe/117768d83df175b308aa15dcc75d2061 to your computer and use it in GitHub Desktop.
Save gustafe/117768d83df175b308aa15dcc75d2061 to your computer and use it in GitHub Desktop.
#! /usr/bin/env perl
use Modern::Perl '2015';
use Data::Dump qw/dump/;
use Test::More tests => 1;
# discussion: https://lobste.rs/s/zpqrpc/random_job_interview_challenge_clojure
my $input = 'aaaabbbcca';
my $target = '[("a", 4), ("b", 3), ("c", 2), ("a", 1)]';
my @list = split( //, $input );
my @output = ( { c => shift @list, n => 1 } );
while (@list) {
my $c = shift @list;
if ( $c eq $output[-1]->{c} ) { $output[-1]->{n}++ }
else { push @output, { c => $c, n => 1 } }
}
my $string = join( ', ',
map { sprintf( "(\"%s\", %d)", $_->{c}, $_->{n} ) } @output );
is( "[$string]", $target, "[$string]" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment