본문 바로가기

Python345

0928. Grasshopper - Terminal game combat function Create a combat function that takes the player's current health and the amount of damage recieved, and returns the player's new health. Health can't be less than 0. Solution: def combat(health, damage): return max(health-damage, 0) 2022. 9. 28.
0926. Merge two sorted arrays into one You are given two sorted arrays that both only contain integers. Your task is to find a way to merge them into a single one, sorted in asc order. Complete the function mergeArrays(arr1, arr2), where arr1 and arr2 are the original sorted arrays. You don't need to worry about validation, since arr1 and arr2 must be arrays with 0 or more Integers. If both arr1 and arr2 are empty, then just return a.. 2022. 9. 26.
0925. Thinkful - Logic Drills: Traffic light You're writing code to control your town's traffic lights. You need a function to handle each change from green, to yellow, to red, and then to green again. Complete the function that takes a string as an argument representing the current state of the light and returns a string representing the state the light should change to. For example, when the input is green, output should be yellow. Solut.. 2022. 9. 25.
python _ traceback 에러 메시지 핸들링하기 traceback 이란? 파이썬으로 개발을 하다보면 traceback 이란 단어를 많이 접하게 된다. traceback은 역 추적 이란 뜻으로 해당 에러가 어디서부터 발생했는지 우리에게 알려준다. 예를들어 다음처럼 빈 dictionary에 “값 내놔” 라는 key를 호출하면, 해당 key가 없기 때문에 KeyError가 발생한다. dict = {} dict["값 내놔"] Traceback (most recent call last): File "example.py", line 2, in dict["값 내놔"] KeyError: '값 내놔' 위의 메시지로 보자면 KeyError가 line 2에서 발생했음을 알 수 있다. 이처럼 에러 메시지는 디버깅을 할 때 매우 유용하다. 하지만 에러가 언제 발생할지 우리는.. 2022. 9. 24.
0924. Sum of positive You get an array of numbers, return the sum of all of the positives ones. Example [1,-4,7,12] => 1 + 7 + 12 = 20 Note: if there is nothing to sum, the sum is default to 0. Solution: def positive_sum(arr): return sum(map(remove_negative, arr)) def remove_negative(i: int): return i if i >= 0 else 0 2022. 9. 24.
0923. Sum of differences in array Your task is to sum the differences between consecutive pairs in the array in descending order. Example [2, 1, 10] --> 9 In descending order: [10, 2, 1] Sum: (10 - 2) + (2 - 1) = 8 + 1 = 9 If the array is empty or the array has only one element the result should be 0 (Nothing in Haskell, None in Rust). Solution: def sort(func): def wrapper(arr): return func(sorted(arr, reverse=True)) return wrap.. 2022. 9. 23.