Skip to content

Instantly share code, notes, and snippets.

Balázs Németh balazsnemeth

View GitHub Profile
@balazsnemeth
balazsnemeth / ES5 to ES6
Last active Apr 9, 2018
How to refactor an object from ES5 to ES6
View ES5 to ES6
### ES5:
var Rectangle = function (w, h) {
this.width = w;
this.height = h;
this.area = function() {
return calcArea();
@balazsnemeth
balazsnemeth / AbsDistinct.js
Last active Feb 16, 2017
Codility solution - AbsDistinct - caterpillar
View AbsDistinct.js
// 100% solution for task: https://codility.com/programmers/lessons/15-caterpillar_method/abs_distinct/
//
function solution(A) {
var nums = {}
for (i of A) {
nums[Math.abs(i)] = 1;
}
@balazsnemeth
balazsnemeth / gist:1e8fbdad6fd286315b1b0485908ae8e5
Created Feb 8, 2017
JS - NumberSolitaire - Dynamic Programming -
View gist:1e8fbdad6fd286315b1b0485908ae8e5
// In a given array, find the subset of maximal sum in which the distance between consecutive elements is at most 6.
// https://codility.com/programmers/lessons/17-dynamic_programming/number_solitaire/
// 100% for both performance & correctness
function solution(A) {
// The basic idea:
// We can compute the best sum of "i" (t[i]) based on the previous best sums!
// So let's store the best sum for each element "i" in this array:
@balazsnemeth
balazsnemeth / gist:3fbadbbdf7a5c27d8073463f5bed09ae
Created Feb 8, 2017
Solution for MinAbsSum (Dynamic Programming)
View gist:3fbadbbdf7a5c27d8073463f5bed09ae
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// Not so optimal...
// Lesson: https://codility.com/programmers/lessons/17-dynamic_programming/min_abs_sum/
function solution(A) {
if (!A.length) {
return 0;
}
@balazsnemeth
balazsnemeth / gist:352e3230a0868ea811817e919a044f07
Last active Feb 5, 2017
Solution for Count Non Divisible
View gist:352e3230a0868ea811817e919a044f07
// Solution for CountNonDivisible (Codility)
// Not the best, just O(N^2)...
// https://codility.com/programmers/lessons/11-sieve_of_eratosthenes/count_non_divisible/
function solution(A) {
// write your code in JavaScript (Node.js 6.4.0)
const divisors = A.map(e => 0);
for (let i = 0; i<A.length; i++) {
View Soliution for CountFactors
// Codility - CountFactors
// Count factors of given number n.
// https://codility.com/demo/take-sample-test/count_factors/
function solution(N) {
// write your code in JavaScript (Node.js 6.4.0)
if (N <= 2) {
return N;
}
@balazsnemeth
balazsnemeth / Extension for all NSManagedObject Subclass
Last active Jun 13, 2016
Trick, how to add a generic function available for each NSManagedObject subclass
View Extension for all NSManagedObject Subclass
extension NSObjectProtocol where Self: NSManagedObject {
static func findAllWithPredicate(searchTerm: NSPredicate?, inContext context: NSManagedObjectContext = NSManagedObjectContext.MR_defaultContext()) -> [Self] {
//You can implement the fetchrequest here by yourself, but now I'm using MagicalRecord service to keep it simple
return self.MR_findAllWithPredicate(searchTerm, inContext: context) as! [Self]
}
}
Usage (There is no cast required here, but the result is a [AnswerObject]):
@balazsnemeth
balazsnemeth / LoadableView.swift
Created Mar 3, 2016
Default protocol implementation to load any view from NIB
View LoadableView.swift
protocol LoadableView {
static func loadViewFromNibName(nibName: String) -> Self
}
extension LoadableView {
/**
Default protocol implementation to load a view from nib!
*/
static func loadViewFromNibName(nibName: String) -> Self {
let nibViews = NSBundle.mainBundle().loadNibNamed(nibName, owner: nil, options:nil)
@balazsnemeth
balazsnemeth / gist:cbe7f4ea38abe3df19fb
Created Mar 30, 2015
Post Twitter status dictionary
View gist:cbe7f4ea38abe3df19fb
- (void) postTwitterStatus:(NSDictionary *)statusParameters withCompletion:(void(^)(NSError *error))completion {
NSAssert(twitterIsSetUp && self.accounts.count > 0, @"To post something to twitter, the twitter API should be set up already");
ACAccount *twitterAccount = [self.accounts objectAtIndex:0];
NSURL * requestURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"];
SLRequest *postRequest = [SLRequest
requestForServiceType:SLServiceTypeTwitter
View gist:0485c91472d6c78d0006
- (void)sendTweetWithExperienceInfo:(NSDictionary*)info withCompletion:(void(^)(NSError *error))completion
{
//Create basic post info.
NSString *status = [self tweetWithDescrition:[expInfo objectForKey:@"statusMessage"] withHeyLetsShortURL:expInfo[@"url"]];
__block NSMutableDictionary *message = [@{
@"status": status,
} mutableCopy];
You can’t perform that action at this time.