Print
카테고리: [ Development ]
조회수: 5159

1. 주식 종가와 날짜를 입력받은 후 해당 날짜만큼 연속으로 하한가(-30%)를 기록했을 때와 상한가(30%)를 기록했을 때의 주가 구하기

stockPrice=int(input("종가: "))
days=int(input("날짜: "))
 
stockUpPrice=stockPrice
stockDownPrice=stockPrice
 
for d in range(days):
  stockUpPrice = stockUpPrice + stockUpPrice * 0.3
  stockDownPrice = stockDownPrice - stockDownPrice * 0.3
 
print(stockUpPrice)
print(stockDownPrice)

<  결과 >

종가: 100000
날짜: 3
219700.0
34300.0

 

2. 3의 배수를 입력할 때까지 계속 숫자를 입력받기

while True:
  num=int(input("Number: "))
  if num%3 == 0:
    break

< 결과 >

Number: 1
Number: 5
Number: 9

 

3. 5초 단위로 서버에서 netstat -an 갯수 출력하기

import datetime
import subprocess
import time
 
while True:
  result=subprocess.check_output('netstat -an | wc -l', shell=True)
  print(datetime.datetime.now(), result.decode('utf-8').strip())
  time.sleep(5)

< 결과 >

2017-09-04 17:55:19.339011 62
2017-09-04 17:55:24.383026 62
2017-09-04 17:55:29.427181 62
2017-09-04 17:55:34.471256 66
2017-09-04 17:55:39.516121 67
2017-09-04 17:55:44.549378 66
2017-09-04 17:55:49.592646 66
2017-09-04 17:55:54.626565 66
2017-09-04 17:55:59.672985 66
2017-09-04 17:56:04.717709 66
2017-09-04 17:56:09.763230 66
2017-09-04 17:56:14.806622 66
2017-09-04 17:56:19.847949 66
2017-09-04 17:56:24.880463 66
2017-09-04 17:56:29.925246 66
2017-09-04 17:56:34.971001 66
2017-09-04 17:56:40.017631 71
^CTraceback (most recent call last):
  File "netstat.py", line 8, in 
    time.sleep(5)
KeyboardInterrupt

 

4. 100 미만 숫자 중 소수 구하기

primeNumber=[2]
 
for i in range(3,100):
  for j in range(2,i):
    if i%j == 0:
      break
    if j == i-1:
      primeNumber.append(i)
 
print(primeNumber)

< 결과 >

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

 

5. 100 미만 숫자 중 소수 구하기 (다른 방법)

import math
 
primeNumber=[2]
 
for i in range(3,100):
  for j in range(2,math.ceil(i/2)):
    if i%j == 0:
      break
    if j == math.ceil(i/2)-1:
      primeNumber.append(i)
 
print(primeNumber)

< 결과 >

[2, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]