Learn How to Write Pseudocode: Questions Answered!

  • Context: Comp Sci 
  • Thread starter Thread starter Sunwoo Bae
  • Start date Start date
  • Tags Tags
    Writing
Click For Summary

Discussion Overview

The discussion revolves around the writing and understanding of pseudocode, particularly in the context of an algorithm to sum the three largest numbers from a list of integers. Participants explore various aspects of pseudocode, including its structure, readability, and the appropriateness of using specific functions and language styles.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • Some participants question whether using functions like max() is acceptable in pseudocode.
  • There is a discussion about the time complexity of the provided pseudocode, with some suggesting it could be described as T(n) = an + b, indicating a time complexity of Θ(n) due to the absence of nested loops.
  • Concerns are raised about the clarity of the phrase "i in range (3)" and its implications for understanding the algorithm's intent.
  • Some participants argue that the pseudocode should be understandable to those unfamiliar with the specific programming language, emphasizing the importance of logic flow over syntax.
  • A suggestion is made to maintain a list of the three largest numbers instead of modifying the original list, which could lead to side effects and inefficiencies.
  • There is a debate over the appropriateness of different pseudocode styles, including Python-like syntax versus more traditional approaches.
  • Some participants express that pseudocode does not need to adhere to a single style and can vary based on the audience's understanding.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness and clarity of the pseudocode provided. While some agree on the importance of readability and logic flow, others contest the specific implementation details and the use of certain programming idioms. The discussion remains unresolved regarding the best practices for writing pseudocode.

Contextual Notes

Some participants highlight that the algorithm does not specify a fixed number of inputs, which could lead to misunderstandings about its intended functionality. Additionally, there are concerns about the side effects of modifying the input list and the efficiency of the approach taken in the pseudocode.

Who May Find This Useful

This discussion may be useful for individuals learning about pseudocode, algorithm design, and those interested in best practices for writing clear and effective pseudocode for various audiences.

Sunwoo Bae
Messages
60
Reaction score
4
Homework Statement
Take as input a list of integers A and find the sum of three maximum numbers
Relevant Equations
.
I just learned about pseudocode, and I am confused about how I should use/write it.
For the question,

1: sum ← 0
2: For i in range (3) do:
3: sum ← sum +max(A)
4: remove max(A) from list A
5: return sum

The following is the kind of pseudocode I want to write.

I have several questions:
Is using functions like max() okay when writing pseudocode?
Would it be right to say that the above code can be described by T(n) = an + b and will thus have time complexity of Θ(n), as there is no nested loop?
Is using informal language like the fourth line (remove max(A) from list A) okay?

Thank you ahead!
 
Physics news on Phys.org
Don't overthink this. Pseudo-code is an informal way of describing an algorithm.

Some people use a python-like pseudo-code style with minimal syntactic elements. Python style is nice because the indentation helps organize the pseudo-code.

This wiki article on pseudo-code shows several styles.

https://en.wikipedia.org/wiki/Pseudocode

I favor the python style (not listed) for the reasons I mentioned earlier.

There is also Structured English as a style of pseudo-code that works well until you have many decisions to make.

https://en.wikipedia.org/wiki/Structured_English

Lastly, there is also an actual programming language called Andover Plain English used in Building Mgmt. Systems to monitor and control AC heat, lighting... and while not pseudo-code emulates it to some degree.

An example of Andover Plain English is shown below:

https://github.com/mitchpaulus/andover-plain-english-udl/blob/master/example.png
 
Last edited:
  • Like
Likes   Reactions: PeroK
Sunwoo Bae said:
Homework Statement:: Take as input a list of integers A and find the sum of three maximum numbers
Relevant Equations:: .

I just learned about pseudocode, and I am confused about how I should use/write it.
For the question,

1: sum ← 0
2: For i in range (3) do:
3: sum ← sum +max(A)
4: remove max(A) from list A
5: return sum

The following is the kind of pseudocode I want to write.

I have several questions:
Is using functions like max() okay when writing pseudocode?
Would it be right to say that the above code can be described by T(n) = an + b and will thus have time complexity of Θ(n), as there is no nested loop?
Is using informal language like the fourth line (remove max(A) from list A) okay?

Thank you ahead!
The most important thing about pseudocode is that it should be understandable by someone who doesn't know the target programming language. And, the focus is on the logic flow of what your program is doing; and, not the precise syntax of the coding languague.
 
  • Like
Likes   Reactions: FactChecker
PeroK said:
The most important thing about pseudocode is that it should be understandable by someone who doesn't know the target programming language. And, the focus is on the logic flow of what your program is doing; and, not the precise syntax of the coding languague.
what he said (small).jpg
 
  • Like
