3.1 2개의 16진수 데이터 문자열을 한 줄로 입력 받고, 이를 정수로 변환하여 a와 b에 저장하라. 이 두 16진수 a, b의 bit-wise AND, bit-wise OR, bit-wise XOR 값을 계산하여 출력하는 파이썬 프로그램을 작성하라.
소스코드
# 22211975_homework3
"""
Project: 22211975_homework3.1
Author:
Date of last update: 22.09.15
Update list:
-v1.0: 22.09.15
- added source code
"""
# input 데이터
data1, data2 = input("16진수 데이터 문자열을 입력: ").split()
# 16진수 데이터 문자열을 정수형 데이터로 변환함
a = int(data1, 16) # 0xa3
b = int(data2, 16) # 0x3a
# 비트 연산자 계산을 위해 2진수 정수형 데이터로 변환함
a = int(bin(a), 2)
b = int(bin(b), 2)
# 비트 연산자
bw_AND = bin(a & b)
bw_OR = bin(a | b)
bw_XOR = bin(a ^ b)
# 출력
print(f'a & b = {bw_AND}')
print(f'a | b = {bw_OR}')
print(f'a ^ b = {bw_XOR}')
실행결과
3.2 날짜를 나타내는 연(year), 월(month), 일(day)의 3개 정수를 입력 받고, 이 날이 서기 1년 1월 1일부터 몇 번째 날짜인지를 계산하며, 이 날이 무슨 요일인지 계산하여 출력하는 파이썬 프로그램을 작성하라. 참고로 서기 1년 1월 1일은 월요일이다. 프로그램은 0 0 0이 입력될 때 까지 반복하도록 할 것. 필요한 기능은 파이썬이 제공하는 datetime 등을 사용하지 않고 직접 구현하여야 하며, 이를 통하여 조건식, 조건문 및 반복문 구성 방법과 응용에 대한 지식을 습득할 것.
소스코드
# 22211975_homework3
"""
Project: 22211975_homework3.2
Author:
Date of last update: 22.09.15
Update list:
-v1.0: 22.09.15
- added source code
"""
while True:
# 변수 설정
# 반복문 안에서 선언하지 않으면 값이 축적되어 예상하지못한 값이 나올 수 있음.
total_days = 0 # 몇 번째 날자인지를 계산하기 위한 변수
month_days = [0,31,28,31,30,31,30,31,30,31,30,31,30] # 달이 지나갈때마다 total_days 에 추가하기 위한 일 수 list
day = '' # 요일 출력을 위한 변수
# 입력
date = list(map(int, input('input year month day: ').split()))
# date 가 [0, 0, 0] 이 아닐 시 조건문 실행
if date[0] != 0 and date[1] != 0 and date[2] != 0:
# 아래 반복문에서는 1년이 지나면서 추가되는 days 만 계산한다.
# 윤년에 의해 발생하는 2월 29일을 계산하기 위한 반복문
for LeapYear in range(1, date[0]):
total_days += 365
if LeapYear % 400 == 0:
total_days = total_days + 1
elif LeapYear % 100 == 0:
pass
elif LeapYear % 4 == 0:
total_days = total_days + 1
else:
pass
# date[1](달)로 인한 month_days의 값을 total_days 에 더한다.
for i in range(1, date[1]):
total_days = total_days + month_days[i]
# 아래 코드는 몇일인가에 따라서 date[2]값을 total_days 에 더한다.
total_days = total_days + date[2]
# 요일 계산 section
if total_days % 7 == 0:
day = "일요일"
elif total_days % 7 == 1:
day = "월요일"
elif total_days % 7 == 2:
day = "화요일"
elif total_days % 7 == 3:
day = "수요일"
elif total_days % 7 == 4:
day = "목요일"
elif total_days % 7 == 5:
day = "금요일"
elif total_days % 7 == 6:
day = "토요일"
print(f"{date[0]}년 {date[1]}월 {date[2]}일은 서기 1년 1월 1일로 부터 {total_days}일 만큼 경과하였고, {day}입니다.")
elif date[0] == 0 and date[1] == 0 and date[2] == 0:
break
실행결과
3.3 날짜를 나타내는 연(year), 월(month), 일(day)의 3개 정수를 입력 받고, 이 달의 달력을 출력하는 파이썬 프로그램을 작성하라. 달력의 첫 줄은 요일을 의미하는 영문 약자 (SUN, MON, TUE, WED, THR, FRI, SAT)를 출력하고, 이 달의 1일 부터 요일에 맞추어 출력되도록 할 것 필요한 기능은 파이썬이 제공하는 모듈 등을 사용하지 않고 직접 구현하여야 하며, 이를 통하여 조건식, 조건문 및 반복문 구성 방법과 응용에 대한 지식을 습득할 것.
소스코드
# 22211975_homework3
"""
Project: 22211975_homework3.3
Author:
Date of last update: 22.09.16
Update list:
-v1.0: 22.09.16
- added source code
"""
year, month, blank = map(int, input('input year, month, day: ').split())
month_list ={1:'January', 2:'February', 3:'March',
4:'April', 5:'May', 6:'June', 7:'July',
8:'August', 9:'September', 10:'October',
11:'November', 12:'December'}
Not_LeapYear = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
LeapYear = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# 공백 계산용 변수 지정 1
day = (year - 1) % 400
day = (day // 100) * 5 + ((day % 100) - (day % 100) // 4) + ((day % 100) // 4) * 2
day = day % 7
s = 0
# 윤년인 경우 계산
if year % 4 == 0:
for i in range(month - 1):
s += LeapYear[i]
else:
for i in range(month - 1):
s += Not_LeapYear[i]
# 공백 계산용 변수 지정 2
day += s % 7
day = day % 7
# 날짜가 비는 곳을 채우기 위한 공백 출력 코드 지정
space =''
space = space.rjust(2, ' ')
# 달력을 출력하는 코드
print(month_list[month], year)
print('SUN', 'MON', 'TUE', 'WED', 'THR', 'FRI', 'SAT')
"""
1. day+1 값 만큼 공백 생성 후, 날짜 출력
2. i+1 값이 7의 배수가 된다면 공백, 날짜를 출력하고 다음줄로 이동
예를들어, month 값이 9 이면 day 값이 3 이고 아래 반복문에 대입해서 연산하면 'i <= day' 를 만족하는 경우가 4번 발생하여
공백이 4개 생기고 목요일부터 날짜가 출력되기 시작한다.
여기서 i+1 값이 7의 배수가 되는 토요일의 날짜 3 을 출력하고 다음줄에서 계속 반복문을 실행한다.
"""
if month == 9 or month == 4 or month == 6 or month == 11: # 한달이 30일인 경우
for i in range(31 + day):
if i <= day:
print(space, end =' ')
else:
print("{:3d}".format(i-day), end =' ') # 요일을 표시하는 문자가 3글자(SUN, MON)라서 3칸을 기준으로 날짜를 입력하게함
if (i + 1) % 7 == 0:
print()
elif month == 2:
if year % 4 == 0:
p = 30
else:
p = 29
for i in range(p + day): # 30 + day or 29 + day
if i <= day:
print(space, end =' ')
else:
print("{:3d}".format(i-day), end =' ')
if (i + 1) % 7 == 0:
print()
else: # 한달이 31일인 경우
for i in range(32 + day):
if i <= day:
print(space, end =' ')
else:
print("{:3d}".format(i-day), end =' ')
if (i + 1) % 7 == 0:
print()
실행결과
근데 이거 2922 년 2월 입력하면 오류남
'과제' 카테고리의 다른 글
컴사파 - 5주차 (0) | 2022.09.28 |
---|---|
컴사파 - 4주차 (0) | 2022.09.22 |
컴사파 - 2주차 (1) | 2022.09.17 |
컴사파 - 1주차 (0) | 2022.09.16 |
[과제10] (0) | 2022.05.19 |