본문 바로가기

Python345

1022. Area of a Square Complete the function that calculates the area of the red square, when the length of the circular arc A is given as the input. Return the result rounded to two decimals. Note: use the π value provided in your language (Math::PI, M_PI, math.pi, etc) Solution: def square_area(A): import math return round((A/math.pi*4/2)**2, 2) 2022. 10. 22.
1021. USD => CNY Create a function that converts US dollars (USD) to Chinese Yuan (CNY) . The input is the amount of USD as an integer, and the output should be a string that states the amount of Yuan followed by 'Chinese Yuan' Examples (Input -> Output) 15 -> '101.25 Chinese Yuan' 465 -> '3138.75 Chinese Yuan' The conversion rate you should use is 6.75 CNY for every 1 USD. All numbers should be represented as a.. 2022. 10. 21.
1020. Convert a string to an array Write a function to split a string and convert it into an array of words. Examples (Input ==> Output): "Robin Singh" ==> ["Robin", "Singh"] "I love arrays they are my favorite" ==> ["I", "love", "arrays", "they", "are", "my", "favorite"] Solution: from typing import List def string_to_array(s: str) -> List[str]: return split(s) def split(s: str, c: str = " ") -> List[str]: list, ci = [], 0 for i.. 2022. 10. 20.
1018. Find the smallest integer in the array Given an array of integers your solution should find the smallest integer. For example: Given [34, 15, 88, 2] your solution will return 2 Given [34, -345, -1, 100] your solution will return -345 You can assume, for the purpose of this kata, that the supplied array will not be empty. Solution: # 1 def find_smallest_int(arr): return sorted(arr, reverse=True).pop() # 2 def find_smallest_int(arr): r.. 2022. 10. 18.
requests _ response 상태에 따라 raise를 할 수 있다? 외부 API로부터 받은 응답 값이 정상이 아니라면 우리는 응답이 잘못되었음을 어떻게 확인할 수 있을까? 매번 요청 때마다 응답의 상태 코드를 일일이 체크하고 핸들링해야 할까? python의 requests 라이브러리에서는 이러한 번거로움을 줄이기 위해 raise_for_status() 메서드가 있다. raise_for_status()는 응답의 상태 코드가 400 이상인 경우 HTTPError를 raise 한다. raise 를 통해 외부 모듈과의 통신상태를 확인하고 쉽게 핸들링을 할 수 있다. 사용법은 간단하다. requests 요청으로 응답받은 res 객체로부터 raise_for_status() 메서드를 호출하면 된다. url = "" res = requests.post(url) res.raise_for.. 2022. 10. 18.
1017. Enumerable Magic - Does My List Include This? Create a method that accepts a list and an item, and returns true if the item belongs to the list, otherwise false. Solution: from typing import List def include(arr: List[int], item: int) -> bool: return bool(arr.count(item)) 2022. 10. 17.