Skip to content

Instantly share code, notes, and snippets.

let result = 2 + 3 + 4 // 左から
var str1: String?
var str2: String?
let string = str1 ?? str2 ?? "string" // 右から
let int1 = 1
let int2 = 2
let int3 = 3
int1 == int2 == int3 // エラー
@KentaKudo
KentaKudo / LogicalConnectives.swift
Created July 10, 2016 05:00
Logical Connectives
// 矛盾
func contradiction(p: Bool, q: Bool) -> Bool {
return false
}
// トートロジー
func tautology(p: Bool, q: Bool) -> Bool {
return true
}
@KentaKudo
KentaKudo / VBCode.swift
Created January 15, 2017 22:14
VBCode in Swift
import Foundation
// encode single UInt32 number
func vbEncodeNumber(n: UInt32) -> [UInt8] {
var n = n
var result = [UInt8]()
while true {
let byte = n % 128
result.insert(UInt8(byte), at: 0)
if n < 128 {
@KentaKudo
KentaKudo / isin.swift
Last active May 22, 2017 15:10
Convert the order of array and needle
// standard
[1,2,3,4,5,6,7].contains(3)
// alternative
protocol A {
func `is`(in haystack: [Self]) -> Bool
func `is`(in haystack: Self...) -> Bool
}
extension A where Self: Comparable {
const { combineReducers } = Redux;
const todoApp = combineReducers({
todos,
visibilityFilter
});
let nextTodoId = 0;
const addTodo = (text) => {
return {
type: 'ADD_TODO',
@KentaKudo
KentaKudo / brain_tester_of_the_day_20170703.py
Last active July 3, 2017 13:58
Take the digits 1,2,3 up to 9 in numerical order and put either a plus sign or a minus sign or neither between the digits to make a sum that adds up to 100. For example, one way of achieving this is: 1 + 2 + 34 - 5 + 67 - 8 + 9 = 100, which uses six plusses and minuses. What is the fewest number of plusses and minuses you need to do this?
# pow(3, 8) = 6561
options = ['', '+', '-']
for i in options:
for j in options:
for k in options:
for l in options:
for m in options:
for n in options:
for o in options:
for p in options:
import Foundation
enum Token: Character {
case A = "A"
case B = "B"
case C = "C"
case D = "D"
case E = "E"
case F = "F"
case G = "G"
public struct Enigma {
private var rotorBox: RotorBox
private let plugboard: Cipher
public typealias Key = (Token, Token, Token)
public init(rotor0: @escaping Cipher, rotor1: @escaping Cipher, rotor2: @escaping Cipher, plugboard: @escaping Cipher, key: Key) {
self.rotorBox = RotorBox(
rotor0: RotorBox.Rotor(rotor0, position: key.0),
rotor1: RotorBox.Rotor(rotor1, position: key.1),
public typealias Cipher = (Token) -> (Token)
public enum Token: Character {
case A = "A", B = "B", C = "C", D = "D", E = "E", F = "F"
case G = "G", H = "H", I = "I", J = "J", K = "K", L = "L"
case M = "M", N = "N", O = "O", P = "P", Q = "Q", R = "R"
case S = "S", T = "T", U = "U", V = "V", W = "W", X = "X"
case Y = "Y", Z = "Z"
}