[Tutor] Planning a program with algorithm?

Andrei project5 at redrival.net
Mon May 30 13:30:01 CEST 2005


. , <administrata <at> hotmail.com> writes:

> But, I don't know how to plan a prog. with algorithm.

For small programs there's little value in planning - in fact, you'll probably
end up wasting time. As far as individual algorithms go, it's usually best to go
with whatever seems the easiest to understand and only do more advanced things
once you know that you need more speed or something like that.

> If algorithm of a part of a prog. is like..

That's a pseudocode implementation of an algorithm. You can describe an
algorithm in human language, in pseudocode or in a programming language.

> create an empty jumble word
> while the chosen word has letters in it
>     extract a random letter from the chosen word
>     add the random letter to the jumble word

That's actually *longer* than just writing the algorithm in Python directly,
which is why we say that Python is executable pseudocode :). Pseudo-code design
has IMO little value in Python and should stop at a higher level if it's used at
all. In this example I'd have said this before going to Python code:

  randomize letters in word

By going into too much detail, you've actually overlooked the fact that you're
recreating functionality from the Python standard library: random.shuffle.

> If the prog. is like..
>
---------------------------------------------------------
> #Game List
> 
> list = ("GT4",
>          "GTA Sanandreas",
>          "Takken 5")
> 
> print "My Games: "
> for i in list:
>    print i
> 
> print "\nI've got", len(list), "games."
> 
> add = raw_input(\n\nAdd more games: ")
> adding = (add)
> list += adding
> 
> print "Total games are..."
> print list
> 
> raw_input("exit.")
>
----------------------------------------------------------
> 

The pseudocode could be something like this:

  list all games
  show nr. of games
  get the name of a new game
  add the new game
  list all games
  exit

If you go into more detail, you'll end up writing a long version of the Python
code, which kind of defeats the purpose of pseudocode.

> Any useful advice for algorithm would be appreciated.

It's more important (at least for larger progams) to think about good design in
terms of modules, classes and functions than in terms of individual lines of
code. Always stop designing when designing becomes as low-level as just writing
the code.

Yours,

Andrei



More information about the Tutor mailing list