목록problem solving/코딩 테스트 (16)
코딩항해기

import mathdef solution(arr): return math.lcm(*arr) #최소공배수를 알려주는 lcmarr = [2,6,8,14]print(solution(arr)) lcm는 math 모듈에서 제공되는 함수로 인자로 들어온 정수의 최소공배수를 반환한다.파이썬 3.9 이상에서만 사용 가능한 기능이라 프로그래머스에서는 작동하지 않았다. 반대로 최대공약수를 구할 때는 gcd()가 있다. 배열로 사용할 때는 *연산자를 통해 배열의 요소를 풀어 전달할 수 있으므로, *연산자를 적용했다.

이번에는 파이썬으로 같은 문제를 풀이하고 풀이한 사람들과 코드를 공유하고 리뷰하는 시간을 가지며 문제를 풀이했다. 혼자 풀이할 때보다 같이 코드를 공유하고 개선점을 논의하며 발전해나가는 과정이 더 재밌는 것 같다! :D Pythonimport mathdef solution(n, words): answer = [] #입력받은 키워드만큼 반복 #배령에 해당 키워드가 있거나 바로 직전 단어의 마지막 단어로 시작하지 않는다면 #i나머지n+1은 번호 #i나누기n+1은 차례 #종료 #기록에 키워드를 저장 #전부 실행했는데도 빈 배열이라면 0,0을 저장 #번호 차례 history = [words[0]] #게..

JAVApackage src.test;public class ModeStr {// 문자열 code가 주어집니다.// code를 앞에서부터 읽으면서 만약 문자가 "1"이면 mode를 바꿉니다. mode에 따라 code를 읽어가면서 문자열 ret을 만들어냅니다.// mode는 0과 1이 있으며, idx를 0 부터 code의 길이 - 1 까지 1씩 키워나가면서 code[idx]의 값에 따라 다음과 같이 행동합니다.// mode가 0일 때// code[idx]가 "1"이 아니면 idx가 짝수일 때만 ret의 맨 뒤에 code[idx]를 추가합니다.// code[idx]가 "1"이면 mode를 0에서 1로 바꿉니다.// mode가 1일 때// code[idx]가 "1"이 아니면 idx가 홀수일 때만 ret의 맨 ..

JAVApackage test;import java.util.Arrays;public class Desc { public static void main(String[] args) { int num = 3; int total = 12; System.out.println("3,4,5 : "+ Arrays.toString(solution(num, total))); } public static int[] solution(int num, int total) { //등차수열을 활용해 시작번호 구하기 //(total - num*(num-1)/2) / num //num만큼 반복해 시작 번호를 배열에 저장 //배열 반..

JAVApackage test;public class Login { /*머쓱이는 프로그래머스에 로그인하려고 합니다. 머쓱이가 입력한 아이디와 패스워드가 담긴 배열 id_pw와 회원들의 정보가 담긴 2차원 배열 db가 주어질 때, 다음과 같이 로그인 성공, 실패에 따른 메시지를 return하도록 solution 함수를 완성해주세요.*/ public static void main(String[] args) { //확인용 String[] id = {"test", "123"}; String[] id2 = {"rardss", "123"}; String[] id3 = {"rardss", "1234"}; String[][] db = ..

문제를 JAVA와 Python 두 가지로 풀어보며 두 언어의 차이를 알며 Python 공부 진행 1. JAVApackage test;public class AvgArray { public static void main(String[] args) { //확인용 int[] arr = {89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99}; System.out.println(solution(arr)); } public static double solution(int[] arr) { double answer = 0; //입력받은 배열의 값을 모두 sum 변수에 저장 double sum = 0; ..

쇠 막대기(초등 3)(중등 2) 여러 개의 쇠막대기를 레이저로 절단하려고 한다. 효율적인 작업을 위해서 쇠막대기를 아래에서 위 로 겹쳐 놓고, 레이저를 위에서 수직으로 발사하 여 쇠막대기들을 자른다. 쇠codeup.kr import java.util.ArrayList;import java.util.Scanner;public class test3 { public static void main(String[] args) { //(는 층을 쌓기 때문에 스택을 올린다 //()는 레이저이므로 스택 쌓은만큼 수가 추가된다 //)끝나는 것이므로 스택을 하나 내리면서 수를 1추가한다 //String[] arr = {"(",")","(","(","(","(","..

1. 빈칸채우기import java.util.Scanner;public class Solution { public static void main(String[] args) { String message = "Let's go!"; System.out.println("3\n2\n1"); System.out.println(message); }} 2. 디버깅문제public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int angle1 = sc.nextInt(); int angle2 = sc.ne..

코딩테스트 연습 > 코딩 기초 트레이닝 > [PCCE 기출문제] 1번 ~ 8번 [문제 1번 : 출력]빈칸 채우기 문제 안내 빈칸 채우기는 이미 완성된 코드 중 빈칸에 알맞은 코드를 입력하는 문제 타입입니다. 빈칸을 제외한 기본 코드는 수정할 수 없습니다. 빈칸을 채우지 않을 경우, 실행 결과에 에러 메시지가 표시됩니다. import java.util.Scanner;public class Solution { public static void main(String[] args) { String msg = ; int val1 = ; String val2 = ; System.out.println(msg); System.out.println(va..

코딩테스트 연습 > 2024 KAKAO WINTER INTERNSHIP >가장 많이 받은 선물 [문제 설명]선물을 직접 전하기 힘들 때 카카오톡 선물하기 기능을 이용해 축하 선물을 보낼 수 있습니다. 당신의 친구들이 이번 달까지 선물을 주고받은 기록을 바탕으로 다음 달에 누가 선물을 많이 받을지 예측하려고 합니다. 두 사람이 선물을 주고받은 기록이 있다면, 이번 달까지 두 사람 사이에 더 많은 선물을 준 사람이 다음 달에 선물을 하나 받습니다. 예를 들어 A가 B에게 선물을 5번 줬고, B가 A에게 선물을 3번 줬다면 다음 달엔 A가 B에게 선물을 하나 받습니다. 두 사람이 선물을 주고받은 기록이 하나도 없거나 주고받은 수가 같다면, 선물 지수가 더 큰 사람이 선물 지수가 더 작은 사람에게 선물을 하나..