그래오늘은이거야

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

세상 개발/IOS(Swift)

Codility 03. CyclicRotation (Swift) 코딩테스트(알고리즘)CodeTest

jinhongstar 2019. 7. 1. 18:57
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/

 

3. CyclicRotation

 

문제는 아래와 같습니다.

 

An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).

The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.

Write a function:

public func solution(_ A : inout [Int], _ K : Int) -> [Int]

that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.

For example, given

A = [3, 8, 9, 7, 6] K = 3

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

[3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7] [6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9] [7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8]

For another example, given

A = [0, 0, 0] K = 1

the function should return [0, 0, 0]

Given

A = [1, 2, 3, 4] K = 4

the function should return [1, 2, 3, 4]

Assume that:

  • N and K are integers within the range [0..100];
  • each element of array A is an integer within the range [−1,000..1,000].

In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

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

Solution

 

 

문제의 내용은 :

 

Shift 개념 

A : 넘겨주는 Array.

K : 오른쪽으로 이동해야 할 숫자.

 

받아온 A 배열을 오른쪽으로 K만큼 >> 움직이면 됩니다.

 

K가 3을 주면 

1번 : [3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7]

2번 : [6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9]

3번 : [7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8]

 

정답은 [9, 7, 6, 3, 8] 겠죠!!

 

 

import UIKit


var questionVal = [Int]()
questionVal = [3, 8, 9, 7, 6]
var moveNum : Int = 5


for _ in 0..<moveNum{
    if let obj = questionVal.popLast(){
        questionVal.insert(obj, at: 0)
    }
}

print(questionVal)


 

Buy Me A Coffee

 

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

 

 

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

 

반응형
Comments