Skip to content

Instantly share code, notes, and snippets.

@pinkopaque22
Last active April 3, 2016 00:58
Show Gist options
  • Save pinkopaque22/2b0391ec26ea727eb1bcab8bdb2ef3dc to your computer and use it in GitHub Desktop.
Save pinkopaque22/2b0391ec26ea727eb1bcab8bdb2ef3dc to your computer and use it in GitHub Desktop.
/////Problem/////
You are going to be given a word. Your job will be to make sure that each character in that word has the exact same number of occurrences. You will return true if it is valid, or false if it is not.
For example:
"abcabc" is a valid word because 'a' appears twice, 'b' appears twice, and'c' appears twice.
"abcabcd" is NOT a valid word because 'a' appears twice, 'b' appears twice, 'c' appears twice, but 'd' only appears once!
"123abc!" is a valid word because all of the characters only appear once in the word.
For this kata, capitals are considered the same as lowercase letters. Therefore: 'A' == 'a' .
/////Input/////
A string (no spaces) containing [a-z],[A-Z],[0-9] and common symbols. The length will be 0 < string < 100.
//////Tests///////
Test.assert_equals(validate_word("abcabc"),True)
Test.assert_equals(validate_word("Abcabc"),True)
Test.assert_equals(validate_word("AbcabcC"),False)
Test.assert_equals(validate_word("AbcCBa"),True)
Test.assert_equals(validate_word("pippi"),False)
Test.assert_equals(validate_word("?!?!?!"),True)
Test.assert_equals(validate_word("abc123"),True)
Test.assert_equals(validate_word("abcabcd"),False)
Test.assert_equals(validate_word("abc!abc!"),True)
Test.assert_equals(validate_word("abc:abc"),False)
/////Output////
def validate_word(word):
list1 = []
list2 = []
dict1 = {}
OneTime = 'False'
list1 = list(word)
for k, v in enumerate(list1):
if v.isalpha:
list2.append(str.lower(v))
for i in list2:
if i in dict1:
dict1[str(i)] += 1
else:
dict1[str(i)] = 1
for k, v in dict1.items():
print (str(k)+ ':'+ str(v))
if(OneTime == 'False'):
OneTime = 'True'
my_first_val = dict1[k]
if my_first_val != v:
invalid_word = 'True'
return False
return True
print validate_word("word")
/////Clever via :hiasen, :Unnamed, :MMMAAANNN, :christoph531, :mihaild////
from collections import Counter
def validate_word(word):
return len(set(Counter(word.lower()).values())) == 1
@pinkopaque22
Copy link
Author

Codewars character_counter. Compares words in a list and returns true if same number of occurances or false if not.
Resource
http://www.codewars.com/kata/56786a687e9a88d1cf00005d/train/python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment