Skip to content

Instantly share code, notes, and snippets.

View raheemazeezabiodun's full-sized avatar
🎯
Solving problems with my craft

Raheem Azeez Abiodun raheemazeezabiodun

🎯
Solving problems with my craft
View GitHub Profile
@raheemazeezabiodun
raheemazeezabiodun / dispatch_on_type.py
Last active September 19, 2019 20:29
A way of dispatching action based on type in Python.
"""
This demonstrates how to use singledispatch for dispatching on type
We want to draw each based on the type. Typically, we can use if else statement to check the type
then call the function that does draw the shape, however, we will keep having multiple if elif statement.
This is just another pythonic way of doing it
"""
from functools import singledispatch
@raheemazeezabiodun
raheemazeezabiodun / gcd.py
Created July 23, 2019 12:04
Computing greatest common divisor
def slow_gcd(arg1,arg2):
gcd = 0
for num in range(1, arg1 + arg2+ 1):
if (arg1 % num == 0) and (arg2 %num == 0):
gcd = num
return gcd
def fast_gcd(arg1,arg2):
if arg2 == 0:
@raheemazeezabiodun
raheemazeezabiodun / fibonacci.py
Created July 22, 2019 12:14
A fibonacci sequence with a better approach
def slow_fibonacci_number(n):
if n <= 1:
return n
return slow_fibonacci_number(n - 1) + slow_fibonacci_number(n - 2)
def fast_fibonacci_number(n):
numbers = [0, 1]
for num in range(2, n+1):
numbers.append(numbers[num - 1] + numbers[num - 2])
@raheemazeezabiodun
raheemazeezabiodun / try_else.py
Created September 19, 2019 21:32
An example of how to use try except else in Python
"""
The try block tries to execute an expression
If an error occurs, it calls the except block alone
If no error occur, it calls the else block
The idea behind this is, if the school exists, print it out
"""
schools = {
@raheemazeezabiodun
raheemazeezabiodun / two_dimensional_vectors.cpp
Created January 8, 2020 11:50
Different ways of looping through a vector in c++
/*
different ways of looping through a vector in c++
*/
#include <iostream>
#include <vector>
#include <numeric> // for style_four
using std::cout;
using std::vector;