From 16d9bf5e3c07756229d5dca902065ed2cc926fe0 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 19 Sep 2024 21:49:53 +0100 Subject: Add Python solution to challenge 067 --- challenge-067/paulo-custodio/perl/ch-1.pl | 4 +-- challenge-067/paulo-custodio/python/ch-1.py | 28 ++++++++++++++++++ challenge-067/paulo-custodio/python/ch-2.py | 46 +++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 challenge-067/paulo-custodio/python/ch-1.py create mode 100644 challenge-067/paulo-custodio/python/ch-2.py diff --git a/challenge-067/paulo-custodio/perl/ch-1.pl b/challenge-067/paulo-custodio/perl/ch-1.pl index 2a0587b5df..a2f47d0534 100644 --- a/challenge-067/paulo-custodio/perl/ch-1.pl +++ b/challenge-067/paulo-custodio/perl/ch-1.pl @@ -2,11 +2,11 @@ # Challenge 067 # -# TASK #1 › Number Combinations +# TASK #1 > Number Combinations # Submitted by: Mohammad S Anwar # # You are given two integers $m and $n. Write a script print all possible -# combinations of $n numbers from the list 1 2 3 … $m. +# combinations of $n numbers from the list 1 2 3 ... $m. # # Every combination should be sorted i.e. [2,3] is valid combination but [3,2] is not. # diff --git a/challenge-067/paulo-custodio/python/ch-1.py b/challenge-067/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..2c0021ce8f --- /dev/null +++ b/challenge-067/paulo-custodio/python/ch-1.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +# Challenge 067 +# +# TASK #1 > Number Combinations +# Submitted by: Mohammad S Anwar +# +# You are given two integers $m and $n. Write a script print all possible +# combinations of $n numbers from the list 1 2 3 ... $m. +# +# Every combination should be sorted i.e. [2,3] is valid combination but [3,2] is not. +# +# Example: +# Input: $m = 5, $n = 2 +# +# Output: [ [1,2], [1,3], [1,4], [1,5], [2,3], [2,4], [2,5], [3,4], [3,5], [4,5] ] + +import sys +from itertools import combinations + +m, n = map(int, sys.argv[1:3]) +m_list = list(range(1, m + 1)) +out = set() + +for combo in combinations(m_list, n): + out.add(str(sorted(combo))) + +print("[", ", ".join(sorted(out)), "]") diff --git a/challenge-067/paulo-custodio/python/ch-2.py b/challenge-067/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..4a9e61a95c --- /dev/null +++ b/challenge-067/paulo-custodio/python/ch-2.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +# Challenge 067 +# +# TASK #2 > Letter Phone +# Submitted by: Mohammad S Anwar +# +# You are given a digit string $S. Write a script to print all possible letter +# combinations that the given digit string could represent. +# +# Letter Phone +# +# +# Example: +# Input: $S = '35' +# +# Output: ["dj", "dk", "dl", "ej", "ek", "el", "fj", "fk", "fl"]. + +import sys +from itertools import product + +digits = { + '1': ['_', ',', '@'], + '2': ['a', 'b', 'c'], + '3': ['d', 'e', 'f'], + '4': ['g', 'h', 'i'], + '5': ['j', 'k', 'l'], + '6': ['m', 'n', 'o'], + '7': ['p', 'q', 'r', 's'], + '8': ['t', 'u', 'v'], + '9': ['w', 'x', 'y', 'z'], + '*': [' '] +} + +def letter_phone(s): + if not s: + return [] + + digit_lists = [digits[digit] for digit in s] + combinations = [''.join(combo) for combo in product(*digit_lists)] + return sorted(combinations) + +# Example usage +s = sys.argv[1] +out = letter_phone(s) +print("[" + ", ".join(f'"{item}"' for item in out) + "]") -- cgit From 6efda219c95b36111a3be91d0b7908f0f7e7bd55 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 20 Sep 2024 09:30:28 +0100 Subject: Add Python solution to challenge 068 --- challenge-068/paulo-custodio/Makefile | 2 ++ challenge-068/paulo-custodio/perl/ch-1.pl | 2 +- challenge-068/paulo-custodio/perl/ch-2.pl | 6 ++-- challenge-068/paulo-custodio/python/ch-1.py | 56 +++++++++++++++++++++++++++++ challenge-068/paulo-custodio/python/ch-2.py | 39 ++++++++++++++++++++ 5 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 challenge-068/paulo-custodio/python/ch-1.py create mode 100644 challenge-068/paulo-custodio/python/ch-2.py diff --git a/challenge-068/paulo-custodio/Makefile b/challenge-068/paulo-custodio/Makefile index ba9a2bf399..6bed5804bf 100644 --- a/challenge-068/paulo-custodio/Makefile +++ b/challenge-068/paulo-custodio/Makefile @@ -1,3 +1,5 @@ all: perl perl/ch-1.pl + python3 python/ch-1.py perl perl/ch-2.pl + python3 python/ch-2.py diff --git a/challenge-068/paulo-custodio/perl/ch-1.pl b/challenge-068/paulo-custodio/perl/ch-1.pl index e7279cec76..929071c1c6 100644 --- a/challenge-068/paulo-custodio/perl/ch-1.pl +++ b/challenge-068/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 068 # -# TASK #1 › Zero Matrix +# TASK #1 > Zero Matrix # Submitted by: Mohammad S Anwar # You are given a matrix of size M x N having only 0s and 1s. # diff --git a/challenge-068/paulo-custodio/perl/ch-2.pl b/challenge-068/paulo-custodio/perl/ch-2.pl index b4730ddce8..e61fa4c0bd 100644 --- a/challenge-068/paulo-custodio/perl/ch-2.pl +++ b/challenge-068/paulo-custodio/perl/ch-2.pl @@ -2,15 +2,15 @@ # Challenge 068 # -# TASK #2 › Reorder List +# TASK #2 > Reorder List # Submitted by: Mohammad S Anwar # You are given a singly linked list $L as below: # -# L0 ? L1 ? … ? Ln-1 ? Ln +# L0 ? L1 ? ... ? Ln-1 ? Ln # Write a script to reorder list as below: # # L0 ? Ln ? L1 ? Ln-1 ? L2 ? Ln-2 ? -# You are ONLY allowed to do this in-place without altering the nodes’ values. +# You are ONLY allowed to do this in-place without altering the nodes' values. # # Example # Input: 1 ? 2 ? 3 ? 4 diff --git a/challenge-068/paulo-custodio/python/ch-1.py b/challenge-068/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..6171b18125 --- /dev/null +++ b/challenge-068/paulo-custodio/python/ch-1.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +# Challenge 068 +# +# TASK #1 > Zero Matrix +# Submitted by: Mohammad S Anwar +# You are given a matrix of size M x N having only 0s and 1s. +# +# Write a script to set the entire row and column to 0 if an element is 0. +# +# Example 1 +# Input: [1, 0, 1] +# [1, 1, 1] +# [1, 1, 1] +# +# Output: [0, 0, 0] +# [1, 0, 1] +# [1, 0, 1] +# Example 2 +# Input: [1, 0, 1] +# [1, 1, 1] +# [1, 0, 1] +# +# Output: [0, 0, 0] +# [1, 0, 1] +# [0, 0, 0] + +from copy import deepcopy +import unittest + +def zero_matrix(m): + orig = deepcopy(m) + for r in range(len(m)): + for c in range(len(m[0])): + if not orig[r][c]: + m = zero_row(r, m) + m = zero_col(c, m) + return m + +def zero_row(r, m): + for c in range(len(m[0])): + m[r][c] = 0 + return m + +def zero_col(c, m): + for r in range(len(m)): + m[r][c] = 0 + return m + +class TestZeroMatrix(unittest.TestCase): + def test_zero_matrix(self): + self.assertEqual(zero_matrix([[1, 0, 1], [1, 1, 1], [1, 1, 1]]), [[0, 0, 0], [1, 0, 1], [1, 0, 1]]) + self.assertEqual(zero_matrix([[1, 0, 1], [1, 1, 1], [1, 0, 1]]), [[0, 0, 0], [1, 0, 1], [0, 0, 0]]) + +if __name__ == '__main__': + unittest.main() diff --git a/challenge-068/paulo-custodio/python/ch-2.py b/challenge-068/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..55e0db5737 --- /dev/null +++ b/challenge-068/paulo-custodio/python/ch-2.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +# Challenge 068 +# +# TASK #2 > Reorder List +# Submitted by: Mohammad S Anwar +# You are given a singly linked list $L as below: +# +# L0 ? L1 ? ... ? Ln-1 ? Ln +# Write a script to reorder list as below: +# +# L0 ? Ln ? L1 ? Ln-1 ? L2 ? Ln-2 ? +# You are ONLY allowed to do this in-place without altering the nodes' values. +# +# Example +# Input: 1 ? 2 ? 3 ? 4 +# Output: 1 ? 4 ? 2 ? 3 + +import unittest + +def reorder_list(l): + # get second element + tail = l[1] + # get and remove last element + p = tail + last = None + while len(p) > 1: + last = p + p = p[1] + eln = last.pop() + + return [l[0], [eln[0], tail]] + +class TestReorderList(unittest.TestCase): + def test_reorder_list(self): + self.assertEqual(reorder_list([1, [2, [3, [4]]]]), [1, [4, [2, [3]]]]) + +if __name__ == '__main__': + unittest.main() -- cgit From e2d0ee960837bca9ad2e85a88be5a0375e112ee5 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 20 Sep 2024 09:41:59 +0100 Subject: Add Python solution to challenge 069 --- challenge-069/paulo-custodio/perl/ch-1.pl | 2 +- challenge-069/paulo-custodio/perl/ch-2.pl | 16 +++++----- challenge-069/paulo-custodio/python/ch-1.py | 34 +++++++++++++++++++++ challenge-069/paulo-custodio/python/ch-2.py | 47 +++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 challenge-069/paulo-custodio/python/ch-1.py create mode 100644 challenge-069/paulo-custodio/python/ch-2.py diff --git a/challenge-069/paulo-custodio/perl/ch-1.pl b/challenge-069/paulo-custodio/perl/ch-1.pl index 1bbbf03f2e..04132dac76 100644 --- a/challenge-069/paulo-custodio/perl/ch-1.pl +++ b/challenge-069/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 069 # -# TASK #1 › Strobogrammatic Number +# TASK #1 > Strobogrammatic Number # Submitted by: Mohammad S Anwar # A strobogrammatic number is a number that looks the same when looked at upside # down. diff --git a/challenge-069/paulo-custodio/perl/ch-2.pl b/challenge-069/paulo-custodio/perl/ch-2.pl index 699d9f7240..e09eaa7437 100644 --- a/challenge-069/paulo-custodio/perl/ch-2.pl +++ b/challenge-069/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 069 # -# TASK #2 › 0/1 String +# TASK #2 > 0/1 String # Submitted by: Mohammad S Anwar # A 0/1 string is a string in which every character is either 0 or 1. # @@ -11,22 +11,22 @@ # # switch: # -# Every 0 becomes 1 and every 1 becomes 0. For example, “101” becomes “010”. +# Every 0 becomes 1 and every 1 becomes 0. For example, "101" becomes "010". # # reverse: # -# The string is reversed. For example, "001” becomes “100”. +# The string is reversed. For example, "001" becomes "100". # UPDATE (2020-07-13 17:00:00): # It was brought to my notice that generating S1000 string would be nearly # impossible. So I have decided to lower it down to S30. Please follow the rule # as below: # -# S0 = “” -# S1 = “0” -# S2 = “001” -# S3 = “0010011” +# S0 = "" +# S1 = "0" +# S2 = "001" +# S3 = "0010011" # -# SN = SN-1 + “0” + switch(reverse(SN-1)) +# SN = SN-1 + "0" + switch(reverse(SN-1)) # Note: modified to S20, as S30 was taking forever diff --git a/challenge-069/paulo-custodio/python/ch-1.py b/challenge-069/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..6fa58d6338 --- /dev/null +++ b/challenge-069/paulo-custodio/python/ch-1.py @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +# Challenge 069 +# +# TASK #1 > Strobogrammatic Number +# Submitted by: Mohammad S Anwar +# A strobogrammatic number is a number that looks the same when looked at upside +# down. +# +# You are given two positive numbers $A and $B such that 1 <= $A <= $B <= 10^15. +# +# Write a script to print all strobogrammatic numbers between the given two +# numbers. +# +# Example +# Input: $A = 50, $B = 100 +# Output: 69, 88, 96 + +import sys + +A, B = map(int, sys.argv[1:3]) +out = [] + +def is_strobogrammatic(n): + if not all(c in '0689' for c in str(n)): + return False + inv = str(n).translate(str.maketrans('69', '96')) + return int(inv[::-1]) == n + +for n in range(A, B + 1): + if is_strobogrammatic(n): + out.append(n) + +print(", ".join(map(str, out))) diff --git a/challenge-069/paulo-custodio/python/ch-2.py b/challenge-069/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..2987057e01 --- /dev/null +++ b/challenge-069/paulo-custodio/python/ch-2.py @@ -0,0 +1,47 @@ +#!/usr/bin/env perl + +# Challenge 069 +# +# TASK #2 > 0/1 String +# Submitted by: Mohammad S Anwar +# A 0/1 string is a string in which every character is either 0 or 1. +# +# Write a script to perform switch and reverse to generate S30 as described +# below: +# +# switch: +# +# Every 0 becomes 1 and every 1 becomes 0. For example, "101" becomes "010". +# +# reverse: +# +# The string is reversed. For example, "001" becomes "100". +# UPDATE (2020-07-13 17:00:00): +# It was brought to my notice that generating S1000 string would be nearly +# impossible. So I have decided to lower it down to S30. Please follow the rule +# as below: +# +# S0 = "" +# S1 = "0" +# S2 = "001" +# S3 = "0010011" +# +# SN = SN-1 + "0" + switch(reverse(SN-1)) + +# Note: modified to S20, as S30 was taking forever + +N = 20 + +def bits_switch(s): + return s.translate(str.maketrans('01', '10')) + +def bits_reverse(s): + return ''.join(reversed(s)) + +prev = "" +s = "" +for _ in range(1, N + 1): + s = prev + "0" + bits_switch(bits_reverse(prev)) + prev = s + +print(s) -- cgit From 5354c57e27196ca67ad5e821b3e999867c1aaef3 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 20 Sep 2024 10:23:09 +0100 Subject: Add Python solution to challenge 070 --- challenge-061/paulo-custodio/python/ch-1.py | 2 +- challenge-069/paulo-custodio/python/ch-1.py | 2 +- challenge-069/paulo-custodio/python/ch-2.py | 2 +- challenge-070/paulo-custodio/perl/ch-1.pl | 2 +- challenge-070/paulo-custodio/perl/ch-2.pl | 2 +- challenge-070/paulo-custodio/python/ch-1.py | 55 +++++++++++++++++++++++ challenge-070/paulo-custodio/python/ch-2.py | 68 +++++++++++++++++++++++++++++ 7 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 challenge-070/paulo-custodio/python/ch-1.py create mode 100644 challenge-070/paulo-custodio/python/ch-2.py diff --git a/challenge-061/paulo-custodio/python/ch-1.py b/challenge-061/paulo-custodio/python/ch-1.py index 47e232e6aa..4af02885dd 100644 --- a/challenge-061/paulo-custodio/python/ch-1.py +++ b/challenge-061/paulo-custodio/python/ch-1.py @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +#!/usr/bin/env python3 # Challenge 061 # diff --git a/challenge-069/paulo-custodio/python/ch-1.py b/challenge-069/paulo-custodio/python/ch-1.py index 6fa58d6338..ca37f65b7d 100644 --- a/challenge-069/paulo-custodio/python/ch-1.py +++ b/challenge-069/paulo-custodio/python/ch-1.py @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +#!/usr/bin/env python3 # Challenge 069 # diff --git a/challenge-069/paulo-custodio/python/ch-2.py b/challenge-069/paulo-custodio/python/ch-2.py index 2987057e01..d5fca17564 100644 --- a/challenge-069/paulo-custodio/python/ch-2.py +++ b/challenge-069/paulo-custodio/python/ch-2.py @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +#!/usr/bin/env python3 # Challenge 069 # diff --git a/challenge-070/paulo-custodio/perl/ch-1.pl b/challenge-070/paulo-custodio/perl/ch-1.pl index fc3c102124..6c931b3227 100644 --- a/challenge-070/paulo-custodio/perl/ch-1.pl +++ b/challenge-070/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 070 # -# TASK #1 › Character Swapping +# TASK #1 > Character Swapping # Submitted by: Mohammad S Anwar # You are given a string $S of size $N. # diff --git a/challenge-070/paulo-custodio/perl/ch-2.pl b/challenge-070/paulo-custodio/perl/ch-2.pl index c2873d5bbb..b2106b8252 100644 --- a/challenge-070/paulo-custodio/perl/ch-2.pl +++ b/challenge-070/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 070 # -# TASK #2 › Gray Code Sequence +# TASK #2 > Gray Code Sequence # Submitted by: Mohammad S Anwar # You are given an integer 2 <= $N <= 5. # diff --git a/challenge-070/paulo-custodio/python/ch-1.py b/challenge-070/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..2c3ee74ebb --- /dev/null +++ b/challenge-070/paulo-custodio/python/ch-1.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 + +# Challenge 070 +# +# TASK #1 > Character Swapping +# Submitted by: Mohammad S Anwar +# You are given a string $S of size $N. +# +# You are also given swap count $C and offset $O such that +# $C >= 1, $O >= 1, $C <= $O and $C + $O <= $N. +# +# +# UPDATE: 2020-07-20 16:10:00 +# Pete Houston suggested to put additional constraint i.e. $C <= $O. +# He presented the use case $S = 'abcd', $C = 2, $O = 1. +# +# Write a script to perform character swapping like below: +# +# $S[ 1 % $N ] <=> $S[ (1 + $O) % $N ] +# $S[ 2 % $N ] <=> $S[ (2 + $O) % $N ] +# $S[ 3 % $N ] <=> $S[ (3 + $O) % $N ] +# ... +# ... +# $S[ $C % $N ] <=> $S[ ($C + $O) % $N ] +# Example 1 +# Input: +# $S = 'perlandraku' +# $C = 3 +# $O = 4 +# +# Character Swapping: +# swap 1: e <=> n = pnrlaedraku +# swap 2: r <=> d = pndlaerraku +# swap 3: l <=> r = pndraerlaku +# +# Output: +# pndraerlaku + +import sys + +def swap(s, c, o): + def swap1(s, c, o): + s_list = list(s) + p1 = c % len(s_list) + p2 = (c + o) % len(s_list) + s_list[p1], s_list[p2] = s_list[p2], s_list[p1] + return ''.join(s_list) + + for _ in range(1, c+1): + s = swap1(s, _, o) + return s + + +S, C, O = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]) +print(swap(S, C, O)) diff --git a/challenge-070/paulo-custodio/python/ch-2.py b/challenge-070/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..3064383aef --- /dev/null +++ b/challenge-070/paulo-custodio/python/ch-2.py @@ -0,0 +1,68 @@ +#!/usr/bin/env perl + +# Challenge 070 +# +# TASK #2 > Gray Code Sequence +# Submitted by: Mohammad S Anwar +# You are given an integer 2 <= $N <= 5. +# +# Write a script to generate $N-bit gray code sequence. +# +# 2-bit Gray Code Sequence +# [0, 1, 3, 2] +# To generate the 3-bit Gray code sequence from the 2-bit Gray code sequence, +# follow the step below: +# +# 2-bit Gray Code sequence +# [0, 1, 3, 2] +# +# Binary form of the sequence +# a) S1 = [00, 01, 11, 10] +# +# Reverse of S1 +# b) S2 = [10, 11, 01, 00] +# +# Prefix all entries of S1 with '0' +# c) S1 = [000, 001, 011, 010] +# +# Prefix all entries of S2 with '1' +# d) S2 = [110, 111, 101, 100] +# +# Concatenate S1 and S2 gives 3-bit Gray Code sequence +# e) [000, 001, 011, 010, 110, 111, 101, 100] +# +# 3-bit Gray Code sequence +# [0, 1, 3, 2, 6, 7, 5, 4] +# Example +# Input: $N = 4 +# +# Output: [0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8] + +import sys + +def gray(n): + if n < 2: + raise ValueError("N must be at least 2") + elif n == 2: + return [0, 1, 3, 2] + else: + # gray sequence of N-1 + prev = gray(n-1) + # binary form to S1 + s1 = [format(x, f'0{n-1}b') for x in prev] + # reverse to S2 + s2 = s1[::-1] + # prefix S1 with 0 + s1 = ['0' + x for x in s1] + # prefix S2 with 1 + s2 = ['1' + x for x in s2] + # concatenate + gray_seq = s1 + s2 + # convert to decimal + gray_seq = [int(x, 2) for x in gray_seq] + + return gray_seq + +if __name__ == "__main__": + N = int(sys.argv[1]) if len(sys.argv) > 1 else 2 + print(", ".join(map(str, gray(N)))) -- cgit From 592752b24e8a00f5b61a35cfc5a6f29246cb42a5 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 20 Sep 2024 10:56:05 +0100 Subject: Add Python solution to challenge 071 --- challenge-070/paulo-custodio/python/ch-2.py | 2 +- challenge-071/paulo-custodio/perl/ch-1.pl | 4 +- challenge-071/paulo-custodio/perl/ch-2.pl | 2 +- challenge-071/paulo-custodio/python/ch-1.py | 39 ++++++++++++++++ challenge-071/paulo-custodio/python/ch-2.py | 70 +++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 challenge-071/paulo-custodio/python/ch-1.py create mode 100644 challenge-071/paulo-custodio/python/ch-2.py diff --git a/challenge-070/paulo-custodio/python/ch-2.py b/challenge-070/paulo-custodio/python/ch-2.py index 3064383aef..96724bc983 100644 --- a/challenge-070/paulo-custodio/python/ch-2.py +++ b/challenge-070/paulo-custodio/python/ch-2.py @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +#!/usr/bin/env python3 # Challenge 070 # 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) -- cgit From 4ae5477a9bc6c8b01ee984ed6a5a90dbec170833 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 20 Sep 2024 14:48:44 +0100 Subject: Add Python solution to challenge 072 --- challenge-072/paulo-custodio/perl/ch-1.pl | 2 +- challenge-072/paulo-custodio/perl/ch-2.pl | 2 +- challenge-072/paulo-custodio/python/ch-1.py | 40 +++++++++++++++++++++++++++++ challenge-072/paulo-custodio/python/ch-2.py | 40 +++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 challenge-072/paulo-custodio/python/ch-1.py create mode 100644 challenge-072/paulo-custodio/python/ch-2.py diff --git a/challenge-072/paulo-custodio/perl/ch-1.pl b/challenge-072/paulo-custodio/perl/ch-1.pl index 6c00f85553..d435b392c7 100644 --- a/challenge-072/paulo-custodio/perl/ch-1.pl +++ b/challenge-072/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 072 # -# TASK #1 › Trailing Zeroes +# TASK #1 > Trailing Zeroes # Submitted by: Mohammad S Anwar # You are given a positive integer $N (<= 10). # diff --git a/challenge-072/paulo-custodio/perl/ch-2.pl b/challenge-072/paulo-custodio/perl/ch-2.pl index b01009ee25..506995a434 100644 --- a/challenge-072/paulo-custodio/perl/ch-2.pl +++ b/challenge-072/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 072 # -# TASK #2 › Lines Range +# TASK #2 > Lines Range # Submitted by: Mohammad S Anwar # You are given a text file name $file and range $A - $B where $A <= $B. # diff --git a/challenge-072/paulo-custodio/python/ch-1.py b/challenge-072/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..8f455f1ac8 --- /dev/null +++ b/challenge-072/paulo-custodio/python/ch-1.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +# Challenge 072 +# +# TASK #1 > Trailing Zeroes +# Submitted by: Mohammad S Anwar +# You are given a positive integer $N (<= 10). +# +# Write a script to print number of trailing zeroes in $N!. +# +# Example 1 +# Input: $N = 10 +# Output: 2 as $N! = 3628800 has 2 trailing zeroes +# +# Example 2 +# Input: $N = 7 +# Output: 1 as $N! = 5040 has 1 trailing zero +# +# Example 3 +# Input: $N = 4 +# Output: 0 as $N! = 24 has 0 trailing zero + +import re +import sys + +def fact(n): + if n < 2: + return 1 + else: + return n*fact(n-1) + +def trailing_zeros(n): + s = str(n) + if m := re.search(r'0+$', s): + return len(m.group(0)) + else: + return 0 + +N = int(sys.argv[1]) +print(trailing_zeros(fact(N))) diff --git a/challenge-072/paulo-custodio/python/ch-2.py b/challenge-072/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..6d897d5b2b --- /dev/null +++ b/challenge-072/paulo-custodio/python/ch-2.py @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +# Challenge 072 +# +# TASK #2 > Lines Range +# Submitted by: Mohammad S Anwar +# You are given a text file name $file and range $A - $B where $A <= $B. +# +# Write a script to display lines range $A and $B in the given file. +# +# Example +# Input: +# $ cat input.txt +# L1 +# L2 +# L3 +# L4 +# ... +# ... +# ... +# ... +# L100 +# $A = 4 and $B = 12 +# Output: +# L4 +# L5 +# L6 +# L7 +# L8 +# L9 +# L10 +# L11 +# L12 + +import sys +FILE, A, B = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]) + +f = open(sys.argv[1], "r") +lines = [x.rstrip("\n") for x in f.readlines()] +print("\n".join(lines[A-1:B])) -- cgit