diff options
Diffstat (limited to 'challenge-071')
| -rw-r--r-- | challenge-071/paulo-custodio/perl/ch-1.pl | 4 | ||||
| -rw-r--r-- | challenge-071/paulo-custodio/perl/ch-2.pl | 2 | ||||
| -rw-r--r-- | challenge-071/paulo-custodio/python/ch-1.py | 39 | ||||
| -rw-r--r-- | challenge-071/paulo-custodio/python/ch-2.py | 70 |
4 files changed, 112 insertions, 3 deletions
diff --git a/challenge-071/paulo-custodio/perl/ch-1.pl b/challenge-071/paulo-custodio/perl/ch-1.pl index 3e8a429961..fde936ddad 100644 --- a/challenge-071/paulo-custodio/perl/ch-1.pl +++ b/challenge-071/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 071 # -# TASK #1 › Peak Element +# TASK #1 > Peak Element # Submitted by: Mohammad S Anwar # You are given positive integer $N (>1). # @@ -11,7 +11,7 @@ # # In the end it should print peak elements in the array, if found. # -# An array element is called peak if it is bigger than it’s neighbour. +# An array element is called peak if it is bigger than it's neighbour. # # Example 1 # Array: [ 18, 45, 38, 25, 10, 7, 21, 6, 28, 48 ] diff --git a/challenge-071/paulo-custodio/perl/ch-2.pl b/challenge-071/paulo-custodio/perl/ch-2.pl index e707c19476..a3671826f3 100644 --- a/challenge-071/paulo-custodio/perl/ch-2.pl +++ b/challenge-071/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 071 # -# TASK #2 › Trim Linked List +# TASK #2 > Trim Linked List # Submitted by: Mohammad S Anwar # You are given a singly linked list and a positive integer $N (>0). # diff --git a/challenge-071/paulo-custodio/python/ch-1.py b/challenge-071/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..01cbebfece --- /dev/null +++ b/challenge-071/paulo-custodio/python/ch-1.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +# Challenge 071 +# +# TASK #1 > Peak Element +# Submitted by: Mohammad S Anwar +# You are given positive integer $N (>1). +# +# Write a script to create an array of size $N with random unique elements +# between 1 and 50. +# +# In the end it should print peak elements in the array, if found. +# +# An array element is called peak if it is bigger than it's neighbour. +# +# Example 1 +# Array: [ 18, 45, 38, 25, 10, 7, 21, 6, 28, 48 ] +# Peak: [ 48, 45, 21 ] +# Example 2 +# Array: [ 47, 11, 32, 8, 1, 9, 39, 14, 36, 23 ] +# Peak: [ 47, 32, 39, 36 ] + +import sys + +def peek_elem(n): + out = [] + if len(n) < 2: + return out + if n[0] > n[1]: + out.append(n[0]) + for i in range(1, len(n) - 1): + if n[i] > n[i-1] and n[i] > n[i+1]: + out.append(n[i]) + if n[-1] > n[-2]: + out.append(n[-1]) + return out + +n = list(map(int, sys.argv[1:])) +print(", ".join(map(str, peek_elem(n)))) diff --git a/challenge-071/paulo-custodio/python/ch-2.py b/challenge-071/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..c7fc989703 --- /dev/null +++ b/challenge-071/paulo-custodio/python/ch-2.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +# Challenge 071 +# +# TASK #2 > Trim Linked List +# Submitted by: Mohammad S Anwar +# You are given a singly linked list and a positive integer $N (>0). +# +# Write a script to remove the $Nth node from the end of the linked list and +# print the linked list. +# +# If $N is greater than the size of the linked list then remove the first +# node of the list. +# +# NOTE: Please use pure linked list implementation. +# +# Example +# Given Linked List: 1 -> 2 -> 3 -> 4 -> 5 +# when $N = 1 +# Output: 1 -> 2 -> 3 -> 4 +# when $N = 2 +# Output: 1 -> 2 -> 3 -> 5 +# when $N = 3 +# Output: 1 -> 2 -> 4 -> 5 +# when $N = 4 +# Output: 1 -> 3 -> 4 -> 5 +# when $N = 5 +# Output: 2 -> 3 -> 4 -> 5 +# when $N = 6 +# Output: 2 -> 3 -> 4 -> 5 + +import sys + +def list_len(lst): + length = 0 + while lst: + length += 1 + lst = lst[1] + return length + +def remove_n(remove, lst): + if remove == 0: + return lst[1] + else: + p = lst + last = None + while p: + if remove == 0: + last[1] = p[1] + last = p + p = p[1] + remove -= 1 + return lst + +def show(lst): + out = [] + while lst: + out.append(lst[0]) + lst = lst[1] + print(" ".join(map(str, out))) + +n = int(sys.argv[1]) if len(sys.argv) > 1 else 1 +lst = None +for arg in reversed(sys.argv[2:]): + lst = [arg, lst] + +length = list_len(lst) +remove = 0 if n > length else length - n +lst = remove_n(remove, lst) +show(lst) |
