Cuối cùng: cổng kho hàng
Bài học 47 trên 48 trong khóa học GDScript dành cho nhà phát triển C của Coddy.
Thử thách
DễChuyển bản tham chiếu C sang ngôn ngữ của bạn: nhận các từ đầu vào, trả về các cặp từ=số lượng được sắp xếp theo thứ tự bảng chữ cái và nối bằng dấu phẩy, hoặc none. Các từ dùng ASCII chữ thường, từ trùng lặp được đếm riêng. Không thay đổi dữ liệu đầu vào của bên gọi.
Đầu vào là một mảng JSON trên stdin. main bị khóa sẽ phân tích cú pháp mảng, gọi các API công khai và in result= theo sau là văn bản được trả về cùng ký tự xuống dòng. Không hàm trợ giúp nào được in ra. Các phép thử cố định độc lập xác minh hành vi API bắt buộc trước test case đầu vào; chúng không áp đặt cách triển khai.
Bản tham chiếu C đầy đủ. Lưu các tệp này cùng nhau và biên dịch main.c bằng trình biên dịch C17. Các header cung cấp API trợ giúp; main đọc cùng mảng JSON và in theo cùng giao thức kết quả. Không cần thư viện bên ngoài.
Danh sách tệp C inventory.h:
#ifndef INVENTORY_H
#define INVENTORY_H
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
static int word_compare(const void *a, const void *b) {
return strcmp(*(const char *const *)a, *(const char *const *)b);
}
/* Returns owned text. The input pointers and strings are never modified. */
static char *inventory_summarize(const char *const *words, size_t n) {
if (n == 0) {
char *out = malloc(5);
if (out) memcpy(out, "none", 5);
return out;
}
if (n > SIZE_MAX / sizeof(char *)) return NULL;
const char **sorted = malloc(n * sizeof *sorted);
if (!sorted) return NULL;
for (size_t i = 0; i < n; ++i) sorted[i] = words[i];
qsort(sorted, n, sizeof *sorted, word_compare);
size_t capacity = 1;
for (size_t i = 0; i < n;) {
size_t j = i + 1;
while (j < n && strcmp(sorted[i], sorted[j]) == 0) ++j;
int length = snprintf(NULL, 0, "%s=%zu", sorted[i], j-i);
if (length < 0 || (size_t)length + 1 > SIZE_MAX-capacity) {
free(sorted); return NULL;
}
capacity += (size_t)length + 1;
i = j;
}
char *out = malloc(capacity);
if (!out) { free(sorted); return NULL; }
size_t used = 0;
for (size_t i = 0; i < n;) {
size_t j = i + 1;
while (j < n && strcmp(sorted[i], sorted[j]) == 0) ++j;
if (used) out[used++] = ',';
int written = snprintf(out+used, capacity-used, "%s=%zu", sorted[i], j-i);
used += (size_t)written;
i = j;
}
free(sorted);
return out;
}
#endif
Tệp C main.c:
#include <ctype.h>
#include "inventory.h"
static char *read_input(void) {
size_t used=0, capacity=256;
char *text=malloc(capacity);
if (!text) return NULL;
int ch;
while ((ch=getchar()) != EOF) {
if (used+1 == capacity) {
if (capacity > SIZE_MAX/2) { free(text); return NULL; }
capacity *= 2;
char *next=realloc(text,capacity);
if (!next) { free(text); return NULL; }
text=next;
}
text[used++]=(char)ch;
}
text[used]=0;
return text;
}
static void space(const char **p) {
while (**p==' ' || **p=='\t' || **p=='\r' || **p=='\n') ++*p;
}
static int hex(char c) {
if (c>='0' && c<='9') return c-'0';
if (c>='a' && c<='f') return c-'a'+10;
if (c>='A' && c<='F') return c-'A'+10;
return -1;
}
/* The input contract is a JSON array of lowercase ASCII words. */
static char *word(const char **p) {
if (*(*p)++ != '"') return NULL;
char *out=malloc(strlen(*p)+1);
if (!out) return NULL;
size_t n=0;
while (**p && **p!='"') {
unsigned value=(unsigned char)*(*p)++;
if (value=='\\') {
if (*(*p)++ != 'u') { free(out); return NULL; }
value=0;
for (int k=0;k<4;++k) {
int h=hex(**p);
if (h<0) { free(out); return NULL; }
++*p; value=value*16+(unsigned)h;
}
}
if (value<'a' || value>'z') { free(out); return NULL; }
out[n++]=(char)value;
}
if (**p!='"') { free(out); return NULL; }
++*p; out[n]=0;
return out;
}
int main(void) {
char *text=read_input();
if (!text) return 1;
const char *p=text;
char **words=NULL; size_t count=0; int status=1;
space(&p);
if (*p++ != '[') goto done;
space(&p);
while (*p != ']') {
char *next=word(&p);
if (!next) goto done;
char **grown=realloc(words,(count+1)*sizeof *words);
if (!grown) { free(next); goto done; }
words=grown; words[count++]=next;
space(&p);
if (*p==']') break;
if (*p++ != ',') goto done;
space(&p);
}
++p; space(&p);
if (*p) goto done;
char *summary=inventory_summarize((const char *const *)words,count);
if (!summary) goto done;
printf("result=%s\n",summary);
free(summary); status=0;
done:
for (size_t i=0;i<count;++i) free(words[i]);
free(words); free(text);
return status;
}
Code của bạn đặt trong exercise.gd. main.gd chứa code đầu vào/đầu ra được cung cấp và không thể chỉnh sửa. Giữ mỗi kiểu class_name trong tệp có tên tương ứng.
Tự làm thử
extends Node
func contract_ok(data: Array) -> bool:
return true
func _ready() -> void:
var data: Array = JSON.parse_string(OS.read_string_from_stdin())
if not contract_ok(data):
print("API_CONTRACT_FAILED")
return
print("result=" + Exercise.solve(data))
Tất cả bài học trong GDScript dành cho nhà phát triển C
1Tệp script và giá trị vô hướng
Chạy tệp scriptVăn bản và chú thíchLiên kết và kiểu dữ liệuPhép toán số nguyênKết quả booleanÔn tập: Nhãn hàng2Luồng điều khiển và hàm
Rẽ nhánh và giá trịKhớp mẫuVòng lặp và phạm viHàm có kiểuĐối số mặc địnhÔn tập: Đếm lệnhTự luyện tập: Trình biên dịch GDScript online