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
""" | |
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 |
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
""" | |
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] |
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
""" | |
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): |
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
""" | |
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): |
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
""" | |
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 |
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
""" | |
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]]) |
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
""" | |
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") |
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
""" | |
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(" ") | |
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
""" | |
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 |
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
""" | |
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 | |
""" |