voidet@nightgen.com
BAN USEROne pass with swift:
func zerosToTheLeft(inout input:Array<Int>) {
var position = 0;
input.count.times { i in
if input[i] == 0 {
swap(&input[i], &input[position++])
}
}
}
var input = [1, 2, 5, 3, 5, 0, 2, 6, 8, 0]
zerosToTheLeft(&input)
One with Objective C:
+ (NSArray *)moveTheZeros:(NSArray *)nums {
NSInteger x = 0;
NSMutableArray *ourNums = [nums mutableCopy];
for (NSNumber *num in nums) {
if (num.integerValue == 0) {
[ourNums removeObjectAtIndex:x];
[ourNums insertObject:@(0) atIndex:0];
}
x++;
}
return ourNums;
A solution that uses an array to store found random indexes, of which is then sorted and then called upon from the original input string.
func getTriplets(input:String) -> Array<String> {
var randomElements:[Int] = []
while randomElements.count < 3 {
var randInt:Int = Int(arc4random_uniform(UInt32(input.length())))
if (randomElements.contains(randInt)) {
continue
}
randomElements.append(randInt)
}
randomElements.sort {$0 < $1}
var letters:[String] = []
for element in randomElements {
letters.append(input[element])
}
return letters
}
getTriplets(stringTest)
This method uses a stack to use a DFS on the hop permutations. It decrements the value of the currently examined node each time it is popped off the stack and used to "leap" forward by x until it is 0, at which point it will look at the previous node at the stack and so on.
//Written in Swift
var hops = [1, 2, 2, 0, 1, 1, 2];
func hops(input:Array<Int>) -> Array<Int> {
var hopStack:[Int] = []
var hop:Int = input[0]
var currentIndex = 0
do {
if (hop == 0 && hopStack.count > 0) {
hop = hopStack.removeAtIndex(hopStack.count - 1)
currentIndex -= hop
hop--
}
if (hop > 0) {
hopStack.append(hop)
currentIndex += hop
if (currentIndex < input.count) {
hop = input[currentIndex]
}
}
if (currentIndex >= input.count) {
return hopStack
}
} while (hopStack.count > 0)
return []
}
hops(hops)
$a = [[1, 5], [10, 14], [19, 20], [21, 24], [27, 30], [35, 39]];
$b = [[3, 5], [12, 15], [18, 21], [23, 24]];
$x = 0;
$times = [];
while ($x < max(count($a), count($b))) {
$aaMinTime = $a[$x][0] ?: INF;
$bbMinTime = $b[$x][0] ?: INF;
$start = min($aaMinTime, $bbMinTime);
if ($end && $start != $end) {
$times[] = [$end + 1, $start - 1];
}
$aaMaxTime = $a[$x][1];
$bbMaxTime = $b[$x][1];
$end = max($aaMaxTime, $bbMaxTime);
$x++;
}
var_dump($times);
RepBhrisBrown, Animator at Achieve Internet
I teach the art of cooking, including food preparation, various cuisines, and techniques of cooking. I work at colleges and ...
Quick couchtime solution. Wish ObjectiveC was kinder to string manipulation
}
- voidet@nightgen.com December 14, 2014