새로운 가장 빈번한 단어를 인쇄,이 문제를 해결하는 데 도움이 필요합니다 :는 목표 - C로 목표 - C 파일 (문자열)에
는 두 개의 매개 변수를받는 함수 쓰기 :
1 텍스트 문서를 나타내는 문자열 및
2 반환 할 항목 수를 제공하는 정수입니다. 가장 자주 발생하는 단어 인 단어 빈도 순으로 정렬 된 문자열 목록을 반환하도록 함수를 구현합니다. 단어가 어떻게 분리되는지를 판단하기 위해 최선의 판단을 사용하십시오. 솔루션은 O (n) 시간에 실행해야합니다. 여기서 n은 문서의 문자 수입니다. 프로덕션/상업용 시스템 에서처럼이 기능을 구현하십시오. 표준 데이터 구조를 사용할 수 있습니다. 내가 지금까지 (작업 진행 중) 시도 무엇
: 진행
// -(NSString *) wordFrequency:(int)itemsToReturn inDocument:(NSString *)textDocument ;
// Get the desktop directory (where the text document is)
NSURL *desktopDirectory = [[NSFileManager defaultManager] URLForDirectory:NSDesktopDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
// Create full path to the file
NSURL *fullPath = [desktopDirectory URLByAppendingPathComponent:@"document.txt"];
// Load the string
NSString *content = [NSString stringWithContentsOfURL:fullPath encoding:NSUTF8StringEncoding error:nil];
// Optional code for confirmation - Check that the file is here and print its content to the console
// NSLog(@" The string is:%@", content);
// Create an array with the words contain in the string
NSArray *myWords = [content componentsSeparatedByString:@" "];
// Optional code for confirmation - Print content of the array to the console
// NSLog(@"array: %@", myWords);
// Take an NSCountedSet of objects in an array and order those objects by their object count then returns a sorted array, sorted in descending order by the count of the objects.
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:myWords];
NSMutableArray *dictArray = [NSMutableArray array];
[countedSet enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
[dictArray addObject:@{@"word": obj,
@"count": @([countedSet countForObject:obj])}];
}];
NSLog(@"Words sorted by count: %@", [dictArray sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"count" ascending:NO]]]);
}
return 0;
}
* 귀하의 * 질문 *은 무엇입니까? 귀하의 알고리즘이 작동합니까? 그렇지 않은 경우 어떤 결과가 나타 납니까? - 귀하의 질문이 * 작업 코드 개선에 관한 것이라면 http://codereview.stackexchange.com에서 요청하는 것이 좋습니다. –
안녕하세요 마틴입니다. 객관적이고 새로운 목표를 달성 한 이후 누군가가이 코딩 과제를 해결할 수 있도록 도와 드리겠습니다. 나는 다른 토론에서 같은 질문에 대해 루비에서 예제를 코딩하는 것을 보았다. 그러나 좀 더 잘 이해하고 올바른 방법으로 나를 얻을 수있는 몇 가지 코드 예제를 objective-c에서 볼 수 있기를 바란다.그런 – user3534708
알고리즘 작업 I가 얻고 일 : 2014년 4월 15일 02 : 59 : 51.387 [6666 : 303]의 단어 수에 의해 분류 ( { 카운트 = 6] = 단어와; }, { 의 = 단어; = 단어; = A 단어; }, { 수 = 3 }, { 수 = 5 = 5를 계산하지만 난이 코드를 통합하는 방법을 잘 모릅니다 적절한 기능 – user3534708