Objective-C 练习题,题目来源 exercism.io,一共 50 题,见 GitHub

第一篇共 10 题。

(二)
(三)
(四)
(五)

# Hello World

题目和测试用例见链接,下同。 Hello World
The classical introductory exercise. Just say “Hello, World!”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// HelloWorld.h
#import <Foundation/Foundation.h>

@interface HelloWorld : NSObject

- (NSString *)hello:(NSString *)input;

@end

// HelloWorld.m
#import "HelloWorld.h"

@implementation HelloWorld

- (NSString *)hello:(NSString *)input {
return [NSString stringWithFormat:@"Hello, %@!", input.length > 0 ? input : @"World"];
}

@end

# Two Fer

Two Fer
Create a sentence of the form “One for X, one for me.”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// TwoFer.h
#import <Foundation/Foundation.h>

@interface TwoFer : NSObject

+ (NSString *)twoFerWithName:(NSString *)name;

@end

// TwoFer.m
#import "TwoFer.h"

@implementation TwoFer

+ (NSString *)twoFerWithName:(NSString *)name {
return [NSString stringWithFormat:@"One for %@, one for me.", name.length > 0 ? name : @"you"];
}

@end

# Bob

Bob
Bob is a lackadaisical teenager. In conversation, his responses are very limited.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Bob.h
#import <Foundation/Foundation.h>

@interface Bob : NSObject

- (NSString *)hey:(NSString *)input;

@end

// Bob.m
#import "Bob.h"

@implementation Bob

- (NSString *)hey:(NSString *)input {
input = [input stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]];
if (input.length == 0) {
return @"Fine. Be that way!";
}
if (isYelling(input)) {
return @"Whoa, chill out!";
}
if (isAsking(input)) {
return @"Sure.";
}
return @"Whatever.";
}

BOOL isYelling(NSString *input) {
if ([[input uppercaseString] isEqualToString:input]
&& ![[input lowercaseString] isEqualToString:input]) {
return YES;
}
return NO;
}

BOOL isAsking(NSString *input) {
if ([input hasSuffix:@"?"]) {
return YES;
}
return NO;
}

@end

# Leap

Leap
Given a year, report if it is a leap year.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Leap.h
#import <Foundation/Foundation.h>

@interface Leap : NSObject

@property (nonatomic, assign) BOOL isLeapYear;

- (instancetype)initWithCalendarYear:(NSNumber *)year;

@end

// Leap.m
#import "Leap.h"

@implementation Leap

- (instancetype)initWithCalendarYear:(NSNumber *)year {
if (self = [super init]) {
if ((year.integerValue % 4 == 0 && year.integerValue % 100 != 0)
|| year.integerValue % 400 == 0) {
self.isLeapYear = YES;
} else {
self.isLeapYear = NO;
}
}
return self;
}

@end

# Gigasecond

Gigasecond
Calculate the moment when someone has lived for 10^9 seconds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Gigasecond.h
#import <Foundation/Foundation.h>

@interface Gigasecond : NSObject

@property (nonatomic, strong) NSDate *gigasecondDate;

- (instancetype)initWithStartDate:(NSDate *)startDate;

@end

// Gigasecond.m
#import "Gigasecond.h"

@implementation Gigasecond

- (instancetype)initWithStartDate:(NSDate *)startDate {
if (self = [super init]) {
self.gigasecondDate = [startDate dateByAddingTimeInterval:1e9];
}
return self;
}

@end

# Difference Of Squares

Difference Of Squares
Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// DifferenceOfSquares.h
#import <Foundation/Foundation.h>

@interface DifferenceOfSquares : NSObject

@property (nonatomic, assign) long differenceOfSquares;
@property (nonatomic, assign) long squareOfSums;
@property (nonatomic, assign) long sumOfSquares;

- (instancetype)initWithMax:(NSInteger)max;

@end

// DifferenceOfSquares.m
#import "DifferenceOfSquares.h"

@implementation DifferenceOfSquares

- (instancetype)initWithMax:(NSInteger)max {
if (self = [super init]) {
self.squareOfSums = (max * (1 + max) / 2) * (max * (1 + max) / 2);
self.sumOfSquares = max * (max + 1) * (max * 2 + 1) / 6;
self.differenceOfSquares = self.squareOfSums - self.sumOfSquares;
}
return self;
}

@end

# Sum Of Multiples

