목록파이썬 (115)
규도자 개발 블로그
파이썬3.x[python3.x]에서 요소가 하나인 튜플을 문자열이 아닌 튜플로 인식시키기 i_wanna_be_tuple = ('really') print(i_wanna_be_tuple) print(type(i_wanna_be_tuple)) u_need_to_do_like_this = ('like this',) print(u_need_to_do_like_this) print(type(u_need_to_do_like_this)) 어제 부딪힌 문제였다. django로 작업을 하던 와중 django form을 이용해 입력란을 만들고 있었는데 modelform은 상속받은 model을 기반으로 해서 입력 form을 작성해준다. 하필 입력받을 form 요소가 딱 하나였는데 이를 tuple형태로 정의해야하는 지라 단순..
[백준/1978/파이썬(python3)] 소수 찾기 문제 주어진 수 N개 중에서 소수가 몇 개인지 찾아서 출력하는 프로그램을 작성하시오. 입력 첫 줄에 수의 개수 N이 주어진다. N은 100이하이다. 다음으로 N개의 수가 주어지는데 수는 1,000 이하의 자연수이다. 출력 주어진 수들 중 소수의 개수를 출력한다. 입출력 예 입력출력 4 1 3 5 73 풀이 def prime_list(n): n = n + 1 #추가된 부분 sieve = [True] * n m = int(n ** 0.5) for i in range(2, m + 1): if sieve[i]: for j in range(i+i, n, i): sieve[j] = False return [i for i in range(2, n) if sieve[..
파이썬(Python) 내장함수의 시간복잡도 나는 내장함수는 뭔가 당연히 최적화돼있겠거니 하고 생각없이 쓰곤 한다. 하지만 그런 자세가 프로그램의 효율에 커다란 영향을 끼치지 않나 싶다. 특히 이번에 에라토스테네스의 체를 응용한 문제를 풀고 있는데 정확한 개념을 알기 위해 위키에 쳐보니 다음과 같은 함수가 나왔다. def prime_list(n): # 에라토스테네스의 체 초기화: n개 요소에 True 설정(소수로 간주) sieve = [True] * n # n의 최대 약수가 sqrt(n) 이하이므로 i=sqrt(n)까지 검사 m = int(n ** 0.5) for i in range(2, m + 1): if sieve[i] == True: # i가 소수인 경우 for j in range(i+i, n, i)..
Objective Today, we're building on our knowledge of Arrays by adding another dimension. Check out the Tutorial tab for learning materials and an instructional video!Context Given a 2D Array, :1 1 1 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 We define an hourglass in to be a subset of values with indices falling in this pattern in 's graphical representation:a b c d e f g T..
[백준/9012/파이썬(python3)] 괄호 문제 괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고 부른다. 한 쌍의 괄호 기호로 된 “( )” 문자열은 기본 VPS 이라고 부른다. 만일 x 가 VPS 라면 이것을 하나의 괄호에 넣은 새로운 문자열 “(x)”도 VPS 가 된다. 그리고 두 VPS x 와 y를 접합(concatenation)시킨 새로운 문자열 xy도 VPS 가 된다. 예를 들어 “(())()”와 “((()))” 는 VPS 이지만 “(()(”, “(())()))” , 그리고 “(()” 는 모두 VPS 가 아닌 문..
Sam's house has an apple tree and an orange tree that yield an abundance of fruit. In the diagram below, the red region denotes his house, where is the start point, and is the endpoint. The apple tree is to the left of his house, and the orange tree is to its right. You can assume the trees are located on a single point, where the apple tree is at point , and the orange tree is at point .When a ..
[백준/10950/파이썬] A+B - 3 문제 두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오. 입력 첫째 줄에 테스트 케이스의 개수 T가 주어진다.각 테스트 케이스는 한 줄로 이루어져 있으며, 각 줄에 A와 B가 주어진다. (0 < A, B < 10) 출력 각 테스트 케이스마다 A+B를 출력한다. 입출력 예 입력출력 5 1 1 2 3 3 4 9 8 5 22 5 7 17 7 풀이 count = int(input()) result_list = [] for i in range(count): number_input = input().split() number_input = map(int, number_input) result_list.append(sum(number_input)) f..
You are in charge of the cake for your niece's birthday and have decided the cake will have one candle for each year of her total age. When she blows out the candles, she’ll only be able to blow out the tallest ones. Your task is to find out how many candles she can successfully blow out.For example, if your niece is turning years old, and the cake will have candles of height , , , , she will be..
Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.For example, . Our minimum sum is and our maximum sum is . We would print16 24 Function DescriptionComplete the miniMaxSum function in the editor below. It ..
Consider a staircase of size : # ## ### #### Observe that its base and height are both equal to , and the image is drawn using # symbols and spaces. The last line is not preceded by any spaces.Write a program that prints a staircase of size .Function DescriptionComplete the staircase function in the editor below. It should print a staircase as described above.staircase has the following paramete..