def reverseWords(s: str) -> str:
words = s.split()
return ' '.join(word[::-1] for word in words)
def reverseWords(s: str) -> str:
words = s.split()
return ' '.join(word[::-1] for word in words)
Socket Programming - Youtube
Socket Address
A socket is one endpoint of a two
way communication link between two programs running on the network.
The socket mechanism provides a means of inter-process communication (IPC) by
establishing named contact points between which the communication take
place.
sockets is created using ‘socket’ system
call. The socket provides bidirectional FIFO Communication
facility over the network. A socket connecting to the network is created at
each end of the communication. Each socket has a specific address. This address
is composed of an IP address and a port number.
The server creates a socket, attaches it to a network port
addresses then waits for the client to contact it. The client creates a socket
and then attempts to connect to the server socket. When the connection is
established, transfer of data takes place.
Types of Sockets : There are two types of
Sockets: the datagram socket and the stream socket.
Sockets: A Comprehensive Overview
1. Socket Address Structures
struct sockaddr_in: For IPv4 addresses.struct sockaddr_in6: For IPv6 addresses.struct sockaddr_un: For Unix domain sockets.sin_family: Address family (AF_INET, AF_INET6, AF_UNIX).sin_port: Port number in network byte order.sin_addr: IP address in network byte order.bind, connect, and listen to specify the address of the socket.2. Value-Result Arguments
accept function takes a sockaddr structure as an argument. After accepting a connection, the function fills the structure with the address of the client and returns it to the caller.3. Byte Ordering and Manipulation Functions
htons: Converts a 16-bit host value to network byte order.htonl: Converts a 32-bit host value to network byte order.ntohs: Converts a 16-bit network value to host byte order.ntohl: Converts a 32-bit network value to host byte order.4. Elementary TCP Sockets
socket(domain, type, protocol)connect(sockfd, addr, addrlen)bind(sockfd, addr, addrlen)listen(sockfd, backlog)accept(sockfd, addr, addrlen)fork()execve(pathname, argv, envp)fork or select mechanisms.5. Close Function and Related Functions
close(sockfd)shutdown(sockfd, how)Classification
Classification is a Supervised Learning algorithm where the goal is to predict the category or class labelof given data points. It is used when the output variable is categorical .
How Classification Works?
1. Training Phase:
A model learns the relationship b/w the input features and their corresponding class labels from a labeled dataset.
2. Testing Phase:
The trained model is used to classify new, unseen data points into one of the predefined classes.
Types of Classification:
Binary Classification
Multi class classification
Multi label classification
Examples of Classification Tasks:
Email filtering
Medical diagnosis
Image recognition
sentiment Analysis
Classification Algorithms
Logistic Regression
Decision Trees
Support Vector Machines
K-Nearest Neighbours
Navie bayes
Random Forest
Evalution Metrics for Classification
Accuracy - Percentage of correctly labeled classes
Precision - How many +ve predictions are correct
Recall
F1 Score
ROC-AUC
Workflow of Classification
Data Collection: Gather labeled data.A Decision Tree is a supervised machine learning algorithm used for classification and regression tasks. It models decisions and their possible consequences as a tree-like structure of nodes, where:
The tree splits the data into subsets based on the feature that provides the most information gain or least impurity at each step.
Root Node:
The starting point of the tree, representing the entire dataset.
Splitting:
Dividing data at a node into subsets based on a condition (e.g., feature > value).
Leaf Node:
A terminal node that provides the final prediction.
Pruning:
Reducing the size of the tree to avoid overfitting.
Impurity Measures (used for splitting):
Bayes' Theorem is a fundamental concept in probability theory and statistics that describes the probability of an event, based on prior knowledge of conditions that might be related to the event. In machine learning, it is widely used for classification tasks, particularly in Naive Bayes classifiers.
Bayes' Theorem is expressed mathematically as:
Where:
Prior Probability (P(A)): This represents what is known about A before any new data is observed. It’s the initial belief or assumption about a class or event.
Likelihood (P(B∣A)): This is the likelihood of observing the evidence B given that A is true. It quantifies how well the evidence supports the hypothesis.
Posterior Probability (P(A∣B)): This is the probability of A occurring after considering the evidence B. It's the updated belief after observing the data.
Evidence (P(B)): This is the total probability of observing B across all possible outcomes. It is used to normalize the result to ensure the probabilities sum to 1.
In machine learning, the Naive Bayes classifier is based on Bayes' Theorem, with the assumption that the features used for prediction are conditionally independent given the class label. This simplifies the calculation, hence the term "naive."
Compute the Prior Probability:
The probability of each class occurring in the dataset.
Compute the Likelihood:
Calculate the likelihood for each feature given the class label. For continuous features, this often assumes a Gaussian distribution.
Compute the Posterior Probability:
For each class Ck, compute the posterior probability of a new data point X=(X1,X2,...,Xn):
Classify the Data Point:
Choose the class Ck with the highest posterior probability as the predicted class.
Class imbalance occurs when the number of samples in one class significantly exceeds those in another class. This can negatively impact the performance of Support Vector Machines (SVM), as the decision boundary may be biased towards the majority class, leading to poor performance on the minority class.
Class Weight Adjustment:
Assign higher weights to the minority class to balance the penalty during training.
In Scikit-learn, this can be done using the class_weight parameter:
from sklearn.svm import SVC
# Assign 'balanced' to automatically compute weights
model = SVC(class_weight='balanced')
Or manually specify weights:
model = SVC(class_weight={0: 1, 1: 10}) # Higher weight for class 1 (minority)
Resampling Techniques:
Example using SMOTE:
from imblearn.over_sampling import SMOTE
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
# Create synthetic samples for the minority class
smote = SMOTE(random_state=42)
X_resampled, y_resampled = smote.fit_resample(X, y)
# Train an SVM on the balanced dataset
model = SVC()
model.fit(X_resampled, y_resampled)
Change the Decision Threshold: Adjust the threshold for classifying a sample as a minority class based on the predicted probabilities.
Example:
from sklearn.svm import SVC
from sklearn.metrics import classification_report
# Fit SVM
model = SVC(probability=True)
model.fit(X_train, y_train)
# Predict probabilities
y_prob = model.predict_proba(X_test)[:, 1]
# Adjust threshold
threshold = 0.3
y_pred = (y_prob >= threshold).astype(int)
# Evaluate performance
print(classification_report(y_test, y_pred))
Kernel Selection: Use kernels that better separate the minority and majority classes, such as the RBF kernel, which can handle non-linear boundaries.
Feature Engineering: Transform or create features that emphasize differences between the classes. This can improve separability in the feature space.
Use of Alternative Metrics: Evaluate the model using metrics like:
Random Forest is a popular ensemble learning technique used for both classification and regression tasks. It combines the predictions of multiple decision trees to improve accuracy, reduce overfitting, and enhance generalization.
Bootstrap Aggregation (Bagging):
Feature Randomness:
Prediction Aggregation:
BOX PLOT
A boxplot (or box-whisker plot) is a graphical representation of the distribution of the dataset.
Components:
Minimum(Q0): The smallest data point.
First Quartile(Q1): The median of the lower half of the dataset(25% Percentile)
Median(Q2): The middle value of the dataset(50% percentile)
Third Quartile(Q3)
Maximum(Q4):
Histogram
A Histogram is a graphical representation of the distribution of a dataset. It divides the range of data into bins(intervals) and displays how many points falls into each bin.
key components:
bins: The intervals in which the data range is divided.
frequency: count of data points in each bin.
Vizualization: Typically represented as a bar chart.
Python
import matplotlib.pyplot as plt
data = [1,2,3,4,5,6,7,8,....]
plt.hist(data, bins = 5, color = 'blue', edgecolor = 'black')
plt.title()
plt.xlabel()
plt.ylabel()
plt.show()
Explanation:
Output:
Types of Histograms
1. bell shaped histogram
2. bimodal histogram
3. skewed right histogram
4. skewed left histogram
5. uniform histogram
Significance of Histograms:
1. Understanding the data distribution
The shape of a histogram reveals the underlying distribution of data, which is essential for statistical analysis. Common distributions include:
Normal distribution
uniform distribution
Bimodal distribution
2. Identifying the Central tendency
3. Understanding the Variability
The width of the histogram (spread of bins) indicates variability or dispersion
4. Detecting Outliers
Resources:
1. https://www.cuemath.com/data/histograms/
What is Lexicographical Order?
Lexicographical Order is a way of arranging words or
sequences in the same order as they would appear in a dictionary. In this
ordering, the sequences are compared character by character, based on their alphabetical
order (or numerical order for digits).
Key Points:
Example 1: Words
Consider the words:
apple, banana, bat, batman.
Final order:
apple, banana, bat, batman.
Example 2: Numbers as Strings
Consider the numbers as strings:
101, 11, 110, 2.
Final order:
101, 11, 110, 2.
Application:
Lexicographical order is used in:
Introduction
Python is a general purpose high level language.
Python was developed by Gudio Van rossam in 1989 while
working at National Research Institute in Netherlands.
But officially python was made available to public in 1991
Example to Print HelloWorld
![]()
To print sum of two numbers

