본문 바로가기
728x90

python example11

[Python] 미국 회사 코딩 테스트 예제: Solve the Two-Sum Problem Using a Hash Map 포스트 난이도: HOO_Middle# Problem StatementGiven an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.Example: Input: nums = [2, 7, 11, 15], target = 9 Output: [0, 1]Explanation: Because nums[0.. 2025. 1. 27.
[Python Examples] pd.DataFrame(): Section별 학생 구분하여 출력하기 pd.DataFrame(): Section별 학생 구분하여 출력하기 포스트 난이도: HOO_Intern # Example Codes import pandas as pd df = pd.DataFrame({"section": [3,1,1,2,2,3], "students": ['James', 'Julia', 'Megan', 'Henry', 'Minji', 'Yelin']}) sec_1 = df[df['section'] == 1] sec_2 = df[df['section'] == 2] sec_3 = df[df['section'] == 3] print(sec_1) print(sec_2) print(sec_3) section students 1 1 Julia 2 1 Megan section students 3 2 .. 2022. 10. 28.
[Python Examples] max()를 사용하지 않고 if문으로 최댓값 구하기 max()를 사용하지 않고 if문으로 최댓값 구하기 포스트 난이도: HOO_Intern # Example Codes x = 5.678 y = 5.988 z = 5.123 max_num = input("please choose one number x, y, z: ") if max_num == 'x': max_num = x if max_num < y: max_num = y if max_num < z: max_num = z elif max_num < z: max_num = z if max_num < y: max_num = y else: max_num = x elif max_num == 'y': max_num = y if max_num < x: max_num = x if max_num < z: max_num =.. 2022. 10. 19.
[Python Examples] 파이썬 if문 예제: Sphere, Cylinder 계산하기 파이썬 if문 예제: Sphere, Cylinder 계산하기 포스트 난이도: HOO_Intern # Example Code val = int(input("Please Enter the Option: 1 is Cylinder and 2 is Sphere: ")) if val == 1: cr = float(input("Enter a radius: ")) ch = float(input("Enter a height: ")) cylinderCal = 3.14*cr*cr*ch print("Volume of a Cylinder: ", cylinderCal) elif val == 2: sr = float(input("Enter a radius: ")) sphereCal = 4*(3.14*sr*sr*sr/3) print.. 2022. 10. 10.
[Python Examples] 파이썬 리스트 예제: 특정 원소 또는 값 찾기 파이썬 리스트 예제: 특정 원소 또는 값 찾기 포스트 난이도: HOO_Intern # Example Code def find_HOO(x): if "HOO" in x: print("HOO is here!") else: print("HOO is not here.") list1 = [15.6,"Wally",54,"Osvaldo"] list2 = ["Wald","HOO",6] list3 = [1,2,3,4,5,6,7,8,"HOO",10,11] print("List1: ") find_HOO(list1) print("List2: ") find_HOO(list2) print("List3: ") find_HOO(list3) List1: HOO is not here. List2: HOO is here! List3: HO.. 2022. 10. 7.
[Python Examples] 파이썬 파일 찾기: pathlib, path() 파이썬 파일 찾기: pathlib, path() 포스트 난이도: HOO_Intern # Example Code 이번 포스트에서는 파이썬에서 현재 위치의 파일들을 검색하고 찾는 방법에 대해서 살펴보도록 하자. 파일을 검색하기 위해서는 pathlib이라는 라이브러리를 import 해야 한다. 다음으로 path()를 통해서 현재 디렉토리에 있는 파일들을 검색하여 확인이 가능하다. import pathlib currentDirectory = pathlib.Path('.') for currentFile in currentDirectory.iterdir(): print(currentFile) .config HOOAI.txt sample_data 위의 예제 코드를 입력하면 현재 존재하는 파일을 확인할 수 있다. 기존.. 2022. 10. 7.
[Python Examples] 파이썬 텍스트 파일 만들기: open(), %pycat 파이썬 텍스트 파일 만들기: externalfile, open(), %pycat 포스트 난이도: HOO_Intern # Example Code externalfile = open("HOOAI.txt",'w') add_text = "Hi, I'm HOO. \n" externalfile.write(add_text) add_text = "This is a HOOAI Post. \n" externalfile.write(add_text) add_text = "Nice to meet you! \n" externalfile.write(add_text) externalfile.close() %pycat HOOAI.txt Hi, I'm HOO. This is a HOOAI Post. Nice to meet you! 다른.. 2022. 10. 7.
[Python Examples] 파이썬 format() 예제 파이썬 format() 예제 포스트 난이도: HOO_Intern # Example Code 1 format() function을 활용해서 문자열이나 숫자와 같은 데이터를 넣어줄 수 있다. 아래의 예제를 통해서 format을 활용해서 문자열을 넣는 방법을 살펴볼 수 있다. str='HOO' print('Hello, I\'m {}. Nice to meet you'.format(str)) Hello, I'm HOO. Nice to meet you 위의 예제를 살펴보면 str이라는 문자열이 format 함수를 통해서 어떻게 사용되고 있는지 알 수 있다. 위와 같이 Variable로 별도로 지정을 해준 다음에 format() 안에 넣어도 되지만 format() 안에 문자열을 바로 넣어도 무방하다. Variable.. 2022. 10. 7.
[Python Example] print(), print(sep=), print(c1+c2+c3..) 차이점 print(), print(sep=), print(c1+c2+c3..) 차이점 포스트 난이도: HOO_Intern [Notice] 포스트 난이도에 대한 설명 안녕하세요, HOOAI의 Henry입니다. Bro들의 질문에 대한 내용을 우선적으로 포스팅이 되다 보니 각각의 포스트에 대한 난이도가 달라서 난이도에 대한 부분을 작성하면 좋겠다는 의견을 들었습니다 whoishoo.tistory.com # Python Example Codes c1='H' c2='O' c3='O' c4='A' c5='I' print(c1,c2,c3,c4,c5) print(c1,c2,c3,c4,c5,sep='') print(c1+c2+c3+c4+c5) H O O A I HOOAI HOOAI 파이썬(Python)에서 문자열(String).. 2022. 9. 29.
[Python Examples] bool을 작성하지 않고 산술 계산식을 True or False로 나타내기 bool을 작성하지 않고 산술 계산식을 True or False로 나타내기 포스트 난이도: HOO_Intern [Notice] 포스트 난이도에 대한 설명 안녕하세요, HOOAI의 Henry입니다. Bro들의 질문에 대한 내용을 우선적으로 포스팅이 되다 보니 각각의 포스트에 대한 난이도가 달라서 난이도에 대한 부분을 작성하면 좋겠다는 의견을 들었습니다 whoishoo.tistory.com # Python Example Code 6+4==10 or 4+5!=9 True 파이썬에서는 별도의 bool 지정 없이도 bool을 사용할 수 있으며, 앞서서 True에 대한 조건이 선언되어 있거나 정의되어 있다면 올바른 bool 값이 산출된다. 위의 예제 코드를 살펴보면, 산술 계산식에 대한 참인지 거짓인지에 대한 코드.. 2022. 9. 26.
[Python Examples] os.walk() os.walk() 포스트 난이도: HOO_Junior [Notice] 포스트 난이도에 대한 설명 안녕하세요, HOOAI의 Henry입니다. Bro들의 질문에 대한 내용을 우선적으로 포스팅이 되다 보니 각각의 포스트에 대한 난이도가 달라서 난이도에 대한 부분을 작성하면 좋겠다는 의견을 들었습니다 whoishoo.tistory.com # os.walk() Example import os for root, dirs, files in os.walk(".", topdown=True): for name in files: print(os.path.join(root, name)) for name in dirs: print(os.path.join(root, name)) # Explanation import os는 os... 2022. 7. 3.
728x90