From 63bcbae5c9a0c938d1e6274b310a86f2c36ca337 Mon Sep 17 00:00:00 2001 From: Andinus Date: Sun, 11 Oct 2020 12:10:58 +0530 Subject: Add challenge-079's ch-1 solution in Perl --- challenge-079/andinus/README | 74 +++++++++++--------------------------- challenge-079/andinus/blog-1.txt | 1 + challenge-079/andinus/perl/ch-1.pl | 17 +++++++++ 3 files changed, 38 insertions(+), 54 deletions(-) create mode 100644 challenge-079/andinus/blog-1.txt create mode 100755 challenge-079/andinus/perl/ch-1.pl diff --git a/challenge-079/andinus/README b/challenge-079/andinus/README index 8455cdb3ee..1b59198156 100644 --- a/challenge-079/andinus/README +++ b/challenge-079/andinus/README @@ -1,79 +1,45 @@ ━━━━━━━━━━━━━━━ - CHALLENGE 078 + CHALLENGE 079 + + Andinus ━━━━━━━━━━━━━━━ Table of Contents ───────────────── -1 Task 1 - Leader Element -.. 1.1 Perl -2 Task 2 - Left Rotation -.. 2.1 Perl +1. Task 1 - Count Set Bits +.. 1. Perl -1 Task 1 - Leader Element +1 Task 1 - Count Set Bits ═════════════════════════ - You are given an array @A containing distinct integers. - - Write a script to find all leader elements in the array @A. Print (0) - if none found. + You are given a positive number $N. - • An element is leader if it is greater than all the elements to its - right side. + Write a script to count the total numbrer of set bits of the binary + representations of all numbers from 1 to $N and return + `$total_count_set_bit % 1000000007'. 1.1 Perl ──────── - • Program: [file:perl/ch-1.pl] - - We take input from `@ARGV', loop over it. And then we loop over the - elements at right, goto next if `$arg' is less than `$elm'. This will - push all the leader elements to `@leader'. - ┌──── - │ my @leader; - │ MAIN: while (my $arg = shift @ARGV) { - │ foreach my $elm (@ARGV) { - │ next MAIN if $arg < $elm; - │ } - │ push @leader, $arg; - │ } - └──── - - -2 Task 2 - Left Rotation -════════════════════════ - - You are given array @A containing positive numbers and @B containing - one or more indices from the array @A. - - Write a script to left rotate @A so that the number at the first index - of @B becomes the first element in the array. Similary, left rotate @A - again so that the number at the second index of @B becomes the first - element in the array. - - -2.1 Perl -──────── - - • Program: [file:perl/ch-2.pl] + • Program: - Loop over `@B' & then rotate the elements. Same could've been done - with Left Rotation, I find this easier to understand. + We loop from `1 ... $input', convert each `$num' to binary & count the + set bits & add them to `$set_bits'. ┌──── - │ my @A = qw(10 20 30 40 50); - │ my @B = qw(3 4); + │ my $input = shift @ARGV; │ - │ foreach (@B) { - │ my @tmp = @A; - │ foreach (1 ... scalar @tmp - $_) { - │ unshift @tmp, pop @tmp; - │ } - │ print join(', ', @tmp), "\n"; + │ my $set_bits; + │ foreach my $num (1 ... $input) { + │ my $binary = sprintf "%b", $num; + │ my $count = $binary =~ tr/1//; + │ $set_bits += $count; │ } + │ print $set_bits % 1000000007, "\n"; └──── diff --git a/challenge-079/andinus/blog-1.txt b/challenge-079/andinus/blog-1.txt new file mode 100644 index 0000000000..4e35657f1f --- /dev/null +++ b/challenge-079/andinus/blog-1.txt @@ -0,0 +1 @@ +https://andinus.tilde.institute/pwc/challenge-079/ diff --git a/challenge-079/andinus/perl/ch-1.pl b/challenge-079/andinus/perl/ch-1.pl new file mode 100755 index 0000000000..d27554d7ef --- /dev/null +++ b/challenge-079/andinus/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +die "usage: ./ch-1.pl [number]\n" + unless scalar @ARGV; + +my $input = shift @ARGV; + +my $set_bits; +foreach my $num (1 ... $input) { + my $binary = sprintf "%b", $num; + my $count = $binary =~ tr/1//; + $set_bits += $count; +} +print $set_bits % 1000000007, "\n"; -- cgit