View Source FreecodecampElixir.BasicAlgo (Freecodecamp using Elixir v0.2.0)
Documentation for Freecodecamp (Basic Alogrithmic Scripting).
Summary
Functions
Check if a value is classified as a boolean primitive. Return true or false.
Remove all falsy values from an array. Falsy values
in JavaScript are false, null, 0, "", undefined, and NaN
,
only "", false, nil or 0 were implemented for simplicity
reasons.
Splits a list (first argument) into groups the length of size (second argument) and returns them as a two-dimensional list.
Check if a string (first argument, string
) ends with the
given target string (second argument, target
).
The formula to convert from Celsius to Fahrenheit is the temperature in Celsius times 9/5
, plus 32
.
Factorialize a number
Returns the first element thats passes the truth test
from a given function.
Return the length of the longest word in the provided sentence.
Inserts the 1st list in 2nd list at its index position (3rd param). Also an SO link why doing binary search on linked list is slower. Used linear search instead.
Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.
Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.
Returns true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
Return repeated string.
Reverses a string.
Capitalize each word in a sentence
Truncate a string (first argument) if it is longer than
the given maximum string length (second argument). Return
the truncated string with a ...
ending.
Functions
Check if a value is classified as a boolean primitive. Return true or false.
source: Boo who
Examples
iex> BasicAlgo.boo_who(true)
true
iex> BasicAlgo.boo_who(false)
true
iex> BasicAlgo.boo_who([])
false
iex> BasicAlgo.boo_who("a")
false
Remove all falsy values from an array. Falsy values
in JavaScript are false, null, 0, "", undefined, and NaN
,
only "", false, nil or 0 were implemented for simplicity
reasons.
source: Falsy Bouncer
Examples
iex> BasicAlgo.bouncer([7, "ate", "", false, 9])
[7, "ate", 9]
iex> BasicAlgo.bouncer(["a", "b", "c"])
["a", "b", "c"]
iex> BasicAlgo.bouncer([false, nil, 0, ""])
[]
iex> BasicAlgo.bouncer([7, [], false, ""])
[7, []]
Splits a list (first argument) into groups the length of size (second argument) and returns them as a two-dimensional list.
source: Chunky Monkey
Examples
iex> BasicAlgo.chunk_array_in_groups(["a", "b", "c", "d"], 2)
[["a", "b"], ["c", "d"]]
iex> BasicAlgo.chunk_array_in_groups([0, 1, 2, 3, 4, 5], 3)
[[0, 1, 2], [3, 4, 5]]
iex> BasicAlgo.chunk_array_in_groups([0, 1, 2, 3, 4, 5], 2)
[[0, 1], [2, 3], [4, 5]]
Check if a string (first argument, string
) ends with the
given target string (second argument, target
).
Returns a boolean.
source: Confirm the Ending
Examples
iex> BasicAlgo.confirm_ending("Bastian", "n")
true
iex> BasicAlgo.confirm_ending("Congratulation", "on")
true
iex> BasicAlgo.confirm_ending("Connor", "n")
false
The formula to convert from Celsius to Fahrenheit is the temperature in Celsius times 9/5
, plus 32
.
Returns an integer.
source: Convert Celsius to Fahrenheit
Examples
iex> BasicAlgo.convert_to_f(30)
86
Factorialize a number
source: Factorialize a Number
Examples
iex> BasicAlgo.factorialize(0)
1
iex> BasicAlgo.factorialize(5)
120
Returns the first element thats passes the truth test
from a given function.
source: Finders Keepers
Examples
iex> BasicAlgo.find_element([1, 3, 5, 8, 9, 10], &Integer.mod(&1, 2) === 0)
8
iex> BasicAlgo.find_element([1, 3, 5, 9], &(Integer.mod(&1, 2) === 0))
nil
iex> BasicAlgo.find_element([], & &1 === 0)
nil
Return the length of the longest word in the provided sentence.
Returns an integer.
source: Find the Longest Word in a String
Examples
iex> BasicAlgo.find_longest_word_length("")
0
iex> BasicAlgo.find_longest_word_length("May the force be with you")
5
@spec franken_splice(Enumerable.t(), Enumerable.t(), integer()) :: Enumerable.t()
Inserts the 1st list in 2nd list at its index position (3rd param). Also an SO link why doing binary search on linked list is slower. Used linear search instead.
source: Slice and Splice
Examples
iex> BasicAlgo.franken_splice([1, 2, 3], [4, 5], 1)
[4, 1, 2, 3, 5]
iex> BasicAlgo.franken_splice([1, 2], ["a", "b"], 1)
["a", 1, 2, "b"]
iex> BasicAlgo.franken_splice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2)
["head", "shoulders", "claw", "tentacle", "knees", "toes"]
Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.
For example, get_index_to_ins([1,2,3,4], 1.5)
should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).
Likewise, get_index_to_ins([20,3,5], 19)
should return 2 because once the array has been sorted it will look like [3,5,20]
and 19 is less than 20 (index 2) and greater than 5 (index 1).
source: Where do I Belong
Examples
iex> BasicAlgo.get_index_to_ins([1, 2, 3, 4], 1.5)
1
iex> BasicAlgo.get_index_to_ins([20, 3, 5], 19)
2
iex> BasicAlgo.get_index_to_ins([3, 10, 5], 3)
0
Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.
source: Return Largest Numbers in Arrays
Examples
iex> BasicAlgo.largest_of_four([])
[]
iex> BasicAlgo.largest_of_four([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]])
[25, 48, 21, -3]
Returns true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
source: Mutations
Examples
iex> BasicAlgo.mutation(["hello", "Hey"])
false
iex> BasicAlgo.mutation(["hello", "neo"])
false
iex> BasicAlgo.mutation(["Noel", "Ole"])
true
Return repeated string.
source: Repeat a String Repeat a String
Examples
iex> BasicAlgo.repeat_string_num_times("abc", 2)
"abcabc"
iex> BasicAlgo.repeat_string_num_times("abc", 0)
""
iex> BasicAlgo.repeat_string_num_times("", 100)
""
iex> BasicAlgo.repeat_string_num_times("abc", -1)
""
Reverses a string.
source: Reverse a String
Examples
iex> BasicAlgo.reverse_string("hello")
"olleh"
Capitalize each word in a sentence
source: Title Case a Sentence
Examples
iex> BasicAlgo.title_case("I'm a little tea pot")
"I'm A Little Tea Pot"
iex> BasicAlgo.title_case("sHoRt AnD sToUt")
"Short And Stout"
iex> BasicAlgo.title_case("HERE IS MY HANDLE HERE IS MY SPOUT")
"Here Is My Handle Here Is My Spout"
Truncate a string (first argument) if it is longer than
the given maximum string length (second argument). Return
the truncated string with a ...
ending.
source: Truncate a String
Examples
iex> BasicAlgo.truncate_string("A-tisket a-tasket A green and yellow basket", 8)
"A-tisket..."
iex> BasicAlgo.truncate_string("Absolutely Longer", 2)
"Ab..."
iex> BasicAlgo.truncate_string("A-", 1)
"A..."
iex> BasicAlgo.truncate_string("A-tisket", -1)
"..."
iex> BasicAlgo.truncate_string("Hello", 50)
"Hello..."