diff options
| -rw-r--r-- | challenge-111/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-111/paulo-custodio/python/ch-1.py | 78 | ||||
| -rw-r--r-- | challenge-111/paulo-custodio/python/ch-2.py | 35 | ||||
| -rwxr-xr-x | challenge-111/paulo-custodio/test.pl | 4 |
4 files changed, 115 insertions, 4 deletions
diff --git a/challenge-111/paulo-custodio/Makefile b/challenge-111/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-111/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-111/paulo-custodio/python/ch-1.py b/challenge-111/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..44b45019f2 --- /dev/null +++ b/challenge-111/paulo-custodio/python/ch-1.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 + +# Challenge 111 +# +# TASK #1 - Search Matrix +# Submitted by: Mohammad S Anwar +# You are given 5x5 matrix filled with integers such that each row is sorted +# from left to right and the first integer of each row is greater than the +# last integer of the previous row. +# +# Write a script to find a given integer in the matrix using an efficient +# search algorithm. +# +# Example +# Matrix: [ 1, 2, 3, 5, 7 ] +# [ 9, 11, 15, 19, 20 ] +# [ 23, 24, 25, 29, 31 ] +# [ 32, 33, 39, 40, 42 ] +# [ 45, 47, 48, 49, 50 ] +# +# Input: 35 +# Output: 0 since it is missing in the matrix +# +# Input: 39 +# Output: 1 as it exists in the matrix + +import sys + +data = [[ 1, 2, 3, 5, 7 ], + [ 9, 11, 15, 19, 20 ], + [ 23, 24, 25, 29, 31 ], + [ 32, 33, 39, 40, 42 ], + [ 45, 47, 48, 49, 50 ]] + +def find_col(n, row): + l = 0 + h = len(data[row])-1 + if n < data[row][0] or n > data[row][-1]: + return -1 + while l < h: + m = int((l+h)/2) + if n < data[row][m]: + h = m-1 + elif n > data[row][m]: + l = m+1 + else: + return m + if n != data[row][l]: + return -1 + else: + return l + +def find_row(n): + l = 0 + h = len(data)-1 + if n < data[0][0] or n > data[-1][-1]: + return -1 + while l < h: + m = int((l+h)/2) + if n < data[m][0]: + h = m-1 + elif n > data[m][-1]: + l = m+1 + else: + return m + return l + +def find(n): + row = find_row(n) + if row < 0: + return 0 + col = find_col(n, row) + if col < 0: + return 0 + else: + return 1 + +print(find(int(sys.argv[1]))) diff --git a/challenge-111/paulo-custodio/python/ch-2.py b/challenge-111/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..43aca5f927 --- /dev/null +++ b/challenge-111/paulo-custodio/python/ch-2.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +# Challenge 111 +# +# TASK #2 - Ordered Letters +# Submitted by: E. Choroba +# Given a word, you can sort its letters alphabetically (case insensitive). +# For example, "beekeeper" becomes "beeeeekpr" and "dictionary" becomes +# "acdiinorty". +# +# Write a script to find the longest English words that don't change when +# their letters are sorted. + +import fileinput +import sys + +def read_input(): + lines = [] + for line in fileinput.input(): + lines.append(line) + return lines + +def find_longest(lines): + max_len = 0 + words = [] + for word in lines: + word = word.strip().lower() + drow = "".join(sorted(word)) + if word==drow: + max_len = max(max_len, len(word)) + words.append(word) + words = list(filter(lambda x: len(x)==max_len, words)) + return words + +print(" ".join(find_longest(read_input()))) diff --git a/challenge-111/paulo-custodio/test.pl b/challenge-111/paulo-custodio/test.pl deleted file mode 100755 index ba6c37260b..0000000000 --- a/challenge-111/paulo-custodio/test.pl +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env perl -use Modern::Perl; -use Test::More; -require '../../challenge-001/paulo-custodio/test.pl'; |
