Senry
BAN USERHere is my code in Objective-C
- (void)printSums:(NSInteger)theNumber {
NSMutableArray* anArray = [NSMutableArray array];
[self sum:theNumber currentValue:theNumber-1 currentArray:anArray];
}
- (void)sum:(NSInteger)n currentValue:(NSInteger)cv currentArray:(NSMutableArray*)array {
NSInteger sum = [[array valueForKeyPath:@"@sum.self"] intValue] + cv;
if (sum > n) {
// Over
[self sum:n currentValue:cv-1 currentArray:array];
}
if (sum < n) {
// Under (find repeaters)
for (int i = cv; i > 0; i--) {
NSMutableArray* b = [array mutableCopy];
[b addObject:@(i)];
[self sum:n currentValue:i currentArray:b];
}
}
if (sum == n) {
// Equal (find more decremented solutions), then print.
for (int i = cv-1; i > 0; i--) {
NSMutableArray* b = [array mutableCopy];
[b addObject:@(i)];
[self sum:n currentValue:i currentArray:b];
}
[array addObject:@(cv)];
NSLog(@"A solution: %@", array);
}
}
Here is my code in Objective-C
- (void)printSums:(NSInteger)theNumber {
NSMutableArray* anArray = [NSMutableArray array];
[self sum:theNumber currentValue:theNumber-1 currentArray:anArray];
}
- (void)sum:(NSInteger)n currentValue:(NSInteger)cv currentArray:(NSMutableArray*)array {
NSInteger sum = [[array valueForKeyPath:@"@sum.self"] intValue] + cv;
if (sum > n) {
// Over
[self sum:n currentValue:cv-1 currentArray:array];
}
if (sum < n) {
// Under (find repeaters)
for (int i = cv; i > 0; i--) {
NSMutableArray* b = [array mutableCopy];
[b addObject:@(i)];
[self sum:n currentValue:i currentArray:b];
}
}
if (sum == n) {
// Equal (find more decremented solutions), then print.
for (int i = cv-1; i > 0; i--) {
NSMutableArray* b = [array mutableCopy];
[b addObject:@(i)];
[self sum:n currentValue:i currentArray:b];
}
[array addObject:@(cv)];
NSLog(@"A solution: %@", array);
}
}
This is my code in Objective-C
- (NSArray *)sumUpNumberArray:(NSArray *)numbA withNumberArray:(NSArray *)numbB {
NSInteger maxCount = MAX([numbA count], [numbB count]);
NSMutableArray *rtMArray = [NSMutableArray arrayWithCapacity:maxCount];
NSInteger gapNumbA = maxCount-[numbA count];
NSInteger gapNumbB = maxCount-[numbB count];
NSInteger addSum = 0;
NSInteger iterator = maxCount;
while ((iterator - gapNumbA - 1 >= 0) && (iterator - gapNumbB - 1 >= 0)) {
addSum += [numbA[iterator - gapNumbA - 1] integerValue] + [numbB[iterator - gapNumbB - 1] integerValue];
[rtMArray insertObject:@(addSum%2) atIndex:0];
--iterator;
addSum /= 2;
}
// if numbA has more
while (iterator - gapNumbA - 1 >= 0) {
addSum += [numbA[iterator - gapNumbA - 1] integerValue];
[rtMArray insertObject:@(addSum%2) atIndex:0];
--iterator;
addSum /= 2;
}
// if numbB has more
while (iterator - gapNumbB - 1 >= 0) {
addSum += [numbA[iterator - gapNumbB - 1] integerValue];
[rtMArray insertObject:@(addSum%2) atIndex:0];
--iterator;
addSum /= 2;
}
if (addSum > 0) {
[rtMArray insertObject:@(addSum%2) atIndex:0];
}
return rtMArray;
}
// Test:
NSArray *array = [self sumUpNumberArray:@[@1, @0, @0, @0, @1, @1] withNumberArray:@[@1, @0, @0, @1, @0, @0]];
Here is code in Objective-C.
- (NSArray *)findMinSumSubarrayFromArray:(NSArray *)array withKey:(NSInteger)key {
// Iterate from head to tail, with 2 indexs. Sum up numbers between the range of numbers.
// 1. Whenever the sum is no less than key, remember the range and sum.
// 2. Then the smaller index move on till the sum is less than key, every step we check if we get a closer sum, then the bigger index move on.
// 3. Whenever the sum is no less than key, compare the remembered sum, if smaller remember the range and sum.
// 4. repeat 1 to 3 till the smaller index reaches the end.
if ([array count]<=1) {
return array;
}
NSMutableArray *rtArray = [NSMutableArray array];
NSUInteger p = 0, q = 1, end = [array count];
NSInteger currentSum = NSIntegerMax;
NSInteger currentBegin = 0;
NSInteger currentEnd = 0;
while (p < end-1 || q < end-1) {
NSArray *subArray = [array subarrayWithRange:NSMakeRange(p, q-p+1)];
NSInteger sum = [[subArray valueForKeyPath:@"@sum.self"] integerValue];
if (sum >= key) {
if (sum < currentSum) {
currentBegin = p;
currentEnd = q;
currentSum = sum;
}
do {
p++;
subArray = [array subarrayWithRange:NSMakeRange(p, q-p+1)];
sum = [[subArray valueForKeyPath:@"@sum.self"] integerValue];
if (sum>=key && sum < currentSum) {//This is important in case we missed sum closer to key
currentBegin = p;
currentEnd = q;
currentSum = sum;
}
} while (sum>=key && p<q);
}
else {
if (q<end-1) {
q++;
}
else {//here is where p moves on
do {
p++;
subArray = [array subarrayWithRange:NSMakeRange(p, q-p+1)];
sum = [[subArray valueForKeyPath:@"@sum.self"] integerValue];
if (sum>=key && sum < currentSum) {//This is important in case we missed sum closer to key
currentBegin = p;
currentEnd = q;
currentSum = sum;
}
} while (sum>=key && p<q);
}
}
}
if (currentSum != NSIntegerMax) {
[rtArray addObjectsFromArray:[array subarrayWithRange:NSMakeRange(currentBegin, currentEnd-currentBegin+1)]];
}
return rtArray;
}
NSArray *sumArray = [self findMinSumSubarrayFromArray:@[@(2), @(7), @(-3), @(8), @(-5), @(3), @(1), @(2), @(-3)] withKey:6];
NSLog(@"%@", sumArray);
(
"-3",
8,
"-5",
3,
1,
2
)
The following is written in Objective-C
- (BOOL)isPalindromeIgnoreALot:(NSString *)string {
BOOL isPalindrome = YES;
NSString *lowercaseString = [string lowercaseString];
NSString *purewordString = [[lowercaseString componentsSeparatedByCharactersInSet:[NSCharacterSet punctuationCharacterSet]] componentsJoinedByString:@""]; // remove punctuation
purewordString = [[purewordString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] componentsJoinedByString:@""]; // remove white space and new line
if (purewordString) {
NSInteger length = [purewordString length];
NSInteger halfLength = (length / 2);
for (int i = 0; i < halfLength; i++) {
if ([purewordString characterAtIndex:i] != [purewordString characterAtIndex:length - i - 1]) {
isPalindrome = NO;
break;
}
}
}
return isPalindrome;
}
The following is my solution in Objective-C
- (NSString *)getCharStringFromCode:(NSInteger)code {
NSString *rtString = @"";
if (code>=1 && code<=26) {
rtString = [NSString stringWithFormat:@"%c", 'a'+ code - 1];
}
return rtString;
}
- (void)stringFromAlphabetCodes:(NSArray *)codes currentString:(NSString *)currentString currentIndex:(NSUInteger)index returnArray:(NSMutableArray *)rtArray{
if ([codes count] > index) {
NSInteger singleCode = [codes[index] integerValue];
if (index + 1 < [codes count]) {
NSInteger afterCode = [codes[index+1]integerValue];
NSInteger twoCodes = singleCode*10+afterCode;
if (afterCode == 0) {
NSString *currStr = [currentString stringByAppendingString:[self getCharStringFromCode:twoCodes]];
[self stringFromAlphabetCodes:codes currentString:currStr currentIndex:index+2 returnArray:rtArray];
}
else if (twoCodes > 0 && twoCodes < 27) {
NSString *currStr = [currentString stringByAppendingString:[self getCharStringFromCode:singleCode]];
[self stringFromAlphabetCodes:codes currentString:currStr currentIndex:index+1 returnArray:rtArray];
currStr = [currentString stringByAppendingString:[self getCharStringFromCode:twoCodes]];
[self stringFromAlphabetCodes:codes currentString:currStr currentIndex:index+2 returnArray:rtArray];
}
else if (twoCodes > 26) {
NSString *currStr = [currentString stringByAppendingString:[self getCharStringFromCode:singleCode]];
[self stringFromAlphabetCodes:codes currentString:currStr currentIndex:index+1 returnArray:rtArray];
}
}
else {
NSString *currStr = [currentString stringByAppendingString:[self getCharStringFromCode:singleCode]];
[self stringFromAlphabetCodes:codes currentString:currStr currentIndex:index+1 returnArray:rtArray];
}
}
else {
[rtArray addObject:currentString];
}
}
Test case:
NSArray *codes = @[@"1",@"0",@"2",@"7",@"1",@"2",@"3"];
NSMutableArray *mArray = [NSMutableArray array];
[self stringFromAlphabetCodes:codes currentString:@"" currentIndex:0 returnArray:mArray];
NSLog(@"%@", mArray);
(
jbgabc,
jbgaw,
jbglc
)
I modified my code, to fix error when "0" is in the codes
- Senry January 19, 2014