Skip to content

Instantly share code, notes, and snippets.

View svdamani's full-sized avatar
🎯
Focusing

Shreevardhan svdamani

🎯
Focusing
View GitHub Profile
@svdamani
svdamani / sorts.hpp
Last active January 5, 2024 14:12
Sorting Algorithms using Iterators in C++
#include <algorithm>
template <class Iterator>
inline void BubbleSort(Iterator begin, Iterator end) {
for (Iterator i = begin; i != end; ++i)
for (Iterator j = begin; j < i; ++j)
if (*i < *j)
std::iter_swap(i, j);
}
@svdamani
svdamani / ls.c
Created April 15, 2015 16:56
Simple ls command implementation in C
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#define GREEN "\x1b[32m"
#define BLUE "\x1b[34m"
#define WHITE "\x1b[37m"
void Usage() {
fprintf(stderr, "\nUsage: exec [OPTION]... [DIR]...\n");
@svdamani
svdamani / Timer.hpp
Last active January 27, 2023 10:15
Timer class for C++
#if __cplusplus > 199711L /// C++11
#include <chrono>
class Timer {
public:
Timer() : beg_(clock_::now()) {}
void reset() { beg_ = clock_::now(); }
@svdamani
svdamani / pow.py
Last active April 17, 2020 10:44
Fast Power calculation in Python
def power(base, exp):
""" Fast power calculation using repeated squaring """
if exp < 0:
return 1 / power(base, -exp)
ans = 1
while exp:
if exp & 1:
ans *= base
exp >>= 1
base *= base
@svdamani
svdamani / mylib.h
Created April 17, 2015 16:38
A C library of simple and useful functions
/*
* Compilation : Include this file in a C program
* Compile with an extra flag
* $compiler -std=c99 $executable $program
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
@svdamani
svdamani / bing.sh
Created April 18, 2015 13:51
Bash script to set Bing wallpaper as desktop background
#!/bin/bash
# Get day from command line (default 0)
[ "$#" -ne 1 ] && set -- "0"
# Options
dir=~/Pictures/BingDesktop/ # Working directory
day="&idx=$1" # Set day. 0 is current day, 1 is previous day, ...
num="&n=1" # No. of images to get
mkt="&mkt=en-IN" # Set required market
@svdamani
svdamani / bing.ps1
Last active July 31, 2016 05:11
Powershell script to set Bing wallpaper as desktop background
[CmdletBinding()]
Param (
[ValidateRange(0, 18)] [int] $idx = 0,
[ValidateSet("en-US", "en-IN")] [string] $mkt = "en-IN"
)
$url = "http://www.bing.com/HPImageArchive.aspx?format=js&n=1&mkt=$mkt&idx=$idx"
$url = "http://www.bing.com" + (Invoke-RestMethod $url).images[0].url
$file = (Resolve-Path "~\Pictures\BingDesktop\").Path + $url.Split('/')[-1].Split('_')[0] + ".jpg"
if (-NOT (Test-Path $file)) { Invoke-WebRequest $url -OutFile $file }
Set-ItemProperty Wallpaper -Path "HKCU:\Control Panel\Desktop" -Value $file
@svdamani
svdamani / Timer2.hpp
Last active August 29, 2015 14:21
C++ 11 Timer class
#include <chrono>
#include <thread>
template <typename T = std::chrono::milliseconds>
class Timer {
typedef std::chrono::high_resolution_clock clock;
public:
Timer() : start(clock::now()) {}
inline void reset() { start = clock::now(); }
@svdamani
svdamani / spline.c
Last active January 20, 2024 02:50
Natural Cubic Spline Interpolation in C
/** Numerical Analysis 9th ed - Burden, Faires (Ch. 3 Natural Cubic Spline, Pg. 149) */
#include <stdio.h>
int main() {
/** Step 0 */
int n, i, j;
scanf("%d", &n);
n--;
float x[n + 1], a[n + 1], h[n], A[n], l[n + 1],
u[n + 1], z[n + 1], c[n + 1], b[n], d[n];
@svdamani
svdamani / crc32.js
Created June 13, 2015 10:23
CRC Implementation in JavaScript
function CRC() {
var table = new Array(256), /// LookUp Table for CRC
initial = 0xFFFFFFFF; /// Initial CRC
return {
/// Reverse a polynomial
reverse: function(poly) {
var rev = 0;
for(var i = 0; i < 32; i++) rev = rev << 1 | (poly >>> i) & 1;
return rev;
},