Ceaser Cipher

Ceaser Cipher

Definition

  • A substitution cipher that shifts each letter in the plaintext by a fixed number of positions down or up the alphabet.
  • Named after Julius Caesar, who used it for his private correspondence.
  • Simple and widely known encryption technique.

Application

  • Historically used for military and personal communication.
  • Modern usage includes educational purposes to introduce cryptography concepts.
  • Used in simple puzzles and games.
  • Not suitable for securing sensitive information due to vulnerability to brute-force attacks.

Examples

  1. Encryption Example:
    • Plaintext: "HELLO"
    • Shift: 3
    • Ciphertext: "KHOOR"
  2. Decryption Example:
    • Ciphertext: "KHOOR"
    • Shift: 3
    • Plaintext: "HELLO"
  3. Handling Non-Alphabet Characters:
    • Plaintext: "HELLO, WORLD!"
    • Shift: 3
    • Ciphertext: "KHOOR, ZRUOG!"

Related Algorithms 

  1. ROT13:
    • A specific case of the Caesar Cipher with a shift of 13.
    • Applying ROT13 twice returns the original text.
  2. Atbash Cipher:
    • Each letter in the alphabet is mapped to its reverse (e.g., 'A' becomes 'Z').
  3. Vigenère Cipher:
    • Uses a keyword to apply multiple Caesar Ciphers based on the letters of the keyword.
    • Provides better security by varying the shift value.
  4. Affine Cipher:
    • Combines the Caesar Cipher with a multiplicative step.
    • Uses a mathematical function of the form E(x)=(ax+b) mod  m to transform each letter.


Python code :

def caesar_cipher(text, shift, mode='encrypt'):

    if mode == 'decrypt':

        shift = -shift

    result = ''

    for char in text:

        if char.isalpha():

            shift_base = 65 if char.isupper() else 97

            result += chr((ord(char) - shift_base + shift) % 26 + shift_base)

        else:

            result += char

    return result

 

# Usage

message = "Hello, World!"

shift = 3

 

encrypted_message = caesar_cipher(message, shift, 'encrypt')

decrypted_message = caesar_cipher(encrypted_message, shift, 'decrypt')

 

print(f"Encrypted: {encrypted_message}")

print(f"Decrypted: {decrypted_message}")




Apptitude - Number System

 Concepts

  • Multiples and Factors
  • Total no. of factors of a composite number
  • LCM & HCF
  • Co-prime Numbers
  • Unit Digit
  • Highest power of P in n!
  • Concepts of Remainder
  • Divisibility

Co-Prime Numbers: 

Co-prime numbers are two numbers that have no other common factor than one. A set of co-prime numbers should consist of at minimum two numbers. Co-prime numbers, for example, {4 and 7}, {5, 7, 9} and 9, have just 1 as their greatest common factor. Co-prime numbers do not always have to be prime numbers.

Highest Common Factors (H.C.F) or greatest common Divisor (G.C.D) 
 two or more than two numbers is the greatest number that divides each one of them exactly. 
 Eg: H.C.F of 8 & 20 is 4 

Lowest Common Multiple (L.C.M): 
 The least number which is exactly divisible by each of them given numbers is called their lowest common multiple (L.C.M) 
 Eg: L.C.M of 8 & 20 is 40

Relation between L.C.M and H.C.F. of two Numbers:
 LCM X HCF = Product of the two numbers 

Compiler Design Part-3

 Parsers

Generation of Parse Tree Using:

  • Top down Approach  - Which production to use
  • Bottom up Approach - When to reduce
Classification of Parsers:


Compiler Design Part-2

 Topic Covers

Grammar:

A phrase structure grammar is (N,T,P,S) where,

  • N - Non terminal
  • T - Terminal
  • N intersection T = phi
  • S - start symbol
  • P- Production rules
Type 0: / unstricted grammar
Type 1: Length Increasing grammar- Context Sensitve Grammar
Type 2: CFG
Type 3: Regular Grammar




Derivation of CFG:

  • Left Derivation
  • Right Derivation
  • Parse tree Derivation

Left Recursion-

  • A production of grammar is said to have left recursion if the leftmost variable of its RHS is same as variable of its LHS.
  • A grammar containing a production having left recursion is called as Left Recursive Grammar.

Example- 

S → Sa / 

(Left Recursive Grammar)

 

  • Left recursion is considered to be a problematic situation for Top down parsers.
  • Therefore, left recursion has to be eliminated from the grammar.

Ambiguity in CFG:

Possible questions to ask
Whether the grammar is predective parsing or not
Check Whether the grammar is ambiguos or not
Problem due to Ambiguity
Associativity Property Violation
Precedence property Violation

Determine Associativity and precedence of the operators

NOTE:
  • For unambiguous grammars, Leftmost derivation and Rightmost derivation represents the same parse tree.
  • For ambiguous grammars, Leftmost derivation and Rightmost derivation represents different parse trees.

Recursion in CFG

Left recursion
Right Recursion
Elimination of Left Recursion
Problems with Left Recursion
Conversion of left to right Recursion

Non-Deterministic CFG
Elimination of Non-determination


Simple Linear Regression

Regression analysis is a statistical process for estimating the relationships between variables. It can be used to build a model to predict the value of the target variable from the predictor variables.

Mathematically, a regression model is represented as y= f(X), where y is the target or dependent variable and X is the set of predictors or independent variables (x1, x2, …, xn).

If a linear regression model involves only one predictor variable, it is called a Simple Linear Regression model.

f(X) = ß0 + ß1*x1 + ∈ 

The ß values are known as weights (ß0 is also called intercept and the subsequent ß1, ß2, etc. are called as coefficients). The error ,  ϵ is assumed to be normally distributed with a constant variance.

Assumptions of Linear Regression

Assumption 1: The target (dependent) variable and the predictor (independent) variables should be continuous numerical values.

Assumption 2: There should be linear relationship between the predictor variable and the target variable. A scatterplot with the predictor and the target variables along the x-axis and the y-axis, can be used as a simple check to validate this assumption.

Assumption 3: There should not be any significant outliers in the data.

Assumption 4: The data is iid (Independent and identically distributed). In other words, one observation should not depend on another.

Assumption 5: The residuals (difference between the actual value and predicted value) of a regression should not exhibit any pattern. That is, they should be homoscedastic (exhibit equal variance across all instances). This assumption can be validated by plotting a scatter plot of the residuals. If the residuals exhibit a pattern, then they are not homoscedastic (in other words, they are heteroscedastic). If the residuals are randomly distributed, then it is homoscedastic in nature.

Assumption 6: The residuals of the regression line should be approximately normally distributed. The assumption can be checked by plotting a Normal Q-Q plot on the residuals.

Implementation:
SLR Implementation



Popular Post

MindMaps

Featured post

Question 1: Reverse Words in a String III

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