Scrabble
문제 링크
https://cs50.harvard.edu/x/2024/psets/2/scrabble/
의사코드 작성
/*
1. 플레이어1 플레이어2 문자열 받기
2. 각각 점수 계산하기
3. 점수 비교후 알맞은 내용 출력하기
*/
실제 코드 작성
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int scoretable[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
int get_score(string answer);
int main(void)
{
string player1answer = get_string("Player 1: ");
string player2answer = get_string("Player 2: ");
int player1score = get_score(player1answer);
int player2score = get_score(player2answer);
if (player1score > player2score)
{
printf("Player 1 Wins!\n");
}
else if (player1score < player2score)
{
printf("Player 2 Wins!\n");
}
else
{
printf("Tie!\n");
}
}
int get_score(string answer)
{
int score = 0;
for (int i = 0, n = strlen(answer); i < n; i++)
{
answer[i] = toupper(answer[i]);
int scoreindex = (int) answer[i] - (int) 'A';
if (scoreindex >= 0 && scoreindex < 26)
{
score = score + scoretable[scoreindex];
}
}
return score;
}
후기
scoretable 설정할 때 어떻게 하면 효율적으로 설정할까, 점수간의 규칙이 있을까
이런 생각에 시간을 꽤 보낸 것 같다.
한번에 해결할 생각은 오히려 문제 해결 시간을 늘리는 것 같다.
최대한 단순한 방법에서 점점 방법을 발전시켜나가야 할 것 같다.
Readability
문제 링크
https://cs50.harvard.edu/x/2024/psets/2/readability/
의사 코드 작성
/*
1. 사용자에게 검사할 텍스트 받기
2. 문자, 단어, 문장 수 계산하기
3. 알고리즘 따라 점수 부여하기
4. 점수에 따라 등급 출력하기
*/
실제 코드 작성
#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int compute_score(float L, float S);
int main(void)
{
string text = get_string("Text: ");
int letters = 0;
int words = 1;
int sentences = 0;
for (int i = 0, n = strlen(text); i < n; i++)
{
if (islower(text[i]) || isupper(text[i]))
{
letters += 1;
}
else if (isblank(text[i]))
{
words += 1;
}
else if (text[i] == '!' || text[i] == '.' || text[i] == '?')
{
sentences += 1;
}
}
float L = letters / (float) words * 100;
float S = sentences / (float) words * 100;
int score = compute_score(L, S);
if (score < 1)
{
printf("Before Grade 1\n");
}
else if (score >= 1 && score < 16)
{
printf("Grade %d\n", score);
}
else
{
printf("Grade 16+\n");
}
}
int compute_score(float L, float S)
{
float score = 0.0588 * L - 0.296 * S - 15.8;
score += 0.5;
return floor(score);
}
후기
문자, 단어, 문장을 어떻게 구분할 지가 핵심이였던 문제였다.
사실 문제 자체는 어렵지 않았으나 debugging을 printf 로 안하고
주어진 디버깅 툴로 하려니 익숙하지 않고 어려운 것 같다.
하지만 디버깅은 습관이다. 익숙해지도록 연습해야겠다.
Substitution
문제 링크
https://cs50.harvard.edu/x/2024/psets/2/substitution/
의사 코드 작성
/*
1. Command Line 치환 문자 받기
2. 치환 문자가 26자인지 확인하기
3. 치환 문자의 중복이 있는지 확인하기
4. 바꿀 문자열 받기
5. 치환 문자를 기준으로 바꿀 문자열 바꾸기
6. 바뀐 문자열 출력
*/
실제 코드 작성
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./substitution key\n");
return 1;
}
int length = strlen(argv[1]);
if (length != 26)
{
printf("Key must contain 26 characters.\n");
return 1;
}
for (int i = 0; i < 26; i++)
{
if (isalpha(argv[1][i]))
{
argv[1][i] = toupper(argv[1][i]);
for (int j = 0; j < i; j++)
{
if (argv[1][j] == argv[1][i])
{
printf("Usage: ./substitution key\n");
return 1;
}
}
}
else
{
printf("Usage: ./substitution key\n");
return 1;
}
}
string plain = get_string("plaintext: ");
for (int i = 0, n = strlen(plain); i < n; i++)
{
if (isupper(plain[i]))
{
int index = plain[i] - 'A';
plain[i] = toupper(argv[1][index]);
}
if (islower(plain[i]))
{
int index = plain[i] - 'a';
plain[i] = tolower(argv[1][index]);
}
}
printf("ciphertext: %s\n", plain);
}
후기
문제에서 제작자가 오류처리와 예외사항 관리를 맛보게 하고 싶다는 느낌이 들었다.
문제 자체는 쉬웠지만 좋은 오류처리에 대해 고민해볼만 한 것 같다.
'입문 > CS50X' 카테고리의 다른 글
[CS50X] 2024 Week3 Algorithms Problem Set 3 (Sort, Plurality, Tideman) (0) | 2024.02.23 |
---|---|
[CS50X] 2024 Week3 Algorithms (1) | 2024.02.23 |
[CS50X] 2024 Week2 Arrays (0) | 2024.02.22 |
[CS50X] 2024 Week1 C Problem Set 1 (Mario, Credit) (0) | 2024.02.21 |
[CS50X] 2024 Week1 C (1) | 2024.02.21 |