230720

송용진·2023년 7월 20일
0
# #문제1
# #절대값기준오름차순
# nums = [7, -9, -2, 4, 3, -8, 6, 1]
# sorted_nums = sorted(nums, key=abs)
# print(sorted_nums)
# del nums,sorted_nums

# #절대값기준내림차순
# nums = [7, -9, -2, 4, 3, -8, 6, 1]
# sorted_nums = sorted(nums, key=abs,reverse=True)
# print(sorted_nums)
# del nums,sorted_nums

#문제2
class Person:

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __repr__(self):
        return f"Person(name='{self.name}',age={self.age})"


people = [
    Person('Alice', 30),
    Person('Bob', 35),
    Person('Charlie', 25),
    Person('Alice', 25),
    Person('David', 30)
]

# people.sort(key=lambda person: person.name)
# print(people)


# people.sort(key=lambda person: person.age)
# print(people)

# people.sort(key=lambda person: (person.name,-person.age))
# print(people)

people.sort(key=lambda person: person.age)
people.sort(key=lambda person: person.name,reverse=True)
print(people)

class Student:
    def __init__(self, name, grade, score):
        self.name = name
        self.grade = grade
        self.score = score
    def __repr__(self):
        return f"Student(name='{self.name}',grade={self.grade},score={self.score})"
    # lt less than 복잡한 정렬 조건
    # def __lt__(self, other):
    #   if self.grade != other.grade
    #...

students = [
    Student('Alice', 3, 85),
    Student('Bob', 2, 90),
    Student('Charlie', 2, 88),
    Student('Alice', 1, 95),
    Student('David', 3, 78)
]

# students.sort(key=lambda student: (student.grade,-student.score))
# print(students)

students.sort(key=lambda student: student.score ,reverse=True)
students.sort(key=lambda student: student.name ,reverse=True)
print(students)


import heapq
nums = [15, 22, 7, -8, 33, 12, 42, -3, 18, 29, -11]

# heapq 모듈을 활용하여 
# nums 리스트를 최소 힙으로 변환하고 결과를 출력하세요.
# heapq.heapify(nums)
# print(nums)

# 최소 힙에서 가장 작은 원소를 추출하고, 
# 그 후의 힙의 상태를 출력하세요.
# min_element = heapq.heappop(nums)
# print(min_element)
# print(nums)

# 변환한 힙에서 가장 큰 원소를 추출하고,
heap1 = []
for num in nums:
    heapq.heappush(heap1, -num)
print(-heapq.heappop(heap1))
# 그 후의 힙의 상태를 출력하세요.
heap2 = []
for i in heap1:    
    heapq.heappush(heap2, -i)
print(heap2)
# 해당 방법이 효율적인지도 설명해주세요.
# 가장 큰 원소를 추출하기 위해 
# 부호를 일일이 바꿔서 새로운 힙에 push하기 때문에 비효율적이다.

'''
문제 2
주어진 시간 내에 가능한 많은 일을 하려고 합니다.
각 일에는 시작 시간과 끝나는 시간이 있습니다.
일은 시작 시간과 끝나는 시간이 겹치지 않게 일정을 짜야 합니다.
주어진 일들 중에서 가장 많은 일을 할 수 있는 방법을 찾아주세요.
작업
주어진 일들의 시작 시간과 끝나는 시간을 입력 받아 
가능한 많은 일을 할 수 있는 일정을 출력하세요.
'''
def schedule_jobs(jobs):
    #끝나는 시간을 기준으로 오름차순 정렬
    jobs.sort(key=lambda x:x[1])
    print(jobs)
    #현재 일의 시작 시간이 이전 일의 끝나는 시간보다 늦거나 같다면
    for i in jobs:
        if i[1] =< i+1[0]
    #일을추가하고
    #마지막으로 끝난 일의 시간을 현재 일의 끝나는 시간으로 갱신한다.
    pass

# Test the function
jobs = [(1, 4), (3, 5), (0, 6), (5, 7), (3, 8), (5, 9), (6, 10),
(8, 11), (8, 12), (2, 13), (12, 14)]
print(schedule_jobs(jobs))  
# 예상 출력: [(1, 4), (5, 7), (8, 11), (12, 14)]

'''
문제 3
주어진 배열에서 k번째로 작은 요소를 찾는 문제입니다.
이 문제는 힙 큐를 사용하여 효율적으로 해결할 수 있습니다.

힌트 : 힙에 k개 보다 많은 요소가 추가되었다면
pop으로 제거하여 높은 효율성 추구

작업
주어진 배열과 k를 입력 받아 k번째로 작은 요소를 찾아 출력하세요.
'''
import heapq

def find_kth_largest(nums, k):
    heap = []
        
    for i in nums:
        heapq.heappush(heap,-i)
        if len(heap) > k:
            heapq.heappop(heap)
    return -heapq.heappop(heap)     
        
# Test the function
nums = [3,2,1,5,6,4]
k = 2
print(find_kth_largest(nums, k))  # 예상 출력: 2


profile
개발자

1개의 댓글

comment-user-thumbnail
2023년 7월 20일

항상 좋은 글 감사합니다.

답글 달기