Skip to content

Instantly share code, notes, and snippets.

View brainyfarm's full-sized avatar

Olawale Akinseye brainyfarm

View GitHub Profile
@brainyfarm
brainyfarm / reverse_a_string.py
Last active August 8, 2016 16:50
FreeCodeCamp - Reverse a String (Python)
"""
Reverse the provided string.
Your result must be a string.
"""
def reverse_string(string):
# Turn the string into a list
string_list = list(string)
# Reverse the list
string_list.reverse()
# Convert list to string and return

Two ways to FizzBuzz

Clever and Naive

@brainyfarm
brainyfarm / js_primality.md
Created July 27, 2016 09:37
Primality Test Comparism Javascript

JAVASCRIPT PRIMALITY TEST (SQRT VS PLAIN)

Demonstrating how two algorithms of the same number of line can be striking different in terms of performance.

I tested both algorithm on repl.it and used the same values for both tests.

The second algorithm:

30313411: true
Time taken: 435ms
@brainyfarm
brainyfarm / primalityReadme.md
Last active July 24, 2016 06:19
Python Primality Test (Square root vs Integer up to x )

PRIMALITY TEST (TIME COMPLEXITY)

Testing all integers to squareroot vs testing all integers to x

It took the first algorithm 6.35099983215 seconds to find out if 30313411 is a prime number It took the second algorithm 0.000999927520752 seconds. Difference in time is 6.349999904629248 seconds

@brainyfarm
brainyfarm / class_and_data_encapsulation.py
Last active June 27, 2016 13:15
Python Example Class and Data Encapsulation
"""
This Gist demonstrate with a simple example
Classes and data encapsulation in python
"""
# Here, I create a class bankAccount
class bankAccount:
# Every new instance of bankAccount created must have a name, accountNumber and balance
# The __init__ method is a special method in python which is invoked when the new instance is created
# The self argument is a reference to that particular instance and it is important when invoking any method
def __init__(self, name, accountNumber, balance):
@brainyfarm
brainyfarm / where_belong.js
Last active August 8, 2016 15:03
Olawale/FreeCodeCamp Algorithm: Where do I Belong
function getIndexToIns(arr, num) {
// Sort the array in ascending order
arr.sort( function( a, b ){ return a - b; } );
// Define a variable index to store the found index
var index;
// If the last item in the array is lesser in value than num
if(arr.indexOf( arr[ arr.length -1 ] ) < num ){
// Add one to the index of the last item and make it the index of num
index = arr.indexOf( arr[ arr.length -1 ] ) + 1;
}// End of if statement
@brainyfarm
brainyfarm / seek_and_destroy.js
Created June 21, 2016 14:46
Olawale/FreeCodeCamp Algorithm: Seek And Destroy
function destroyer(arr) {
// Covert the arguments into a workable array and changeable array.
var args = Array.prototype.slice.call(arguments);
// Save the first part of the arguments which is our actual array
var mainArr = args[0];
// Remove the first part of the argument from the array which it the array in array
// This would return the other parts alone in the array since arr.splice mutates the array unlike slice
args.splice(0,1);
@brainyfarm
brainyfarm / falsy_bouncer.js
Created June 21, 2016 14:45
Olawale/FreeCodeCamp Algorithm: Falsy Bouncer
function bouncer(arr) {
// Filter through the array and return values that do not evaluate to false through Boolean()
return arr.filter(function(a){return Boolean(a);});
}
bouncer([7, "ate", "", false, 9]);
@brainyfarm
brainyfarm / mutation.js
Created June 21, 2016 14:44
Olawale/FreeCodeCamp Algorithm: Mutation
/*
Here is my old solution.
We get better with time!
function mutation(arr) {
// Split the first and second string into an array
var firstWord = arr[0].toLowerCase().split("");
var secondWord = arr[1].toLowerCase().split("");
// Define a variable keeper to to store number of chars in secondWord present in the first.
@brainyfarm
brainyfarm / slasher_flick.js
Created June 21, 2016 14:41
Olawale/FreeCodeCamp Algorithm: Slasher Flick
function slasher(arr, howMany) {
// Keep looping as long as howMany is greater than 0
while(howMany > 0){
// Remove the first item from the array
arr.shift(); // arr.shift() mutates the array
// Decrement howMany
howMany --;
}
// Return the slashed array.
return arr;