Skip to content

This example scans a range of values and then it builds a string-based distribution. The distribution function iterates through inputs, applies compute(x) using the main arithmetic expression (x+x/x–x*x), and, it concatenates results. Examples are found in Python, MATLAB, and JavaScript.

License

Notifications You must be signed in to change notification settings

Gagniuc/A-simple-scanner-to-find-the-output-distribution

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

A simple scanner to find the output distribution

Ex. (140) - A simple scanner to find the output distribution, is presented here in three programming languages: Python, MATLAB, and JavaScript. Although the implementations differ in syntax, the underlying concept remains identical across all three versions. Each code sample is reproduced from its respective volume of the series Coding Examples from Simple to Complex (Springer, 2024).


The above program calculates a distribution of values within a specified range and stores the result in a string variable. The distribution is obtained by applying the compute function to each value within the given range and concatenating the results with newline characters in between. For a short description, this code generates a string output representing a distribution of values. It begins by declaring a variable a and assigns it the result of a function call to distribution(3, 21). Then, it proceeds to print the value of a using a print function. The distribution function is the heart of the program. It takes two parameters, start and stop, representing the range of values to consider. Inside the function, a variable t is initialized as an empty string. A for-loop is used to iterate over a range of values from start (inclusive) to stop (exclusive). During each iteration, the compute function is called with the current value of i, and the result is concatenated to the string t with a newline character to separate each value. Next, the resulting string t is returned. Note that the compute function is a simple mathematical operation that takes a single parameter x, and it calculates a value based on the formula: x + x / x - x * x (Please see the previous subchapter in the main text of the book).

Example in Python:

def compute(x):
    return x + x / x - x * x

def distribution(start, stop):
    t = ""
    for i in range(start, stop):
        t += str(compute(i)) + "\n"
    return t

a = distribution(3, 21)
print(a)
Python output:
-5.0
-11.0
-19.0
-29.0
-41.0
-55.0
-71.0
-89.0
-109.0
-131.0
-155.0
-181.0
-209.0
-239.0
-271.0
-305.0
-341.0
-379.0

Example in Javascript:

let a = distribution(3, 21);
print(a);

function distribution(start, stop){
    
    let t = "";
    
    for (let i = start; i < stop; i++) {
        t += compute(i) + "\n";
    }
    
    return t;
}

function compute(x){
    return x + x / x - x * x;
}
Javascript output:
-5.0
-11.0
-19.0
-29.0
-41.0
-55.0
-71.0
-89.0
-109.0
-131.0
-155.0
-181.0
-209.0
-239.0
-271.0
-305.0
-341.0
-379.0

Example in Matlab:

a = distribution(3, 21);
disp(a);

function t = distribution(start, stop)
    t = "";
    for i = start:(stop-1)
        t = t + compute(i) + newline;
    end
end

function result = compute(x)
    result = x + x / x - x * x;
end
Matlab output:
-5.0
-11.0
-19.0
-29.0
-41.0
-55.0
-71.0
-89.0
-109.0
-131.0
-155.0
-181.0
-209.0
-239.0
-271.0
-305.0
-341.0
-379.0

References

  • Paul A. Gagniuc. Coding Examples from Simple to Complex - Applications in Python, Springer, 2024, pp. 1-245.
  • Paul A. Gagniuc. Coding Examples from Simple to Complex - Applications in MATLAB, Springer, 2024, pp. 1-255.
  • Paul A. Gagniuc. Coding Examples from Simple to Complex - Applications in Javascript, Springer, 2024, pp. 1-240.

About

This example scans a range of values and then it builds a string-based distribution. The distribution function iterates through inputs, applies compute(x) using the main arithmetic expression (x+x/x–x*x), and, it concatenates results. Examples are found in Python, MATLAB, and JavaScript.

Topics

Resources

License

Stars

Watchers

Forks