Skip to content

Instantly share code, notes, and snippets.

@fitsyu
Created October 19, 2018 03:43
Show Gist options
  • Save fitsyu/fa54a3616c9ee6c8a6ac563052fb8419 to your computer and use it in GitHub Desktop.
Save fitsyu/fa54a3616c9ee6c8a6ac563052fb8419 to your computer and use it in GitHub Desktop.
Easy Easy Medium
import UIKit
var str1 = "Hi"
var str2 = "World"
func twoString(s1: String, s2: String) -> String {
let filtered = str1.filter { char in str2.contains(char) }
let out = filtered.isEmpty ? "NO" : "YES"
return out
}
//print( twoString(s1: str1, s2: str2) )
let ints = [1,1,0,-1,-1]
func plusMinus(ints: [Int])
{
let positives = ints.filter { $0 > 0 }
let negatives = ints.filter { $0 < 0 }
let zeroes = ints.filter { $0 == 0 }
fractionLine(arr: positives, ints: ints)
fractionLine(arr: negatives, ints: ints)
fractionLine(arr: zeroes, ints: ints)
}
func fractionLine( arr: [Int], ints: [Int])
{
let fraction = Float(arr.count) / Float(ints.count)
print(fraction)
}
//plusMinus(ints: ints)
//var msg = "haveaniceday"
//var msg = "feedthedog"
var msg = "chillout"
// factors
let sqr = ( Float(msg.count).squareRoot() )
var row = Int( sqr )
var col = row + 1
if (row*col) < msg.count {
row = col
}
//print(sqr, row, col)
// normalization
if msg.count < (row*col) {
let diff = (row*col) - msg.count
let spaces = String(repeating: " ", count: diff)
msg.append(spaces)
}
// make grid
var grid: [String] = []
for i in 0..<row {
let start = msg.index(msg.startIndex, offsetBy: i*col)
let end = msg.index(start, offsetBy: col)
grid.append( String(msg[start..<end]) )
}
//print(grid)
// encrypt
var enc = ""
for c in 0..<col {
for r in grid {
let i = r.index(r.startIndex, offsetBy: c)
enc += String(r[i]).trimmingCharacters(in: .whitespaces)
}
enc += " "
}
print(enc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment