Hacker Rank problem solution
1. Sort Item based on the column :
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *ary = [self fetchItemsToDisplay:@[@"Item4,10,15",@"Item2,3,4",@"Item3,17,8"] sortParameter:@(1) sortOrder:@(0) itemPerPage:@(2) pageNumber:@(1)];
NSLog(@"%@",ary);
}
- (NSArray *) fetchItemsToDisplay:(NSArray *)items sortParameter:(NSNumber *)sortParameter sortOrder:(NSNumber *)sortOrder itemPerPage:(NSNumber *)itemPerPage pageNumber:(NSNumber *)pageNumber {
NSMutableArray *itemArray = [NSMutableArray new];
for (int i = 0; i<items.count; i++) {
NSString *item = items[i];
NSArray *itemDetails = [item componentsSeparatedByString:@","];
ItemModel *itemModel = [[ItemModel alloc] init];
itemModel.name = itemDetails[0];
itemModel.relevance = [itemDetails[1] integerValue];
itemModel.price = [itemDetails[2] integerValue];
[itemArray addObject:itemModel];
}
NSString *key = @"name";
if ([sortParameter integerValue] == 1) {
key = @"relevance";
} else if ([sortParameter integerValue] == 2) {
key = @"price";
}
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:key ascending:![sortOrder integerValue]];
NSArray *sortedArry = [itemArray sortedArrayUsingDescriptors:@[sortDescriptor]];
NSArray *pageArray = [sortedArry subarrayWithRange:NSMakeRange(itemPerPage.integerValue * pageNumber.integerValue, sortedArry.count - itemPerPage.integerValue * pageNumber.integerValue)];
return [pageArray valueForKeyPath:@"self.name"];
}
- (NSArray *) getFirstTwoItemsWithoutPair:(NSArray *)list {
NSMutableArray *rejectedAry = [NSMutableArray new];
NSMutableArray *resultAry = [NSMutableArray new];
for (int i = 0; i < list.count; i++) {
if ([resultAry containsObject:list[i]]) {
[resultAry removeObject:list[i]];
[rejectedAry addObject:list[i]];
}else{
if (![rejectedAry containsObject:list[i]]) {
[resultAry addObject:list[i]];
}
}
}
return [NSArray arrayWithObjects:resultAry[0],resultAry[1], nil];
}
2. Bracket problem Hacker Rank (Swift)
@objc func showValue() {
let input = ["(","{{(())}}","{)}","{"]
print(result(array: input))
}
func result(array : [String]) -> [String] {
var result:[String] = []
var holdingAry:[String] = [String]()
for srt in array {
var yesNo = "YES"
for char in srt {
if yesNo == "NO" {
break;
}
switch char {
case "{","(","[":
holdingAry.append("\(char)")
break
case "}",")","]":
if holdingAry.count == 0 || (char == "}" && holdingAry.last != "{" ) || (char == ")" && holdingAry.last != "(" ) || (char == "]" && holdingAry.last != "[" ) {
yesNo = "NO"
break;
} else {
holdingAry.removeLast()
}
break
default:
print("")
}
yesNo = holdingAry.count > 0 ? "NO" : "YES"
}
result.append(yesNo)
}
return result
}
3. Bracket proble (Objective C
- (NSArray *)braces:(NSArray *)values {
// {[}]
NSMutableArray *resultAry = [NSMutableArray new];
for (int i = 0 ; i < values.count; i++) {
NSString *str = values[i];
NSString *resu = @"YES";
NSMutableArray *addSubString = [NSMutableArray new];
for (int j = 0; j<str.length; j++) {
NSString *subStr = [str substringWithRange:NSMakeRange(j, 1)];
NSString *subStrNew = @"";
NSString *rangeString = @"})]";
if ([rangeString containsString:subStr]) {
if ([subStr isEqualToString:@"}"]) {
subStrNew = @"{";
}else if ([subStr isEqualToString:@")"]) {
subStrNew = @"(";
}else if ([subStr isEqualToString:@"]"]) {
subStrNew = @"[";
}
if ([addSubString containsObject:subStrNew]) {
if ([addSubString.lastObject isEqualToString:subStrNew]) {
[addSubString removeLastObject];
} else {
resu = @"NO";
break;
}
}else {
resu = @"NO";
break;
}
}
else {
[addSubString addObject:subStr];
}
}
if (addSubString.count) {
resu = @"NO";
}
[resultAry addObject: resu];
}
return resultAry;
})
Comments
Post a Comment