diff options
Diffstat (limited to 'challenge-307/steven-wilson')
| -rw-r--r-- | challenge-307/steven-wilson/perl/ch-1.pl | 15 | ||||
| -rw-r--r-- | challenge-307/steven-wilson/perl/ch-2.pl | 23 | ||||
| -rw-r--r-- | challenge-307/steven-wilson/python/ch-1.py | 21 | ||||
| -rw-r--r-- | challenge-307/steven-wilson/python/ch-2.py | 39 |
4 files changed, 98 insertions, 0 deletions
diff --git a/challenge-307/steven-wilson/perl/ch-1.pl b/challenge-307/steven-wilson/perl/ch-1.pl new file mode 100644 index 0000000000..452ffb9c67 --- /dev/null +++ b/challenge-307/steven-wilson/perl/ch-1.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl + +use v5.35; +use Test2::Bundle::More; + +sub checkOrder { + my @ints = @_; + my @sorted_ints = sort { $a <=> $b } @ints; + return grep { $ints[$_] != $sorted_ints[$_] } 0..scalar @ints - 1; +} + +is_deeply([checkOrder(5, 2, 4, 3, 1)], [0, 2, 3, 4], "Test 1" ); +is_deeply([checkOrder(1, 2, 1, 1, 3)], [1, 3], "Test 2" ); +is_deeply([checkOrder(3, 1, 3, 2, 3)], [0, 1, 3], "Test 3" ); +done_testing(); diff --git a/challenge-307/steven-wilson/perl/ch-2.pl b/challenge-307/steven-wilson/perl/ch-2.pl new file mode 100644 index 0000000000..68a4819a5c --- /dev/null +++ b/challenge-307/steven-wilson/perl/ch-2.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl + +use v5.35; +use Test2::Bundle::More; +use Memoize; +memoize('sortString'); + +sub findAnagrams { + my @words = @_; + my @drop_index = grep { + sortString($words[$_]) eq + sortString($words[$_ + 1]) } + 0..scalar @words - 2; + return scalar @words - scalar @drop_index; +} + +sub sortString { + return join "", sort split //, shift; +} + +is(findAnagrams("acca", "dog", "god", "perl", "repl") , 3, "Test 1"); +is(findAnagrams("abba", "baba", "aabb", "ab", "ab") , 2, "Test 2"); +done_testing(); diff --git a/challenge-307/steven-wilson/python/ch-1.py b/challenge-307/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..555435eb77 --- /dev/null +++ b/challenge-307/steven-wilson/python/ch-1.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + + +def check_order(integers): + """ Given an array of integers, re-arrange the given array in an increasing + order and return the indices where it differs from the original array. + + >>> check_order((5, 2, 4, 3, 1)) + (0, 2, 3, 4) + >>> check_order((1, 2, 1, 1, 3)) + (1, 3) + >>> check_order((3, 1, 3, 2, 3)) + (0, 1, 3) + """ + return tuple(n for n, i in enumerate(sorted(integers)) if i != integers[n]) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) diff --git a/challenge-307/steven-wilson/python/ch-2.py b/challenge-307/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..f910e3bf29 --- /dev/null +++ b/challenge-307/steven-wilson/python/ch-2.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + + +def find_anagrams(*words): + """ Given a list of words, find any two consecutive words and if they are + anagrams, drop the first word and keep the second. You continue this until + there is no more anagrams in the given list and return the count of final + list. + >>> find_anagrams("acca", "dog", "god", "perl", "repl") + 3 + >>> find_anagrams("abba", "baba", "aabb", "ab", "ab") + 2 + """ + length_words = len(words) + drop = 0 + + if length_words < 2: + raise ValueError("At least 2 arguments are required") + + for n, _ in enumerate(words): + if n < length_words - 1 and are_anagrams(words[n], words[n+1]): + drop += 1 + return length_words - drop + + +def are_anagrams(first, second): + """ Are two words an anagram of each other + >>> are_anagrams("dog", "god") + True + >>> are_anagrams("aabb", "ab") + False + """ + return sorted(first) == sorted(second) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) |