Likes   Reactions: PeroK
Your pseudocode is a good example of the benefit of pseudocode. It allows you to discuss an algorithm before getting into the details of making compilable code. For instance, I think you want to keep an updated list, Max3, of the three largest to date. For each new number, see if it should replace one of the Max3 entries and update the sum appropriately. Can you rewrite your pseudocode for that or, if I have misunderstood, make your current version clear?
 
A couple of observations:

It is not obvious (to me) what "i in range (3)" is meant to do. (I am quite familiar with For and Do loops - that's not the problem.) What is range(3)? I mean, presumably you're wanting to iterate through the (ostensibly) three inputs, but your use of "range" and "3" doesn't make that clear.

But, more-to-the-point: I think you have misunderstood the algorithm. It does not specify only three inputs. It could be any number. But the idea is to sum only the largest three.
 
Last edited:
  • Like
Likes   Reactions: FactChecker
DaveC426913 said:
It is not obvious (to me) what "i in range (3)" is meant to do. (I am quite familiar with For and Do loops - that's not the problem.)
This is a standard Python idiom. "for i in range(3)" means to iterate something for i = 0, 1, and 2. IOW, for values starting at 0 up to, but not including 3.

for i in range(0, 3) is exactly the same, again starting at 0 and going up to, but not including, 3.

for i in range(1, 6, 2) starts i off at 1, but with steps of 2. Here i takes on values of 1, 3, and 5.
 
Last edited:
Mark44 said:
This is a standard Python idiom.
Yeah, which is why it doesn't really make good pseudocode. I'm being a bit nitpicky here. Every language has a for loop.

The bigger question is why the hard-coded 3?
The answer is: because of the misinterpretation of the stated problem.
 
DaveC426913 said:
Yeah, which is why it doesn't really make good pseudocode.
Sure it does. There's not just one style of pseudocode. As @jedishrfu mentioned, a Python-ish style is just one possibility.
DaveC426913 said:
Every language has a for loop.
But Python's for loop uses range() exclusively, and is unlike for loops in most other high-level languages.
DaveC426913 said:
The bigger question is why the hard-coded 3?
Most likely because the code is supposed to find the 3 largest numbers in the list.
 
  • #10
Mark44 said:
Sure it does. There's not just one style of pseudocode. As @jedishrfu mentioned, a Python-ish style is just one possibility.

But Python's for loop uses range() exclusively, and is unlike for loops in most other high-level languages.
One point of pseudocode is that it should be readable by someone whodoesnt know the language.
Mark44 said:
Most likely because the code is supposed to find the 3 largest numbers in the list.
Tjats not what it's doing
 
  • #11
Mark44 said:
This is a standard Python idiom. "for i in range(3)" means to iterate something for i = 0, 1, and 2. IOW, for values starting at 0 up to, but not including 3.

for i in range(0, 3) is exactly the same, again starting at 0 and going up to, but not including, 3.

for i in range(1, 6, 2) starts i off at 1, but with steps of 2. Here i takes on values of 1, 3, and 5.
The fact that this is only an exact description in the Python language is a handycap for pseudocode. Pseudocode is better if it is understandable by non-Python people.
 
  • Like
Likes   Reactions: phinds and DaveC426913
  • #12
When i write pseudocode for loops i revert to a BASIC style

For i= 1,10 step 2

Or

For i=1 to 10 step 2
 
  • Like
Likes   Reactions: FactChecker
  • #13
DaveC426913 said:
Tjats not what it's doing
Right, but I offered a possible explanation of why 3 was hardcoded. Whether the algorithm is correct is a different matter.
 
  • #14
Pseudo code is pseudo code it’s not a language and doesn’t need to follow any particular style. We can agree to disagree as to how something should be written with the onus on the writer to make it understandable to his/ her audience.

I think we’ve pretty much exhausted this topic and so without further ado I hereby close this thread and thank all who contributed here.
 
  • Like
Likes   Reactions: Mark44, berkeman and FactChecker
  • #15
@Sunwoo Bae your code is not wrong, I think @Mark44 and @DaveC426913 have misread it due to formatting - you should post code (even pseudocode) inside "code" tags so the spaces display properly:
Code:
sum ← 0
For i in range (3) do:
    sum ← sum +max(A)
    remove max(A) from list A
return sum
If anyone thinks this doesn't work, look closer at line 4. However this is still not a good solution for two main reasons:
  1. As well as calculating the sum, the code modifies the input list (by removing the greatest 3 values). This is known as a side effect and is almost always undesireable.
  2. Removing an item from a list can be an expensive process depending on the implementation, and if the list has many items then you don't want to traverse it 3 times to find the maximum. Athough it may still run in time ~kn, it will have a much bigger k than a single list traversal keeping track of 3 maximum values as you go as @FactChecker suggested.
 
  • Informative
  • Like
Likes   Reactions: DaveC426913 and FactChecker

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 7 ·
Replies
7
Views
2K