diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-16 20:06:35 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-16 20:06:35 +0000 |
| commit | df42df8b73d85878b0a58a345f1b9c089dc12e5e (patch) | |
| tree | d6860613c199d73fd094c9ac2a40df37d0dabc2d /challenge-256/packy-anderson/python | |
| parent | db57470322df59135d8475ce632b6b016315ac01 (diff) | |
| parent | d23fbdd681ca2a9b02c5cb2234caa61d5ec32cef (diff) | |
| download | perlweeklychallenge-club-df42df8b73d85878b0a58a345f1b9c089dc12e5e.tar.gz perlweeklychallenge-club-df42df8b73d85878b0a58a345f1b9c089dc12e5e.tar.bz2 perlweeklychallenge-club-df42df8b73d85878b0a58a345f1b9c089dc12e5e.zip | |
Merge pull request #9591 from packy/master
Challenge 256 solutions by Packy Anderson
Diffstat (limited to 'challenge-256/packy-anderson/python')
| -rwxr-xr-x | challenge-256/packy-anderson/python/ch-1.py | 36 | ||||
| -rwxr-xr-x | challenge-256/packy-anderson/python/ch-2.py | 23 |
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-256/packy-anderson/python/ch-1.py b/challenge-256/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..d5aab629f4 --- /dev/null +++ b/challenge-256/packy-anderson/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +def maximumPairs(words): + count = 0 + while words: + # the the first word off the list + first = words.pop(0) + # now compare to the rest of the words in the list + for i in range(len(words)): + second = words[i] + if first == second[::-1]: + # we found a pair + count += 1 + # remove words[i] from the list + words.pop(i) + # we don't need to compare + # any more words to first + break + return count + +def comma_join(arr): + return '", "'.join(arr) + +def solution(words): + print(f'Input: @words = ({words})') + count = maximumPairs(words) + print(f'Output: {count}') + +print('Example 1:') +solution(["ab", "de", "ed", "bc"]) + +print('\nExample 2:') +solution(["aa", "ba", "cd", "ed"]) + +print('\nExample 3:') +solution(["uv", "qp", "st", "vu", "mn", "pq"]) diff --git a/challenge-256/packy-anderson/python/ch-2.py b/challenge-256/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..a178ffab9e --- /dev/null +++ b/challenge-256/packy-anderson/python/ch-2.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python + +def mergeStrings(str1, str2): + chars1 = list(str1) + chars2 = list(str2) + result = '' + while chars1 or chars2: + if chars1: + result += chars1.pop(0) + if chars2: + result += chars2.pop(0) + return result + +def solution(str1, str2): + print(f'Input: $str1 = "{str1}", $str2 = "{str2}"') + output = mergeStrings(str1, str2) + print(f'Output: {output}') + +print('Example 1:') +solution("abcd", "1234") + +print('\nExample 2:') +solution("abc", "12345")
\ No newline at end of file |
