32일차

Suhyeon Lee·2024년 11월 14일

CodeKata

SQL

100. User Activity for the Past 30 Days I

  • 작성한 쿼리
SELECT
  activity_date AS day
  , COUNT(DISTINCT user_id) AS active_users
FROM
  activity
WHERE
  activity_date BETWEEN '2019-06-28' AND '2019-07-27'
GROUP BY
  activity_date
;

참고할 만한 다른 풀이

1-1.

SELECT activity_date AS day, COUNT(DISTINCT user_id) AS active_users
FROM Activity
WHERE (activity_date > "2019-06-27" AND activity_date <= "2019-07-27")
GROUP BY activity_date;

1-2.

SELECT activity_date AS day, count(distinct user_id) AS active_users
FROM Activity
WHERE datediff('2019-07-27', activity_date) >= 0
  AND datediff('2019-07-27', activity_date) < 30
GROUP BY activity_date;
  • The WHERE clause in your SQL query filters the data to include only records where the activity_date falls within a specific 30-day range before July 27, 2019.

    • datediff('2019-07-27', activity_date) >= 0:
      This ensures that the activity_date is on or before '2019-07-27'. The datediff function calculates the difference in days between '2019-07-27' and activity_date. If the result is >= 0, it means the activity occurred on or before July 27, 2019.

    • datediff('2019-07-27', activity_date) < 30:
      This limits the selection to activity dates within the last 30 days prior to July 27, 2019. It ensures that only records with activity within that time frame are included.

Together, these conditions create a date filter from June 27, 2019, to July 27, 2019.

select
  activity_date as day
  ,count(distinct user_id) AS active_users 
from
  Activity 
where
  activity_date > date_sub("2019-07-27",INTERVAL 30 DAY) 
  and activity_date <="2019-07-27" 
group by
  activity_date

Python

39. 최대공약수와 최소공배수

  • 작성한 코드
def solution(n, m):
    answer = []
    multiple_n = n*m
    while(m>0):
        print(n)
        print(m)
        n, m = m, n % m
    gcd = n
    answer.append(gcd)
    answer.append(multiple_n//gcd)
    return answer

→ 좀 짧게 써보고 싶어져서 아래처럼도 해 보았음

import math
def solution(n,m):
    return [math.gcd(n,m), (n*m)//(math.gcd(n,m))]

참고할 만한 다른 풀이

def solution(a, b):
    c,d = max(a, b), min(a, b)
    t = 1
    while t>0:
        t = c%d
        c, d = d, t
    answer = [ c, int (a*b/c)]
    return answer

→ Euclidean algorithm(유클리드 호제법) 코드
→ 추가: while d: c, d = d, c % d로 하면 t 안 쓸 수 있다고 함

def solution(n, m):
    gcd = lambda a,b : b if not a%b else gcd(b, a%b)
    lcm = lambda a,b : a*b//gcd(a,b)
    return [gcd(n, m), lcm(n, m)]
  1. for문으로 직접 찾기
def gcdlcm(a, b):
    for i in range(a):
        if a%(a-i)+b%(a-i) == 0:
            return [a-i, a*b/(a-i)]
def solution(n, m):
    nlist = []
    mlist = []
    for i in range(1, n+1):
        if n%i == 0:
            nlist.append(i)
    for i in range(1, m+1):
        if m%i == 0:
            mlist.append(i)
    maxi=[]
    for i in nlist:
        if i in mlist:
            maxi.append(i)
    maximum = max(maxi)

    minimum = int(maximum * (n/maximum) * (m/maximum))

    answer = [maximum, minimum]
    return answer
  1. def 안에 def
def solution(a, b):
    def _gcd(x,y):
        if y==0: return x
        return _gcd(y,x%y)
    g = _gcd(a,b)
    return [ g, a*b//g ]
def gcdlcm(a, b):
    def gcd(a, b):
        if a < b:
            (a, b) = (b, a)
        while b != 0:
            (a ,b) = (b, a % b)
        return a

    return [gcd(a, b), a * b // gcd(a, b)]
def solution(a, b):
    def gcd(a, b):
        return b if a % b == 0 else gcd(b, a % b)

    def lcm(a, b):
        return int(a * b / gcd(a, b))


    def gcdlcm(a, b):
        answer = [gcd(a,b), lcm(a,b)]
        return answer
    
    return gcdlcm(a,b)

SDL

추가로 공부한 내용

라이브 세션

통계야 놀자 2회차

아티클 스터디

A/B 테스트 제대로 이해하기: ③ A/B 테스트 계산기의 세팅과 해석

파이썬 심화반 과제

웹 크롤링 과제

회고

  • 팀원분께서 '시티즌 데이터 사이언티스트'에 대해 소개해 주셨음!
profile
2 B R 0 2 B

0개의 댓글