The name Python was selected from a TV Show
“The complete Monty Python’s Circus” was broadcasted by BBC from 1969 to 1974.
Guido developed Python language by taking almost all
Programming features from different languages.
·
Functional Programming from c.
·
OOPS from C++
·
Scripting Languages from perl and shellscript
·
Modular language features from Modular-3
For Developing desktop Applications
For Developing web Applications
For Developing database Applications
For Data Analysis
For Machine Learning
Features
of Python
·
Simple and easy to learn
·
Open source
·
High level Programming Language
·
Platform Independent
·
Portability
·
Dynamically Types
·
Procedured Oriented and Object oriented
·
Interpreted
·
Extensible
·
Embeded
Limitations
of Python
Performance wise is not up to the mark
It is not used to build Mobile Applications
Identifiers
A name in python program is called an identifier.
It can be class name or function name or variable name or
module name
Rules to
define Identifiers in Python
·
Allowed Characters – Alphabet symbols, digits,
underscore.
·
Identifiers are case sensitive.
·
It should not start with a digit.
NOTE
·
If Identifier is start with Underscore(_) then
it indicates it is Private.
·
If Identifier is start with __(2 underscores)
indicating strongly private.
·
If the identifier starts and ends with two
underscore symbols then the identifier is language defined special name, which
is also known as magic words.
Reserve
Words
In python some words are reserved to represent some meaning
or functionality.
There are 33 reserved words in python
·
True, False, None
·
And, or, not, is
·
If, elseif, else
·
While, for, break, continue, return, in, yield
·
Try, except, finally, raise, assert
·
Import, from, as, class, def, pass, global,
nonlocal, lambda, del, with
DataTypes
Datatype represents the type of data present inside a
variable.
In python we are not required to specify the type exclicitly.
Based on the value provided, the type will be assigned automatically.
Python contains the following inbuilt datatypes
·
Int
·
Float
·
Complex
·
Bool
·
Byte
·
Str
·
Bytearray
·
Range
·
List
·
Tuple
·
Set
·
Frozenset
·
Dict
·
None
Slicing Strings
[] operater is called Slice operator.
In python String index can be +ve or -ve

