Python - Writing pseudocode?

54,405

Solution 1

I would be even more generic eg.

Loop with x from 1 to 8
    Loop with y from 1 to 8
        draw square at x, y

Solution 2

Pseudo code is writing out the code in form that is like code but not quite code. So for opening a file and printing printing out its lines of text

if file exists(path_to_file) then :
 open (path_to_file)
 for each line in file  : print the line of the file

All you should do is create the sequence of steps needed for your problem and write it out like that. Since you mention python, just use use a more python like syntax in your pseudo code.

I suspect that you problem will be to encourage you to consider how to make functions and classes, and writing the pseudo code first will help you do this.

Solution 3

Wikipedia articles use Pseudocode a lot, quite successfully. There is no standard for Pseudocode on wikipedia, and syntax varies, but here is some general information with examples: Algorithms on Wikipedia

Here are two good examples of articles with Pseudocode (more):

Using Wikipedia-like style, I'd do:

for i from 0 to 7
    for j from 0 to 7
        if (i + j) is even then
            paint square (i, j) black
        else
            paint square (i, j) white

(Marking end of if or end of for with 'end if' or 'repeat'/'end for' is a matter of style I guess).

Solution 4

Just write something that looks like a hybrid between code and normal human explanation.

for i from 1 to 8
    for j from 1 to 8
        print "[ ]"
    print "\n"

Solution 5

I'm guessing this is a class assignment, right?

In short, pseudocode is very similar to an outline. It's the structure of how you're going to go about solving the problem, without the specific details.

In this case, you'd probably use a couple for-loops, and sketch out the drawing and there...

for x in range(0,10):
    for y in range(0,10):
        #print out the square (x,y)
Share:
54,405
Admin
Author by

Admin

Updated on March 05, 2020

Comments

  • Admin
    Admin about 4 years

    How would you write pseudocode for drawing an 8-by-8 checkerboard of squares, where none of the squares have to be full? (Can all be empty)

    I don't quite get the pseudocode concept.

  • Thomas Owens
    Thomas Owens over 14 years
    You suggest using a more Python-like syntax in pseudocode. But then, it's not pseudocode anymore. All pseudocode should be language independent, using plain English (or, in some cases, your native language). No language constructs should be used.
  • Admin
    Admin over 14 years
    So that would be all there is to it? You don't have to include actual code?
  • greatgesture
    greatgesture over 14 years
    @Steve: pseudo=fake. so no code is needed :-)
  • Bugmaster
    Bugmaster over 14 years
    Given the sheer number of languages out there, chances are that your pseudocode will resemble at least one of them. I say, don't sweat it, just make sure your pseudo-syntax is somewhat consistent.
  • Alex Martelli
    Alex Martelli over 14 years
    @Thomas, Wikipedia (at en.wikipedia.org/wiki/Pseudocode) disagrees -- showing how pseudocode can be and generally is Pascal-like, math-like, etc, etc -- and I agree with them. See the 90+ articles listed at en.wikipedia.org/wiki/Pseudocode and you'll see they use pseudocode including programming constructs such as if, then, else, while and so on. Natural language is too often ambiguous and verbose -- e.g. saying "the bit-by-bit exclusive-or operation executed on the binary-string equivalents of integers x and y" rather than "x XOR y" would be just silly.
  • Preet Sangha
    Preet Sangha over 14 years
  • alpha_989
    alpha_989 about 6 years
    I am guessing the idea of functions and classes are language agnostic because its part of OOP. Struggling how to talk about classes in a pseudocode without talking about clases. In your opinion, is it ok to use function names and class names in PseudoCode?
  • Preet Sangha
    Preet Sangha about 6 years
    Yes. It's about conveying information. Classes and functions are programming abstractions that languages can implement in many ways, but using classes and functions are absolutely correct in pseudocode.