Discrete Mathematics and Its Applications

Kenneth Rosen

Mentioned 1

Discrete Mathematics and Its Applications is intended for one or two term introductory Discrete Mathematics courses taken by students from a wide variety of majors, including Computer Science, Mathematics, and Engineering. This renowned best-selling text, which has been used at over 500 institutions around the world, gives a focused introduction to the primary themes in a Discrete Mathematics course and demonstrates the relevance and practicality of Discrete Mathematics to a wide variety of real-world applications—from Computer Science to Data Networking, to Psychology, to Chemistry, to Engineering, to Linguistics, to Biology, to Business, and many other important fields. McGraw-Hill Education's Connect, is also available as an optional, add on item. Connect is the only integrated learning system that empowers students by continuously adapting to deliver precisely what they need, when they need it, how they need it, so that class time is more effective. Connect allows the professor to assign homework, quizzes, and tests easily and automatically grades and records the scores of the student's work. Problems are randomized to prevent sharing of answers an may also have a "multi-step solution" which helps move the students' learning along if they experience difficulty.

More on Amazon.com

Mentioned in questions and answers.

I want to build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in alphabetical order. But when I add a string to the list, it shows "argument of type 'NoneType' is not iterable". What' worry?

    fh = ("But soft what light through yonder window breaks"
    "It is the east and Juliet is the sun"
    "Arise fair sun and kill the envious moon"
    "Who is already sick and pale with grief")
    lst = list()
    for line in fh:
        words = line.split()
        for word in line:
                if word not in lst:
                    lst = lst.append(word)
    print lst

I will try to help you out.

First, the quality of the question could be better. I think you should try to provide some rationale to your snippet, that way anyone responding could help you by improving not only your solution, but also the thinking behind it (making you a better coder).

Second, this problem has already been solved with lots of good explanations around the web. Check out e.g. item frequency count in python which has a fantastic number of clever solutions.

Third, and this is perhaps the part that you expected, your question shows both that you need to improve your understanding of Python syntax, functionality and general algorithmic thinking. Let me elaborate.

Python Syntax and Functionality

Many of the other answers highlight that the way you are using fh is not very good. Typically, you would have a multiline string written like this in Python:

fh = '''this is the first row
second row
third row'''

Other notable examples of where you would gain from having better understanding of the syntax and functionality of Python is e.g. list comprehension, builtin types and so on. Walk through the is the Python tutorial. Dig in to it, it is very useful!

Algorithmic Thinking

Usually, when I see code such as yours, I try to figure out if the problem is lack of understanding/knowledge of the tool (in this case Python), or of the solution itself. In you case, I think it is a bit of both.

When it comes to the solution, what many experienced coders do (or at least did in the beginning of their careers), is to work out how they intend to solve the problem. A typical experienced coder would check their toolbox to see what would work, and then apply it.

I would solve the problem by using a set use the set datatype to solve this problem. Sets are pretty convenient in most cases, and are well described in the area of discrete mathematics. Check out the book Discrete Mathematics and Its Applications by Rosen, which covers that and a whole bunch of other stuff that you will find useful in the future. (You can probably find a lot of online resources describing sets, but that book is the reference to the area of discrete mathematics. I recommend getting it.)

Because sets are so convenient, Python naturally has builtin support for it. Again, the Python tutorial comes to the rescue, check out the section on sets.

Possible Solution

So, what could a possible solution look like? I would do the following:

  1. Make sure that everything is in a multiline string.
  2. Split the string into a list.
  3. Create a set based on the list content.
  4. Convert back into a list so that it can be sorted.
  5. Sort.
  6. Print.

None of this requires any for or ifstatements, and uses a lot of the builtin functionality of the language.

Hope this advice will help you onwards!