Skip to content

Instantly share code, notes, and snippets.

Avatar

Heberti Almeida hebertialmeida

View GitHub Profile
@hebertialmeida
hebertialmeida / TwitterText.swift
Created Nov 23, 2017
Twitter text counter 280 characters
View TwitterText.swift
import Foundation
struct WeightRange {
let range: ClosedRange<UInt32>
let weight: Int
}
struct TwitterText {
static func lengthOf(tweet: String) -> Int {
let defaultWeight = 200
View gist:8903a70ad23a7477d98e0f1fe960809c
### Keybase proof
I hereby claim:
* I am hebertialmeida on github.
* I am heberti (https://keybase.io/heberti) on keybase.
* I have a public key whose fingerprint is BC4D 8E00 C9D2 F132 128A F95F 2561 43B3 57D6 0E45
To claim this, I am signing this object:
@hebertialmeida
hebertialmeida / gist:f0b949835ba157ad2bb9
Created Nov 20, 2014
Finding the most common character in a string. Swift implementation.
View gist:f0b949835ba157ad2bb9
import UIKit
var str = "aaaabbbaaaa * bbbb b bbbbbbb ccccccccdd ddddddd dddd"
var cnt = [String: Int]()
var i = 0
for char in str {
var c = String(char)
if let match = cnt[c] {
@hebertialmeida
hebertialmeida / gist:be0b689bf0370d5a2764
Created Nov 20, 2014
Given a dictionary of words, return an array of the words whose match. (i.e. pattern "c.t" match with "cat", "cut", etc. because the dot notation stand for ANY character). Swift implementation.
View gist:be0b689bf0370d5a2764
import UIKit
var words = ["Cat", "Cot", "Brazil", "Cut", "cat", "Apple", "Watch"]
var filter = [String]()
for word in words {
if let match = word.rangeOfString("(C|c)(.*)t", options: .RegularExpressionSearch) {
filter.append(word)
}
}
@hebertialmeida
hebertialmeida / gist:da65f587b44c6aeef76e
Created Sep 25, 2014
Given 2 arrays of integer (A and B) find the elements from A not belonging to B. Swift implementation.
View gist:da65f587b44c6aeef76e
import UIKit
let a = [1, 2, 3, 4, 5, 6, 7, 50, 30]
let b = [3, 10, 5, 6, 7, 8, 9, 51, 1]
var filter = [Int]()
for number in a {
if !contains(b, number) {
filter.append(number)
}
@hebertialmeida
hebertialmeida / gist:3a21d3517e5de9a051a7
Last active Aug 29, 2015
Function that merges two lists, but removing duplicate ones. Swift implementation.
View gist:3a21d3517e5de9a051a7
import UIKit
var namesA = ["Luis", "Hector", "Selena", "Emmanuel", "Amish", "Rawle", "Selena"]
var namesB = ["Luis", "Selena", "Amish", "Rawle", "George", "Heberti"]
var filter = [String]()
for item in namesA+namesB {
if !contains(filter, item) {
filter.append(item)
}
@hebertialmeida
hebertialmeida / gist:6ba1331e3bc9ecb0cb89
Last active Aug 29, 2015
Remove repeated values from a INT array, returning the resultant array in the same order as original. Swift implementation.
View gist:6ba1331e3bc9ecb0cb89
import UIKit
var numbers = [7, 7, 7, 1, 1, 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 8]
var filter = [Int]()
for number in numbers {
if !contains(filter, number) {
filter.append(number)
}
}
@hebertialmeida
hebertialmeida / gist:d447903dbfc1cd85ae5d
Last active Dec 10, 2016
Given a set of names, sort them in the following manner the next word should start with the last letter of the previous word. Swift implementation.
View gist:d447903dbfc1cd85ae5d
import UIKit
var names = ["Luis", "Hector", "Selena", "Emmanuel", "Amish", "Rawle"]
var firsts = [String: String]()
var lasts = [String: String]()
var output = [String]()
for name in names {
var key = (name as NSString).substringFromIndex(name.utf16Count-1)
@hebertialmeida
hebertialmeida / gist:9680780
Created Mar 21, 2014
Open native Apps or open on Safari if don't have the App
View gist:9680780
- (void)openThisURL:(NSString *)launchUrl withFallback:(NSString *)fallback
{
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:launchUrl]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:launchUrl]];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:fallback]];
}
}
@hebertialmeida
hebertialmeida / gist:9234391
Last active Feb 15, 2019
Resize UIView to fit subviews
View gist:9234391
- (void)resizeToFitSubviews:(UIView *)view
{
float w, h;
for (UIView *v in view.subviews) {
float fw = v.frame.origin.x + v.frame.size.width;
float fh = v.frame.origin.y + v.frame.size.height;
w = MAX(fw, w);
h = MAX(fh, h);
}