diff options
| author | Steven Wilson <steven1170@zoho.eu> | 2023-09-14 11:10:35 +0100 |
|---|---|---|
| committer | Steven Wilson <steven1170@zoho.eu> | 2023-09-14 11:10:35 +0100 |
| commit | 5e2a6a297cfb4c9d54ca8441e83a9c27f0ec05ef (patch) | |
| tree | 968323a988447cef585150e4051616a620e8aae4 /challenge-234 | |
| parent | 3f85224aa3a84d4fb99755152cba5176cf49b795 (diff) | |
| download | perlweeklychallenge-club-5e2a6a297cfb4c9d54ca8441e83a9c27f0ec05ef.tar.gz perlweeklychallenge-club-5e2a6a297cfb4c9d54ca8441e83a9c27f0ec05ef.tar.bz2 perlweeklychallenge-club-5e2a6a297cfb4c9d54ca8441e83a9c27f0ec05ef.zip | |
add solutions week 234 task 1 in python and javascript
Diffstat (limited to 'challenge-234')
| -rw-r--r-- | challenge-234/steven-wilson/javascript/ch-01.js | 28 | ||||
| -rw-r--r-- | challenge-234/steven-wilson/javascript/test/index.html | 31 | ||||
| -rw-r--r-- | challenge-234/steven-wilson/javascript/test/tests.js | 13 | ||||
| -rw-r--r-- | challenge-234/steven-wilson/python/ch-01.py | 39 |
4 files changed, 111 insertions, 0 deletions
diff --git a/challenge-234/steven-wilson/javascript/ch-01.js b/challenge-234/steven-wilson/javascript/ch-01.js new file mode 100644 index 0000000000..3b52aff987 --- /dev/null +++ b/challenge-234/steven-wilson/javascript/ch-01.js @@ -0,0 +1,28 @@ +"use strict"; + + +function commonCharacters(...words) { + let common = words.shift(); + let countCommon = new Map(); + for (let character of common) { + countCommon.set( character, (countCommon.get(character) ?? 0) + 1 ); + } + for (let word of words) { + let countCharacters = new Map(); + for (let character of word) { + countCharacters.set( character, (countCharacters.get(character) ?? 0) + 1); + } + for (let character of common) { + if( word.indexOf(character) == -1 ) { + let last = common.indexOf(character); + common = common.slice(0, last) + common.slice(last+1); + } + else if (countCommon.get(character) > countCharacters.get(character)){ + countCommon.set( character, (countCommon.get(character) - 1)); + let last = common.lastIndexOf(character); + common = common.slice(0, last) + common.slice(last+1); + } + } + } + return common.split(""); +} diff --git a/challenge-234/steven-wilson/javascript/test/index.html b/challenge-234/steven-wilson/javascript/test/index.html new file mode 100644 index 0000000000..22088af679 --- /dev/null +++ b/challenge-234/steven-wilson/javascript/test/index.html @@ -0,0 +1,31 @@ +<!DOCTYPE html> +<html lang="en"> + +<head> + <meta charset="utf-8" /> + <title>Mocha Tests</title> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + <link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" /> + <script src="https://unpkg.com/mocha/mocha.js"></script> + <script class="mocha-init"> + mocha.setup('bdd'); + </script> + <script src="https://unpkg.com/chai/chai.js"></script> + <script> + // make assert/expect global + let assert = chai.assert; + let expect = chai.expect; + </script> +</head> + +<body> + <script src="../ch-01.js"></script> +<!-- <script src="../ch-02.js"></script>--> + <script src="tests.js"></script> + <div id="mocha"></div> + <script class="mocha-exec"> + mocha.run(); + </script> +</body> + +</html> diff --git a/challenge-234/steven-wilson/javascript/test/tests.js b/challenge-234/steven-wilson/javascript/test/tests.js new file mode 100644 index 0000000000..56b36796a3 --- /dev/null +++ b/challenge-234/steven-wilson/javascript/test/tests.js @@ -0,0 +1,13 @@ +describe("Challenge 234 task 1: Common Characters", function() { + + it("Example 1", function() { + expect(commonCharacters("java", "javascript", "julia")).to.eql(['j', 'a']); + }); + it("Example 2", function() { + expect(commonCharacters("bella", "label", "roller")).to.eql(['e', 'l', 'l']); + }); + it("Example 3", function() { + expect(commonCharacters("cool", "lock", "cook")).to.eql(['c', 'o']); + }); + +}); diff --git a/challenge-234/steven-wilson/python/ch-01.py b/challenge-234/steven-wilson/python/ch-01.py new file mode 100644 index 0000000000..89f42dd5b3 --- /dev/null +++ b/challenge-234/steven-wilson/python/ch-01.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +from collections import Counter + + +def common_characters(*words): + """return all alphabetic characters that show up in all words + including duplicates + >>> common_characters("java", "javascript", "julia") + ('j', 'a') + >>> common_characters("bella", "label", "roller") + ('e', 'l', 'l') + >>> common_characters("cool", "lock", "cook") + ('c', 'o') + """ + words = list(words) + common = list(words.pop(0)) + count_common = Counter(common) + + for word in words: + count_char = Counter(word) + common_copy = common.copy() + for char in common_copy: + if char not in word: + common.remove(char) + elif count_common[char] > count_char[char]: + count_common[char] -= 1 + common.reverse() + common.remove(char) + common.reverse() + + return tuple(common) + + + +if __name__ == "__main__": + import doctest + + doctest.testmod() |
