diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-05-26 21:13:19 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-05-26 21:13:19 +0200 |
| commit | 061ce8440f8259df938c87beb7be092711c259e0 (patch) | |
| tree | 482caff33137c3cec38d1814b8ce5453a6aa1f23 | |
| parent | a838cde65d75d5bfd325ff4faae83ecce14db0b8 (diff) | |
| download | perlweeklychallenge-club-061ce8440f8259df938c87beb7be092711c259e0.tar.gz perlweeklychallenge-club-061ce8440f8259df938c87beb7be092711c259e0.tar.bz2 perlweeklychallenge-club-061ce8440f8259df938c87beb7be092711c259e0.zip | |
feat(challenge-113/lubos-kolouch/perl,python/): Challenge 113 LK Perl Python
| -rw-r--r-- | challenge-113/lubos-kolouch/perl/ch-1.pl | 15 | ||||
| -rw-r--r-- | challenge-113/lubos-kolouch/perl/ch-2.pl | 35 | ||||
| -rw-r--r-- | challenge-113/lubos-kolouch/python/ch-1.py | 15 | ||||
| -rw-r--r-- | challenge-113/lubos-kolouch/python/ch-2.py | 29 |
4 files changed, 94 insertions, 0 deletions
diff --git a/challenge-113/lubos-kolouch/perl/ch-1.pl b/challenge-113/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..097ac01c74 --- /dev/null +++ b/challenge-113/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,15 @@ +use strict; +use warnings; + +sub represent_integer { + my ( $N, $D ) = @_; + my $str_N = "$N"; + return 0 if index( $str_N, $D ) == -1; + my $sum_N = 0; + $sum_N += $_ for split( //, $str_N ); + return $sum_N == $N ? 1 : 0; +} + +# Testing +print represent_integer( 25, 2 ), "\n"; # Outputs: 0 +print represent_integer( 22, 2 ), "\n"; # Outputs: 1 diff --git a/challenge-113/lubos-kolouch/perl/ch-2.pl b/challenge-113/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..c4247bfeae --- /dev/null +++ b/challenge-113/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,35 @@ +package Node; +use strict; +use warnings; + +sub new { + my ( $class, $val, $left, $right ) = @_; + return bless { val => $val, left => $left, right => $right }, $class; +} + +package main; +use strict; +use warnings; + +sub sum_tree { + my ( $node, $total ) = @_; + $total //= 0; + return 0 unless defined $node; + $total = + sum_tree( $node->{left}, $total ) + + sum_tree( $node->{right}, $total ) + + $node->{val}; + $node->{val} = $total - $node->{val}; + return $total; +} + +# Testing +my $root = Node->new(1); +$root->{left} = Node->new(2); +$root->{right} = Node->new(3); +$root->{left}->{left} = Node->new(4); +$root->{left}->{right} = Node->new(5); +sum_tree($root); +print $root->{val}, "\n"; # Outputs: 14 +print $root->{left}->{val}, "\n"; # Outputs: 11 +print $root->{right}->{val}, "\n"; # Outputs: 2 diff --git a/challenge-113/lubos-kolouch/python/ch-1.py b/challenge-113/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..883f53eae1 --- /dev/null +++ b/challenge-113/lubos-kolouch/python/ch-1.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +def represent_integer(N, D): + str_N = str(N) + if str(D) not in str_N: + return 0 + sum_N = sum(int(digit) for digit in str_N if digit == str(D)) + return sum_N == N + + +# Testing +print(represent_integer(25, 2)) # Outputs: 0 +print(represent_integer(22, 2)) # Outputs: 1 diff --git a/challenge-113/lubos-kolouch/python/ch-2.py b/challenge-113/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..2be3846587 --- /dev/null +++ b/challenge-113/lubos-kolouch/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +class Node: + def __init__(self, val, left=None, right=None): + self.val = val + self.left = left + self.right = right + + +def sum_tree(node, total=0): + if node is None: + return 0 + total = sum_tree(node.left, total) + sum_tree(node.right, total) + node.val + node.val = total - node.val + return total + + +# Testing +root = Node(1) +root.left = Node(2) +root.right = Node(3) +root.left.left = Node(4) +root.left.right = Node(5) +sum_tree(root) +print(root.val) # Outputs: 14 +print(root.left.val) # Outputs: 11 +print(root.right.val) # Outputs: 2 |
