나는 이렇게 학습한다/Algorithm & SQL

1220. The 'if' function

daco2020 2022. 12. 20. 20:23
반응형

Create a function called _if which takes 3 arguments: a boolean value bool and 2 functions (which do not take any parameters): func1 and func2

When bool is truth-ish, func1 should be called, otherwise call the func2.

Example:

def truthy(): 
  print("True")

def falsey(): 
  print("False")

_if(True, truthy, falsey)
# prints 'True' to the console



Solution:

def _if(bool, func1, func2):
    return func1() if bool else func2()


반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

1222. Formatting decimal places #0  (0) 2022.12.23
1221. Name Shuffler  (0) 2022.12.21
1219. Return the day  (0) 2022.12.19
1218. Remove First and Last Character  (0) 2022.12.18
1217. Regexp Basics - is it a digit?  (0) 2022.12.17