Language detail: Objective-C
Coverage: 22.14%
|
number of '+' ratings |
contribution for coverage |
Unsolved challenges
- タブ区切りデータの処理 (Nested Flatten)
- LL Golf Hole 9 - トラックバックを打つ (Nested Flatten)
- 2^i * 3^j * 5^k なる整数 (Nested Flatten)
- 起動オプションの解析 (Nested Flatten)
- 文字列型日時ののN秒後時間取得 (Nested Flatten)
codes
固定長データ
(Nested
Flatten)
開発環境: Mac OS X 10.5.2 Xcode 3.1 (Beta)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #import <Foundation/Foundation.h>
#define FilePath @"fixedlengthdata.txt"
const unsigned int RecordSize = 12 + 12 + 1 + 3 + 4 + 2 + 31 * 1502;
const unsigned int RecordCount = 500;
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSData* data = [NSData dataWithContentsOfMappedFile:FilePath];
if ( data ) {
NSMutableArray* array = [NSMutableArray array];
for ( int i = 0; i < RecordCount; i++ )
[array addObject:[data subdataWithRange:NSMakeRange( i * RecordSize, RecordSize )]];
}
[pool drain];
return 0;
}
|
アルファベットの繰り上がり
(Nested
Flatten)
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 | #import <Foundation/Foundation.h>
NSString* toCol(int n)
{
NSMutableString* s = [NSMutableString stringWithFormat:@"%C", 'A' + n%26];
n /= 26;
while (n > 0) {
if (n <= 26) {
[s insertString:[NSString stringWithFormat:@"%C", 'A' + n-1] atIndex:0];
break;
}
n -= 1;
[s insertString:[NSString stringWithFormat:@"%C", 'A' + n%26] atIndex:0];
n /= 26;
}
return s;
}
int main()
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
int i;
for (i=0; i<100; i++) {
NSLog(@"%3d: %@", i+1, toCol(i));
}
[pool release];
return 0;
}
|
実行時間の測定
(Nested
Flatten)
任意のオブジェクトに任意のメッセージを送ってその実行時間を測定するようにしてみました。
実行結果はこんな感じ。
$ ./profile
2007-11-16 02:11:49.268 profile[3982:10b] start profiling [TestClass func:]
2007-11-16 02:11:49.274 profile[3982:10b] 一言言って1.000000秒待つことを3回繰り返すだけの簡単なお仕事です
2007-11-16 02:11:50.275 profile[3982:10b] 一言言って1.000000秒待つことを3回繰り返すだけの簡単なお仕事です
2007-11-16 02:11:51.276 profile[3982:10b] 一言言って1.000000秒待つことを3回繰り返すだけの簡単なお仕事です
2007-11-16 02:11:52.277 profile[3982:10b] finished!
2007-11-16 02:11:52.278 profile[3982:10b] process time: 3.003400 (sec)
実行結果はこんな感じ。
$ ./profile
2007-11-16 02:11:49.268 profile[3982:10b] start profiling [TestClass func:]
2007-11-16 02:11:49.274 profile[3982:10b] 一言言って1.000000秒待つことを3回繰り返すだけの簡単なお仕事です
2007-11-16 02:11:50.275 profile[3982:10b] 一言言って1.000000秒待つことを3回繰り返すだけの簡単なお仕事です
2007-11-16 02:11:51.276 profile[3982:10b] 一言言って1.000000秒待つことを3回繰り返すだけの簡単なお仕事です
2007-11-16 02:11:52.277 profile[3982:10b] finished!
2007-11-16 02:11:52.278 profile[3982:10b] process time: 3.003400 (sec)
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | #import <Foundation/Foundation.h>
@interface Profiler : NSObject
{
id target;
}
- (id)initWithObject:(id)aTarget;
- (void)setTarget:(id)aTarget;
@end
@implementation Profiler
- (id)initWithObject:(id)aTarget
{
[super init];
target = aTarget;
return self;
}
- (id)init
{
return [self initWithObject:nil];
}
- (void)setTarget:(id)aTarget
{
target = aTarget;
return;
}
- (void)profile:(NSInvocation *)anInvocation
{
NSLog(@"start profiling [%@ %s]", [target className], [anInvocation selector]);
NSDate *startTime = [NSDate date];
[anInvocation invokeWithTarget:target];
NSDate *endTime = [NSDate date];
NSLog(@"finished!");
NSLog(@"process time: %f (sec)", [endTime timeIntervalSinceDate:startTime]);
return;
}
// メッセージ転送によって「対象の関数と同じ引数でプロファイル」を実現する
// Profilerの実装していないメッセージが来たら、
// 実行時間の計測をしつつ実際の処理はtargetに移譲
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
if ([target respondsToSelector:aSelector])
return [target methodSignatureForSelector:aSelector];
else
return [super methodSignatureForSelector:aSelector];
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
if ([super respondsToSelector:aSelector])
return YES;
if ([self methodForSelector:aSelector] != (IMP)NULL)
return YES;
if ([target respondsToSelector:aSelector])
return YES;
return NO;
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
if ([target respondsToSelector:[anInvocation selector]])
{
[self profile:anInvocation];
}
else
{
[super forwardInvocation:anInvocation];
}
return;
}
//--- メッセージ転送処理ここまで
@end
@interface TestClass : NSObject
{
NSTimeInterval interval;
}
- (void)func:(int)times;
@end
@implementation TestClass
- (id)init
{
[super init];
interval = 1.0;
return self;
}
- (void)func:(int)times
{
int i;
for (i = 0; i < times; i++)
{
NSLog(@"一言言って%f秒待つことを%d回繰り返すだけの簡単なお仕事です", interval, times);
[NSThread sleepForTimeInterval:interval];
// Xcode 2.x だとスレッドのスリープはこっち
// [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:interval]];
}
}
@end
int main(int argc, const char *argv[])
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
id testTarget = [TestClass new];
Profiler *profiler = [[Profiler alloc] initWithObject:testTarget];
//profilerに本来testTargetが受けとるはずのメッセージを送ってやる
[profiler func:3];
[pool release];
return 0;
}
|
リストを逆順に表示
(Nested
Flatten)
reverseObjectEnumeratorで。
1 2 3 4 5 6 7 8 9 10 | #import <Foundation/Foundation.h>
int main()
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSArray *givenList = [NSArray arrayWithObjects:@"a", @"b", @"c", @"d", @"e", nil];
for (id obj in [givenList reverseObjectEnumerator]) NSLog(obj);
[pool release];
return 0;
}
|
ファイル更新の監視
(Nested
Flatten)
NSRunLoopで回すのはいいのかどうか分かりません。NSTimerを使いたくてこの形に。 一応Leopardタグ付けますがfor ~ in ~のところをNSEnumeratorを使うように書き換えればXcode2.xでもコンパイルできるはず。 あと、Leopardだとそのままずばりファイルの監視をするFSEventというAPIがあります。 TimeMachineが使ってるやつ。これはCなんですが、詳細は参考ページ参照で。
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | #import <Foundation/Foundation.h>
#define WATCH_PATH @"target path"
#define WATCH_FILE_ATTR @"target file atrributes"
#define WATCH_FILE_MODIFIED @"target file modified"
@interface Watcher : NSObject
{
NSMutableDictionary *targetPathes;
NSFileManager *manager;
NSMutableDictionary *fileAttributes;
}
- (id)initWithTargetPathes:(NSArray *)pathes;
- (void)checkModified;
@end
@implementation Watcher
- (id)initWithTargetPathes:(NSArray *)pathes;
{
targetPathes = [NSMutableDictionary dictionary];
manager = [NSFileManager defaultManager];
fileAttributes = [NSMutableDictionary dictionary];
for (NSString *path in pathes)
{
[targetPathes setValue:path forKey:path];
NSDictionary *status = [manager fileAttributesAtPath:path traverseLink:YES];
if (status) [fileAttributes setValue:status forKey:path];
}
return self;
}
- (void)sendNotification:(NSString *)aPath withStatus:(id)aStatus
{
NSString *message = [NSString stringWithFormat:@"'%@' is modified!", aPath];
[[NSNotificationCenter defaultCenter] postNotificationName:WATCH_FILE_MODIFIED
object:message
userInfo:aStatus];
return;
}
- (void)checkModified
{
for (NSString *path in targetPathes)
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSDictionary *_prev = [fileAttributes objectForKey:path];
NSDictionary *_current = [manager fileAttributesAtPath:path traverseLink:YES];
if (_prev || _current)
{
if (!_current)
{
[fileAttributes removeObjectForKey:path];
[self sendNotification:path withStatus:nil];
}
else if (![_current isEqual:_prev])
{
[fileAttributes setObject:[_current copy] forKey:path];
[self sendNotification:path withStatus:_current];
}
}
[pool release];
}
return;
}
@end
@interface FileStatusLogger : NSObject
{
NSNotificationCenter* center;
}
- (id)initWithNotificationCenter:(NSNotificationCenter *)aCenter;
- (void)log:(NSNotification *)notice;
@end
@implementation FileStatusLogger
- (id)initWithNotificationCenter:(NSNotificationCenter *)aCenter
{
[super init];
[aCenter addObserver:self selector:@selector(log:) name:WATCH_FILE_MODIFIED object:nil];
return self;
}
- (id)init
{
return [self initWithNotificationCenter:[NSNotificationCenter defaultCenter]];
}
- (void)log:(NSNotification *)notice;
{
NSLog(@"%@", [notice object]);
return;
}
@end
void usage(name)
{
printf("Usage: %s <interval> <duration> <path1> [<path2>...]\n", name);
exit(1);
}
int main(int argc, char *argv[])
{
if (argc < 4) usage(argv[0]);
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSMutableArray *pathes = [NSMutableArray array];
NSTimeInterval interval = strtod(argv[1], NULL);
NSTimeInterval duration = strtod(argv[2], NULL);
if (interval <= 0) usage(argv[0]);
int i;
for (i = 3; i < argc; i++)
{
[pathes addObject:[NSString stringWithCString:argv[i]]];
}
Watcher *watcher = [[Watcher alloc] initWithTargetPathes:pathes];
FileStatusLogger *logger = [FileStatusLogger new];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:interval
target:watcher
selector:@selector(checkModified)
userInfo:nil
repeats:YES];
NSLog(@"monitoring start");
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
if (duration > 0)
{
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:duration]];
}
else
{
[[NSRunLoop currentRunLoop] run];
}
NSLog(@"monitoring end");
[pool release];
return 0;
}
|
重複無し乱数
(Nested
Flatten)
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 | #import <Foundation/Foundation.h>
@interface NSArray (Bingo)
+ (NSArray*)bingo:(int)n;
@end
@implementation NSArray (Bingo)
+ (NSArray*)bingo:(int)n
{
NSMutableArray* array = [NSMutableArray array];
int i;
for (i=1; i<=n; i++) [array addObject:[NSNumber numberWithInteger:i]];
NSMutableArray* result = [NSMutableArray array];
for (i=1; i<=n; i++) {
int size = n - i + 1;
int index = rand() % size;
[result addObject:[array objectAtIndex:index]];
[array removeObjectAtIndex:index];
}
return result;
}
@end
int main(int argc, char** argv)
{
NSAutoreleasePool* pool = [NSAutoreleasePool new];
srand(time(0));
NSLog(@"%@", [NSArray bingo:3]);
NSLog(@"%@", [NSArray bingo:10]);
[pool release];
return 0;
}
|
隣り合う二項の差
(Nested
Flatten)
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #import <Foundation/Foundation.h>
@interface NSMutableArray (IntegerAccess)
- (void)addInteger:(int)val;
- (int)integerAt:(int)index;
@end
@implementation NSMutableArray (IntegerAccess)
- (void)addInteger:(int)val
{
[self addObject:[NSNumber numberWithInteger:val]];
}
- (int)integerAt:(int)index
{
return [[self objectAtIndex:index] intValue];
}
@end
@interface NSArray (Diff)
- (NSArray*)diff;
@end
@implementation NSArray (Diff)
- (NSArray*)diff
{
NSMutableArray* result = [NSMutableArray array];
id prev = nil;
for (id obj in self) {
if (prev) [result addInteger:[obj intValue] - [prev intValue]];
prev = obj;
}
return result;
}
@end
int main(int argc, char** argv)
{
NSAutoreleasePool* pool = [NSAutoreleasePool new];
NSMutableArray* array = [NSMutableArray array];
[array addInteger:3];
[array addInteger:1];
[array addInteger:4];
[array addInteger:1];
[array addInteger:5];
[array addInteger:9];
[array addInteger:2];
[array addInteger:6];
[array addInteger:5];
NSLog(@"%@", array);
NSLog(@"%@", [array diff]);
[pool release];
return 0;
}
|
与えられた文字列でピラミッド
(Nested
Flatten)
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 | #include <stdio.h>
#include <string.h>
void print_pyramid(const char* s)
{
int i, j;
int n = strlen(s);
for (i=0; i<n; i++) {
for (j=n-i-2; j>=0; j--) putchar(' ');
for (j=n-i-1; j<n-1; j++) {
putchar(s[j]);
putchar(' ');
}
putchar(s[j]);
puts("");
}
}
int main(int argc, char** argv)
{
print_pyramid("hoge");
print_pyramid("abracadabra");
return 0;
}
|
格子点の列挙
(Nested
Flatten)
デバッグ用のコードが残っていたので削除しました。
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #import <Foundation/Foundation.h>
@interface MyPoint : NSObject
{
int x;
int y;
long distance;
}
+ (MyPoint*)pointWithX:(int)x y:(int)y;
- (int)x;
- (int)y;
- (long)distance;
- (NSComparisonResult)compareWithPoint:(MyPoint*)rhs;
@end
@implementation MyPoint
- (int)x { return x; }
- (int)y { return y; }
- (long)distance { return distance; }
- (void)setX:(int)px y:(int)py
{
x = px;
y = py;
distance = x*x + y*y;
}
+ (MyPoint*)pointWithX:(int)x y:(int)y
{
MyPoint* obj = [[[MyPoint alloc] init] autorelease];
[obj setX:x y:y];
return obj;
}
- (NSComparisonResult)compareWithPoint:(MyPoint*)rhs
{
long d = [rhs distance];
if (distance < d) return NSOrderedAscending;
if (distance > d) return NSOrderedDescending;
int rx = [rhs x];
int ry = [rhs y];
if (y >= 0) {
if (ry >= 0) {
return x < rx ? NSOrderedDescending : NSOrderedAscending;
} else {
return NSOrderedAscending;
}
} else {
if (ry >= 0) {
return NSOrderedDescending;
} else {
return x < rx ? NSOrderedAscending : NSOrderedDescending;
}
}
}
@end
int main(int argc, char** argv)
{
NSAutoreleasePool* pool = [NSAutoreleasePool new];
NSMutableArray* input = [NSMutableArray array];
int r = 25;
int i, j;
for (i=-r; i<=r; i++) {
for (j=-r; j<r; j++) {
[input addObject:[MyPoint pointWithX:i y:j]];
}
}
NSArray* result = [input sortedArrayUsingSelector:@selector(compareWithPoint:)];
printf("count: %d\n", [result count]);
for (id p in result) {
printf("%3d, %3d\n", [p x], [p y]);
}
[pool release];
return 0;
}
|
あらかじめ点の配列を作っておいてソートするという力業。
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #import <Foundation/Foundation.h>
@interface MyPoint : NSObject
{
int x;
int y;
long distance;
}
+ (MyPoint*)pointWithX:(int)x y:(int)y;
- (int)x;
- (int)y;
- (long)distance;
- (NSComparisonResult)compareWithPoint:(MyPoint*)rhs;
@end
@implementation MyPoint
- (int)x { return x; }
- (int)y { return y; }
- (long)distance { return distance; }
- (void)setX:(int)px y:(int)py
{
x = px;
y = py;
distance = x*x + y*y;
}
+ (MyPoint*)pointWithX:(int)x y:(int)y
{
MyPoint* obj = [[[MyPoint alloc] init] autorelease];
[obj setX:x y:y];
return obj;
}
- (NSComparisonResult)compareWithPoint:(MyPoint*)rhs
{
long d = [rhs distance];
if (distance < d) return NSOrderedAscending;
if (distance > d) return NSOrderedDescending;
int rx = [rhs x];
int ry = [rhs y];
if (y >= 0) {
if (ry >= 0) {
return x < rx ? NSOrderedDescending : NSOrderedAscending;
} else {
return NSOrderedAscending;
}
} else {
if (ry >= 0) {
return NSOrderedDescending;
} else {
return x < rx ? NSOrderedAscending : NSOrderedDescending;
}
}
}
@end
int main(int argc, char** argv)
{
NSAutoreleasePool* pool = [NSAutoreleasePool new];
NSMutableArray* input = [NSMutableArray array];
int r = 25;
int i, j;
for (i=-r; i<=r; i++)
for (j=-r; j<r; j++)
[input addObject:[MyPoint pointWithX:i y:j]];
NSArray* result = [input sortedArrayUsingSelector:@selector(compareWithPoint:)];
printf("count: %d\n", [result count]);
i = 0;
for (id p in result) {
printf("%3d, %3d\n", [p x], [p y]);
i++;
}
[pool release];
return 0;
}
|
音声合成でHello, world!
(Nested
Flatten)
NSLog(@"%@", [NSSpeechSynthesizer availableVoices]); で使える声の一覧を確認できます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #import <Foundation/Foundation.h>
#import <Appkit/Appkit.h>
int main(int argc, char** argv)
{
NSAutoreleasePool* pool = [NSAutoreleasePool new];
NSSpeechSynthesizer* s = [[[NSSpeechSynthesizer alloc] initWithVoice:@"com.apple.speech.synthesis.voice.Alex"] autorelease];
[s startSpeakingString:@"Hello, world!"];
while ([s isSpeaking]) ;
[s startSpeakingString:@"con nitch were"];
while ([s isSpeaking]) ;
[s startSpeakingString:@"daw cat coo org, sole what program mar know tum yen know Colosseum death"];
while ([s isSpeaking]) ;
[pool release];
return 0;
}
|
重複する要素を取り除く
(Nested
Flatten)
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 | #import <Foundation/Foundation.h>
@interface NSArray (UniqueElements)
- (NSArray*)uniqueElements;
@end
@implementation NSArray (UniqueElements)
- (NSArray*)uniqueElements
{
NSMutableDictionary* dic = [NSMutableDictionary dictionary];
for (id obj in self) {
NSNumber* n = [dic objectForKey:obj];
if (n) {
[dic setObject:[NSNumber numberWithInteger:[n intValue] + 1] forKey:obj];
} else {
[dic setObject:[NSNumber numberWithInteger:1] forKey:obj];
}
}
NSMutableArray* result = [NSMutableArray array];
for (id obj in self) {
NSNumber* n = [dic objectForKey:obj];
if ([n intValue] == 1) [result addObject:obj];
}
return result;
}
@end
int main(int argc, char** argv)
{
NSAutoreleasePool* pool = [NSAutoreleasePool new];
NSArray* array = [NSArray arrayWithObjects:@"a", @"b", @"c", @"b", @"d", @"a", @"d", @"e", nil];
NSLog(@"%@", array);
NSLog(@"%@", [array uniqueElements]);
[pool release];
return 0;
}
|
九九の表示
(Nested
Flatten)
plain C ですが。
1 2 3 4 5 6 7 8 9 10 | #include <stdio.h>
int main(int argc, char** argv)
{
int i, j;
for (i=1; i<10; i++)
for (j=1; j<10; j++)
printf("%d * %d = %2d\n", i, j, i*j);
return 0;
}
|
XMLから情報を取り出す
(Nested
Flatten)
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | #import <Foundation/Foundation.h>
#import <Appkit/Appkit.h>
@interface AppController : NSObject
{
NSURLConnection* connection;
NSMutableData* buffer;
}
@end
@implementation AppController
- (void)dealloc
{
[connection release];
[buffer release];
[super dealloc];
}
- (void)startRequest
{
buffer = [NSMutableData data];
NSURL* url = [NSURL URLWithString:@"http://ja.doukaku.org/feeds/comments/"];
NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[req |




tilde #6380() [ Objective-C ] Rating0/0=0.00
開発環境: Mac OS X 10.5.2, Xcode 3.1 (Beta)
Rating0/0=0.00-0+
[ reply ]