게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
파이썬 프로그래밍 언어 잘하는 횽들! 도와주면 여친 생겨요!!
게시물ID : computer_42917짧은주소 복사하기
작성자 : 허허54752;
추천 : 0
조회수 : 491회
댓글수 : 1개
등록시간 : 2012/04/06 09:30:55
calculator class 를 쓰는것 인데, initiating functio 을 사용 하는것 같습니다. 그런데 여기서 아무것도 return 이 안된다는 부분이 이해가 잘 안되네요. a 부터 e 까지는 리턴을 하면 안된다고 합니다. a) add_numbers 입니다. 여기서, 변수 파라미터를 리스트로 쓰는데요, 일단 제가 한것 올려 보겠습니다. class Calculator(object): ''' A calculator which will operate simple calculations such as addition, multiplication, division, subtraction, or dot product of vectors''' def__init__(self,var1,var2): ''' a new variable with variable1 (var1), and variable2(var2). All components are lists''' def__add_numbers__(self,var1,var2): '''This functio will operate the basic addition of components of var1 and var2''' i = 0 result = [] while i <= len(var1) or len(var2): sum= var1[i] + var2[i] result.append(sum) i+=1 return result 여기서 pass 라는걸 쓰면 되는걸까요? b) def__multiply_numbers__(self,var1,var2): ''' This functio will operate the basic multiplication of components of var1 and var2''' i = 0 result = [] while i <= len(var1) or len(var2): mult= var1[i] * var2[i] result.append(mult) i+=1 return result 여기서도 pass 를 써야 하는게 아닌가 싶네요. c) def__divide_two_numbers__(self,var1,var2): '''This functio will operate the basic division of components of var1,var2 and only accepts two components''' i = 0 result = [] while i <= len(var1) or len(var2): div= var1[i] / var2[i] result.append(div) i+=1 return result pass (?) d) def__subtract_numbers__(self,var1,var2): '''This functio will operate the basic subtraction of components of var1,var2''' i = 0 result = [] while i <= len(var1) or len(var2): subt= var1[i] + var2[i] result.append(subt) i+=1 return result pass(?) e) def__dot_product_of_two_vectors__(self,var1,var2): '''This functio will carry out the basic dot product of vector operation of var1, var2. If the length of two variable is different, then it will show a message''' if len(var1) != len(var2): return "Sorry, both vectors are of unequal lengths" else: i = 0 result = [] while i <= len(var1): dot= var1[i] * var2[i] result.append(dot) i+=1 return sum.result 여기서 result 라는 리스트를 새로 만들었으나 계산 하는것은 벡터니까, 나중에 sum 이라는 내장된 함수를 쓰는게 좋지 않을까 해서 해 봤습니다. pass f) 여기서 부터가 좀 이해가 잘 안가서 그런데요, 이거는 위 calculation 중 마지막으로 실행 되었던 결과를 불러오는 것 입니다. calculator = Calculator() def get_result_of_last_calculation(self,var1,var2): ''' This functio will return the last value of calculation''' print result 이렇게 하면 되는건가요? 이건 도저희 이해가 잘 안가네요... g) 위 a 에서 했던 마지막 addition 을 return 하는것 입니다. def get_result_of_last_addition(self,var1,var2): '''This functio will return the last value of the functio add_numbers''' return self.add_numbers(result) h) 위 b 에서 했던 마지막 multiplication 을 return 하는 것입니다. def get_result_of_last_multiplication(self,var1,var2): '''This functio will return the last value of the functio multiply_numbers''' return self.multiply_numbers(result) i) 위 c에서 했던 마지막 division 을 return 하는 것입니다. def get_result_of_last_division(self,var1,var2): '''This functio will return the last value of the functio divide_two_numbers''' return self.divide_two_numbers(result) j) 위 e 에서 했던 마지막 dot product of vector 을 return 하는 것입니다. def get_result_of_last_vector_product(self,var1,var2): '''This functio will return the last value of the functio dot_product_of_two_vectors''' return self.dot_product_of_two_vectors(result) 그 다음 문제는, Q. 바이너리 검색과 리니어 검색을 수학적 표현을 이용해서 나타내어라 이건 도저희 모르겠네요..ㅠ_ㅠ 예를들어, 벡터가 있습니다. [1,0,0,0,0,0,3,0,0,0,] 이렇게 하면 파이선 메모리를 너무 잡아 먹는다더고 하더군요. 그래서 dictionary 를 사용해서 표현을 한다고 합니다. {0:1,6:3} 요는, 제가 dictionary를 사용하는 연산 방법을 잘 모른다는게 문제라면 문제일까요? ㅠ_ㅠ Q. 벡터(dictionary) + 벡터(dictionary) 를 하는것입니다. def sparse_add(v1,v2): '''This functio adds the vectors in the form of dictionary and returns a new dictionary which is the sum of the two vectors''' new_vec = {} i1 =0 i2 = 0 while i1 != len(v1) and i2 != len(v2): if v1[i1] <= v2[i2]: new_vec.append(v1[i1]) i1 += 1 else: new_vec.append(v2[i2]) i2 +=2 return new_vec 이렇게 해 보았습니다. 위의 리스트 소팅 하는것을 응용 해야 하는것 같긴 합니다만..ㅠ_ㅠ 여기까지가 제가 생각 할 수 있는 한계이네요.. Q. dot product 를 하는 거네요. 그래서 그 전에 했던거랑 좀 합쳐서, def sparse_dot(v1,v2): '''This functio returns the dot product of vectors in the form of dictionary and returns a new dictionary.''' new_vec = {} i1 =0 i2 = 0 while i1 != len(v1) : v3= v1[i1] * v2[i2] new_vec.append(v3) i1 += 1 and i2+=1 return sum.new_vec 이렇게 하면 되는건가요? 가르쳐 주세요!! 이번에도 dictionary 를 쓰는거군요. 예를들어. {'red':1, 'green':1, 'blue':2} 면 2의 값을 return 해야된다고 합니다. 그래서 제가 생각 해 낸것은, def unique_values(d): seen = {} # dict (value, key) result = set() # keys with unique values for k,v in d.iteritems(): if v in seen: result.discard(seen[v]) else: seen[v] = k result.add(k) return len(result) 이런것도 있고, 자료 좀 찾아보니, def unique(a): from collections import defaultdict count = defaultdict(lambda: 0) for k, v in a.iteritems(): count[v] += 1 for v, c in count.iteritems(): if c <= 1: yield v 이런것도 있더군요... 저는 여기서 람다 가 어떻게 사용되는지 전혀 이해를 못하고 있어요..ㅠ_ㅠ 그래서 람다를 쓰는 방법이 좋은거 같긴 하지만, 혹시 설명 해주실 수 있으신가요??
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호