This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-| | |
- 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Int(UINT64_MAX) // error: integer overflows when converted from 'UInt64' to 'Int' | |
let a = UINT64_MAX | |
Int(a) // Compiles. Why? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
UIControlState().rawValue // 0 | |
UIControlState.normal.rawValue // 0 | |
enum A: OptionSet { | |
typealias RawValue = Int | |
case a, b | |
var rawValue: Int { | |
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
var done = false | |
DispatchQueue.main.async { | |
print("hello") | |
done = true | |
} | |
while !done { | |
print("waiting... done:\(done)") | |
sleep(1) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PokemonKit | |
let a = PokeDeck(pokemons: [ | |
Pikachu(level: 1), | |
Bulbasaur(level: 5), | |
Pikachu(level: 11), | |
Pikachu(level: 4), | |
Pikachu(level: 20) | |
]) |