x = rand(1:100, 100000)
@time sort(x; alg=InsertionSort, rev=false)
@time sort(x; alg=QuickSort, rev=false)
Reading. Practice. Writing.
Please feel free to comment. Suggestions are also welcomed here. Write something "wholesome serving purpose" here.
x = rand(1:100, 100000)
@time sort(x; alg=InsertionSort, rev=false)
@time sort(x; alg=QuickSort, rev=false)
Program:
abstract type Shape end
# poymorhpic function computation
function area(shape::Shape)
throw(MethodError("area() is not implemented for abstract type Shape"))
end
struct Circle <: Shape
radius::Real
end
function area(c::Circle)
return 3 * c.radius ^ 2
end
struct Rectangle <: Shape
width::Real
height::Real
end
function area(r::Rectangle)
return r.width * r.height
end
struct Square <: Shape
width::Real
end
function area(s::Square)
return s.width * s.width
end
function main()
shapes = Shape[Circle(1), Square(2), Rectangle(3, 4)]
println("Area Circle(1) = ", area(shapes[1]))
println("Area Square(2) = ", area(shapes[2]))
println("Area Rectangle(3, 4) = ", area(shapes[3]))
end
main()
Output:
Area Circle(1) = 3
Area Square(2) = 4
Area Rectangle(3, 4) = 12
> julia
julia> f(x) = 2x + 3
f (generic function with 1 method)
julia> g(x) = x^2
g (generic function with 1 method)
# "∘" can be typed by \circ<tab>
julia> @show (f ∘ g)(1)
(f ∘ g)(1) = 5
julia> @show f(g(1))
f(g(1)) = 5
julia> typeof(f ∘ g)
ComposedFunction{typeof(f), typeof(g)}
julia> (f ∘ g)(1) == f(g(1))
true
julia>
Here's the unsafe Rust vectors program that implements a vector Vec template with the <String> data type.
This program demonstrates the use of Vec<String> to store a collection of strings.
The push and pop methods are used to add and remove elements from the vector.
Indexing is used to access specific elements within the vector.
The {:?} format specifier is used to print the value of a variable in a readable format.
This Rust program is unsafe and has out-of-bounds access.
If the index 3 is out of bounds. i.e., if the vector has fewer than four elements, accessing people_names[3] would result in undefined behavior, potentially leading to memory errors or crashes.
To have safe alternative, you can achieve the same functionality using safe Rust constructs. For example, you could use the get() method on the vector to safely access elements, which returns an Option to handle out-of-bounds case.
fn main() {
let mut people_names : Vec<String> = Vec::new();
people_names.push("Person_Name_01".to_string());
people_names.push("Person_Name_02".to_string());
people_names.push("Person_Name_03".to_string());
people_names.push("Person_Name_04".to_string());
people_names.pop();
// index out of bounds: the len is 3 but the index is 3
let fourth = &people_names[3];
println!("Fourth person is {:?}", fourth);
}
The safe C++ at() member function automatically checks whether array index is within the bounds of valid elements in the container, throwing an out_of_range exception if the array index is not, i.e. if the array index is greater than or equal to its array size.
The member function at() does check against bounds.
Here's a C++ program that uses the array data structure and at() member function.
#include <iostream>
#include <array>
using namespace std;
int main()
{
array<string, 12> month_names = { "January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
};
cout << "month_names.at(11): " << month_names.at(11) << endl;
// terminate called after throwing an instance of std::out_of_range exception
cout << "month_names.at(13): " << month_names.at(13) << endl;
return 0;
}
# random integers between 1 and 100 inclusive, generate thousands of them x = rand ( 1 : 100 , 100000 ) @time sort (x; alg=InsertionSort, r...