diff --git a/week10/counting_anagrams.md b/week10/counting_anagrams.md new file mode 100644 index 0000000..db0ba98 --- /dev/null +++ b/week10/counting_anagrams.md @@ -0,0 +1,15 @@ +--- +Author: Kayode +--- + +Counting Anagrams + +Have the function CountingAnagrams(str) take the str parameter and determine how many anagrams exist in the string. An anagram is a new word that is produced from rearranging the characters in a different word, for example: cars and arcs are anagrams. Your program should determine how many anagrams exist in a given string and return the total number. For example: if str is "aa aa odg dog gdo" then your program should return 2 because "dog" and "gdo" are anagrams of "odg". The word "aa" occurs twice in the string but it isn't an anagram because it is the same word just repeated. The string will contain only spaces and lowercase letters, no punctuation, numbers, or uppercase letters. + + +Examples +Input: "aa aa odg dog gdo" +Output: 2 + +Input: "a c b c run urn urn" +Output: 1 \ No newline at end of file diff --git a/week10/counting_anagrams.py b/week10/counting_anagrams.py new file mode 100644 index 0000000..27ad8f2 --- /dev/null +++ b/week10/counting_anagrams.py @@ -0,0 +1,28 @@ +def counting_anagrams(arr): + arr2= arr.split() + anna= 0 + + for i,j in enumerate(arr2): + for k,l in enumerate(arr2): + if j == l and i != k: + arr2.pop(k) + + for i in arr2: + for j in arr2: + if len(i) == len(j): + if i != j: + for k in i: + if k in j: + i.replace(k, '') + else: + break + else: + arr2.remove(j) + anna= anna + 1 + + print(anna) + + + +# counting_anagrams("aa aa odg dog gdo") +counting_anagrams("a c b c run urn urn") \ No newline at end of file