티스토리 뷰
Say "Hello, World!" With Python
Here is a sample line of code that can be executed in Python:
print("Hello, World!")
You can just as easily store a string as a variable and then print it to stdout:
my_string = "Hello, World!"
print(my_string)
The above code will print Hello, World! on your screen. Try it yourself in the editor below!
Input Format
You do not need to read any input in this challenge.
Output Format
Print Hello, World! to stdout.
Sample Output 0
Hello, World!
파이썬3에서 화면 출력을 위해서는 print() 함수를 사용한다.
print("Hello, World!")
Python If-Else
Task
Given an integer, n, perform the following conditional actions:
- If n is odd, print Weird
- If n is even and in the inclusive range of 2 to 5, print Not Weird
- If n is even and in the inclusive range of 6 to 20, print Weird
- If n is even and greater than 20, print Not Weird
파이썬3에서 else if의 경우 elif 구문으로 사용한다.
#!/bin/python3
import math
import os
import random
import re
import sys
if __name__ == '__main__':
n = int(input().strip())
if n % 2 != 0: print("Weird")
elif n % 2 == 0 and n >= 2 and n <= 5: print("Not Weird")
elif n % 2 == 0 and n >= 6 and n <= 20: print("Weird")
elif n % 2 == 0 and n > 20: print("Not Weird")
Discussions에서 잘하는 사람은 elif 구문을 사용하지 않고 아래처럼 코딩하였다.
Not Weird인 경우만 check하고 false인 경우 Weird가 출력된다.
범위 체크할 때 range() 함수 사용이 눈에 띈다!
#!/bin/python3
import math
import os
import random
import re
import sys
if __name__ == '__main__':
n = int(input().strip())
check = {True: "Not Weird", False: "Weird"}
print(check[
n%2==0 and (
n in range(2,6) or
n > 20)
])
Arithmetic Operators
Task
The provided code stub reads two integers from STDIN, a and b. Add code to print three lines where:
- The first line contains the sum of the two numbers.
- The second line contains the difference of the two numbers (first - second).
- The third line contains the product of the two numbers.
Example
a = 3
b = 5
Print the following:
8
-2
15
각각의 더하기, 빼기, 곱하기 값을 구하는 것으로 단순히 아래처럼 작성하였다.
if __name__ == '__main__':
a = int(input())
b = int(input())
print(a+b)
print(a-b)
print(a*b)
Discussions에서 코딩한 방법이다.
변수의 값은 {0}, {1}, {2} 이런 식으로 주면 되는 듯 하고 중간에 \n을 추가하여 줄바꿈을 할 수 있는 것 같다.
format() 함수를 통하여 값 할당을 하는 것 같다.
if __name__ == '__main__':
a = int(input())
b = int(input())
print('{0} \n{1} \n{2}'.format((a + b), (a - b), (a * b)))
Python: Division
Task
The provided code stub reads two integers, a and b, from STDIN.
Add logic to print two lines. The first line should contain the result of integer division, a // b. The second line should contain the result of float division, a / b.
No rounding or formatting is necessary.
Example
a = 3
b = 5
- The result of the integer division 3 // 5 = 0.
- The result of the float division is 3/5 = 0.6.
Print:
0
0.6
위의 Arithmetic Operator의 Discussion에서 풀이한 것처럼 format()함수를 사용해 보았다.
Python에서 정수 나누기는 //, 소수점 나누기는 / 를 사용하는 것이 특이하였다.
이번에는 Discussions에서도 동일하게 작성하였다!
if __name__ == '__main__':
a = int(input())
b = int(input())
print("{0}\n{1}".format(a//b, a/b))
Loops
Task
The provided code stub reads and integer, n, from STDIN. For all non-negative integers i < n, print i2(i의제곱).
Example
n = 3
The list of non-negative integers that are less than n = 3 is [0, 1, 2] . Print the square of each number on a separate line.
0
1
4
while구문과 pow() 함수를 사용하여 코드를 작성하였다.
if __name__ == '__main__':
n = int(input())
i = 0
while i < n:
print(pow(i, 2))
i += 1
Discussions에서는 for문과 range() 함수를 사용하였다.
[] 괄호 사용이 어떤 경우인지 아직 잘 모르겠는데 사용하지 않고 그냥 for문으로 작성해도 될 듯 하다.
if __name__ == '__main__':
n = int(input())
[print(i**2) for i in range(n)]
#[print(i**2) for i in range(n) if n in range(1, 21)]
#괄호 사용하지 않은 코드
for i in range(n):
print(i * i)
Write a function
An extra day is added to the calendar almost every four years as February 29, and the day is called a leap day. It corrects the calendar for the fact that our planet takes approximately 365.25 days to orbit the sun. A leap year contains a leap day.
In the Gregorian calendar, three conditions are used to identify leap years:
- The year can be evenly divided by 4, is a leap year, unless:
- The year can be evenly divided by 100, it is NOT a leap year, unless:
- The year is also evenly divisible by 400. Then it is a leap year.
- The year can be evenly divided by 100, it is NOT a leap year, unless:
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years. Source
Task
Given a year, determine whether it is a leap year. If it is a leap year, return the Boolean True, otherwise return False.
Note that the code stub provided reads from STDIN and passes arguments to the is_leap function. It is only necessary to complete the is_leap function.
Input Format
Read year, the year to test.
Constraints
Output Format
The function must return a Boolean value (True/False). Output is handled by the provided code stub.
Sample Input 0
1990
Sample Output 0
False
Explanation 0
1990 is not a multiple of 4 hence it's not a leap year.
윤년구하기 조건은 아래와 같다.
1. 4로 나누어지면 윤년이다.
2. 4로 나누어지더라도 100으로 나누어지면 윤년이 아니다.
3. 100으로 나누어지더라도 400으로 나누어지면 윤년이다.
def is_leap(year):
leap = False
# Write your logic here
if year % 400 == 0:
leap = True
elif year % 100 == 0:
leap = False
elif year % 4 == 0:
leap = True
else:
leap = False
return leap
year = int(input())
print(is_leap(year))
'Programming > Python' 카테고리의 다른 글
[Python/Hackerrank] Basic Data Types > Finding the percentage (0) | 2021.12.07 |
---|---|
[Python/Hackerrank] Basic Data Types > Nested Lists (0) | 2021.12.06 |
[Python/Hackerrank] Basic Data Types > Find the Runner-Up Score! (0) | 2021.12.06 |
[Python/Hackerrank] Basic Data Types > List Comprehensions (0) | 2021.12.06 |
[Python/Tutorial따라하기] python install (0) | 2021.12.06 |
- Total
- Today
- Yesterday
- 대원화성
- DATABASE
- tensorflow
- python
- python3
- 매매일지
- 해커랭크
- list
- Tableau
- mysql
- insert
- 넥스트BT
- 넷플릭스
- 리비안
- hackerrank
- 미중무역전쟁
- string
- 몰누피라비르
- TSQL
- MSSQL
- 분석탭
- 코로나19
- MS SQL Server
- Weather Observation Station
- 경구치료제
- 에코캡
- HK이노엔
- 테슬라
- 동국알앤에스
- SQL Server
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |