Comp Sci Learn How to Write Pseudocode: Questions Answered!

  • Thread starter Thread starter Sunwoo Bae
  • Start date Start date
  • Tags Tags
    Writing
AI Thread Summary
Pseudocode serves as an informal method to describe algorithms, focusing on logic rather than specific syntax. The use of functions like max() is acceptable, but the pseudocode should remain understandable to those unfamiliar with programming languages. The discussion highlights that hardcoding values, such as the number three in the loop, may lead to misunderstandings of the algorithm's intent. It is suggested that pseudocode should avoid side effects, like modifying the input list, which can complicate the algorithm's efficiency. Overall, clarity and readability are paramount when writing pseudocode.
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 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 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 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 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 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 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 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 DaveC426913 and FactChecker

Similar threads

Replies
3
Views
2K
Replies
7
Views
2K
Replies
8
Views
2K
Replies
2
Views
4K
Replies
4
Views
1K
Replies
2
Views
5K
Replies
7
Views
2K
Back
Top