diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-06-22 09:19:30 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-06-22 09:19:30 +0200 |
| commit | 7e67de501e224b745a5abbbb1b93030cccf51265 (patch) | |
| tree | c29956339035d887a5f9272aa247b3f5f8c05882 /challenge-130 | |
| parent | 6d6224ffb263d5224f356ecddace6b3dcba2fd0d (diff) | |
| download | perlweeklychallenge-club-7e67de501e224b745a5abbbb1b93030cccf51265.tar.gz perlweeklychallenge-club-7e67de501e224b745a5abbbb1b93030cccf51265.tar.bz2 perlweeklychallenge-club-7e67de501e224b745a5abbbb1b93030cccf51265.zip | |
feat(challenge-130/lubos-kolouch/perl,python,java/): Challenge 130 LK Perl Python Java
Diffstat (limited to 'challenge-130')
| -rw-r--r-- | challenge-130/lubos-kolouch/java/ch-1.java | 9 | ||||
| -rw-r--r-- | challenge-130/lubos-kolouch/java/ch-2.java | 45 | ||||
| -rw-r--r-- | challenge-130/lubos-kolouch/perl/ch-1.pl | 9 | ||||
| -rw-r--r-- | challenge-130/lubos-kolouch/perl/ch-2.pl | 14 | ||||
| -rw-r--r-- | challenge-130/lubos-kolouch/python/ch-1.py | 9 | ||||
| -rw-r--r-- | challenge-130/lubos-kolouch/python/ch-2.py | 20 |
6 files changed, 106 insertions, 0 deletions
diff --git a/challenge-130/lubos-kolouch/java/ch-1.java b/challenge-130/lubos-kolouch/java/ch-1.java new file mode 100644 index 0000000000..f33b8c3b16 --- /dev/null +++ b/challenge-130/lubos-kolouch/java/ch-1.java @@ -0,0 +1,9 @@ +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + int[] N = {2, 5, 4, 4, 5, 5, 2}; + int odd = Arrays.stream(N).reduce(0, (a, b) -> a ^ b); + System.out.println(odd); + } +} diff --git a/challenge-130/lubos-kolouch/java/ch-2.java b/challenge-130/lubos-kolouch/java/ch-2.java new file mode 100644 index 0000000000..cccb25c4b7 --- /dev/null +++ b/challenge-130/lubos-kolouch/java/ch-2.java @@ -0,0 +1,45 @@ +class Node { + int value; + Node left, right; + + Node(int item) { + value = item; + left = right = null; + } +} + +public class Main { + Node root; + + boolean isBST() { + return isBSTUtil(root, Integer.MIN_VALUE, Integer.MAX_VALUE); + } + + boolean isBSTUtil(Node node, int min, int max) { + if (node == null) { + return true; + } + + if (node.value < min || node.value > max) { + return false; + } + + return (isBSTUtil(node.left, min, node.value - 1) && + isBSTUtil(node.right, node.value + 1, max)); + } + + public static void main(String args[]) { + Main tree = new Main(); + tree.root = new Node(8); + tree.root.left = new Node(5); + tree.root.right = new Node(9); + tree.root.left.left = new Node(4); + tree.root.left.right = new Node(6); + + if (tree.isBST()) { + System.out.println(1); + } else { + System.out.println(0); + } + } +} diff --git a/challenge-130/lubos-kolouch/perl/ch-1.pl b/challenge-130/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..39e768182a --- /dev/null +++ b/challenge-130/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,9 @@ +#!/usr/bin/perl +use strict; +use warnings; +use List::Util qw(reduce); + +my @N = (2, 5, 4, 4, 5, 5, 2); +my $odd = reduce { $a ^ $b } @N; +print "$odd\n"; + diff --git a/challenge-130/lubos-kolouch/perl/ch-2.pl b/challenge-130/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..31bbb5335a --- /dev/null +++ b/challenge-130/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub is_bst { + my ($tree, $min, $max) = @_; + return 1 unless defined $tree; + return 0 if $tree < $min || $tree > $max; + return is_bst($tree->{left}, $min, $tree - 1) && is_bst($tree->{right}, $tree + 1, $max); +} + +my $tree = { value => 8, left => { value => 5, left => { value => 4 }, right => { value => 6 } }, right => { value => 9 } }; +print is_bst($tree, -1, 100000) ? "1\n" : "0\n"; + diff --git a/challenge-130/lubos-kolouch/python/ch-1.py b/challenge-130/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..c06058975f --- /dev/null +++ b/challenge-130/lubos-kolouch/python/ch-1.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from functools import reduce +from operator import xor + +N = [2, 5, 4, 4, 5, 5, 2] +odd = reduce(xor, N) +print(odd) diff --git a/challenge-130/lubos-kolouch/python/ch-2.py b/challenge-130/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..55e4358cea --- /dev/null +++ b/challenge-130/lubos-kolouch/python/ch-2.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +class Node: + def __init__(self, value, left=None, right=None): + self.value = value + self.left = left + self.right = right + + +def is_bst(node, min_val=float('-inf'), max_val=float('inf')): + if node is None: + return True + if not min_val <= node.value <= max_val: + return False + return is_bst(node.left, min_val, node.value - 1) and is_bst(node.right, node.value + 1, max_val) + + +tree = Node(8, Node(5, Node(4), Node(6)), Node(9)) +print(1 if is_bst(tree) else 0) |
