Techletter #73 | May 18, 2023
How to calculate the shortest distance between two places?
To calculate the shortest distance between two places, we use the haversine formula. It is a mathematical formula used to calculate the great-circle distance between two points on a sphere. Here, we get the shortest straight-line difference and is not the exact on-road distance.
function haversine_distance(lat1, lon1, lat2, lon2) {
const R = 6371; // Radius of the Earth in kilometers
// Convert latitudes and longitudes to radians
const lat1_rad = lat1 * Math.PI / 180;
const lon1_rad = lon1 * Math.PI / 180;
const lat2_rad = lat2 * Math.PI / 180;
const lon2_rad = lon2 * Math.PI / 180;
// Calculate the differences in latitude and longitude
const dlat = lat2_rad - lat1_rad;
const dlon = lon2_rad - lon1_rad;
// Calculate the Haversine distance
const a = Math.sin(dlat/2)**2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * Math.sin(dlon/2)**2;
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
const d = R * c;
return d;
}
// Example usage:
const lat1 = 12.91733;
const lon1 = 77.63148;
const lat2 = 12.92567;
const lon2 = 77.63672;
const distance = haversine_distance(lat1, lon1, lat2, lon2);
console.log({ distance });
console.log(`The distance between the two points is ${distance.toFixed(2)} kilometers.`);
isBooleanTooLongAndComplex
Have you ever come across an if statement that is too long? If you want it to be more readable and meaningful, here is how it can be done.
// Decide whether this pizza is fantastic.
if ((!pepperoniService.empty() || sausages.size() > 0)
&& (useOnionFlag.get() || hasMushroom(ENOKI, PORTOBELLO)) && hasCheese()) {
...
}
The above steps can be broken down into multiple booleans like below
boolean isPizzaFantastic() {
if (!hasCheese()) {
return false;
}
if (pepperoniService.empty() && sausages.size() == 0) {
return false;
}
return useOnionFlag.get() || hasMushroom(ENOKI, PORTOBELLO);
}
Link to the original article
What is a TCP?
Transmission Control Protocol(TCP) is a standard that defines how to establish and maintain a network conversation by which applications can exchange data.
Key characteristics of TCP include:
- Connection-oriented: The TCP establishes a connection between the client and the server before data can be exchanged.
- Reliable data transfer: It provides reliable, ordered, and error-checked delivery of data by breaking it into packets, numbering them, and waiting for acknowledgments from the receiver.
- Flow control: It manages the flow of data to prevent the sender from overwhelming the receiver with too many packets
- Layered architecture: It resides in the transport layer of the OSI model, providing host-to-host connectivity
Lists, tuples, and dictionaries in Python
List: The Python list is just like a dynamic-sized array.
variable_name = [item1, item2, item3, item4]
Tuple: is used to store multiple values separated by a comma in an ordered manner. The tuple is immutable, which means once it is created it cannot be modified.
variable_name = (item1, item2, item3)
Dictionary:
A dictionary is an unordered collection of various data types used for storing data values like maps. It can store key-value pairs.
variable_name = { k1: v1, k2: v2 }
The dictionaries are also known as associative memories or associative arrays. The keys in the dictionary must be unique.
Machine Learning Section
This week in Machine learning I did explore a few things. Here are a few points.
- 2 types of learning: Supervised learning and unsupervised learning.
- In supervised learning, we have two kinds: classification and regression.
- Machine learning is nothing but turning data information.
- Features are also called attributes.
- In unsupervised learning, we have two kinds clustering and density estimation.
- Steps while building an ML application:
- Collect data
- Prepare the input data
- Analyze the input data
- Train the algorithm
- Test the algorithm
- Use the algorithm
What is a machine learning model?
A machine learning model is a program that is trained to recognize patterns in data and make decisions or predictions without being explicitly programmed.