This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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())) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |