Created
January 10, 2015 21:56
-
-
Save mayoralito/964e49580b11ab1a3664 to your computer and use it in GitHub Desktop.
examples
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
// | |
// ViewController.m | |
// DemoInterview | |
// | |
// Created by amayoral on 1/10/15. | |
// Copyright (c) 2015 vRoutes. All rights reserved. | |
// | |
#import "ViewController.h" | |
@interface ViewController () | |
{ | |
NSString *abc1; | |
NSString *abc2; | |
} | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
abc1 = @"abcdefghijklmnopqrstuvwxyz"; | |
abc2 = @"etaoinshrdlucmfwypvbgkjqxz"; | |
NSArray *p = [self splitStringBy:@"abc"]; | |
NSLog(@"%@", p); | |
p = [self splitStringBy:@"abcdef"]; | |
NSLog(@"%@", p); | |
NSInteger lost = [self findLostNumber:@[@1,@3]]; | |
NSLog(@"lost: %i", lost); | |
lost = [self findLostNumber:@[@2,@3,@4]]; | |
NSLog(@"lost: %i", lost); | |
lost = [self findLostNumber:@[@13,@11,@10,@3,@2,@1,@4,@5,@6,@9,@7,@8]]; | |
NSLog(@"lost: %i", lost); | |
// | |
// ------ | |
NSString *encoded = [self encode:@"abc"]; | |
NSLog(@"encoded %@", encoded); | |
NSString *decoded = [self decode:encoded]; | |
NSLog(@"decoded %@", decoded); | |
//--- | |
encoded = [self encode:@"xyz"]; | |
NSLog(@"encoded %@", encoded); | |
decoded = [self decode:encoded]; | |
NSLog(@"decoded %@", decoded); | |
//--- | |
encoded = [self encode:@"aeiou"]; | |
NSLog(@"encoded %@", encoded); | |
decoded = [self decode:encoded]; | |
NSLog(@"decoded %@", decoded); | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
/** | |
* | |
Complete the solution so that it splits the string into pairs of two characters. If the string contains an odd number of characters then it should replace the missing second character of the final pair with an underscore ('_'). | |
Examples: | |
solution('abc') // should return ['ab', 'c_'] | |
solution('abcdef') // should return ['ab', 'cd', 'ef'] | |
*/ | |
- (NSArray *)splitStringBy: (NSString *)string { | |
NSMutableArray *output = [[NSMutableArray alloc] init]; | |
NSString *strTmp = @""; | |
int i = 0; | |
int ix = 0; | |
for(i= 0; i < string.length;i++){ | |
strTmp = [strTmp stringByAppendingFormat:@"%c", [string characterAtIndex:i]]; | |
ix = [strTmp length]; | |
if(ix%2==0 && i > 0) { | |
[output addObject:strTmp]; | |
strTmp = @""; | |
} | |
} | |
ix = [strTmp length]; | |
if(ix%2!=0){ | |
strTmp = [strTmp stringByAppendingString:@"_"]; | |
[output addObject:strTmp]; | |
} | |
return output; | |
} | |
/** | |
You're working in a number zoo, and it seems that one of the numbers has gone missing! | |
Zoo workers have no idea what number is missing, and are too incompetent to figure it out, so they're hiring you to do it for them. | |
In case the zoo loses another number, they want your program to work regardless of how many numbers the zoo has in total. | |
Write the function findNumber(array) that takes an array of numbers. The numbers will be unsorted values between 1 and one more than the length of the array. No values will be repeated within the array. Return the number that is missing. | |
Examples: | |
findNumber( [1,3] ) => 2 | |
findNumber( [2,3,4] ) => 1 | |
findNumber( [13,11,10,3,2,1,4,5,6,9,7,8] ) => 12 | |
*/ | |
- (NSInteger)findLostNumber:(NSArray *)numberList { | |
NSInteger lostNumber = -1; | |
// Sort array using descriptors. | |
NSArray *sorted = [numberList sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"integerValue" | |
ascending:YES]]]; | |
int idx = 1; | |
for(NSString *n in sorted) { | |
NSInteger current = [n integerValue]; | |
if(idx != current) { | |
lostNumber = idx; | |
break; | |
} | |
idx++; | |
} | |
return lostNumber; | |
} | |
/** | |
* | |
A simple substitution cipher replaces one character from an alphabet with a character from an alternate alphabet, where each character's position in an alphabet is mapped to the alternate alphabet for encoding or decoding. | |
E.g. | |
var abc1 = "abcdefghijklmnopqrstuvwxyz"; | |
var abc2 = "etaoinshrdlucmfwypvbgkjqxz"; | |
var sub = new SubstitutionCipher(abc1, abc2); | |
sub.encode("abc") // => "eta" | |
sub.encode("xyz") // => "qxz" | |
sub.encode("aeiou") // => "eirfg" | |
sub.decode("eta") // => "abc" | |
sub.decode("qxz") // => "xyz" | |
sub.decode("eirfg") // => "aeiou" | |
If a character provided is not in the opposing alphabet, simply leave it as be. | |
*/ | |
- (NSString *)encode:(NSString *)plaintext { | |
NSString *cipher_text = @""; | |
for(int x = 0; x < plaintext.length; x ++) { | |
for(int y = 0; y < abc1.length; y ++) { | |
if([plaintext characterAtIndex:x] == [abc1 characterAtIndex:y]) { | |
cipher_text = [cipher_text stringByAppendingFormat:@"%c",[abc2 characterAtIndex:y]]; | |
break; | |
} | |
} | |
} | |
return cipher_text; | |
} | |
- (void)validateRange { | |
} | |
- (NSString *)decode:(NSString *)encrypted { | |
NSString *cipher_text = @""; | |
for(int x = 0; x < encrypted.length; x ++) { | |
for(int y = 0; y < abc2.length; y ++) { | |
if([encrypted characterAtIndex:x] == [abc2 characterAtIndex:y]) { | |
cipher_text = [cipher_text stringByAppendingFormat:@"%c",[abc1 characterAtIndex:y]]; | |
break; | |
} | |
} | |
} | |
return cipher_text; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment