본문 바로가기

Python345

Calculate average Description: Write a function which calculates the average of the numbers in a given list. Note: Empty arrays should return 0. Solution: 1. If 'numbers' is an empty array, 0 is returned. 2. If it is not empty, the average value is returned. import statistics def find_average(numbers): return statistics.mean(numbers) if numbers else 0 statistics.mean() is a Python built-in library that calculates.. 2022. 4. 7.
pre-commit 을 이용해 commit 전 코드 체크를 자동화하자. pre-commit 적용하기 해당 글은 깃헙 레포지토리에서도 확인가능합니다. 깃헙 링크 pre-commit란? 커밋 메시지를 작성하기 전에 호출이 되는 명령어입니다. 커밋이 되기 전 문법 오류나 스타일, 정렬, 타입 오류 등을 체크할 때 사용합니다. 개발자의 기호에 따라 선택하고 커스텀까지 할 수 있습니다. pre-commit 적용 순서 1. git init 2. pip install pre-commit 3. pre-commit install >>> pre-commit installed at .git/hooks/pre-commit 4. pre-commit run >>> An error has occurred: InvalidConfigError: =====> .pre-commit-config.yaml is.. 2022. 4. 6.
Hide Kata Description Description: You get an array of arrays. If you sort the arrays by their length, you will see, that their length-values are consecutive. But one array is missing! You have to write a method, that return the length of the missing array. Example: [[1, 2], [4, 5, 1, 1], [1], [5, 6, 7, 8, 9]] --> 3 If the array of arrays is null/nil or empty, the method should return 0. When an array in the array is.. 2022. 4. 5.
How Green Is My Valley? Description: Input : an array of integers. Output : this array, but sorted in such a way that there are two wings: the left wing with numbers decreasing, the right wing with numbers increasing. the two wings have the same length. If the length of the array is odd the wings are around the bottom, if the length is even the bottom is considered to be part of the right wing. each integer l of the le.. 2022. 4. 4.
Regexp Basics - is it a letter? Description: Complete the code which should return true if the given object is a single ASCII letter (lower or upper case), false otherwise. Solution: 1. Get the ASCII code of the argument. 2. Check whether the obtained ASCII code corresponds to the alphabet. 3. True if true, false otherwise. def is_letter(s): ascii_num = len(s) == 1 and ord(s) return (ascii_num >= 65 and ascii_num = 97 and asci.. 2022. 4. 3.
Python _ @property로 getter, setter 구현하기 (feat. 캡슐화) 캡슐화 파이썬은 클래스를 작성할 때, 변수나 함수 앞에 '__' 를 붙여 캡슐화, 즉 은닉을 할 수 있습니다. 예를 들어 객체 내 변수에 접근할 때, 일반적으로 다음처럼 '.'과 변수명만으로 쉽게 접근할 수 있습니다. class Robot: def __init__(self, name, num): self.name = name self.num = num robot = Robot('다코', '0001') print(robot.name, robot.num) ''' 결과 >>> 다코 0001 ''' 하지만 이것은 외부에서 변수를 쉽게 조작할 수 있음을 의미합니다. 이를 방지하기 위해 변수앞에 '__'를 붙여 외부에서 접근할 수 없도록 막을 수 있습니다. class Robot: def __init__(self, .. 2022. 4. 2.