39일차

Suhyeon Lee·2024년 11월 25일

CodeKata

SQL

120. Fix Names in a Table

  • 작성한 쿼리
SELECT
  user_id
  , CONCAT(UPPER(SUBSTRING(name,1,1)),LOWER(SUBSTRING(name,2))) AS name
FROM
  Users
ORDER BY
  user_id
;

→ MySQL에는 문자열의 첫 글자만 대문자로 쓸 수 있는 함수가 없음 🡆 함수 중첩을 사용

오라클은 INITCAP이 있다고 함

참고할 만한 다른 풀이

SELECT user_id, CONCAT(UPPER(LEFT(name,1)),LOWER(RIGHT(name,LENGTH(name)-1))) AS name
FROM Users
ORDER BY user_id ASC;

121. Patients With a Condition

  • 작성한 쿼리
SELECT
  *
FROM
  Patients
WHERE
  conditions LIKE 'DIAB1%'
  OR conditions LIKE '% DIAB1%'
;

→ 정규표현식으로도 쓸 수 있을 텐데 방법을 모르겠음

참고할 만한 다른 풀이: 정규표현식

SELECT *
FROM Patients
WHERE conditions REGEXP '(^|\\s)DIAB1[0-9]*';
select * from patients
where conditions regexp "[ ]DIAB1|^DIAB1"

🡆 또 다른 방식으로도 적을 수 있나 궁금해서 더 찾아보았음

SELECT * FROM patients WHERE conditions REGEXP '\\bDIAB1'

The expression conditions REGEXP \\bDIAB1 is actually the same as conditions LIKE '% DIAB1%' OR conditions LIKE 'DIAB1%';, but it is obviously shorter.
The reason they are the same is that \b matches either a non-word character (in our case, a space) or the position before the first character in the string.
Also, you need to escape a backslash with another backslash, like so: \\b.
Otherwise, the regular expression won't evaluate.
P.S. \b also matches the position after the last character, but it doesn't matter in the context of this problem.

→ 추가: 해당 쿼리는 현재 testcase는 통과 못한다고 함

🡆 \b가 a non-word character라 space 말고 다른 특수문자도 다 통과시킴

Python

50. 가장 가까운 같은 글자

  • 작성한 코드
def solution(s):
    answer = []
    s_dict = {}
    for idx, i in enumerate(s):
        if i not in s_dict:
            answer.append(-1)
        else:
            answer.append(idx-s_dict[i])
        s_dict[i] = idx
    return answer

참고할 만한 다른 풀이

def solution(s):
    answer = []
    dic = dict()
    for i in range(len(s)):
        if s[i] not in dic:
            answer.append(-1)
        else:
            answer.append(i - dic[s[i]])
        dic[s[i]] = i

    return answer
  • 딕셔너리에 없는 알파벳이 나오면
    1. answer 리스트에 -1 추가
    2. dic 딕셔너리에 {알파벳:현재 인덱스} 추가
  • 딕셔너리에 있는 알파벳이 나오면
    1. answer 리스트에 (현제 인덱스 - dic[알파벳]) 값 추가
    2. dic 딕셔너리 값을 현재 인덱스로 바꿔주기
  • 딕셔너리 안 쓰고 풀기
def solution(s):
    answer = []
    for i in range(len(s)):
        if s[i] in s[:i] and i>0 :
            temp = list(reversed(s[:i]))
            answer.append(temp.index(s[i])+1)
        else :
            answer.append(-1)
    return answer
  • Pythonic한 풀이
def solution(s):
    return [s[i::-1].find(s[i],1) for i in range(len(s))]

→ 한 줄로 쓸 수 있다니 정말 멋지다!

SDL

추가 공부

머신러닝 빌드업

2회차

회고

  • 내가 우수 TIL이라니…😱
    • 더욱 열심히 적어야겠다는 생각
  • Excel의 Python 시작
    • 같은 조에 있는 동현님이 알려주셨음!
    • 기존에 있던 visual basic이나 매크로를 대체하는 건가? 싶었는데 그건 아닌 것 같고 python을 이용해 데이터 분석하는 기능을 추가한 것 같음
  • 진짜 이번 주 아니면 복습할 시간이 없다! 최대한 많이 내용 복습히고 팀 프로젝트 준비해야겠다.
    • 동민님이 이야기하신 것처럼 팀 프로젝트용 EDA 가이드라인 같은 걸 짜 두는 게 좋을 것 같음
profile
2 B R 0 2 B

0개의 댓글