From b1706186629563669fdfc3ef8916f89197165568 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 6 Mar 2021 12:19:56 +0100 Subject: Challenge 102 LK Perl --- challenge-102/lubos-kolouch/perl/ch-1.pl | 45 ++++++++++++++++++++++++++++ challenge-102/lubos-kolouch/perl/ch-2.pl | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 challenge-102/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-102/lubos-kolouch/perl/ch-2.pl diff --git a/challenge-102/lubos-kolouch/perl/ch-1.pl b/challenge-102/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..066d25b295 --- /dev/null +++ b/challenge-102/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-1.pl +# +# USAGE: ./ch-1.pl +# +# DESCRIPTION: Perl Weekly Challenge #102 +# Task 1 - Rare Numbers +# +# AUTHOR: Lubos Kolouch +# CREATED: 03/06/2021 11:18:30 AM +#=============================================================================== + +use strict; +use warnings; + +sub get_rare_numbers { + my $what = shift; + + my $pos = 1 . '0'x($what-1); + + my @output; + while (1) { + $pos++; + last unless (length($pos) == $what); + my $rev_num = reverse $pos; + + next unless $pos - $rev_num > 0; + next unless sqrt($pos - $rev_num) == int(sqrt($pos - $rev_num)); + next unless sqrt($pos + $rev_num) == int(sqrt($pos + $rev_num)); + print "$pos \n"; + + push @output, $pos; + } + + return \@output; +} + +use Test::More; + +is_deeply(get_rare_numbers(2), [65]); +is_deeply(get_rare_numbers(6), [621770]); +is_deeply(get_rare_numbers(9), [281089082]); + diff --git a/challenge-102/lubos-kolouch/perl/ch-2.pl b/challenge-102/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..600a29572a --- /dev/null +++ b/challenge-102/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-2.pl +# +# USAGE: ./ch-2.pl +# +# DESCRIPTION: Perl Weekly Challenge #102 +# Task 2 - Hash-counting String +# +# AUTHOR: Lubos Kolouch +# CREATED: 03/06/2021 11:18:30 AM +#=============================================================================== + +use strict; +use warnings; + +sub get_hash { + my $what = shift; + + # we know the last position has length + # of the input. So we can just backtrack + + my $output = ''; + + my $pos = $what; + while ($pos > 0) { + my $append_str = $pos.'#'; + + if ($pos > 1) { + $output = $pos.'#'.$output; + } else { + $output = '#'.$output; + } + + $pos -= length($append_str); + } + + return $output; +} + +use Test::More; + +is_deeply(get_hash(1), '#'); +is_deeply(get_hash(2), '2#'); +is_deeply(get_hash(3), '#3#'); +is_deeply(get_hash(10), '#3#5#7#10#'); +is_deeply(get_hash(14), '2#4#6#8#11#14#'); + +done_testing; -- cgit From 2fe873a82153977fc43eebe84424fa4349389f3d Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 6 Mar 2021 12:39:27 +0100 Subject: Challenge 102 LK Python --- challenge-102/lubos-kolouch/python/ch-1.py | 51 ++++++++++++++++++++++++++++++ challenge-102/lubos-kolouch/python/ch-2.py | 42 ++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 challenge-102/lubos-kolouch/python/ch-1.py create mode 100644 challenge-102/lubos-kolouch/python/ch-2.py diff --git a/challenge-102/lubos-kolouch/python/ch-1.py b/challenge-102/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..27093ae3fe --- /dev/null +++ b/challenge-102/lubos-kolouch/python/ch-1.py @@ -0,0 +1,51 @@ +#!/bin/env python +""" +#=============================================================================== +# +# FILE: ch-1.py +# +# USAGE: ./ch-1.py +# +# DESCRIPTION: Perl Weekly Challenge #102 +# Task 1 - Rare Numbers +# +# AUTHOR: Lubos Kolouch +# CREATED: 03/06/2021 11:18:30 AM +#=============================================================================== +""" +from math import sqrt + + +def get_rare_numbers(what: int): + """ Get all rare number in the give range """ + + pos = '1' + '0'*(what-1) + + output = [] + while 1: + pos = str(int(pos)+1) + + if len(pos) > what: + break + + rev_num = pos[::-1] + + if int(pos) - int(rev_num) <= 0: + continue + + if sqrt(int(pos) - int(rev_num)) != int(sqrt(int(pos) - int(rev_num))): + continue + + if sqrt(int(pos) + int(rev_num)) != int(sqrt(int(pos) + int(rev_num))): + continue + + print(pos) + + output.append(int(pos)) + + return output + + +assert get_rare_numbers(2) == [65] +assert get_rare_numbers(6) == [621770] +assert get_rare_numbers(9) == [281089082] diff --git a/challenge-102/lubos-kolouch/python/ch-2.py b/challenge-102/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..16d7705793 --- /dev/null +++ b/challenge-102/lubos-kolouch/python/ch-2.py @@ -0,0 +1,42 @@ +#!/bin/env python +""" +#=============================================================================== +# +# FILE: ch-2.py +# +# USAGE: ./ch-2.p +# +# DESCRIPTION: Perl Weekly Challenge #102 +# Task 2 - Hash-counting String +# +# AUTHOR: Lubos Kolouch +# CREATED: 03/06/2021 11:18:30 AM +#=============================================================================== +""" + + +def get_hash(what: int): + """ get the hash """ + # we know the last position has length + # of the input. So we can just backtrack + output = '' + + pos = what + while pos > 0: + append_str = str(pos)+'#' + + if pos > 1: + output = str(pos)+'#'+output + else: + output = '#'+output + + pos = int(pos) - len(append_str) + + return output + + +assert get_hash(1) == '#' +assert get_hash(2) == '2#' +assert get_hash(3) == '#3#' +assert get_hash(10) == '#3#5#7#10#' +assert get_hash(14) == '2#4#6#8#11#14#' -- cgit