프로그래밍 27

Leetcode 2114 풀이(파이썬, 스위프트)

단어의 개수가 가장 많은 인덱스를 고르는 문제이다 for문 연습하기 좋은 문제 스위프트 class Solution { func mostWordsFound(_ sentences: [String]) -> Int { var x = 0 for i in sentences{ x = max(x, numberOfWords(i)) } return x } func numberOfWords(_ s: String) -> Int { var w = 1 for i in s where i == " "{ w+=1 } return w } } 파이썬 class Solution: def mostWordsFound(self, sentences: List[str]) -> int: x=0 for i in range(len(sentences)):..

리트코드 풀이 2022.02.04

Leetcode 2011 풀이(파이썬, 스위프트)

++가 들어있으면 a라는 변수를 만들어서 +=1 해주고 아니면 -=1 해주는 단순한 문제 스위프트: class Solution { func finalValueAfterOperations(_ operations: [String]) -> Int { var a = 0 for i in operations{ if i.contains("++"){ a+=1 } else{ a-=1 } } return a } } 파이썬: class Solution: def finalValueAfterOperations(self, operations: List[str]) -> int: x=0 for i in range(len(operations)): if "++" in operations[i]: x+=1 else: x-=1 return x

리트코드 풀이 2022.02.04

Leetcode 1480 풀이(파이썬, 스위프트)

어떤 인덱스에 대하여 0부터 그 인덱스까지의 모든 배열인덱스의 값들의 합을 인덱스에 저장하는 것이다. 스위프트 class Solution { func runningSum(_ nums: [Int]) -> [Int] { var n=nums for i in 0...(n.count-2){ n[i+1] = n[i]+n[i+1] } return n } } 파이썬 class Solution: def runningSum(self, nums: List[int]) -> List[int]: for i in range(len(nums)): if i ==0: continue nums[i]=nums[i]+ nums[i-1] return nums

리트코드 풀이 2022.02.03

스위프트 배열과 enumerated()

스위프트 배열은 어떻게 선언 할까? let emptyArray: [Int] = [] let emptyArray2 = Array() let emptyArray3 = [Int]() 이렇게 3가지가 가능하다. 빈배열을 만들때는 반드시 Type을 명시하도록 하자. 크기가 정해진 배열 2가지로 선언이 가능하다. let zeroArray1 = [Int](repeating: 0, count: 10) // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] let zeroArray2 = Array(repeating: 0, count: 10) // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 1~12까지 숫자가 든 배열 let numbers: [Int] = Array(1...12) 배열에 들어있는 값의 개..

swift 2022.02.03

리트코드 1119 풀이(파이썬, 스위프트)

파이썬으로는 그냥 전부다 탐색하면서 해당되는 vowels를 배열에 담고 담긴 애들을 주어진 s에서 지워주면 되는데 스위프트는 removeAll이라는 새로운 메소드를 써서 까다롭지만 배울게 많았다. (contains랑 $0도) 스위프트 코드 class Solution { func removeVowels(_ s: String) -> String { var a = s let vowels : Set = ["a", "e", "i", "o", "u"] a.removeAll(where: {vowels.contains($0)}) return a } } 파이썬 코드 class Solution: def removeVowels(self, s: str) -> str: arr=[] for i in range(len(s)): i..

리트코드 풀이 2022.02.03

Leetcode 1929 풀이(파이썬, 스위프트)

단순히 배열을 두배로 늘려주는 문제. 지금보니 젤 쉬운문제.. 파이썬 class Solution: def getConcatenation(self, nums: List[int]) -> List[int]: return nums+nums #nums*2 스위프트 class Solution { func getConcatenation(_ nums: [Int]) -> [Int] { var a = nums //let으로 선언된 nums는 수정불가 하기 때문에 a라는 배열에 담음 return a+a //a*2는 스위프트에서 지원 안함 } }

리트코드 풀이 2022.02.03

Leetcode 1920번 풀이(파이썬, swift)

인덱스의 값을 배열에 한번더 참조한 값을 정답 배열에 집어넣으면 된다 젤 쉬운문제 파이썬 class Solution: def buildArray(self, nums: List[int]) -> List[int]: ans=[] for i in range(len(nums)): ans.append(nums[nums[i]]) return ans swift class Solution { func buildArray(_ nums: [Int]) -> [Int] { var a = [Int]() for (i,v) in nums.enumerated(){ a.append(nums[nums[nums.index(nums.startIndex, offsetBy:i)]]) } return a } }

리트코드 풀이 2022.02.03

swift String.Index

파이썬이나 여타 언어들에서는 손쉽게 int로 배열의 인덱스에 접근할 수 있지만 swift의 경우 String.Index 타입을 이용해야한다. 바로 예를 들겠다. a = "abc" print(a[0]) # a 파이썬에서는 이는 너무 당연하다. 하지만 스위프트의 경우 var a = "abc" print(a[a.startIndex]) // a 위의 파이썬 코드와 동일한 코드이다. startIndex와 endIndex는 처음과 끝 인덱스를 의미하지만 endIndex는 파이썬으로 치면 len(a)와 같아서 a라는 문자열이 있다고 치면 a의 길이와 같다. 즉 인덱스 측면에서 봤을때는 1을 빼줘야한다. 코드로 설명하면 var endIndex = a.index(before: a.endIndex) var last = a..

swift 2022.02.03

백준 2292 풀이

def sol(a): return 0 빈 함수를 만들고 import unittest class TestNotebook(unittest.TestCase): def test_sol(self): self.assertEqual(sol(13), 3) unittest.main(argv=[''], verbosity=2, exit=False) 테스트 먼저 진행한다. 당연히 fail한다. def sol(a): x=1 cnt=1 while a>x: x+=6*cnt cnt+=1 return cnt 테스트 케이스를 이용해서 문제를 풀어 보았다. 벌집의 개수는 한 칸이 멀어질수록 6n을 1~n까지 더한 개수이다. 즉 처음 벌집까지 포함해야함으로 cnt를 1로 두고 1씩증가해가면서 6*cnt를 x라는 변수에 더해간다. x가 a보..

백준 풀이 2022.02.02