diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-06-05 18:57:56 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-06-05 18:57:56 +0200 |
| commit | 8b7bba6065eff4b43f6016549b3bf3dbb59131c4 (patch) | |
| tree | 13eb46082b8aba03273facc2b28ddc9ba4d7c74d /challenge-116 | |
| parent | c968d01a52b99e050fead41f876ccbc119e03dab (diff) | |
| download | perlweeklychallenge-club-8b7bba6065eff4b43f6016549b3bf3dbb59131c4.tar.gz perlweeklychallenge-club-8b7bba6065eff4b43f6016549b3bf3dbb59131c4.tar.bz2 perlweeklychallenge-club-8b7bba6065eff4b43f6016549b3bf3dbb59131c4.zip | |
feat(challenge-116/lubos-kolouch/perl,python/): Challenge 116 LK Perl Python
Diffstat (limited to 'challenge-116')
| -rw-r--r-- | challenge-116/lubos-kolouch/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-116/lubos-kolouch/perl/ch-2.pl | 22 | ||||
| -rw-r--r-- | challenge-116/lubos-kolouch/python/ch-1.py | 33 | ||||
| -rw-r--r-- | challenge-116/lubos-kolouch/python/ch-2.py | 20 |
4 files changed, 102 insertions, 0 deletions
diff --git a/challenge-116/lubos-kolouch/perl/ch-1.pl b/challenge-116/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..94c58b1a3c --- /dev/null +++ b/challenge-116/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,27 @@ +sub split_number { + my ($n) = @_; + + my @result; + if($n >= 10){ + my @digits = split //, $n; + my $first = shift @digits; + push @result, $first; + foreach my $digit (@digits){ + if($digit - $first == 1){ + push @result, $digit; + $first = $digit; + } else { + @result = ($n); + last; + } + } + } + return join(',', @result); +} + +use Test::More tests => 3; + +is(split_number(1234), "1,2,3,4", "Test 1"); +is(split_number(91011), "9,10,11", "Test 2"); +is(split_number(10203), "10203", "Test 3"); + diff --git a/challenge-116/lubos-kolouch/perl/ch-2.pl b/challenge-116/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..beb009e047 --- /dev/null +++ b/challenge-116/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,22 @@ +sub sum_of_squares { + my ($n) = @_; + + my $sum = 0; + while($n){ + my $digit = $n % 10; + $sum += $digit * $digit; + $n = int($n/10); + } + + my $sqrt = int(sqrt($sum)); + if($sqrt * $sqrt == $sum){ + return 1; + } else { + return 0; + } +} + +print sum_of_squares(34), "\n"; # Prints 1 +print sum_of_squares(50), "\n"; # Prints 1 +print sum_of_squares(52), "\n"; # Prints 0 + diff --git a/challenge-116/lubos-kolouch/python/ch-1.py b/challenge-116/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..900ae8986c --- /dev/null +++ b/challenge-116/lubos-kolouch/python/ch-1.py @@ -0,0 +1,33 @@ +import unittest + + +def split_number(n): + result = [] + if n >= 10: + digits = [int(x) for x in str(n)] + first = digits[0] + result.append(first) + for digit in digits[1:]: + if digit - first == 1: + result.append(digit) + first = digit + else: + result = [n] + break + return ','.join(map(str, result)) + + +class TestSplitNumber(unittest.TestCase): + + def test_1(self): + self.assertEqual(split_number(1234), "1,2,3,4") + + def test_2(self): + self.assertEqual(split_number(91011), "9,10,11") + + def test_3(self): + self.assertEqual(split_number(10203), "10203") + + +if __name__ == '__main__': + unittest.main() diff --git a/challenge-116/lubos-kolouch/python/ch-2.py b/challenge-116/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..427e9a6339 --- /dev/null +++ b/challenge-116/lubos-kolouch/python/ch-2.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +def sum_of_squares(n): + sum = 0 + while n: + digit = n % 10 + sum += digit * digit + n = n // 10 + + sqrt = int(sqrt(sum)) + if sqrt * sqrt == sum: + return 1 + else: + return 0 + + +print(sum_of_squares(34)) # Prints 1 +print(sum_of_squares(50)) # Prints 1 +print(sum_of_squares(52)) # Prints 0 |
