Skip to content

Instantly share code, notes, and snippets.

View toshi0383's full-sized avatar
🏠
Working from home

Toshihiro Suzuki toshi0383

🏠
Working from home
  • Tokyo
View GitHub Profile
{-|
- Cryptography Week 1 Problem Set #7 | Corsera
-
- 1. Extract One-Time-Pad key from PT and CT pair.
- 2. Encrypt a new message using the key.
- 3. Print the result as a Hex String.
-}
import Text.Printf (printf)
import Data.Bits (xor)
@toshi0383
toshi0383 / CodePiece.swift
Created January 7, 2017 03:27
2つの配列の要素すべての組み合わせを出力 (swift) #CodePiece
import Foundation
func combine<E1, E2>(_ arr1: [E1], _ arr2: [E2]) -> [(E1, E2)] {
guard !arr1.isEmpty && !arr2.isEmpty else {
return []
}
func _c(_ e1: E1, _ arr2: [E2]) -> [(E1, E2)] {
return arr2.map{(e1, $0)}
}
return _c(arr1[0], arr2) + combine(arr1.dropFirst().map{$0}, arr2)
}
extension Array where Element: Equatable {
typealias Enumerated = (Int, Element)
func diff(fromNewArray newArray: [Element]) -> (added: [Enumerated], deleted: [Enumerated]) {
// 削除分: newArrayにはないもの
var added: [Enumerated] = []
var deleted: [Enumerated] = []
for (i, e) in newArray.enumerated() {
guard i < self.count else {
added.append((i, e))
continue
@toshi0383
toshi0383 / CodePiece.swift
Created November 24, 2016 01:17
UInt64 to Int conversion mistery #CodePiece
Int(UINT64_MAX) // error: integer overflows when converted from 'UInt64' to 'Int'
let a = UINT64_MAX
Int(a) // Compiles. Why?
@toshi0383
toshi0383 / CodePiece.swift
Created November 24, 2016 01:12
SwiftのArrayは暗黙的にcountの型で長さが制限される。countはIntなので、64bitのMacBookProだとINT64_MAXが限界になる。ということを知った。 #CodePiece
import Foundation
let a = (0..<UINT32_MAX).lazy.map{$0}
print(a.count) // 4294967295
let b = (0..<INT64_MAX).lazy.map{$0}
print(b.count) // 9223372036854775807
let bb = (0...INT64_MAX).lazy.map{$0}
print(bb.count) // <= EXC_BAD_INSTRUCTION
let c = (0..<UINT64_MAX).lazy.map{$0}
print(c.count) // <= EXC_BAD_INSTRUCTION
@toshi0383
toshi0383 / CodePiece.swift
Created October 31, 2016 07:56
ついついStringのsubscriptにIntを突っ込みたくなるけど違うんでしたねー。 #CodePiece
let a = "89078"
for i in (0..<a.characters.count) {
// let index: Int = i // <= wrong!
let index: String.CharacterView.Index =
a.characters.index(a.startIndex, offsetBy: i)
print(a[index])
}
// 8
// 9
// 0
@toshi0383
toshi0383 / CodePiece.swift
Created October 31, 2016 07:55
ついついintをsubscriptに突っ込みたくなるけど違うんでしたねー。 #CodePiece
let a = "89078"
for i in (0..<a.characters.count) {
// let index: Int = i // <= wrong!
let index: String.CharacterView.Index =
a.characters.index(a.startIndex, offsetBy: i)
print(a[index])
}
// 8
// 9
// 0
@toshi0383
toshi0383 / CodePiece.swift
Created October 31, 2016 07:28
UIControlState.NormalがUIControlState()に変換されて何かと思ったらOptionSetだった。 #CodePiece
import UIKit
UIControlState().rawValue // 0
UIControlState.normal.rawValue // 0
enum A: OptionSet {
typealias RawValue = Int
case a, b
var rawValue: Int {
return 0
@toshi0383
toshi0383 / CodePiece.swift
Created October 24, 2016 01:30
これなんでdoneにならないんだろう。 #CodePiece
import Foundation
var done = false
DispatchQueue.main.async {
print("hello")
done = true
}
while !done {
print("waiting... done:\(done)")
sleep(1)
}
@toshi0383
toshi0383 / CodePiece.swift
Created July 14, 2016 12:32
Swift3でリストの要素にprotocolを指定できるようになったとのことで、ポケモン攻撃問題をやってみた。 #CodePiece
import PokemonKit
let a = PokeDeck(pokemons: [
Pikachu(level: 1),
Bulbasaur(level: 5),
Pikachu(level: 11),
Pikachu(level: 4),
Pikachu(level: 20)
])