동글c
동글한 스터디룸
동글c
전체 방문자
오늘
어제
  • 분류 전체보기 (14)
    • 입문 (14)
      • CS50X (14)
      • 컴퓨터과학이 여는 세계 (0)
    • 컴퓨터 구조 (0)
      • CSAPP (0)
    • 프로그래밍 (0)
      • SICP (0)
    • 회고 (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • bubblesort
  • overflow
  • 반복문
  • 조건문
  • MergeSort
  • Harvard CS50
  • tree
  • compiling
  • tiedman
  • types
  • CS50
  • recover
  • int
  • Datastructure
  • Strings
  • problemset2
  • 포인터
  • Hashtable
  • Volume
  • DICTIONARY
  • scratch
  • speller
  • selectionsort
  • computerscience
  • Trie
  • loops
  • LinkedList
  • hash
  • command-line arguments
  • pointers

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
동글c

동글한 스터디룸

입문/CS50X

[CS50X] 2024 Week2 Arrays Problem Set 2 (Scrabble, Readability, Substitution)

2024. 2. 22. 14:43

 

Scrabble

문제 링크

https://cs50.harvard.edu/x/2024/psets/2/scrabble/

 

Scrabble - CS50x 2024

Harvard University's introduction to the intellectual enterprises of computer science and the art of programming.

cs50.harvard.edu

 

 

의사코드 작성

/*
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/

 

Readability - CS50x 2024

Harvard University's introduction to the intellectual enterprises of computer science and the art of programming.

cs50.harvard.edu

 

 

 

의사 코드 작성

/*
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/

 

Substitution - CS50x 2024

Harvard University's introduction to the intellectual enterprises of computer science and the art of programming.

cs50.harvard.edu

 

 

의사 코드 작성

/*
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
    '입문/CS50X' 카테고리의 다른 글
    • [CS50X] 2024 Week3 Algorithms Problem Set 3 (Sort, Plurality, Tideman)
    • [CS50X] 2024 Week3 Algorithms
    • [CS50X] 2024 Week2 Arrays
    • [CS50X] 2024 Week1 C Problem Set 1 (Mario, Credit)
    동글c
    동글c
    깃허브 : https://github.com/asena1006

    티스토리툴바