Skip to content

Instantly share code, notes, and snippets.

View jakubpetrik's full-sized avatar
:octocat:

Jakub Petrík jakubpetrik

:octocat:
View GitHub Profile
@jakubpetrik
jakubpetrik / rle.swift
Last active February 5, 2021 09:01
Run-length encoding in Swift.
/**
http://rosettacode.org/wiki/Run-length_encoding
Given a string containing uppercase characters (A-Z),
compress repeated 'runs' of the same character by storing the length of that run,
and provide a function to reverse the compression.
The output can be anything, as long as you can recreate the input with it.
*/
@jakubpetrik
jakubpetrik / reverse-words.swift
Created September 24, 2015 06:04
Reverse words in a string.
/**
http://rosettacode.org/wiki/Reverse_words_in_a_string
The task is to reverse the order of all tokens in each of a number of strings and display the result;
the order of characters within a token should not be modified.
Example: “Hey you, Bub!” would be shown reversed as: “Bub! you, Hey”
Tokens are any non-space characters separated by spaces (formally, white-space);
the visible punctuation forms part of the word within which it is located and should not be modified.
@jakubpetrik
jakubpetrik / handle-signal.swift
Created September 23, 2015 21:34
Signal handling in Swift
/**
http://rosettacode.org/wiki/Handle_a_signal
Most general purpose operating systems provide interrupt facilities, sometimes called signals.
Unhandled signals generally terminate a program in a disorderly manner. Signal handlers are
created so that the program behaves in a well-defined manner upon receipt of a signal.
For this task you will provide a program that displays a single integer on each line of output at
the rate of one integer in each half second. Upon receipt of the SigInt signal (often created by the user typing ctrl-C)
the program will cease printing integers to its output, print the number of seconds the program has run,
@jakubpetrik
jakubpetrik / drama.swift
Created September 23, 2015 06:30
String and print with support for a drama
extension String {
var dramatically: String { return self + ", ..." }
}
func printWithDrama(items: Any..., separator: String = " ", terminator: String = "\n") {
print(items.map { "\($0)" }.joinWithSeparator(separator).dramatically, terminator: terminator)
}
@jakubpetrik
jakubpetrik / naive-shunting-yard.swift
Last active September 11, 2015 20:00
Naive aproach to ShuntingYard in Swift 2.0
/**
http://rosettacode.org/wiki/Parsing/Shunting-yard_algorithm
Given the operator characteristics and input from the Shunting-yard algorithm page and tables
Use the algorithm to show the changes in the operator stack and RPN output as each individual token is processed.
*/
import Foundation
@jakubpetrik
jakubpetrik / markov-algorithm.swift
Last active September 11, 2015 19:49
Markov Algorithm in Swift 2.0
/**
http://rosettacode.org/wiki/Execute_a_Markov_algorithm
Create an interpreter for a Markov Algorithm. Rules have the syntax:
<ruleset> ::= ((<comment> | <rule>) <newline>+)*
<comment> ::= # {<any character>}
<rule> ::= <pattern> <whitespace> -> <whitespace> [.] <replacement>
<whitespace> ::= (<tab> | <space>) [<whitespace>]
@jakubpetrik
jakubpetrik / Reducer.m
Last active September 23, 2015 07:36
Reducing BOOLs.
@interface Reducer : NSObject
+ (id(^)())reduceToBoolMultiple:(NSNumber *)boolArgs, ... NS_REQUIRES_NIL_TERMINATION;
+ (id(^)())reduceToBoolArray:(NSArray *)array;
@end
@implementation Reducer
+ (id (^)())reduceToBoolMultiple:(NSNumber *)firstArg, ... NS_REQUIRES_NIL_TERMINATION {
BOOL retVal = firstArg.boolValue;