https://school.programmers.co.kr/learn/courses/30/lessons/77484
— 문제 설명
로또 6/45
(이하 '로또'로 표기)는 1부터 45까지의 숫자 중 6개를 찍어서 맞히는 대표적인 복권입니다. 아래는 로또의 순위를 정하는 방식입니다. 1
순위 | 당첨 내용 |
---|---|
1 | 6개 번호가 모두 일치 |
2 | 5개 번호가 일치 |
3 | 4개 번호가 일치 |
4 | 3개 번호가 일치 |
5 | 2개 번호가 일치 |
6(낙첨) | 그 외 |
로또를 구매한 민우는 당첨 번호 발표일을 학수고대하고 있었습니다. 하지만, 민우의 동생이 로또에 낙서를 하여, 일부 번호를 알아볼 수 없게 되었습니다. 당첨 번호 발표 후, 민우는 자신이 구매했던 로또로 당첨이 가능했던 최고 순위와 최저 순위를 알아보고 싶어 졌습니다.
알아볼 수 없는 번호를 0
으로 표기하기로 하고, 민우가 구매한 로또 번호 6개가 44, 1, 0, 0, 31 25
라고 가정해보겠습니다. 당첨 번호 6개가 31, 10, 45, 1, 6, 19
라면, 당첨 가능한 최고 순위와 최저 순위의 한 예는 아래와 같습니다.
당첨 번호 | 31 | 10 | 45 | 1 | 6 | 19 | 결과 |
---|---|---|---|---|---|---|---|
최고 순위 번호 | 31 | 0→10 | 44 | 1 | 0→6 | 25 | 4개 번호 일치, 3등 |
최저 순위 번호 | 31 | 0→11 | 44 | 1 | 0→7 | 25 | 2개 번호 일치, 5등 |
민우가 구매한 로또 번호를 담은 배열 lottos, 당첨 번호를 담은 배열 win_nums가 매개변수로 주어집니다. 이때, 당첨 가능한 최고 순위와 최저 순위를 차례대로 배열에 담아서 return 하도록 solution 함수를 완성해주세요.
— 제한 조건
— 입출력 예
lottos | win_nums | result |
---|---|---|
[44, 1, 0, 0, 31, 25] | [31, 10, 45, 1, 6, 19] | [3, 5] |
[0, 0, 0, 0, 0, 0] | [38, 19, 20, 40, 15, 25] | [1, 6] |
[45, 4, 35, 20, 3, 9] | [20, 9, 3, 45, 4, 35] | [1, 1] |
입출력 예 #1
문제 예시와 같습니다.
입출력 예 #2
알아볼 수 없는 번호들이 아래와 같았다면, 1등과 6등에 당첨될 수 있습니다.
당첨 번호 | 38 | 19 | 20 | 40 | 15 | 25 | 결과 |
---|---|---|---|---|---|---|---|
최고 순위 번호 | 0→38 | 0→19 | 0→20 | 0→40 | 0→15 | 0→25 | 6개 번호 일치, 1등 |
최저 순위 번호 | 0→21 | 0→22 | 0→23 | 0→24 | 0→26 | 0→27 | 0개 번호 일치, 6등 |
입출력 예 #3
민우가 구매한 로또의 번호와 당첨 번호가 모두 일치하므로, 최고 순위와 최저 순위는 모두 1등입니다.
※ 실제로 사용되는 로또 순위의 결정 방식과는 약간 다르지만, 이 문제에서는 지문에 명시된 대로 로또 순위를 결정하도록 합니다.
— 문제 풀이
class Solution {
public int[] solution(int[] lottos, int[] win_nums) {
int winCnt = 0; // 맞는 숫자 카운트
int zCnt = 0; // 알아볼 수 없는 수 카운트
for(int i=0;i<6;i++){
if(lottos[i]==0){
zCnt++;
continue;
}
for(int j=0;j<6;j++){
if(lottos[i]==win_nums[j]){
winCnt++;
break;
}
}
}
int[] answer = new int[2];
answer[0] = checkLotto(winCnt+zCnt);
answer[1] = checkLotto(winCnt);
return answer;
}
public int checkLotto(int num){
switch(num){
case 6:
return 1;
case 5:
return 2;
case 4:
return 3;
case 3:
return 4;
case 2:
return 5;
default:
return 6;
}
}
}
https://school.programmers.co.kr/learn/courses/30/lessons/133499
— 문제 설명
머쓱이는 태어난 지 11개월 된 조카를 돌보고 있습니다. 조카는 아직 "aya", "ye", "woo", "ma" 네 가지 발음과 네 가지 발음을 조합해서 만들 수 있는 발음밖에 하지 못하고 연속해서 같은 발음을 하는 것을 어려워합니다. 문자열 배열 babbling
이 매개변수로 주어질 때, 머쓱이의 조카가 발음할 수 있는 단어의 개수를 return하도록 solution 함수를 완성해주세요.
— 제한 조건
babbling
의 길이 ≤ 100babbling[i]
의 길이 ≤ 30— 입출력 예
babbling | result |
---|---|
["aya", "yee", "u", "maa"] | 1 |
["ayaye", "uuu", "yeye", "yemawoo", "ayaayaa"] | 2 |
입출력 예 #1
입출력 예 #2
※ 네 가지를 붙여 만들 수 있는 발음 이외에는 어떤 발음도 할 수 없는 것으로 규정합니다. 예를 들어 "woowo"는 "woo"는 발음할 수 있지만 "wo"를 발음할 수 없기 때문에 할 수 없는 발음입니다.
— 문제 풀이
class Solution {
public int solution(String[] babbling) {
String[] pronun = {"aya", "ye", "woo", "ma"};
String[] wrong = {"ayaaya", "yeye", "woowoo", "mama"};
int answer = 0;
for(int i=0;i<babbling.length;i++){
boolean flag = false;
for(int j=0;j<4;j++){
if(babbling[i].contains(wrong[j])) flag = true;
}
if(flag)continue;
for(int j=0;j<4;j++){
babbling[i] = babbling[i].replaceAll(pronun[j]," ");
}
babbling[i] = babbling[i].replaceAll(" ", "");
if(babbling[i].length()==0) answer++;
}
return answer;
}
}
Market 서비스를 만들어보는 실습
Order(Producer) - Product(Consumer) 2개, Payment(Consumer) 1개
RabbitMQ 설치 ( 컨테이너 )
docker run -d --name rabbitmq -p5672:5672 -p 15672:15672 --restart=unless-stopped rabbitmq:management
Producer 프로젝트 생성(Order)
spring:
application:
name: order
rabbitmq:
host: localhost
port: 5672
stream:
username: guest
message:
exchange: market
queue:
product : market.product
payment : market.payment
@Configuration
public class OrderApplicationQueueConfig {
@Value("${message.exchange}")
private String exchange;
@Value("${message.queue.product}")
private String queueProduct;
@Value("${message.queue.payment}")
private String queuePayment;
@Bean public TopicExchange exchange() { return new TopicExchange(exchange); }
@Bean public Queue queueProduct() { return new Queue(queueProduct); }
@Bean public Queue queuePayment() { return new Queue(queuePayment); }
@Bean public Binding bindingProduct() { return BindingBuilder.bind(queueProduct()).to(exchange()).with(queueProduct); }
@Bean public Binding bindingPayment() { return BindingBuilder.bind(queuePayment()).to(exchange()).with(queuePayment); }
}
@RestController
@RequiredArgsConstructor
public class OrderController {
private final OrderService orderService;
@GetMapping("/order/{id}")
public String order(@PathVariable String id) {
orderService.createOrder(id);
return "Order complete";
}
}
@Service
@RequiredArgsConstructor
public class OrderService {
@Value("${message.queue.product}")
private String productQueue;
@Value("${message.queue.payment}")
private String paymentQueue;
private final RabbitTemplate rabbitTemplate;
public void createOrder(String orderId) {
rabbitTemplate.convertAndSend(productQueue, orderId);
rabbitTemplate.convertAndSend(paymentQueue, orderId);
}
}
Producer 실행 후 확인
Consumer 프로젝트 생성
spring:
application:
name: payment
rabbitmq:
host: localhost
port: 5672
stream:
username: guest
password: guest
message:
queue:
payment : market.payment
@Slf4j
@Component
public class PaymentEndpoint {
@Value("${spring.application.name}")
private String appName;
@RabbitListener(queues = "${message.queue.payment}")
public void receiveMessage(String orderId) {
log.info("receive orderId:{}, appName : {}", orderId, appName);
}
}
spring:
application:
name: product
rabbitmq:
host: localhost
port: 5672
stream:
username: guest
password: guest
message:
queue:
product : market.product
@Slf4j
@Component
public class ProductEndpoint {
@Value("${spring.application.name}")
private String appName;
@RabbitListener(queues = "${message.queue.product}")
public void receiveMessage(String orderId) {
log.info("receive orderId:{}, appName : {}", orderId, appName);
}
}
전체 실행 후 확인