Questions tagged [pseudocode]

Pseudocode is a compact and informal high-level description of a computer programming algorithm. It represents the code and may look similar to the code or code constructs, but it isn't actual code. It is a representation of the code or code construct. Use this tag for questions which are about pseudocode. Do not use this tag for questions which include pseudocode incidentally.

An easy way of understanding pseudocode is by comparing it to the actual programming language.

For example, you would like to write a code that checks if the student's grade is above the permissible threshold, and if it is then you would like to indicate that the student passed.

This is how it is achieved in Python:

if studentsGrade >= 70:
    print("Student passed")
else
    print("Student failed")

Pseudocode to describe the abovementioned code will be as follows:

if a student's grade is greater than or equal to 70
    Print "Student passed"
else
    Print "Student failed"
1942 questions
204
votes
10 answers

What are the mathematical/computational principles behind this game?

My kids have this fun game called Spot It! The game constraints (as best I can describe) are: It is a deck of 55 cards On each card are 8 unique pictures (i.e. a card can't have 2 of the same picture) Given any 2 cards chosen from the deck, there…
Callmeed
  • 4,972
  • 5
  • 34
  • 49
182
votes
26 answers

Algorithm to calculate the number of divisors of a given number

What would be the most optimal algorithm (performance-wise) to calculate the number of divisors of a given number? It'll be great if you could provide pseudocode or a link to some example. EDIT: All the answers have been very helpful, thank you. I'm…
sker
  • 17,842
  • 8
  • 37
  • 41
133
votes
14 answers

Quicksort: Choosing the pivot

When implementing Quicksort, one of the things you have to do is to choose a pivot. But when I look at pseudocode like the one below, it is not clear how I should choose the pivot. First element of list? Something else? function quicksort(array) …
Jacob T. Nielsen
  • 2,938
  • 6
  • 26
  • 30
81
votes
3 answers

Static (Lexical) Scoping vs Dynamic Scoping (Pseudocode)

Program A() { x, y, z: integer; procedure B() { y: integer; y=0; x=z+1; z=y+2; } procedure C() { z: integer; procedure D() { x: integer; x = z…
petrov
  • 1,085
  • 3
  • 12
  • 13
60
votes
9 answers

Two Rectangles intersection

I have two rectangles characterized by 4 values each : Left position X, top position Y, width W and height H: X1, Y1, H1, W1 X2, Y2, H2, W2 Rectangles are not rotated, like so: +--------------------> X axis | | (X,Y) (X+W, Y) | …
Majid Laissi
  • 19,188
  • 19
  • 68
  • 105
54
votes
5 answers

Algorithm to find which number in a list sum up to a certain number

I have a list of numbers. I also have a certain sum. The sum is made from a few numbers from my list (I may/may not know how many numbers it's made from). Is there a fast algorithm to get a list of possible numbers? Written in Python would be great,…
avacariu
  • 2,780
  • 3
  • 25
  • 25
51
votes
11 answers

From milliseconds to hour, minutes, seconds and milliseconds

I need to go from milliseconds to a tuple of (hour, minutes, seconds, milliseconds) representing the same amount of time. E.g.: 10799999ms = 2h 59m 59s 999ms The following pseudo-code is the only thing I could come up with: # The division operator…
Mads Skjern
  • 5,648
  • 6
  • 36
  • 40
49
votes
8 answers

Find the paths between two given nodes?

Say I have nodes connected in the below fashion, how do I arrive at the number of paths that exist between given points, and path details? 1,2 //node 1 and 2 are connected 2,3 2,5 4,2 5,11 11,12 6,7 5,6 3,6 6,8 8,10 8,9 Find the paths from 1 to 7:…
yesraaj
  • 46,370
  • 69
  • 194
  • 251
47
votes
4 answers

What is the meaning of "from distinct vertex chains" in this nearest neighbor algorithm?

The following pseudo-code is from the first chapter of an online preview version of The Algorithm Design Manual (page 7 from this PDF). The example is of a flawed algorithm, but I still really want to understand it: [...] A different idea might be…
ClosureCowboy
  • 20,825
  • 13
  • 57
  • 71
44
votes
8 answers

transitive reduction algorithm: pseudocode?

I have been looking for an algorithm to perform a transitive reduction on a graph, but without success. There's nothing in my algorithms bible (Introduction To Algorithms by Cormen et al) and whilst I've seen plenty of transitive closure pseudocode,…
i alarmed alien
  • 9,412
  • 3
  • 27
  • 40
42
votes
6 answers

How to generate n different colors for any natural number n?

Say n = 100; How do I generate 100 visually distinct colors? Is this mathematically possible?
deostroll
  • 11,661
  • 21
  • 90
  • 161
38
votes
5 answers

Checking to see if 3 points are on the same line

I want to know a piece of a code which can actually tell me if 3 points in a 2D space are on the same line or not. A pseudo-code is also sufficient but Python is better.
Hossein
  • 40,161
  • 57
  • 141
  • 175
33
votes
7 answers

Standards for pseudo code?

I need to translate some python and java routines into pseudo code for my master thesis but have trouble coming up with a syntax/style that is: consistent easy to understand not too verbose not too close to natural language not too close to some…
ferdystschenko
  • 1,527
  • 1
  • 9
  • 18
29
votes
7 answers

Writing pseudocode for parallel programming

How do you write pseudo-code for parallel programming? Especially, how do you differentiate local and shared variables? How do you represent operations like scatter, gather, reduce, broadcast, and point-to-point communications? Is there some…
Charles Brunet
  • 21,797
  • 24
  • 83
  • 124
28
votes
4 answers

Maximum Likelihood Estimate pseudocode

I need to code a Maximum Likelihood Estimator to estimate the mean and variance of some toy data. I have a vector with 100 samples, created with numpy.random.randn(100). The data should have zero mean and unit variance Gaussian distribution. I…
user103021
1
2 3
99 100