aboutsummaryrefslogtreecommitdiff
path: root/challenge-116
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-116')
-rw-r--r--challenge-116/colin-crain/perl/ch-1.pl81
-rw-r--r--challenge-116/colin-crain/perl/ch-2.pl62
-rw-r--r--challenge-116/colin-crain/raku/ch-1.raku59
-rw-r--r--challenge-116/colin-crain/raku/ch-2.raku42
4 files changed, 244 insertions, 0 deletions
diff --git a/challenge-116/colin-crain/perl/ch-1.pl b/challenge-116/colin-crain/perl/ch-1.pl
new file mode 100644
index 0000000000..8afe176c16
--- /dev/null
+++ b/challenge-116/colin-crain/perl/ch-1.pl
@@ -0,0 +1,81 @@
+#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# sequet-number.pl
+#
+# Number Sequence
+# Submitted by: Mohammad S Anwar
+# You are given a number $N >= 10.
+#
+# Write a script to split the given number such that
+# the difference between two consecutive numbers is
+# always 1 and it shouldn’t have leading 0.
+#
+# Print the given number if it impossible to split the number.
+#
+# Example
+#
+# Input : $N = 1234
+# Output: 1,2,3,4
+#
+# Input : $N = 91011
+# Output: 9,10,11
+#
+# Input : $N = 10203
+# Output: 10203 as it is impossible to split satisfying the conditions.
+#
+# method:
+# cases: 9991000 -> 999, 1000 additional digit length
+# 565 -> 5, 6, 5 up and down
+#
+# foreach number segment length 1..number length/2
+# remove substr seg-num from copy
+# foreach seg-num + 1, seg-num - 1
+# get length, look at substr that length
+# doen it match inc value?
+# if yes recurse
+#
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+my $num = $ARGV[0] || '';
+ $num += 0;
+my $out;
+
+my $ceil = length($num) - int(length($num)/2);
+for my $group (1..$ceil) {
+ if ($out = match_next_segment($num, 0, $group)) {
+ say join ',', $out->@*;
+ exit;
+ }
+}
+say $num;
+
+sub match_next_segment ($num, $start = 0, $len = 1, $part = []) {
+ my $seg = substr $num, $start, $len;
+ return if $seg eq '-';
+ my @part = ($part->@*, $seg);
+ return \@part if $start+$len == length $num;
+ for my $next ( $seg+1, $seg-1 ) {
+ my $len2 = length $next;
+ if ( substr($num, $start+$len, $len2) == $next ) {
+ my $sol = match_next_segment($num, $start+$len, $len2, \@part);
+ return $sol if defined $sol;
+ }
+ }
+ return undef;
+}
+
+
+
+
diff --git a/challenge-116/colin-crain/perl/ch-2.pl b/challenge-116/colin-crain/perl/ch-2.pl
new file mode 100644
index 0000000000..d79ef9c0c2
--- /dev/null
+++ b/challenge-116/colin-crain/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# that-cats-some-square.pl
+#
+# Sum of Squares
+# Submitted by: Mohammad Meraj Zia
+# You are given a number $N >= 10.
+#
+# Write a script to find out if the given number $N is such that sum of
+# squares of all digits is a perfect square. Print 1 if it is otherwise
+# 0.
+#
+# Example
+# Input: $N = 34
+# Ouput: 1 as 3^2 + 4^2 => 9 + 16 => 25 => 5^2
+#
+# Input: $N = 50
+# Output: 1 as 5^2 + 0^2 => 25 + 0 => 25 => 5^2
+#
+# Input: $N = 52
+# Output: 0 as 5^2 + 2^2 => 25 + 4 => 29
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+use List::Util qw( sum );
+
+die "usage:\n ./that-cats-some-square.pl positive-integer \n" unless @ARGV && $ARGV[0] > 0;
+my $num = $ARGV[0] ;
+
+my $sum = sum map { $_ ** 2 } split //, $num;
+(int(sqrt($sum)))**2 == $sum
+ ? say 1
+ : say 0 ;
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+# use Test::More;
+#
+# is
+#
+# done_testing();
diff --git a/challenge-116/colin-crain/raku/ch-1.raku b/challenge-116/colin-crain/raku/ch-1.raku
new file mode 100644
index 0000000000..b0f8d9d45d
--- /dev/null
+++ b/challenge-116/colin-crain/raku/ch-1.raku
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl6
+#
+#
+# sequet-number.raku
+#
+# Number Sequence
+# Submitted by: Mohammad S Anwar
+# You are given a number $N >= 10.
+#
+# Write a script to split the given number such that
+# the difference between two consecutive numbers is
+# always 1 and it shouldn’t have leading 0.
+#
+# Print the given number if it impossible to split the number.
+#
+# Example
+#
+# Input : $N = 1234
+# Output: 1,2,3,4
+#
+# Input : $N = 91011
+# Output: 9,10,11
+#
+# Input : $N = 10203
+# Output: 10203 as it is impossible to split satisfying the conditions.
+#
+#
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+unit sub MAIN (Int $num = 10099) ;
+
+my $out ;
+
+for 1..($num.chars/2).ceiling -> $group {
+ if $out = match_next_segment($num, 0, $group) {
+ $out.join(',').say;
+ return;
+ }
+}
+say $num;
+
+sub match_next_segment ($num, $start = 0, $len = 1, $prev = () ) {
+ my $seg = $num.substr: $start, $len;
+ my $part = (|$prev, $seg);
+ return $part if $num.chars == $start+$len;
+ for $seg+1, $seg-1 -> $next {
+ my $len2 = $next.chars;
+ if $num.substr($start+$len, $len2) == $next {
+ my $sol = match_next_segment($num, $start+$len, $len2, $part);
+ return $sol if $sol.defined;
+ }
+ }
+ return;
+}
diff --git a/challenge-116/colin-crain/raku/ch-2.raku b/challenge-116/colin-crain/raku/ch-2.raku
new file mode 100644
index 0000000000..e1ca281cf0
--- /dev/null
+++ b/challenge-116/colin-crain/raku/ch-2.raku
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl6
+#
+#
+# that-cats-some-square.raku
+#
+# Sum of Squares
+# Submitted by: Mohammad Meraj Zia
+# You are given a number $N >= 10.
+#
+# Write a script to find out if the given number $N is such that sum of
+# squares of all digits is a perfect square. Print 1 if it is otherwise
+# 0.
+#
+# Example
+# Input: $N = 34
+# Ouput: 1 as 3^2 + 4^2 => 9 + 16 => 25 => 5^2
+#
+# Input: $N = 50
+# Output: 1 as 5^2 + 0^2 => 25 + 0 => 25 => 5^2
+#
+# Input: $N = 52
+# Output: 0 as 5^2 + 2^2 => 25 + 4 => 29
+#
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+unit sub MAIN (Int $num where {$num > 0} = 50) ;
+
+my $sum = $num.comb
+ .map(*²)
+ .sum ;
+
+say
+$sum == (floor sqrt $sum)²
+ ?? 1
+ !! 0 ;
+
+