2021년 4월 1일 목요일

[ Practice Scala ] List선언 - Range (Array Of N Elements)

 

문제 ]

Create an array of  integers, where the value of  is passed as an argument to the pre-filled function in your editor. This challenge uses a custom checker, so you can create any array of  integers. For example, if , you could return , or any other array of equal length.

Note: Code stubs are provided for almost every language in which you must either fill in a blank (i.e., ____) or write your code in the area specified by comments.

Method Signature

Number Of Parameters: 1
Parameters: [n]
Returns: List or Vector

Input Format

A single integer, .

Constraints

  • The members returned by the list/vector/array must be integers.

Output Format

The function must return an array, list, or vector of  integers. Stub code in the editor prints this to stdout as a space, comma, or semicolon-separated list (depending on your submission language).

Note: Your output need not match the Expected Output exactly; the size of your printed list is confirmed by a custom checker, which determines whether or not you passed each test case.

Sample Input 0

10

Sample Output 0

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Sample Input 1

3

Sample Output 1

[1, 2, 3]



제출 ]

object Solution extends App {

    import scala.io.StdIn.readInt


    def f(num:Int) : List[Int] = {

        return List.fill(num)(1)

        Or

        return List.range(0,num)

    }

    println(f(readInt))

}



풀이 ]

num = 4


return List.fill(num)(1) : 리스트 선언시 1로 첫번째인자(num) 만큼 채운다.

ex List(1,1,1,1)


return List.range(0,num) : 리스트 선언시 0~num까지의 수로 채운다.

ex List(0,1,2,3)

세번자 인자 추가시 배열의 스텝을 정한다.

List.range(0,10,2) 

ex List(0,2,4,6,8)










댓글 없음:

댓글 쓰기