그래오늘은이거야

Codility 04. FrogJmp (Swift) 코딩테스트(알고리즘)CodeTest 본문

세상 개발/IOS(Swift)

Codility 04. FrogJmp (Swift) 코딩테스트(알고리즘)CodeTest

jinhongstar 2019. 7. 1. 19:24
728x90
반응형

요즘 온라인으로 코딩 테스트 하는 곳이 많습니다.

 

해외에 유명한 기업및 국내 잘나가는 IT기업들은 Codility 를 본다.

 

IBM, FIFAOnline, 11번가, 우아한형제들, 티몬, 쿠팡, Grab 등등..

 

저도 우아한형제들 코딩 테스트를 통하여 아래 사이트를 알게 되었습니다.

 

목적은 100% 

 

온라인 코딩 테스트 

 

제가 할 수 있는 언어는 :  Java(상), Object-c(상), Swift(중상), Android(상), C#(하), C,C++(하), SQL(하)

각각의 언어로 프로젝트 한번 이상 해봤습니다.

 

주종목 코드는 Object-c  인데 공부할겸 Swift로 테스트코딩

이 글이 누군가에겐 "약"이 되고 누군가에겐 "독"이 될 것이다.

개발자라면 적어도 기본 알고리즘 정도는 스스로 학습하여 풀어봐야 한다.참고로 코딩테스트 문제를 제출 한 심사위원 분도제 블로그에서 답을 확인 한다는 것을 역으로 알아두고 보세요!! ㅎㅎ

 

요즘 운동에만 신경쓰고 있어서 알고리즘을 다시 풀어 봤을때 너무 놀랐다...

 

나는 코딩을 잘 하고 생각하고 있었는데... 내가 고작 이정도 였을까...

 

총 20개 정도로 알고있다. 이걸 다 작성하고 100점을 맞을 것이다.

 

Lesson 1

Iterations

Lesson 2

Arrays

Lesson 3

Time Complexity

Lesson 4

Counting Elements

Lesson 5

Prefix Sums

Lesson 6

Sorting

Lesson 7

Stacks and Queues

Lesson 8

Leader

Lesson 9

Maximum slice problem

Lesson 10

Prime and composite numbers

Lesson 11

Sieve of Eratosthenes

Lesson 12

Euclidean algorithm

Lesson 13

Fibonacci numbers

Lesson 14

Binary search algorithm

Lesson 15

Caterpillar method

Lesson 16

Greedy algorithms

Lesson 17

Dynamic programming

Lesson 90

Tasks from Indeed Prime 2015 challenge

Lesson 91

Tasks from Indeed Prime 2016 challenge

Lesson 92

Tasks from Indeed Prime 2016 College Coders challenge

Lesson 99

Future training

 

테스트 할 수 있는 언어는 : 

C, C++, C#, Go, Java, Javascript, Kotlin, Lua, Object-c, Pascal, Perl, PHP, Python, Ruby, Scala, Swift4, VisualBasic

 

https://app.codility.com/

 

4.  FrogJmp

 

문제는 아래와 같습니다.

 

A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D.

Count the minimal number of jumps that the small frog must perform to reach its target.

Write a function:

public func solution(_ X : Int, _ Y : Int, _ D : Int) -> Int

that, given three integers X, Y and D, returns the minimal number of jumps from position X to a position equal to or greater than Y.

For example, given:

X = 10 Y = 85 D = 30

the function should return 3, because the frog will be positioned as follows:

  • after the first jump, at position 10 + 30 = 40
  • after the second jump, at position 10 + 30 + 30 = 70
  • after the third jump, at position 10 + 30 + 30 + 30 = 100

Write an efficient algorithm for the following assumptions:

  • X, Y and D are integers within the range [1..1,000,000,000];
  • X ≤ Y.

    Copyright 2009–2019 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

 

문제의 내용은 :

 

X,Y,D 값을 받아 

 

X = 시작위치

Y = 총 가야할 거리

D = 한번당 점프거리

 

시작위치 에서 총가야할 거리를 계삭 후 총 몇번의 점프를 하면 갈 수 있는지 맞추는 것 입니다.

 

UI 그리다 보면 X,Y 에 익숙할텐데요 저는 그 개념을 머리로 그리면서 코딩했습니다.

 

문제가 참 쉽더군요

 

풀이 방식은 여러가지 방식이있는데 

 

X,Y,D 의 값을 받아!

 

X = 10 Y = 85 D = 30

문제를 주면 85-10 = 75 / 30 을 하면 2번이 나오는데 거기에서 나머지가 0으로 안떨어지고 15로 나오니 한번 더 점프를 해야 한다고 가정하고 계산하면 됩니다.

 

정답은 3이 겠죠!!

 

 

import UIKit


var X : Int = 10
var Y : Int = 120
var D : Int = 30



print((Y-X) / D + ((Y-X) % D == 0 ? 0 : 1))

 

Buy Me A Coffee

 

100% 모든 문제를 백점! 도전!! ㅜㅜ

 

 

시간날때 주말에 하던지 아니면 저처럼 틈틈히 하려면 1시간40분의 긴 시간을 주니 쬐끔씩 시간 놓치지말고 풀어보세요 ㅎㅎ

 

반응형
Comments