Essential Swift Programming Snippets and Algorithms

Essential Swift Programming Snippets and Algorithms

Loops

Loop Forward

// Loop forward
for i in 0..

Loop in Reverse

// Loop in reverse
for index in stride(from: 5, through: 1, by: -1) {
    print(index) // 5, 4, 3, 2, 1
}

// OR
for i in (0..<10).reversed() {
    print(i)
}

Strings

String Manipulation

  • Convert String to Array of Strings containing 1 Character:
var strArr = str.characters.map { String($0) }
Join Array of Strings into 1 String:
var str = strArr.joined(separator: "")
Split String into array
Read More

Machine Learning Models: Regression and Classification

Regression Models and Regularization Techniques

1. Linear Regression (Ordinary Least Squares – OLS)

Linear Regression is the most basic form, aiming to model the relationship between a dependent variable (Y) and one or more independent variables (X) by fitting a straight line (or hyperplane) to the data.

  • Goal: To find the coefficient values (β) that minimize the Residual Sum of Squares (RSS), which is the sum of the squared differences between the observed data points and the values predicted by the
Read More

Java Binary Tree and Graph Algorithm Practice

Practice Problems

1. Check if a Binary Tree is a Mirror of Itself

The idea is to write a recursive function isMirror() that takes two trees as arguments and returns true if the trees are mirrors and false if they are not. The isMirror() function recursively checks two roots and the subtrees under those roots.

// Java program to check if a binary tree is symmetric or not 
class Node { 
    int key;
    Node left, right;

    Node(int item) { 
        key = item;
        left = right = null;
    } 
}
Read More

Deep Learning for Audio and Speech Processing

Motivation for Deep Learning in Audio Processing

Why DL4ASP? Why use deep learning to analyze audio and speech? An audio file or stream consists of two main parts:

  • Header: Contains metadata such as the file name, path, number of channels, sample frequency, and duration.
  • Content: The actual sound data. It is unstructured binary data (0s and 1s). Without analysis, we do not know if it is music or voice; to understand the sound, you have to listen to it all.

The content is raw and complex, motivating the

Read More

Database Recovery Mechanisms Using Transaction Logs

Log-Based Recovery in DBMS

Log-based recovery is a mechanism used in DBMS to restore the database to a consistent state after a system crash or failure. The system maintains a log file stored on stable storage, recording every transaction’s operations before applying them to the database. Each log entry contains the transaction ID, data item name, old value, and new value. Using these logs, the DBMS can reconstruct the database by either undoing incomplete transactions or redoing completed ones.

Major

Read More

Array-Based Java Queue Implementation with Example

Array-Based Java Queue Example

Corrected Java Queue Code

The following Java classes implement a simple array-based queue and a tester. Spelling, grammar, and capitalization in comments and output strings have been corrected while preserving all original code logic and content.

class Queue {
  private int front; // Front is the index of the first element in the queue
  private int rear; // Rear is the index of the last element in the queue
  private int maxSize; // maxSize represents the maximum number 
Read More