From 4eebc07786114e281c00a6002eb0183daa35e0f4 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sun, 21 May 2023 13:26:30 +0200 Subject: feat(challenge-071/lubos-kolouch/perl,python/ch-1,-ch-2): Challenge 071 LK Perl Python --- challenge-071/lubos-kolouch/perl/ch-1.pl | 23 +++++++++ challenge-071/lubos-kolouch/perl/ch-2.pl | 77 ++++++++++++++++++++++++++++++ challenge-071/lubos-kolouch/python/ch-1.py | 26 ++++++++++ challenge-071/lubos-kolouch/python/ch-2.py | 52 ++++++++++++++++++++ 4 files changed, 178 insertions(+) create mode 100644 challenge-071/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-071/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-071/lubos-kolouch/python/ch-1.py create mode 100644 challenge-071/lubos-kolouch/python/ch-2.py diff --git a/challenge-071/lubos-kolouch/perl/ch-1.pl b/challenge-071/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..5499ab959b --- /dev/null +++ b/challenge-071/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub find_peaks { + my @arr = @_; + my $n = scalar @arr; + + my @peaks; + push @peaks, $arr[0] if $arr[0] > $arr[1]; + + for my $i ( 1 .. $n - 2 ) { + push @peaks, $arr[$i] if $arr[$i] > $arr[ $i - 1 ] and $arr[$i] > $arr[ $i + 1 ]; + } + + push @peaks, $arr[-1] if $arr[-1] > $arr[-2]; + + return @peaks; +} + +# Tests +print join( ", ", find_peaks( 18, 45, 38, 25, 10, 7, 21, 6, 28, 48 ) ), "\n"; # 48, 45, 21 +print join( ", ", find_peaks( 47, 11, 32, 8, 1, 9, 39, 14, 36, 23 ) ), "\n"; # 47, 32, 39, 36 diff --git a/challenge-071/lubos-kolouch/perl/ch-2.pl b/challenge-071/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..1f8970ad3b --- /dev/null +++ b/challenge-071/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,77 @@ +#!/usr/bin/perl +use strict; +use warnings; + +package Node; + +sub new { + my ( $class, $data ) = @_; + my $self = { + data => $data, + next => undef + }; + return bless $self, $class; +} + +package LinkedList; + +sub new { + my ($class) = @_; + my $self = { head => undef }; + return bless $self, $class; +} + +sub append { + my ( $self, $data ) = @_; + my $new_node = Node->new($data); + if ( !$self->{head} ) { + $self->{head} = $new_node; + } + else { + my $cur = $self->{head}; + $cur = $cur->{next} while $cur->{next}; + $cur->{next} = $new_node; + } +} + +sub remove_from_end { + my ( $self, $n ) = @_; + my $size = 0; + my $cur = $self->{head}; + while ($cur) { + $size++; + $cur = $cur->{next}; + } + if ( $n >= $size ) { + $self->{head} = $self->{head}->{next}; + } + else { + $cur = $self->{head}; + for ( 1 .. $size - $n - 1 ) { + $cur = $cur->{next}; + } + $cur->{next} = $cur->{next}->{next} if $cur->{next}; + } +} + +sub print { + my ($self) = @_; + my @values; + my $cur = $self->{head}; + while ($cur) { + push @values, $cur->{data}; + $cur = $cur->{next}; + } + print join( " -> ", @values ), "\n"; +} + +# Tests +my $ll = LinkedList->new(); +for my $i ( 1 .. 5 ) { + $ll->append($i); +} + +for my $i ( 1 .. 6 ) { + $ll->remove_from_end($i); + $ll->print(); +} diff --git a/challenge-071/lubos-kolouch/python/ch-1.py b/challenge-071/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..7a92d52f8b --- /dev/null +++ b/challenge-071/lubos-kolouch/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def find_peaks(arr: List[int]) -> List[int]: + n = len(arr) + # Initialize peaks list with the first element if it's bigger than the second + peaks = [arr[0]] if arr[0] > arr[1] else [] + + # Iterate over the array checking for peaks + for i in range(1, n-1): + if arr[i] > arr[i-1] and arr[i] > arr[i+1]: + peaks.append(arr[i]) + + # Add the last element if it's bigger than the penultimate + if arr[-1] > arr[-2]: + peaks.append(arr[-1]) + + return peaks + + +# Tests +print(find_peaks([18, 45, 38, 25, 10, 7, 21, 6, 28, 48])) # [48, 45, 21] +print(find_peaks([47, 11, 32, 8, 1, 9, 39, 14, 36, 23])) # [47, 32, 39, 36] diff --git a/challenge-071/lubos-kolouch/python/ch-2.py b/challenge-071/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..5662d38226 --- /dev/null +++ b/challenge-071/lubos-kolouch/python/ch-2.py @@ -0,0 +1,52 @@ +class Node: + def __init__(self, data=None): + self.data = data + self.next = None + + +class LinkedList: + def __init__(self): + self.head = None + + def append(self, data): + if not self.head: + self.head = Node(data) + else: + cur = self.head + while cur.next: + cur = cur.next + cur.next = Node(data) + + def remove_from_end(self, n): + if self.head is None: + return + size = 0 + cur = self.head + while cur: + size += 1 + cur = cur.next + if n >= size: + self.head = self.head.next if self.head.next else None + else: + cur = self.head + for _ in range(size - n - 1): + cur = cur.next + cur.next = cur.next.next if cur.next and cur.next.next else None + + def __str__(self): + values = [] + cur = self.head + while cur: + values.append(str(cur.data)) + cur = cur.next + return ' -> '.join(values) + + +# Tests +ll = LinkedList() +for i in range(1, 6): + ll.append(i) + +for i in range(1, 7): + ll.remove_from_end(i) + print(ll) -- cgit