본문 바로가기

분류 전체보기

[Codility] Lesson 5 : GenomicRangeQuery - JAVA 문제 A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A, C, G and T have impact factors of 1, 2, 3 and 4, respectively. You are going to answer several queries of the form: What is the minimal impact factor of.. 더보기
[Codility] Lesson 5 : CountDiv - JAVA 문제 Write a function: · class Solution { public int solution(int A, int B, int K); } that, given three integers A, B and K, returns the number of integers within the range [A..B] that are divisible by K, i.e.: · { i : A ≤ i ≤ B, i mod K = 0 } For example, for A = 6, B = 11 and K = 2, your function should return 3, because there are three numbers divisible by 2 within the range [6..11], namely 6, .. 더보기
[Codility] Lesson 5 : PassingCars - JAVA 문제 A non-empty array A consisting of N integers is given. The consecutive elements of array A represent consecutive cars on a road. Array A contains only 0s and/or 1s: · 0 represents a car traveling east, · 1 represents a car traveling west. The goal is to count passing cars. We say that a pair of cars (P, Q), where 0 ≤ P < Q < N, is passing when P is traveling to the east and Q is traveling to .. 더보기
[Codility] Lesson 4 : MissingInteger - JAVA 문제 This is a demo task. Write a function: class Solution { public int solution(int[] A); } that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A. For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5. Given A = [1, 2, 3], the function should return 4. Given A = [−1, −3], the function should return 1. Write an effic.. 더보기
[Python] 리스트 요소 정렬하기, 뒤집기 - sort(), reverse() 리스트 정렬하기 - sort() 파이썬은 리스트를 정렬하기 위해 sort() 함수를 지원해줘요. my_list를 정렬해볼까요? 오름차순과 내림차순 모두 작성해볼게요. my_list = [4, 2, 3, 5, 1] # 오름차순 : [1, 2, 3, 4, 5] my_list.sort() print(my_list) >> (출력) [1, 2, 3, 4, 5] # 내림차순 : [5, 4, 3, 2, 1] my_list.sort(reverse=True) print(my_list) >> (출력) [5, 4, 3, 2, 1] 그냥 sort()함수만 이용하면 오름차순으로 정렬해줘요. 그런데 내림차순으로 하려면? sort()함수에 reverse=True라는 파라미터를 전달해주면 돼요! 역순으로 바꿔달라는 의미의 파라미터에.. 더보기
[Python] 리스트 확장하기 - extend() 리스트를 사용하다 보면 데이터를 더 집어넣어야 될 때가 있어요. 그럴 때는 어떻게 할까요? 새로운 리스트를 만들어서 데이터를 추가해도 돼요. 하지만 파이썬이잖아요. 리스트를 확장할 수 있도록 기능을 지원해줘요. 그 역할을 하는게 바로 extend()! extend()는 기존 리스트에 새로운 요소들을 추가해서 확장해줘요. 1부터 5까지 담긴 리스트를 확장해 6, 7, 8, 9를 담아볼까요? my_list = [1, 2, 3, 4, 5] my_list.extend([6, 7, 8, 9]) print(my_list) >> (출력) [1, 2, 3, 4, 5, 6, 7, 8, 9] my_sub_list = [10, 11] my_list.extend(my_sub_list) print(my_list) >> (출력).. 더보기
[Python] 리스트에서 요소 개수 세기 - count() 이번에는 찾으려고 하는 데이터가 리스트에 몇 개나 저장되어 있는지 확인해볼게요. 처음에 count니까 리스트 안에 요소가 몇 개 있는지 확인하는건가 했어요. 하지만 그 기능은 이미 len()이 하고있죠? count()는 리스트 안에 a라는 데이터가 몇개 들어있는지 확인하는 함수에요. 소스코드를 보면 금방 이해될테니 한 번 작성해 볼게요. my_list = [1, 2, 1, 3, 5, 3, 2, 4, 1] # my_list에 숫자 1은 몇 개가 있을까요? print(my_list.count(1)) >> (출력) 3 # my_list에 숫자 5는 몇 개가 있을까요? print(my_list.count(5)) >> (출력) 1 쉽게 이해가 되죠? my_list에는 숫자 1이 3개가 있어요. count()가 바.. 더보기
[Python] 리스트에 요소가 있는지 확인하기 - in, not in 이번에는 리스트에 데이터가 있는지 확인해볼게요. my_list = [1, 2, 3, 4, 5] my_list에 숫자 3이 있는지 확인하고 싶은데 어떻게 해야할까요? 반복문을 이용해서 my_list에 숫자 3이 있는지 하나씩 확인해야 할까요? 파이썬에서는 in이라는 기능를 이용해서 데이터가 있는지 쉽게 확인할 수 있어요. 그럼 어떻게 사용하는지 알아볼까요? is_exist_1 = 3 in my_list is_exist_2 = 7 in my_list print(is_exist_1) >> (출력) True print(is_exist_2) >> (출력) False not_exist = 7 not in my_list print(not_exist) >> (출력) True 먼저 숫자 3이 my_list에 있는지 확인.. 더보기