Skip to content

Instantly share code, notes, and snippets.

View lnarasim's full-sized avatar
🎯
Focusing

Lakshmi Narayanan N lnarasim

🎯
Focusing
View GitHub Profile
@lnarasim
lnarasim / sorting_generator.py
Created June 1, 2019 13:24
Generator that generates numbers in sorted order
"""
Problem Statement:
Given a list of numbers, write a generator that yields the numbers in sorted order. For every yield,
the generator should yield numbers in sorted order. Also, implement reverse sorting (reverse=True).
When the reverse is set to True, the generator should yield numbers in reverse sorting order.
"""
def sorting_generator(lst, reverse=False):
swap_natural_order = lambda x, y: (y, x) if y < x else (x, y)
swap_reverse_order = lambda x, y: (y, x) if y > x else (x, y)
import unittest
from prime_numbers import is_prime
# Write this as a supplement to my blog post
# http://unstuck.grabyourfreedom.in/2018/01/why-unit-test-is-important.html
class TestPrimeNumber(unittest.TestCase):
def test_valid_prime(self):
# This is a simple program that finds whether a given number is
# prime or not
# Let us define the contract for the function
# The function takes an integer as input and returns
# True if prime and
# False if the number if non-prime
# False if the input is not a number
@lnarasim
lnarasim / blogger_email_subscription_widget.html
Last active January 25, 2023 21:26
Email Subscription Popup for Blogger
<html>
<head>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js' type='text/javascript'> </script>
<style>
.subscribe_dialog {
background-color: #2C3E50;
background-image: url(https://4.bp.blogspot.com/-NgaMQbtnNAI/WkOTzfpAMTI/AAAAAAAAFMw/qUJtvhwhlYEOLO9dxkWoJKrZ5ogK8MotACLcBGAs/s1600/dark-background-pics-hd-17-light-fume-chaotic-motion-on-dark-background-perfect-abstract-stock-footage-video-6388121-shutterstock.jpg);
color: #FFFFFF;
position: fixed;
z-index: 98;
@lnarasim
lnarasim / simple_exit.py
Last active December 23, 2017 05:33
A simple program to demonstrate how os._exit() works
# This is a simple program to demonstrate os.exit. But it is
# always advisable to call system.exit
import os
import time
def exit_program():
print("Calling exit")
os._exit(1)
print("This will never get printed")
@lnarasim
lnarasim / simple_wait.py
Created December 22, 2017 18:11
An example to show how to use os.wait
# Let us use couple of functions we developed in other modules and see
# what happens when a child dies before a parent and how to clean-up
import os
import time
from simple_fork import create_process
from simple_exec import overlay_program
# This function simply uses waits to retrieve the status of child
@lnarasim
lnarasim / simple_exec.py
Created December 22, 2017 14:50
An example to show os.exec function
import os
# This oneliner function overlays the command that is passed
# and starts executing from the beginning of the new program
# The process ID remains the same as it overlays in the same
# process space
def overlay_program(cmd, args):
os.execv(cmd, args)
@lnarasim
lnarasim / simple_fork.py
Last active December 22, 2017 12:58
A tiny program to demonstrate how fork works
# This is a simple program that creates a new process using fork. There
# is nothing complex here if you already did something like this in
# C/C++. This code just shows how this can be done in Python though
import os
# This is the only function in this file that create a new process
def create_process():
print("Creating a process. My PID is - " + str(os.getpid()))
@lnarasim
lnarasim / signal_alarm.py
Created December 21, 2017 09:55
Code to demonstrate signal/alarm API that fires an alarm in Python
# A simple program to demonstrate alarm in python. This tiny code
# registers a signal/alarm handler that calls after set number
# of seconds repeatedly
import signal
import time
import os
timer_seconds = 5
@lnarasim
lnarasim / simple_single_handler.py
Last active December 20, 2017 14:27
Simple Signal Handling in Python
# This program demonstrate how to write a signal handler in Python
# (and how simple it is to implement one in Python)
# Tested in Linux (Ubuntu, and should work in any Linux/Unix)
# Author: Lakshmi Narayanan Narasimhan (grabyourfreedom)
import signal
import time
import os
import sys
import traceback