1. 문제 소개

파이썬으로 R을 만들어보자

2. 문제 기획 의도

배운 것을 복습해보자

3. 시연


In [ ]:
import csv

list1 = []
list2 = []
cnt = 1

f = open("test.csv", 'r')
list_temp = csv.reader(f)

for row in list_temp:
    if cnt == 1:
        list1 = row
        cnt += 1
    else:
        list2 = row
f.close()

def grasp_command(command):
    try:
        temp = command[0:command.index('(')]
        
        if command == 'q()':
            return 'exit'
        elif temp == 'stat' or temp =='cov' or temp =='min_sort' or temp =='max':
            return command
        else:
            return 'false'
    
    except:
        return 'false'

def func_str(command):
    temp = 0
    for i in command:
        temp = temp + int(i)
    print("average : %.2f" % (temp / len(command)))

def func_min_sort(command):
    cnt = len(command)
    new_list = []

    while cnt != 0:
        a = min(command)
        new_list.append(a)
        command.remove(a)
        cnt -= 1
    
    print(new_list)        
    
def run_func(meaning):
    command = meaning[:meaning.index('(')]
    parameter_temp = meaning[meaning.index('(')+1:meaning.index(')')]
    
    if parameter_temp == 'list1':
        parameter = list1
    elif parameter_temp == 'list2':
        parameter = list2
    else:
        print("Error in str(" + str(parameter_temp) + ") : object '" + str(parameter_temp) + "' not found")
        return 0
         
    if command == 'stat':
        func_str(parameter)
    elif command == 'cov':
        print("cov")
    elif command == 'min_sort':
        func_min_sort(parameter)
    elif command == 'max':
        print("max")

print("R version 3.2.4 Revised (2016-04-23 r70336) -- 'PYTHON'")
print("Copyright (C) 2016 The R Foundation for Statistical Computing")
print("Platform: x86_64-w64-mingw32/x64 (64-bit)")
print("")
print("R is free software and comes with ABSOLUTELY NO WARRANTY.")
print("You are welcome to redistribute it under certain conditions.")
print("Type 'license()' or 'licence()' for distribution details.")
print("")
print("R is a collaborative project with many contributors.")
print("Type 'contributors()' for more information and")
print("'citation()' on how to cite R or R packages in publications.")
print("")
print("Type 'demo()' for some demos, 'help()' for on-line help, or")
print("help.start()' for an HTML browser interface to help.")
print("Type 'q()' to quit R.\n")
    
while(1):
    command = input("> ")
    meaning = grasp_command(command)
    
    if meaning == 'exit':
        break
    elif meaning == 'false':
        print("Error: object '" + command + "' not found")
    else:
        run_func(meaning)

4. 문제풀이


In [ ]:
import csv

list1 = []
list2 = []
cnt = 1

f = open("test.csv", 'r')
list_temp = csv.reader(f)

for row in list_temp:
    if cnt == 1:
        list1 = row
        cnt += 1
    else:
        list2 = row
f.close()

def grasp_command(command):
    try:
        temp = command[0:command.index('(')]
        
        if command == 'q()':
            return 'exit'
        elif temp == 'stat' or temp =='cov' or temp =='min_sort' or temp =='max':
            return command
        else:
            return 'false'
    
    except:
        return 'false'

def func_str(command):
    temp = 0
    for i in command:
        temp = temp + int(i)
    print("average : %.2f" % (temp / len(command)))

def func_min_sort(command):
    cnt = len(command)
    new_list = []

    while cnt != 0:
        a = min(command)
        new_list.append(a)
        command.remove(a)
        cnt -= 1
    
    print(new_list)        
    
def run_func(meaning):
    command = meaning[:meaning.index('(')]
    parameter_temp = meaning[meaning.index('(')+1:meaning.index(')')]
    
    if parameter_temp == 'list1':
        parameter = list1
    elif parameter_temp == 'list2':
        parameter = list2
    else:
        print("Error in str(" + str(parameter_temp) + ") : object '" + str(parameter_temp) + "' not found")
        return 0
         
    if command == 'stat':
        func_str(parameter)
    elif command == 'cov':
        print("cov")
    elif command == 'min_sort':
        func_min_sort(parameter)
    elif command == 'max':
        print("max")

print("R version 3.2.4 Revised (2016-04-23 r70336) -- 'PYTHON'")
print("Copyright (C) 2016 The R Foundation for Statistical Computing")
print("Platform: x86_64-w64-mingw32/x64 (64-bit)")
print("")
print("R is free software and comes with ABSOLUTELY NO WARRANTY.")
print("You are welcome to redistribute it under certain conditions.")
print("Type 'license()' or 'licence()' for distribution details.")
print("")
print("R is a collaborative project with many contributors.")
print("Type 'contributors()' for more information and")
print("'citation()' on how to cite R or R packages in publications.")
print("")
print("Type 'demo()' for some demos, 'help()' for on-line help, or")
print("help.start()' for an HTML browser interface to help.")
print("Type 'q()' to quit R.\n")
    
while(1):
    command = input("> ")
    meaning = grasp_command(command)
    
    if meaning == 'exit':
        break
    elif meaning == 'false':
        print("Error: object '" + command + "' not found")
    else:
        run_func(meaning)

5. 난관

R의 x <- c(1, 2, 3, 4, 5)를 어떻게 구현해야 하는가