Skip to content

Instantly share code, notes, and snippets.

View brainyfarm's full-sized avatar

Olawale Akinseye brainyfarm

View GitHub Profile
@brainyfarm
brainyfarm / slasher_flick.py
Created August 8, 2016 09:46
FreeCodeCamp: Slasher Flick (Python)
"""
Return the remaining elements of an array after chopping off n elements from the head.
The head means the beginning of the array, or the zeroth index.
"""
def slasher(the_list, n):
if(n >= len(the_list)):
return []
while(n > 0):
the_list.remove(the_list[0])
n -= 1
@brainyfarm
brainyfarm / chunk_array.py
Created August 8, 2016 09:33
FreeCodeCamp: Chunk an Array (Python)
"""
Write a function that splits an array (first argument) into
groups the length of size (second argument) and returns them as a two-dimensional array.
"""
def chunk_list(the_list, size):
m = size
index = 0
big_list = []
while(size <= len(the_list) +m):
to_append = the_list[index:size]
@brainyfarm
brainyfarm / truncate_a_string.py
Created August 8, 2016 08:03
FreeCodeCamp: Truncate a String (Python)
"""
Truncate a string (first argument) if it is longer than the given maximum string length (second argument).
Return the truncated string with a ... ending.
Note that inserting the three dots to the end will add to the string length.
However, if the given maximum string length num is less than or equal to 3,
then the addition of the three dots does not add to the string length in determining the truncated string.
"""
def truncate(string, num):
@brainyfarm
brainyfarm / repeat_a_string.py
Created August 8, 2016 07:38
FreeCodeCamp: Repeat a String (Python)
"""
Repeat a given string (first argument) num times (second argument).
Return an empty string if num is not a positive number.
"""
def repeat_string(string, num):
result = ""
if(num < 0):
return ""
else:
while(num >= 1):
@brainyfarm
brainyfarm / confirm_the_ending.py
Created August 8, 2016 07:28
FreeCodeCamp: Confirm the Ending (Python)
"""
Check if a string (first argument, string) ends
with the given target string (second argument, target).
"""
def confirmEnding(string, target):
# Some intelligent stuff going on here
# We fing out what part of string we need to compare with target
start_reading_from = len(string) - len(target)
# String slicing and comparing
return string[start_reading_from:] == target
@brainyfarm
brainyfarm / largest_of_four.py
Created August 8, 2016 07:10
FreeCodeCamp: Largest in an array of arrays [four] (Python)
"""
Return an array consisting of the largest number from each provided sub-array.
For simplicity, the provided array will contain exactly 4 sub-arrays.
"""
def largest_of_four(super_list):
# This is so easy thanks to list comprehension :)
return [max(element) for element in super_list]
print largest_of_four([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]])
@brainyfarm
brainyfarm / titlecase_a_sentence.py
Last active August 8, 2016 07:39
FreeCodeCamp: Title Case a Sentence (Python)
"""
Return the provided string with the first letter of each word capitalized.
Make sure the rest of the word is in lower case.
For the purpose of this exercise, you should also capitalize connecting words like "the" and "of".
"""
def title_case(sentence):
# Pretty Straight Forward ;)
return sentence.title()
print title_case("This is so sweet")
@brainyfarm
brainyfarm / length_of_longest_word.py
Created August 8, 2016 06:54
FreeCodeCamp Return length of longest word in sentence (Python)
"""
Return the length of the longest word in the provided sentence.
Your response should be a number.
"""
def longest_word_length(sentence):
# Length of longest word before looping
longest_length = 1
# Split the sentence into a list of words
word_list = sentence.split(" ")
@brainyfarm
brainyfarm / check_for_palindrome.py
Created August 8, 2016 06:32
FreeCodeCamp Check for Palindromes (Python)
"""
Return true if the given string is a palindrome. Otherwise, return false.
A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing.
You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols)
and turn everything lower case in order to check for palindromes.
"""
# Import regular expression
import re
@brainyfarm
brainyfarm / factorialize_a_number.py
Created August 8, 2016 05:48
FreeCodeCamp Factorialize a number (Python)
"""
Return the factorial of the provided integer.
If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.
Factorials are often represented with the shorthand notation n!
For example: 5! = 1 * 2 * 3 * 4 * 5 = 120
"""