Two ways to FizzBuzz
Clever and Naive
| """ | |
| 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
| """ | |
| 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): |
| 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 |
| 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); |
| 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]); |
| /* | |
| 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. |
| 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; |