Python 108

[python]Complementary DNA

www.codewars.com/kata/554e4a2f232cdd87d9000038/train/python Codewars: Achieve mastery through challenge Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential. www.codewars.com 이 문제는 지정된 문자를 바꾸는 것입니다. A -> T T -> A C -> G G -> C 저는 이 문제를 첫 번째는 for문과 if문을 이용하였고 두 번째는 dictionary를 이용해였습니다. 1. for문과 if문 사용 def DNA_strand(dna): ..

[python]Take a Ten Minute Walk

www.codewars.com/kata/54da539698b8a2ad76000228/train/python Codewars: Achieve mastery through challenge Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential. www.codewars.com 이 문제를 간단하게 설명하면 동서남북으로 10걸음 갔을 때 제자리로 돌아왔는지 확인하는 것입니다. 입력은 string 리스트 형식입니다. e : 동쪽 w : 서쪽 n : 북쪽 s : 남쪽 이 문제를 풀기 위해서는 2가지 조건을 만족하면 됩니다. 1. 배열의 길이가 ..

[python]List Filtering

www.codewars.com/kata/53dbd5315a3c69eed20002dd/train/python Codewars: Achieve mastery through challenge Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential. www.codewars.com 이 문제는 입력받은 배열 중 string 타입을 제외하는 문제입니다. 이 변수 타입을 구별하기 위해서 여러 가지 방법이 있지만 대표적으로 isinstance와 type이 있습니다. (사실 지금 이 두 가지밖에 생각이 안 나네요... 더 좋은 방법 있으시면 댓글로 남..

[프로그래머스/Python]풍선 터트리기

programmers.co.kr/learn/courses/30/lessons/68646?language=python3 코딩테스트 연습 - 풍선 터트리기 [-16,27,65,-2,58,-92,-71,-68,-61,-33] 6 programmers.co.kr 이 문제는 [월간 코드 챌린지 시즌1]의 9월 문제였습니다. 테스트 때는 풀지 못했었지만 프로그래머스에서 올려주신 풀이를 보고 문제를 풀었습니다. 프로그래머스에서 올려주신 풀이는 아래의 링크를 클릭하시면 보러 가실 수 있습니다. prgms.tistory.com/29 [월간 코드 챌린지 시즌1] 9월 문제 해설 코딩이 재미있는 사람들을 위한 챌린지! 프로그래머스에서 9월 10일부터 11월 5일까지 월간 코드 챌린지 시즌1이 진행되고 있습니다. 2020년 ..

[LeetCode/Python]Array and String - Longest Common Prefix

문제 Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". 문자열 배열 중에서 가장 긴 공통 접두사 문자열을 찾아서 반환합니다. 공통 접두사가 없는 경우는 빈 문자열("")을 반환합니다. Input : ["flower", "flow", "flight"] Output : "fl" 이번 문제는 효율성도 생각하지 못할 만큼 문제를 푸는데 급급했던 문제였습니다. 제 코드는 참고만 하시고 다르게 풀어보시는 것을 권유드립니다. 제 코드를 보시려면 더보기를 클릭하시면 됩니다. 더보기 파이썬 코드 class Solut..

[LeetCode/Python]Array and String - Implement strStr()

문제 Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. haystack에서 needle이 처음 발견된 인덱스를 반환하거나 인덱스가 발견되지 않는다면 -1을 반환합니다. Input : haystack = "hello", needle = "ll" Output : 2 이 문제도 저는 알고리즘으로 풀었다기보다 기존 함수를 이용했습니다. 참고만 하시길 바랍니다. 추후에 알고리즘으로 푼다면 추가하도록 하겠습니다. 제 코드를 보시려면 더보기를 클릭하시면 됩니다. 더보기 파이썬 코드 find를 이용하여 간단하게 풀었습니다. class Solution: def strStr(self, ..

[LeetCode/Python]Array and String - Add Binary

문제 Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. 2진수 a와 b를 입력받아서 더한 값을 출력하는 문제입니다. 제 코드는 완벽한 답이 아닙니다. 그래서 이번에는 다른 사람의 풀이도 같이 첨부했습니다. 참고만 하시길 바랍니다. 제 코드를 보시려면 더보기를 클릭하시면 됩니다. 더보기 파이썬 코드 나의 풀이 class Solution: def addBinary(self, a: str, b: str) -> str: return bin(int(a, 2)+int(b, 2))[2:] 다른 사람의 풀이 class ..

[LeetCode/Python]Array and String - Pascal's Triangle

문제 Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. 입력받은 수만큼 파스칼 삼각형의 수들을 배열에 넣어서 출력한다. 파스칼 삼각형 https://ko.wikipedia.org/wiki/%ED%8C%8C%EC%8A%A4%EC%B9%BC%EC%9D%98_%EC%82%BC%EA%B0%81%ED%98%95 파스칼의 삼각형 - 위키백과, 우리 모두의 백과사전 위키백과, 우리 모두의 백과사전. 둘러보기로 가기 검색하러 가기 파스칼의 삼각형(Pascal's triangle)은 수학에서 이항계수를 삼각형 모양의 기하학적 형태로 배열한 것이다. 이것은 블레즈 파스칼� ko.wikipedia.org 예) Input..

[LeetCode/Python]Array and String - Spiral Matrix

문제 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 입력받은 배열을 다음과 같이 배열로 만들어서 출력합니다. 코드를 보시려면 더보기를 클릭해 주세요. 더보기 파이썬 코드 class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: tmp = matrix.copy() answer = [] while len(tmp) != 0: answer.extend(tmp[0]) tmp.remove(tmp[0]) tmp = list(map(list, zip(*tmp)))[::-1] return answer ..

[LeetCode/Python/Java]Array and String - Diagonal Traverse

문제 Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image. 2차원 배열을 입력받았을 때 아래와 같은 순서로 배열을 출력한다. 코드를 보시려면 더보기를 클릭해 주세요. 더보기 파이썬 코드 class Solution: def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]: if len(matrix) == 0: return [] N, M = len(matrix), len(matrix[0]) result, inter = [], [] for d in range(N ..

728x90