diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-04-16 15:24:13 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-04-16 15:24:13 +0200 |
| commit | 8091d154c159948b012ce9728cbd8942ea3d148c (patch) | |
| tree | 11224395d1559dd0e7df4e865fbf0c132fd16a10 /challenge-059/lubos-kolouch/perl | |
| parent | fc7f30eaf4f1b81354ea7a10b3240efccf28a084 (diff) | |
| download | perlweeklychallenge-club-8091d154c159948b012ce9728cbd8942ea3d148c.tar.gz perlweeklychallenge-club-8091d154c159948b012ce9728cbd8942ea3d148c.tar.bz2 perlweeklychallenge-club-8091d154c159948b012ce9728cbd8942ea3d148c.zip | |
Challenge 057 059 LK Perl Python
Diffstat (limited to 'challenge-059/lubos-kolouch/perl')
| -rw-r--r-- | challenge-059/lubos-kolouch/perl/ch-1.pl | 94 | ||||
| -rw-r--r-- | challenge-059/lubos-kolouch/perl/ch-2.pl | 28 |
2 files changed, 122 insertions, 0 deletions
diff --git a/challenge-059/lubos-kolouch/perl/ch-1.pl b/challenge-059/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..66d5a1b60d --- /dev/null +++ b/challenge-059/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,94 @@ +#!/usr/bin/perl +use strict; +use warnings; + +package LinkedList; + +sub new { + my ( $class, $value ) = @_; + my $self = { + value => $value, + next => undef, + }; + bless $self, $class; + return $self; +} + +sub append { + my ( $self, $value ) = @_; + my $current = $self; + while ( defined $current->{next} ) { + $current = $current->{next}; + } + $current->{next} = LinkedList->new($value); +} + +sub partition { + my ( $self, $k ) = @_; + my $less_head; + my $greater_head; + my $less_tail; + my $greater_tail; + + my $current = $self; + while ( defined $current ) { + if ( $current->{value} < $k ) { + if ( defined $less_tail ) { + $less_tail->{next} = $current; + } + else { + $less_head = $current; + } + $less_tail = $current; + } + else { + if ( defined $greater_tail ) { + $greater_tail->{next} = $current; + } + else { + $greater_head = $current; + } + $greater_tail = $current; + } + $current = $current->{next}; + } + + if ( defined $less_tail ) { + $less_tail->{next} = $greater_head; + } + else { + $less_head = $greater_head; + } + + if ( defined $greater_tail ) { + $greater_tail->{next} = undef; + } + + return $less_head; +} + +sub to_string { + my $self = shift; + my $str = ''; + my $current = $self; + while ( defined $current ) { + $str .= $current->{value} . ' → '; + $current = $current->{next}; + } + $str .= 'END'; + return $str; +} + +package main; + +my $list = LinkedList->new(1); +$list->append(4); +$list->append(3); +$list->append(2); +$list->append(5); +$list->append(2); + +print "Original list: ", $list->to_string(), "\n"; +my $k = 3; +my $partitioned_list = $list->partition($k); +print "Partitioned list with k=$k: ", $partitioned_list->to_string(), "\n"; diff --git a/challenge-059/lubos-kolouch/perl/ch-2.pl b/challenge-059/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..70f82c4355 --- /dev/null +++ b/challenge-059/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub count_different_bits { + my ( $a, $b ) = @_; + my $count = 0; + while ( $a > 0 or $b > 0 ) { + $count += ( $a % 2 ) ^ ( $b % 2 ); + $a >>= 1; + $b >>= 1; + } + return $count; +} + +sub sum_different_bits { + my @numbers = @_; + my $sum = 0; + for my $i ( 0 .. $#numbers ) { + for my $j ( $i + 1 .. $#numbers ) { + $sum += count_different_bits( $numbers[$i], $numbers[$j] ); + } + } + return $sum; +} + +my @input = ( 2, 3, 4 ); +print sum_different_bits(@input), "\n"; |
