Another Python assignment from my programming languages class:
Write a program that will find isograms from user input.
dog is a 1 letter isogram.
TartAr is a 2 letter isogram.
Remove is not an isogram.
Aqwsssaqwaqwswqa is a 4 letter isogram.
Test-test has an illegal character
Here's my answer:
#!/usr/bin/python
def char_count(word):
"""Return a dictionary of character counts."""
chars = {} # Should use py2.7's Counter class, not dict
for char in word: chars[char] = chars.get(char,0) + 1
return chars
def func(word):
"""If word is an isogram, return its degree, otherwise return 0."""
if not word.isalpha(): raise Exception("Illegal character.")
vals = char_count(word).values()
return vals[0] if not any([cmp(vals[0],i) for i in vals]) else 0
4 comments: