파이썬에서는 함수를 일급 시민(first class citizen)으로 취급한다. 아래의 예시코드를 보면, 함수에서 또 다른 함수를 반환할 수 있으며, 함수에서 반환하는 lambda 함수의 경우 본래 heap에 저장이 되지만, 함수에서 반환된 lambda함수를 변수에 넣음으로써 data 영역에 저장된다.
1 2 3 4 5 6 7 8 9
defmake_difference(operator): if operator == '+': returnlambda x,y:x+y if operator == '-': returnlambda x,y:x-y
plus = make_difference('+') # lambda는 기본적으로 heap에 저장이 되지만, 변수로 다시 넣기 때문에 데이터의 영역으로 저장이 된다. print(plus(1,2)) #3
# Class Constructor # # Parameters: # firstName - A string denoting the Person's first name. # lastName - A string denoting the Person's last name. # id - An integer denoting the Person's ID number. # scores - An array of integers denoting the Person's test scores. # # Write your constructor here def__init__(self, _firstName, _lastName, _id, _scores): Person.__init__(self, _firstName, _lastName, _id) self.scores = _scores # Function Name: calculate # Return: A character denoting the grade. # # Write your function here defcalculate(self): average = sum(self.scores) / len(self.scores) if90 <= average <= 100: grade = "O" elif80 <= average < 90: grade = "E" elif70 <= average < 80: grade = "A" elif55 <= average < 70: grade = "P" elif40 <= average < 55: grade = "D" else: grade = "T" return grade
@classmethod defset_default(cls, amount): while amount < 1: print("Err: You should set default value over 1. Try again!") amount = int(input("Set default value: ")) cls.balance = amount print('Set default balance to {}'.format(amount)) #instance method defadd_to_account(self, amount): self.account += amount print('Your total balance is {}'.format(self.account + Wallet.balance))
# 2호선 내부순환선 (총 43개 역) 시청역 <->시청역 (내부순환) # 성수지선 (총 5개 역) 성수역 <-> 신설동 # 신정지선 (총 38개 역) 성수역 <->까치산 # class instance를 만들때 각 노선의 최대 정착역의 수를 구한다. classSubwayLine: def__init__(self, num_of_stn): # 지하철역의 수 만큼 [0]리스트를 만들어서 # 역별로 정차하고 있는 지하철의 수를 관리 self.num_of_stop_train_list = [0]*num_of_stn
# 해당 index위치의 count값을 1증가 시켜준다. defincreaseNumOfStopTrain(self, idx): if self.num_of_stop_train_list[idx] == 0: self.num_of_stop_train_list[idx] += 1 else: # 위험 알람 method를 호출해서 False값을 반환하도록 한다. self.setDangerAlert()
defdecreaseNumOfStopTrain(self, idx): if self.num_of_stop_train_list[idx] == 1: self.num_of_stop_train_list[idx] -= 1
# 해당 index위치에 정차되어있는 지하철의 수를 반환한다. defgetNumOfStopTrain(self, idx): return self.num_of_stop_train_list[idx] # 해당 역 위치에 지하철이 정차되어있는 경우, False값을 반환해서 출발할 수 없도록 한다.
오늘은 평소에 내가 관심이 많이 있었던 Clean code, TypeScript 등 개발과 관련해서 강사님이 수업도중에 여러 책들을 추천해주셨다.
Clean code는 이전부터 관심이 있던 내용이고, TypeScript는 최근에 공부를 시작해서 관심이 있던 내용이라 추천해주셨을때 너무 좋았다. 요즘 무슨 책을 읽어볼까 고민하던 참이었는데, 온라인 북으로 구매해서 아이패드에 넣고 통학시간이나 주말, 머리식힐때 읽어봐야겠다.
읽은 내용에서 유익한 내용이 있으면, 블로그에 포스팅해봐야겠다.
추천해주신 책은 아래와 같다. 일단 내가 최근에 공부를 시작한 TypeScript와 Clean code 책을 구매해서 읽어 볼 생각이다. 생각에서 끝나지 않기 위해서 내일 오전에 공부 시작할때 당장 구매해서 읽어봐야 겠다.
Code 코드
파이썬을 이용한 클린 코드를 위한 테스트 주도 개발
TypeScript Programming
Two Scoops of Django
Two Scoops of Django책에서 다루고 있는 장고의 버전이 낮기 때문에 google에서 Two Scoops of Django를 검색해서 two scoops-of-django-3.x github의 코드파일을 참고하는 것을 추천한다.
이 문제는 앞 뒤로는 자음만 위치하고, 가운데에는 2개 이상의 모음으로만 구성되어 있는 문자를 정규표현식을 통해 출력하는 문제이다.
Task You are given a string . It consists of alphanumeric characters, spaces and symbols(+, -). Your task is to find all the substrings of that contains or more vowels. Also, these substrings must lie in between consonants and should contain vowels only.
우선 문제를 풀이하기에 앞서 다음 긍정/부정예측(positive/negative lookahead assertion), 긍정 후 읽기에 대한 개념에 대해서 알아보자.