Skip to content

Instantly share code, notes, and snippets.

View joanmolinas's full-sized avatar
🎯
Focusing

Joan Molinas joanmolinas

🎯
Focusing
View GitHub Profile
// Fibonacci is a Struct that generate an Infinite Array from Fibonacci Numbers.
// Fibonacci include a functions with calculate a positions from the Fibonacci Sequence, plus two numbers from this and
// The max number will be 2,147,483,647. It's the maximum number from Int.
// If want generate number more bigger than 2,147,483,647, you can use Int64 I think.
extension Fibonacci {
func numberAtIndex (index : Int) -> Int {
return ^^index
}
func plusTwoNumbers(posOne pos : Int, posTwo pos2 : Int) -> Int? {
prefix operator ^^ {}
prefix func ^^ (rhs : Int) -> Int {
return rhs < 0 ? 0 : rhs <= 2 ? 1 : ^^(rhs - 1) + ^^(rhs - 2)
}
postfix operator ^^ {}
postfix func ^^ (lhs : Int) -> Int {
return lhs < 0 ? 0 : lhs <= 2 ? 1 : ^^(lhs - 1) + ^^(lhs - 2)
}
postfix operator ~ {}
postfix func ~(rhs : Int) -> Int {
var result = 1
for i in reverse(1...rhs){ result *= i }
return result
}
infix operator ~/~ { associativity left precedence 150 }
func ~/~ (lhs : Int, rhs : Int ) -> Int {
assert(lhs > 0, "left number < 0")
'.source.js':
'comment function':
'prefix': 'comment'
'body': """
/**
* ${1:function}
* Methode: ${2:Type}
* @Param ${3:param}: ${4:name}
* Return: ${5:return}
*/
/* Two exponentiate algorithms, the first one it's better than second. */
func exponentiate(number : Int, exponent : Int) -> Int {
var n = number, e = exponent, r = 1, v = 0
func descriptionVar() -> String { return "Loop v :\(v), n =\(n), e = \(e), r = \(r)" }
while (e > 0) {
if e%2 != 0 { r = r*n }
e /= 2
n *= n
v++
/*
Melchoriforme Array is an Array V[1..N] with N >= 0 if some of elements is "rubio".
"Rubio" number is an number if your value it's equals to the sum of previous numbers.
Example: [1,2,5,8] -> 8 is Rubio because 1 + 2 + 5 = 8.
*/
/*
This is the predicate. Pass 2 parameters:
v -> Array of all elements.
num -> value for check if "rubio".
@joanmolinas
joanmolinas / 1.1.java
Last active August 29, 2015 14:21
Cracking the Coding Interview -> Arrays and String
/* Implement an algorithm to determine if a string has a all unique characters. What if tou can not use additional data Structures? */
/* Book Solution */
public boolean isUniqueChars(String s) {
boolean[] char_set = new boolean[256];
for (int i = 0; i < s.length(); i++) {
int val = s.charAt(i);
if(char_set[val]) return false;
char_set[val] = true;
}
//New Feature on Swift2 doCatch-guard-markup
//: # Swift2 Features - do-Catch
import Darwin
//: Do-Catch type errors
enum DoCatchError : ErrorType {
case DifferentValuesError
}
//New Feature on Swift2
//: # Swift2 Features - Extension Protocol
import Darwin
protocol PrintDescription {
func printDescription() -> String
}
var numbers = [Int]()
func appendNumbers() {
numbers.append(3)
//: defer{} is executed in the final on the function
//: use defer{} when you want that any code run it always whatever the function result
defer {numbers.append(4)}
numbers.append(5)
}
numbers.append(1)