Sum Of Multiples
Given a number, find the sum of all the multiples of particular numbers up to but not including that number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// SumOfMultiples.h
#import <Foundation/Foundation.h>

@interface SumOfMultiples : NSObject

+ (NSNumber *)toLimit:(NSNumber *)limit inMultiples:(NSArray<NSNumber *> *)multiples;

@end

// SumOfMultiples.m
#import "SumOfMultiples.h"

@implementation SumOfMultiples

+ (NSNumber *)toLimit:(NSNumber *)limit inMultiples:(NSArray<NSNumber *> *)multiples {
NSInteger sum = 0;
NSMutableSet *set = [NSMutableSet set];
for (NSNumber *number in multiples) {
if ([number isEqual:@0]) {
continue;
}
for (NSInteger i = 1; i < [limit integerValue]; ++i) {
if (i * [# number integerValue] < [limit integerValue]) {
[set addObject:@(i * [# number integerValue])];
}
}
}
for (NSNumber *number in set) {
sum += [number integerValue];
}
return @(sum);
}

@end

# Space Age

Space Age
Given an age in seconds, calculate how old someone is in terms of a given planet’s solar years.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// SpaceAge.h
#import <Foundation/Foundation.h>

@interface SpaceAge : NSObject

@property (nonatomic, assign) double seconds;
@property (nonatomic, assign) double onEarth;
@property (nonatomic, assign) double onMercury;
@property (nonatomic, assign) double onVenus;
@property (nonatomic, assign) double onMars;
@property (nonatomic, assign) double onJupiter;
@property (nonatomic, assign) double onSaturn;
@property (nonatomic, assign) double onUranus;
@property (nonatomic, assign) double onNeptune;

- (instancetype)initWithSeconds:(double)seconds;

@end

// SpaceAge.m
#import "SpaceAge.h"

@implementation SpaceAge

- (instancetype)initWithSeconds:(double)seconds {
if (self = [super init]) {
self.seconds = seconds;
self.onEarth = seconds / 31557600;
self.onMercury = self.onEarth / 0.2408467;
self.onVenus = self.onEarth / 0.61519726;
self.onMars = self.onEarth / 1.8808158;
self.onJupiter = self.onEarth / 11.862615;
self.onSaturn = self.onEarth / 29.447498;
self.onUranus = self.onEarth / 84.016846;
self.onNeptune = self.onEarth / 164.79132;
}
return self;
}

@end

# Hamming

Hamming
Calculate the Hamming difference between two DNA strands.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Hamming.h
#import <Foundation/Foundation.h>

@interface Hamming : NSObject

+ (NSInteger)compute:(NSString *)strands against:(NSString *)strands2;

@end

// Hamming.m
#import "Hamming.h"

@implementation Hamming

+ (NSInteger)compute:(NSString *)strands against:(NSString *)strands2 {
NSInteger hammingDistance = 0;
if ([strands isEqualToString:strands2]) {
return hammingDistance;
}
for (NSInteger i = 0; i < MIN(strands.length, strands2.length); ++i) {
if ([strands characterAtIndex:i] != [strands2 characterAtIndex:i]) {
hammingDistance++;
}
}
return hammingDistance;
}

@end

# Nucleotide Count

Nucleotide Count
Given a DNA string, compute how many times each nucleotide occurs in the string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// NucleotideCount.h
#import <Foundation/Foundation.h>

@interface NucleotideCount : NSObject

- (instancetype)initWithStrand:(NSString *)strand;
- (NSInteger)count:(NSString *)symbol;
- (NSDictionary *)nucleotideCounts;

@end

// NucleotideCount.m
#import "NucleotideCount.h"

@implementation NucleotideCount {
NSString *_kStrand;
}

- (instancetype)initWithStrand:(NSString *)strand {
if (self = [super init]) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^[ACGT]{0,}$"];
NSAssert([predicate evaluateWithObject:strand], @"NOT DNA STRAND!");
_kStrand = strand;
}
return self;
}

- (NSInteger)count:(NSString *)symbol {
return _kStrand.length - [[_kStrand copy] stringByReplacingOccurrencesOfString:symbol withString:@""].length;
}

- (NSDictionary *)nucleotideCounts {
return @{
@"A" : @([self count:@"A"]),
@"C" : @([self count:@"C"]),
@"G" : @([self count:@"G"]),
@"T" : @([self count:@"T"])
};
}

@end