"""_summary_
https://www.acmicpc.net/problem/1001
문제
두 정수 A와 B를 입력받은 다음, A-B를 출력하는 프로그램을 작성하시오.
입력
첫째 줄에 A와 B가 주어진다. (0 < A, B < 10)
출력
첫째 줄에 A-B를 출력한다.
예제 입력 1
3 2
예제 출력 1
1
"""
# A, B = input("값을 입력하세요~ (ex; 3 2 ): ").split()
A, B = input().split()
print(int(A)-int(B))
"""_summary_
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
https://www.acmicpc.net/problem/1000
입력
첫째 줄에 A와 B가 주어진다. (0 < A, B < 10)
출력
첫째 줄에 A+B를 출력한다.
예제 입력 1
1 2
예제 출력 1
3
입력을 받을 때 A와 B는 같은 줄에 입력을 받아야하기 때문에
공백을 기준으로 나눠주는 split 함수를 이용하도록 하겠습니다.
받아온 a와 b는 문자형이기 때문에 int() 를 이용하여 정수로 바꾸어 더해주었습니다.
"""
# 아래 처럼 쓸데없이 정보를 많이 넣으면 틀렸습니다. 로 나오니 주의하자.
# A, B = input("값을 입력하세요~ (ex; 1 2 ): ").split()
A, B = input().split()
print(int(A)+int(B))
a, b = map(int, input().split())
print(a+b)
"""_summary_
타일 채우기
https://www.acmicpc.net/problem/2133
문제 3×N 크기의 벽을 2×1, 1×2 크기의 타일로 채우는 경우의 수를 구해보자.
입력 첫째 줄에 N(1 ≤ N ≤ 30)이 주어진다.
"""
n = int(input("값을 입력하세요~ : "))
tile = [0 for _ in range(31)]
tile[2] = 3
for i in range(4, n+1):
if i%2 == 0:
tile[i] = tile[i-2] * 3 + sum(tile[:i-2]) * 2 + 2
else:
tile[i] = 0
print(tile[n])
#2번째 풀이
n = int(input())
dp = [0]*(n+1)
if n % 2 != 0:
print(0)
else:
dp[2] = 3
for i in range(4, n+1, 2):
dp[i] = dp[i-2] * 3 + 2
for j in range(2, i-2, 2):
dp[i] += dp[j] * 2
print(dp[n])
### 데이터 과학자를 위한 6가지 Python 팁
### Top 6 Python Tips for Data Scientists
### https://towardsdatascience.com/top-6-python-tips-for-data-scientists-4f4a25e44d15
codeString = '''a,b = 4,5; print(f"a = {a} and b = {b}"); print(f"a+b = {a+b}")'''
exec(codeString)
print("\n\n\n")
import os
import sys
#작업하는 경로(위치)가 어디인지 확인
print(os.getcwd())
exec(open("./Project/Datascientist/myFullFileName.py").read())
print("\n\n\n")
### 각각 현재 작업 디렉토리 또는 사용자 지정 디렉토리의 모든 파일을 나열합니다.
print( os.listdir() )
"""_summary_
"""
print("\n\n\n")
### 4. Code timer as a decorator
import time
import requests
def timerWrapper(func):
"""Code the timer"""
def timer(*args, **kwargs):
"""Start timer"""
start = time.perf_counter()
output = func(*args, **kwargs)
timeElapsed = time.perf_counter() - start
print(f"Current function: {func.__name__}\n Run Time: {timeElapsed}")
return output
return timer
## Func to make a request to an user-defined url
@timerWrapper
def getArtile(url):
return requests.get(url, allow_redirects=True)
## Monitor the runTime
if __name__ == "__main__":
getArtile('https://towardsdatascience.com/6-sql-tricks-every-data-scientist-should-know-f84be499aea5')
print("\n\n\n")
## 이제 다른 함수의 시간을 측정 @timeWrapper하려면 함수 앞에 the를 놓는 것뿐입니다.
@timerWrapper
def getMultiplication(num):
for val in range(num):
print(10**(10**val))
getMultiplication(3)
Scenario: You inherited a Python project from a colleague, and immediately noticed that those scripts all have a whopping 5000+ lines of code. The same chunks (of code) got copied and pasted multiple times! So, is there a more efficient option to go about code reusability?
Let’s explore theexec()function in Python. Simply put, it takes in a string or object code, and execute it as shown in this example,
a = 4 and b = 5
a+b = 9
Even more handy? We can useexec(open().read())to call and execute a file within the Python interpreter. For example,
With this powerful one-liner, data scientists can save programs that will be reused as standalone files, and execute them whenever needed within the main program. No code copying and pasting any more!
Being a cool functionality in Python,exec()has one pitfall to avoid — it does NOT return any value,
a = 4 and b = 5
a+b = 9
** Is the return from exec() is None? True **
As we can see, the output of theexec()function isNone; hence, it cannot be used to store any values, which is equivalent to thesounce()function in R.
2. Operating system commands with {os} and {shutil}
Scenario: continue from our previous tip, now you want to check out the script before executing it. Don’t bother to double-click your mouse all the way through to open up the file? No problem, you can easily achieve this in Python directly without interrupting your train of thought.
Here, theos.startfile()function allows users to open up any type of files, including MS documents, Excels, R and SQL scripts.
Similarly, we can also delete a FILE usingos.remove(“myFullFileName.ANYFORMAT”)
or delete the entire DIRECTORY usingshutil.rmtree(“folderToBeRemoved”). where {shutil} is a Python module that offers a number of high-level file operations, particularly for file copying and removal.
Therefore, if you haven’t used {os} other thanos.getcwd()oros.chdir()or if the {shutil} sounds unfamiliar, it’s time to check out their documents. You will definitely find useful commands or file system methods that make your coding easier. Here lists a few of my favorites,
os.listdir()oros.listdir(“someDirectory”)— list all files in the currently working directory or any user-specified directory, respectively;
os.path.join()— automatically create a path with elements in the arguments for later use, e.g.,os.path.join(‘D’, ‘Medium’, ‘New Folder’)will return
‘D\\Medium\\New Folder’
os.makedirs()— create a directory;
shutil.copy2(“sourcePath”, “destinationPath”)orshutil.move(“sourcePath”, “destinationPath”)— copy or cut a file respectively.
3. One-liner: Nested list comprehension to get rid of the for loops
Scenario: this “simple” task we come across is to combine several lists into one big long list,
Surely, we can write five nestedforloops to append each sublist to the final output list. But it’s smarter to turn to nested list comprehension for the most concise way,
Scenario: while Python is recognized as one of the most effective programming languages, data scientists still need to check the runtime of our programs.
It’s not the hardest thing if we just implement a bare-bones Python timer for each function we want to monitor. However, if we code it as a decorator, we would make our timer much easier to be version-controlled and reused!
Here is how,
In this snippet,
the timer is wrapped in atimerWrapperfunction, which then is used as a decorator called prior to the main function;
The example main function is to return a request connecting to an URL, which isone of my previous blogs.
Running this code gives us the time elapsed,
Current function: getArtile
Run Time: 1.6542516000008618Out[101]: <Response [200]>
Now, to time another function, all we need is to put the@timeWrapperin front of the function,
getMultiplication(3)
10
10000000000
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Current function: getMultiplication
Run Time: 0.00014700000065204222
5. Leverage theoptionssystem to customize your display
Scenario: as data scientists, we analyze data with {pandas} and {numpy} on a daily basis. When I first learned coding in Python, I was frustrated seeing this after reading my data into the IDE,
Clearly, data display is cut off both row-wise and column-wise, and the following code can fix it,
Here, we are explicitly setting the maximum columns, rows and column width to display/print in the console. There are numerous customizableoptions and settingsin {pandas}, and similar operations are also available in{numpy}for arrays and matrix,
6. Reproduce your machine learning model results? Set seeds!
Scenario: Due to the stochastic nature of machine learning modeling process, we have all encountered the non-deterministic aspect of machine learning. This randomness results in our difficulty reproducing the same results across different runs. Consequently, it’s challenging to figure out whether an improvement in performance metrics is a result of successful model tuning or simply a different random training/testing sample.
Luckily the reproducibility can be achieved bysetting the random seedthroughout your model pipeline, provided that you do it correctly! How many times have you seen questions like “Getting a different result despite random seed defined” popping up on Stack Overflow? Well, how to appropriately set seeds should be in the first page of documentations, but it took me some time to dig it out.
I found that NOT every seed is defined the same in {numpy}, {sklearn}, {tensorflow} or {torch}. Thus, it’s a best practice to use a definitive function that sets all SEEDS for all your frameworks. For example,
Adding this tacticalreset_random_seed()function to all necessary steps of your workflow, such as train-test split, model compile/training, and interpretation, will get you half way to full reproducibility. More detailed visibility into your experiments will finish the second half!