1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#!/usr/bin/python3
# https://theweeklychallenge.org/blog/perl-weekly-challenge-256/#TASK1
#
# Task 1: Maximum Pairs
# =====================
#
# You are given an array of distinct words, @words.
#
# Write a script to find the maximum pairs in the given array. The words
# $words[i] and $words[j] can be a pair one is reverse of the other.
#
## Example 1
##
## Input: @words = ("ab", "de", "ed", "bc")
## Output: 1
##
## There is one pair in the given array: "de" and "ed"
#
## Example 2
##
## Input: @words = ("aa", "ba", "cd", "ed")
## Output: 0
#
## Example 3
##
## Input: @words = ("uv", "qp", "st", "vu", "mn", "pq")
## Output: 2
#
############################################################
##
## discussion
##
############################################################
#
# Just check all possible pairs.
def reverse_word(word: str) -> str:
chars = list(word)
return "".join(reversed(chars))
def maximum_pairs(words: list) -> None:
print("Input: (\"", "\", \"".join(words), "\")", sep="")
count = 0
for i in range(len(words)):
for j in range(i+1, len(words)):
if words[i] == reverse_word(words[j]):
count += 1
break
print(f"Output: {count}")
maximum_pairs(["ab", "de", "ed", "bc"]);
maximum_pairs(["aa", "ba", "cd", "ed"]);
maximum_pairs(["uv", "qp", "st", "vu", "mn", "pq"]);
|