[Tutor] Planning a program with algorithm?

Andrei project5 at redrival.net
Mon May 30 22:30:35 CEST 2005


. , <administrata <at> hotmail.com> writes:  
  
> And I'm doing chapter4. In the book it says it's recommended to plan a prog.   
> with pseudocode.  
>   
> Can i just ignore it?  
  
Let me expand a bit on my previous post. You can use pseudocode iteratively,  
starting at a very high level and dropping lower until you start recognizing 
your programming language. Let's go back to your shuffling example and assume 
that code is part of a weak password generation tool. You could start with 
this (either in your editor, or in your mind):  
  
  get source of letters  
  process letters to generate password  
  show password  
  
Then you can start expanding. What is that processing? Could be this:  
  
  ask user for the desired password length  
  randomize source of letters   
  return piece of desired length  
  
At this point you probably already recognize Python code directly in there: a  
raw_input with validity checking (password can't be longer than the source), a  
random.shuffle and a list slice combined with a join(). There's no point in  
going even deeper, because you'd end up writing this:  
  
  while desired length larger than source or smaller than 1  
    get desired length  
  convert source to list  
  shuffle source  
  take slice of desired length 
  return joined slice 
 
That's just a clumsy way of writing Python and therefore a waste of time. If 
on the other hand you were programming in ObjectPascal, C++ or something 
similar, this code would still not be quite high level, because shuffling the 
source, taking a slice and such are not built into those languages. In fact, 
in those languages you'd most likely choose a different approach at this 
level. 
 
Yours, 
 
Andrei 



More information about the Tutor mailing list