Pages

Isograms

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:

  1. None of these work, or at least I can't use python correctly. >:( Perl was easier to run and test.
    ReplyDelete
  2. I believe that without a good interactive shell, perl was more difficult to run and test. For some info on running python scripts, go to http://kaleb.hornsby.ws/CS/Python#TOC-Using-Python
    ReplyDelete
  3. noticed one problem you might have...the code would regard uppercase letters as different characters.
    ReplyDelete