Skip to content
Minsing Log
Go back

English Edition

Regular Expressions: re

Programming & CS

Motivation While looking through excellent Programmers solutions, I found code that represented the Dart game using regular expressions, which sparked a question during my review.

import re


def solution(dartResult):
    bonus = {'S' : 1, 'D' : 2, 'T' : 3}
    option = {'' : 1, '*' : 2, '#' : -1}
    p = re.compile('(\d+)([SDT])([*#]?)')
    dart = p.findall(dartResult)
    for i in range(len(dart)):
        if dart[i][2] == '*' and i > 0:
            dart[i-1] *= 2
        dart[i] = int(dart[i][0]) ** bonus[dart[i][1]] * option[dart[i][2]]

    answer = sum(dart)
    return answer

What Are Regular Expressions?

-> Using regular expressions makes it easy to extract the desired data from large amounts of text files, and allows you to check whether the data conforms to the required format


How to Use Them

  1. re module import
  2. Compile according to regular expression syntax rules and search with findall findall -> https://velog.io/@beanlove97/%ED%8C%8C%EC%9D%B4%EC%8D%AC-findall-%EB%A9%94%EC%84%9C%EB%93%9C

Summary

References

  1. See the regular expression summary (shaun.log) https://velog.io/@wnsqud70/%EC%A0%95%EA%B7%9C%EC%8B%9D
  2. Code https://school.programmers.co.kr/learn/courses/30/lessons/17682/solution_groups?language=python3
  3. How to use regular expressions https://whatisthenext.tistory.com/116
  4. Vegeta meme https://www.youtube.com/watch?v=nwhmV-KO1pw

공개 여정 구독

글이 올라오면 이메일로 보내드릴게요.

개발, 창업, 배움, 실패와 회고를 짧고 솔직하게 기록합니다. 검색으로 스쳐가는 독자가 아니라 여정을 따라오는 팬을 만들기 위한 뉴스레터입니다.

뉴스레터는 준비 중입니다. 그동안 RSS 로 구독하거나 이메일 로 알림을 요청해 주세요.


Share this post:

Comments

Sign in with GitHub to leave a comment.


Previous Post
What is enumerate()?
Next Post
Python Class Terminology Summary