본문 바로가기

전체 글

(79)
순열과 관련된 것 permutation - [1, 2, 3] = [3, 2, 1] 아니면 "abc" = "cab" 그냥 간단하게 파이썬 함수로 작성하면 아래 처럼 된다. def permut(arg1, arg2): if len(arg1) == 0 or len(arg2) == 0: return print("글자가 0이면 안됩니다.") elif(len(arg1) != len(arg2)): return print("글자나 숫자는 서로 길이가 동일해야 합니다.") else: listA = list(arg1) listB = list(arg2) listA.sort() listB.sort() if listA == listB: return True else: return False 다른 방법 def permut(arg1, arg2): if len(arg1) == 0 or len(arg2) == 0: return print("글자가 ..
딕셔너리의 오퍼레이션 여러 가지의 오퍼레이션이 있다. 오퍼레이션들: in, all(), any(), sorted() 예시] #in my_dict = {1:'a', 2:'b', 3:'c'} (2 in my_dict) ---> True 를 반환한다. in을 딕셔너리에 쓰면 Boolean값으로 키 값이 있나 확인 한다. ('a' in my_dict.values) ---> True를 반환한다. 여기서는 dict.values라서 즉 값이 있냐 확인 하는 거니까 True가 반환된다. #all() my_dict = {1:'a', 2:'b', 3:'c'} print(all(my_dict)) --> 여기서는 True로 반환된다. 키 값 중에 0이나 False 키가 없기 때문이다. 하나라도 0이나 False가 있다면 False로 반환된다. A..
파워쉘의 For문과 ForEach문 파워쉘에도 일반 프로그래밍 언어 처럼 For문과 ForEach문이 있다. 아래 처럼 script로 써서 쓸 수가 있으며 Powershell ISE에서 작성 하면 이렇게 작성 할 수가 있다. #For문 $Vehicles = @("Hyundai", "Toyota", "BMW", "Jaguar") for($i = 0; $i -lt $Vehicles.Count; $i++){ echo ("Element $i : "+$Vehicles[$i] } => Element 0 Hyundai => Element 1 Toyota => Element 2 BMW => Element 3 Jaguar ---> 위의 결과가 나온다. for 문 써서 스트링 안에 $배열[$인덱스]를 쓰면 안되고 따로 빼야 한다. 그러나 변수 $변수는 스트..
배열이나 리스트에 중복값이 있으면 True로 변환 하는 코드풀이 def contains_duplicate(in_list): my_dict = {} for i in range(len(in_list)): my_dict[in_list[i]] = 0 for i in range(len(in_list)): my_dict[in_list[i]] = my_dict[in_list[i] +1 for key in my_dict: if my_dict[key] > 1: return true
파이썬의 리스트의 요소 두 개 합들의 짝들을 찾기 def pair_sum(arr, sum): result = [] for i in range(len(arr)): for j in range(i+1, len(arr)): if arr[i]+arr[j] == sum: result.append(f"{arr[i]}+{arr[j]}) return result
dictionary 메서드 #fromkeys() 메서드 -> 새로운 딕셔너리를 만들때 사용하며 키값들을 원하는 것으로 정할 수 있다. -> 구조 dictionary.fromkeys([키값들의 리스트], 옵션: 각 키값들의 값) 예시] my_dict = {'name':'Richard', 'age':34, 'city':'London'} new_dict = {}.fromkeys([1,2,3], 0) => 이러면 new_dict 에서는 {'1': 0, '2':0, '3':0} 이런 딕셔너리 형태가 만들어진다. #get() 메서드 -> 키값의 value값을 가지고 오는 메서드이다. -> 구조 dictionary.get('키값', 옵션: 왼쪽의 키값이 없으면 여기 지정된 값이 반환된다.) 예시] my_dict = dict(name='Marc..
리스트에서 중복 요소 제거 문제 풀이 def remove_duplicate(arr): | unique_list = [] | seen = set() | | for item in arr: 시간복잡도-->O(N) | | if item not in seen: | | | unique_list.append(item) | | | seen.add(item) | | return unique_list
파워쉘의 while문 scripting #1 while($userInput -ne "xyz"){ $userInput = Read-Host -Prompt "type a word : " } $userInput = $null ----> 이거 쓰는 이유가 전에 스크립팅을 작성하다가 $userInput 변수 값이 "xyz"가 들어와 있으면 아무리 강제로 null로 제시하지 않으면 while문이 아예 안돈다. #2 while($userInput -ne "quit"){ $userInput = Read-Host -Prompt "Say 'yes' if you like scripting (enter 'quit' to stop the loop)" if($userInput -eq "yes") {echo "I also love scipting! That is coo..