Note
In python, we can represent char values also by using str
type and explicitly char type is not available.
Type
casting
We can convert one type to another type. This conversion is
called Type conversion.
Note
·
We can convert from any type to int type except
complex type.
·
If we want to convert str type to int type, compulsory
str should contain only integral value and should be specified in base 10.
All Fundamental data types are immutable. Once we create an
object, we cannot perform any changes in that object. If we are trying to
change then with those changes a new object will be created. This is called
immutability.
List Data
type
If we want to represent a group of values as a single entity
where insertion order required to preserve and duplicates are allowed then we
should go for list data type.
·
Insertion order is preserved.
·
Heterogeneous objects are allowed
·
Duplicates are allowed.
·
Growable in nature.
·
mutable
·
Values should be enclosed within square
brackets.


List is growable in nature, based on our requirement we can
increase or decrease the size.
Tuple data type
It is exactly same as list data type except that is
immutable. We cannot change values.
Tuple elements can be represented in parenthesis.
Note
Tuple is read only version of list

Set data
type
·
Insertion order is not preserved
·
Duplicates are not allowed
·
Heterogeneous objects are allowed
·
Index concept is not applicable
·
It is mutable collection
·
Growable in nature.

Frosenset
datatype
·
It is exactly same as set except that is
immutable.
·
Hence we cannot use add or remove functions.

Dict datatype
·
If we represent a group of values as key value
pairs then we should go dict data type.
·
Duplicates keys are not allowed.

NOTE
Dict is mutable and the order won’t be preserved.
Escape
characters
·
/n – new line
·
/t – Horizontal tab
·
/r – Carriage return
·
/b – backspace
·
/f – form feed
·
/v – vertical tab
Constants
·
Constants concept is not applicable in python
·
But it is convention to use uppercase characters
if we don’t want to change value.
·
It is just convention but we can change value.
Arithmetic operators

2. relational Operators
![]()
3. bitwise operators

4. Assignment Operators


Special
Operators
·
Identity operators
·
Membership operators
Identity
Operators
·
We can use for addresses comparison
·
Operators are – is, is not

NOTE
We can use is operator for addresses comparision where as ==
operator for content comparision.
Membership
operators
·
Used to check whether the given object present
in the given collection.
·
Operators – in, not in

Operator Precedence

Mathematical Functions(math module)
Math is a module that
contains several functions to perform mathematical operators



Input()
Input() function can be used to read data directly in our
required format. We are not required to perform type casting.

How to read mulptiple values from the keyboard in a single
line:

Write a program to read 3 float numbers and their sum

Eval()
Eval function take a string and evaluate the result.

NOTE
Eval() can evaluate the input to list, tuple, set, etc
based on provided input.
Write a program to accept list from the keyboard



NOTE
There is no switch statement in python.
Write a program to find Biggest of given 3 numbers

To print the characters present in string index wise is



Loops
with else block
·
Inside loop execution, if break statement not
executed, then only else part will be executed.
·
Else means loop without break.


Pass statement
·
It is an empty statement
·
It is null statement
·
It wont do anything
·
It is like abstract method in java.
Del statement
We can delete variable by using del keyword.



def reverseWords(s: str) -> str: words = s.split() return ' '.join(word[::-1] for word in words)