Skip to content

Instantly share code, notes, and snippets.

View santosh's full-sized avatar
:octocat:

Santosh Kumar santosh

:octocat:
View GitHub Profile
@santosh
santosh / malloc.c
Created September 9, 2018 04:47
Get a string, make a copy of it, capitalize the copy.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(void) {
// define a string (or get from user input)
char *s = "santosh";
// copy that string into memory
@santosh
santosh / templatemethodpattern.py
Created September 2, 2018 10:25
Template Method Pattern example in Python.
from abc import ABC, abstractmethod
class AbstractRecipe(ABC):
def execute(self):
self.prepare()
self.recipe()
self.cleanup()
@abstractmethod
def prepare(self):
@santosh
santosh / regex01.py
Created September 1, 2018 15:21
regex fun
import re
# print(re.split(r'(s*)', 'here are some words'))
# print(re.split(r'[a-f]', 'kjfsldhjakcnv', re.I | re.M))
print(re.findall(r'\d', 'ocinwe324 main st.asdvce'))
@santosh
santosh / overload.py
Last active August 12, 2018 14:19
Operator overloading in Python.
# In this script, we'll overload '+' operator to add two classes.
class CurrenciesDoNotMatchError(TypeError):
def __init__(self, message):
super().__init__(message)
class Currency:
def __init__(self, currency, amount):
self.currency = currency
@santosh
santosh / webcam.cpp
Created August 5, 2018 10:38
Webcam feed in OpenCV. #ComputerVision
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(void) {
// capture a video, for webcam, pass 0 instead of file path..
VideoCapture cap(0);
if (!cap.isOpened()) {
@santosh
santosh / ageinhours.py
Last active July 30, 2018 11:40
Calculate hours passed since your birth. #Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
File: ageinhours.py
Author: Santosh Kumar
Email: [email protected]
Github: @santosh
Description: Calculate age in seconds
"""
@santosh
santosh / singleton.py
Created July 6, 2018 15:10
Singleton example in #Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Singleton is something I have already used in Game Development with Unity.
class MySingleton(object):
"""docstring for MySingleton"""
_instance = None
def __new__(self):
if not self._instance:
@santosh
santosh / simple_decorator.py
Last active July 6, 2018 02:24
#Python Decorators. #SelfRef
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def addOne(oldFunc):
def addOneInside(*args, **kwargs):
return oldFunc(*args, **kwargs) + 1
return addOneInside
@addOne
def someFunct(x):
@santosh
santosh / argskwargs.py
Created July 5, 2018 17:14
*args and **kwargs use cases.. #python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def arguments(*args):
for i in args:
print(i)
l = [1, 2, 3, 5]
# arguments(1, 2, 3, 5)
@santosh
santosh / DTD_Sample.dtd
Created June 24, 2018 14:49
Example of a DTD document.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Bookstore with DTD -->
<!-- Try changes: -->
<!-- Make Edition required -->
<!-- Swap order of First_Name, Last_Name -->
<!-- Add empty Remark -->
<!-- Add Magazine, omit closing tag -->
<!DOCTYPE Bookstore [
<!ELEMENT Bookstore (Book | Magazine)*>
<!ELEMENT Book (Title, Authors, Remark?)>