Skip to content

Instantly share code, notes, and snippets.

@SunXiaoShan
Last active July 13, 2017 03:33
Show Gist options
  • Save SunXiaoShan/1725325968fa8e492892315f46f9a81d to your computer and use it in GitHub Desktop.
Save SunXiaoShan/1725325968fa8e492892315f46f9a81d to your computer and use it in GitHub Desktop.
ExamAengin
// JAVA
// QUESTION 1
static String getReverseString(String input) {
char[] in = input.toCharArray();
char buff;
for (int i=0,j=input.length()-1; i<input.length()/2;i++,j--) {
buff = in[i];
in[i] = in[j];
in[j] = buff;
}
return new String(in);
}
// Using .....
// String s = "hello";
// String rs = getReverseString(s);
//
// QUESTION 2
static int _result = 999999999;
static void getMinValue(int[][] input) {
_result = 999999999;
for (int y=0; y<input.length; y++) {
forceSolve(input, 0, 0, input[0].length);
}
}
static void forceSolve(int[][] input, int indexX, int buff, int maxDepth) {
if (indexX >= maxDepth) {
if (buff < _result) {
_result = buff;
}
return;
}
for (int y=0; y<input.length; y++) {
int _buff = input[y][indexX] + buff;
forceSolve(input, indexX+1, _buff, maxDepth);
}
}
// Using .....
// int[][] data = .....
// getMinValue(data);
// int ans = _result;
// Objective C
// QUESTION 3
@interface Beacon : NSObject
@property (atomic, strong) NSString *beaconId;
@property (atomic, strong) NSNumber *timestamp;
@property (atomic, strong) NSData *data;
- (void)setCurrentTime;
@end
@implementation Beacon
- (instancetype)initWithData:(NSString *)beaconId withData:(NSData *)data {
if (self = [super init]) {
self.data = data;
[self setCurrentTime];
}
return self;
}
- (void)setCurrentTime {
unsigned long long timeInMiliseconds = (unsigned long long)[[NSDate date] timeIntervalSince1970];
_timestamp = [NSNumber numberWithUnsignedLongLong:timeInMiliseconds];
}
- (BOOL) isOverTime {
const unsigned long long INTERVAL_TIME = 10; //10 sec
const unsigned long long now = (unsigned long long)[[NSDate date] timeIntervalSince1970] - INTERVAL_TIME;
return now > [_timestamp unsignedLongLongValue];
}
@end
@interface Manager : NSObject
+(Manager *) getInstance;
@property (atomic, strong) NSTimer *timer;
@property (atomic, strong) NSMutableDictionary<NSString *, Beacon *> *list;
@end
@implementation Manager
static Manager* instance;
+(Manager *) getInstance
{
static dispatch_once_t token;
dispatch_once(&token, ^{
instance = [[Manager alloc] init];
});
return instance;
}
- (id)init {
self = [super init];
if(self){
self.list = [NSMutableDictionary new];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(checkTime)
userInfo:nil
repeats:YES] ;
}
return self;
}
- (void)setBeacon:(Beacon *)beacon {
if (beacon.beaconId == nil) { return; }
if ([self.list objectForKey:beacon.beaconId]) {
Beacon *bc = [self.list objectForKey:beacon.beaconId];
[bc setCurrentTime];
} else {
[self.list setObject:beacon forKey:beacon.beaconId];
}
}
- (void)checkTime {
NSArray *keys = [self.list allKeys];
NSMutableArray *temp = [NSMutableArray new];
for (NSString *key in keys) {
if ([self.list objectForKey:key] == nil) { continue; }
Beacon *buff = [self.list objectForKey:key];
if ([buff isOverTime]) {
[temp addObject:buff.beaconId];
}
}
if ([temp count] > 0) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"beacon_timeout" object:self userInfo:@{@"beacons": temp}];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment