๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฌธ์ œํ’€์ด๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป

[python] ๋ฆฌ์ŠคํŠธ ์ €์žฅ ๊ณ„์‚ฐ ๋ฐ ํ•จ์ˆ˜ ๋ชจ๋“ˆํ™”

by hyerong 2024. 11. 5.
num_arr = []
num = int(input("์ˆซ์ž ์ž…๋ ฅ (์ข…๋ฃŒ:999)"))
num_arr.append(num)
while num != 999: #while True: ๋กœ ์ง„ํ–‰ํ•˜๋ฉด ์œ„์— ์ž…๋ ฅ๋ฐ›๋Š” ์ฝ”๋“œ ์•ˆ ์จ์ค˜๋„ ๋จ 
    num = int(input("์ˆซ์ž ์ž…๋ ฅ (์ข…๋ฃŒ:999)"))
    num_arr.append(num)
    num_arr.sort()
    print("์ค‘๊ฐ„ ์ •๋ ฌ ๊ฒฐ๊ณผ", num_arr)
    
print("์ตœ์ข… ์ •๋ ฌ ๊ฒฐ๊ณผ", num_arr)

๋ฌธ์ œ 3. 
์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ n๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅ์„ ๋ฐ›์€ ํ›„(์ž…๋ ฅ ์ข…๋ฃŒ๋Š” 999) ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•˜๊ณ , 

์ด ์ˆซ์ž๋“ค์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค. 

 



๋ฌธ์ œ 4. 
์œ„ ์ฝ”๋“œ์—์„œ ๊ณ„์‚ฐ ์ถ”๊ฐ€
๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ, ์•„์ดํ…œ ๊ฐœ์ˆ˜, ํ•ฉ๊ณ„, ํ‰๊ท (์†Œ์ˆ˜ ์ดํ•˜ ๋‘˜์งธ ์ž๋ฆฌ ๊นŒ์ง€ ์ถœ๋ ฅ),
์ตœ๋Œ€, ์ตœ์†Œ๊ฐ’ ์ถœ๋ ฅ

-> ์ด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•œ .sort()์™€ ๋‚ด๋ฆผ์ฐจ์ˆœํ• ๋•Œ sorted(์›๋ž˜๋ฆฌ์ŠคํŠธ, reverse=True)ํ•จ์ˆ˜, ๋ฐ ์‚ฌ์น™์—ฐ์‚ฐ์— ๋Œ€ํ•ด ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. 

-> ์ตœ๋Œ“๊ฐ’์€ ์ •๋ ฌ๋œ ํ›„ ๋งจ ๋งˆ์ง€๋ง‰ ์š”์†Œ์ธ๋ฐ ์ด๋•Œ ๋ฆฌ์ŠคํŠธ์˜ ์ธ๋ฑ์Šค ๊ฐ’์€ ๊ฐœ์ˆ˜๋ณด๋‹ค 1์ž‘์€์ˆ˜๋ผ๋Š”๊ฑฐ ์žˆ์ง€๋ง๊ธฐ (0๋ถ€ํ„ฐ ์‹œ์ž‘) 

-> ๊ถ๊ธˆํ•œ ์ ์€ 999๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋ฐ”๋กœ ์ข…๋ฃŒํ•˜๋Š”์ง€ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ๋ฐ›๊ณ  ์ข…๋ฃŒํ•˜๋Š”์ง€ ๊ถ๊ธˆํ–ˆ๋‹ค. ๊ทธ์น˜๋งŒ ๋…ผ๋ฆฌ์ƒ 999๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ๋ฐ”๋กœ ์ข…๋ฃŒํ•˜๋Š”๊ฒŒ ๋งž์„๊ฒƒ ๊ฐ™์•„์„œ ๋ฐ”๋กœ ์ข…๋ฃŒํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ž‘์„ฑ 

#999๋Š” ๋ฆฌ์ŠคํŠธ์— ์•ˆ ๋“ค์–ด๊ฐ€๊ณ  ์ข…๋ฃŒ? 
num_arr = []
desc_arr = []

while True:
    num = int(input("์ˆซ์ž ์ž…๋ ฅ (์ข…๋ฃŒ:999)"))
    if(num == 999): break
    num_arr.append(num)
    num_arr.sort()
    # print("์ค‘๊ฐ„ ์ •๋ ฌ ๊ฒฐ๊ณผ", num_arr)
    #๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ  
    desc_arr = sorted(num_arr, reverse=True)
    
    #์•„์ดํ…œ ๊ฐœ์ˆ˜ 
    item_num = len(num_arr) 
    #ํ•ฉ๊ณ„ 
    sum_num = sum(num_arr)
    #๋‚ด๋ฆผ์ฐจ์ˆœ 
    #ํ‰๊ท  
    ex_num = sum_num / item_num
    #์ตœ๋Œ€ 
    i = item_num-1
    #์ตœ๋Œ€ 
    max_num = num_arr[i]
    #์ตœ์†Œ 
    min_num = num_arr[0]
    
    
print("์ตœ์ข… ์ •๋ ฌ ๊ฒฐ๊ณผ: ", num_arr)  
print("๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ: ", desc_arr)       
  
print("item ๊ฐœ์ˆ˜: ", item_num)
print("ํ•ฉ๊ณ„ : ", sum_num)
print(f"ํ‰๊ท : {ex_num:.2f}")

print("์ตœ๋Œ€: ", max_num) 
print("์ตœ์†Œ: ", min_num)



๋ฌธ์ œ 5. 
์œ„ ์ฝ”๋“œ์—์„œ ์ž…๋ ฅ ์‹œ ์ž…๋ ฅ ์ž๋ฃŒ ๊ฐœ์ˆ˜๋ฅผ ๋จผ์ € ์ž…๋ ฅํ•˜๊ณ , ์ฐจ๋ก€๋Œ€๋กœ ์ž…๋ ฅ
(2 ์ด์ƒ์ด ์ž…๋ ฅ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต ์ˆ˜ํ–‰) 

num_arr = []
desc_arr = []

number = int(input("์ž…๋ ฅํ•  ์ž๋ฃŒ ๊ฐœ์ˆ˜(๋ฐ˜๋ณต ํšŸ์ˆ˜) ์ž…๋ ฅ: "))
while True: 
    if(number < 2): 
        print("๋ฐ˜๋ณต ํšŸ์ˆ˜๋Š” 2๋ฒˆ ์ด์ƒ๋ถ€ํ„ฐ!")
        break 
    else: 
        for _ in range(number): 
            num = int(input("์ˆซ์ž ์ž…๋ ฅ (์ข…๋ฃŒ:999)"))
            if(num == 999): 
                break
            
            num_arr.append(num)
            num_arr.sort()
            # print("์ค‘๊ฐ„ ์ •๋ ฌ ๊ฒฐ๊ณผ", num_arr) #๋””๋ฒ„๊น… 
            
            #๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ  
            desc_arr = sorted(num_arr, reverse=True)
            
        #์•„์ดํ…œ ๊ฐœ์ˆ˜ 
        item_num = len(num_arr) 
        #ํ•ฉ๊ณ„ 
        sum_num = sum(num_arr)
            
        #ํ‰๊ท  
        ex_num = sum_num / item_num
        #์ตœ๋Œ€ 
        i = item_num-1
        #์ตœ๋Œ€ 
        max_num = num_arr[i]
        #์ตœ์†Œ 
        min_num = num_arr[0]
    
        print("์ตœ์ข… ์ •๋ ฌ ๊ฒฐ๊ณผ: ", num_arr)  
        print("๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ: ", desc_arr)       
    
        print("item ๊ฐœ์ˆ˜: ", item_num)
        print("ํ•ฉ๊ณ„ : ", sum_num)
        print(f"ํ‰๊ท : {ex_num:.2f}")

        print("์ตœ๋Œ€: ", max_num) 
        print("์ตœ์†Œ: ", min_num)
        
        break


๋ฌธ์ œ 6. 
์œ„ ์ฝ”๋“œ๋ฅผ ํ•จ์ˆ˜๋กœ ๋ชจ๋“ˆํ™”
module 1: ์‚ฌ์šฉ์ž๋กœ ๋ถ€ํ„ฐ ์ž…๋ ฅ
module 2: ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ
module 3: ๊ณ„์‚ฐ, ์ถœ๋ ฅ

def input_number(): 
    number = int(input("์ž…๋ ฅํ•  ์ž๋ฃŒ ๊ฐœ์ˆ˜(๋ฐ˜๋ณต ํšŸ์ˆ˜) ์ž…๋ ฅ: "))
    while number > 2: 
        if(number < 2): 
            print("๋ฐ˜๋ณต ํšŸ์ˆ˜๋Š” 2๋ฒˆ ์ด์ƒ๋ถ€ํ„ฐ!")
            break
        else:
            return number 


def save_to_list(num, num_arr):
    for _ in range(num):
        num = int(input("์ˆซ์ž ์ž…๋ ฅ (์ข…๋ฃŒ:999): "))
        if(num == 999): 
            break
        num_arr.append(num)
        num_arr.sort()
        print("์ค‘๊ฐ„ ์ •๋ ฌ ๊ฒฐ๊ณผ", num_arr) #๋””๋ฒ„๊น… 
            


def evaluate_print(num_arr):
    # 999๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ณ„์‚ฐํ•˜์ง€๋ง๊ณ  ์ข…๋ฃŒ 
    length = len(num_arr)
     
    #์•„์ดํ…œ ๊ฐœ์ˆ˜ 
    item_num = len(num_arr) 
        #ํ•ฉ๊ณ„ 
    sum_num = sum(num_arr)
        #ํ‰๊ท  
    ex_num = sum_num / item_num
        #์ตœ๋Œ€ 
    i = item_num-1
        #์ตœ๋Œ€ 
    max_num = num_arr[i]
        #์ตœ์†Œ 
    min_num = num_arr[0]
    
    desc_arr = sorted(num_arr, reverse=True)
    
    print("์ตœ์ข… ์ •๋ ฌ ๊ฒฐ๊ณผ: ", num_arr)  
    print("๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ: ", desc_arr)       
    
    print("item ๊ฐœ์ˆ˜: ", item_num)
    print("ํ•ฉ๊ณ„ : ", sum_num)
    print(f"ํ‰๊ท : {ex_num:.2f}")

    print("์ตœ๋Œ€: ", max_num) 
    print("์ตœ์†Œ: ", min_num)
    
####################################
num_arr = []
num = input_number()
save_to_list(num, num_arr)
evaluate_print(num_arr)

-> ์ถ”๊ฐ€ํ•  ์‚ฌํ•ญ : ์ž…๋ ฅํ•  ์ž๋ฃŒ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ• ๋•Œ 999๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ์ข…๋ฃŒ๋ฌธ๊ตฌ์™€ ํ•จ๊ป˜ ์ข…๋ฃŒ๋˜๋„๋ก ์ˆ˜์ •ํ•ด๋ด์•ผ๊ฒ ๋‹ค. 

์ง€๊ธˆ์€ 999๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์•„๋ž˜ ํ™”๋ฉด๊ณผ ๊ฐ™์ด ํ‰๊ท ์„ ๊ตฌํ•˜๋Š”๋ฐ์—์„œ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋ฉด์„œ ์ข…๋ฃŒ๊ฐ€ ๋˜๋Š” ์ƒํ™ฉ 

line 32, in evaluate_print
    ex_num = sum_num / item_num