
Scala๋ ํจ์ํ ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ก์จ Spark๋ฅผ ์ฌ์ฉํ ๋ ๋น ๋ฅธ ์ฑ๋ฅ์ ์ด์ฉํ ์ ์์์ผ๋ก ๋ฐ์ดํฐ ์์ง๋์ด๋ง ์ญ๋์ ํ์ํ ์ธ์ด์ ๋๋ค.
ํ์๋ Scala์ธ์ด์ ์นํด์ง๊ธฐ ์ํด์ Scala ์ธ์ด๋ฅผ ํ์ฉํ์ฌ ๋ง์ ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ๋ฅผ ํ์ด๋ณด์์ต๋๋ค.
https://github.com/DeepFlame-JR/Algorithm_Solving
GitHub - DeepFlame-JR/Algorithm_Solving: ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ํ์ด
์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ํ์ด . Contribute to DeepFlame-JR/Algorithm_Solving development by creating an account on GitHub.
github.com
์๋์ ์๊ณ ๋ฆฌ์ฆ์ ํ์ํ Scala ๊ธฐ๋ณธ ๋ฌธ๋ฒ์ ๋ํด์ ์ ๋ฆฌํด๋ณด๊ฒ ์ต๋๋ค.
๊ธฐ๋ณธ
var a = "Hello"// ์ผ๋ฐ ๋ณ์ (Readonly)
val b = 3 // ์์
var (a, b) = (1, "Hello") // ๋ค์ค ํ ๋น
// ํด๋์ค ์ ์ธ
class MyClass private(var name: String){
def sayName(): Unit ={ // Unit = void
println(name)
}
}
๋ฐฐ์ด
// ์ ์ธ
var a = List(1,2,3) // 1,2,3
var a = List.fill(2){0} // 0,0
var a = List.empty[Type] // ๋น ๋ฐฐ์ด
// ํธ์ง
var a = List(1,2,3)
a :+= 5 // 1,2,3,5
a +:= 0 // 0,1,2,3,5
a = a ++ Array(10) // 0,1,2,3,5,10
a = a.updated(3, 9) // 0,1,2,9,5,10
a.take(2) // 0,1 (0~n-1)
a.drop(2) // 2,3,4,... (n~N-1)
a.slice(1,3) // 1,2 (start~end-1)
a = a.sortWith(_ < _) // ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
println(a.mkString(",")) // print
// 2์ฐจ์ ๋ฐฐ์ด
val matrix = Array.ofDim[Int](2,2)
์กฐ๊ฑด๋ฌธ/๋ฐ๋ณต๋ฌธ
// ๋ฐ๋ณต๋ฌธ
for(x <- 1 until 3)
println("x ๊ฐ:" + x)
for(item <- items)
println(item)
// 2์ค ๋ฐ๋ณต๋ฌธ
for(x <- 1 to 2; y <-1 to 9 ){
print( x +"*" + y + "=" + x*y +", ")
}
// 1*1=1, 1*2=2, 1*3=3, 1*4=4, 1*5=5, 1*6=6, 1*7=7, 1*8=8, 1*9=9, 2*1=2, 2*2=4, 2*3=6, 2*4=8, 2*5=10, 2*6=12, 2*7=14, 2*8=16, 2*9=18,
// ์กฐ๊ฑด๋ฌธ 1 line
x = if (a > b) a else b
๊ธฐํ ์๋ฃ๊ตฌ์กฐ
import scala.collection.mutable._ // ๊ธฐ๋ณธ๊ฐ์ด immutable์ด๋ผ ๋ณ์ ๋ด ๊ฐ์ ๋ณ๊ฒฝํ๋ ค๊ณ ํ ๋ ์๋ฌ๊ฐ ๋ํ๋จ
// HashMap
val map = Map[Char, Int]()
map += ("b" -> 2)
map -= "b"
map.toArray.sortBy(_._2)
// Queue
val queue = new Queue[Int]
queue.enqueue(1)
while(queue.nenEmpty)
var cur = queue.dequeue()
// Stack
val stack = new Stack[Int]
stack.push(1)
var cur = stack.pop()
Map/Reduce
// map, flatMap (์์์ ๋ณํ๋ฅผ ์ค๋ค)
val fruits = Array("apple", "banana", "orange")
fruits.map(_.toUpperCase) // APPLE, BANANA, ORANGE
fruits.flatMap(_.toUpperCase) // A, P, P, L, E, B, A, N, A, N, A, O, R, A, N, G, E
val strings = List("1", "2", "foo", "3", "bar")
strings.map(toInt) // Some(1), Some(2), None, Some(3), None
strings.flatMap(toInt) // 1, 2, 3
// filter
val a = List(1,2,3,4)
a.filter(_%2==0) // 2,4
a.partition(_%2==0) // (List(2,4), List(1,2))
// reduce (๋จ์ผ ๊ฒฐ๊ณผ๋ฅผ ์ป๊ณ ์ถ๋ค)
List(1,2,3).reduceLeft(_+_) // ์ข์ธก_: ๋์ ๊ฐ, ์ฐ์ธก_: ๋์๊ฐ
List(1,2,3).reduceRight(_+_) // ์ข์ธก_: ๋์๊ฐ, ์ฐ์ธก_: ๋์ ๊ฐ
List(1,2,3).foldLeft(4)(_+_) // 4(์์๊ฐ)+1+2+3
// Char Counter
val Counter = scala.collection.mutable.Map.empty[Char, Int]
str.distinct.toCharArray.map(x => Counter += (x -> str.count(_==x)))
'Engineering ๐ป > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Leetcode/Python] 96. Unique Binary Search Trees (0) | 2022.05.24 |
|---|---|
| [ํ๋ก๊ทธ๋๋จธ์ค/Python] ๊ฒฝ์ฃผ๋ก ๊ฑด์ค (0) | 2022.03.25 |
| [Algorithm] ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ ๊ฐ๋จ ์ ๋ฆฌ์ Python ๊ตฌํ (๋ฒ๋ธ์ ๋ ฌ, ์ ํ์ ๋ ฌ, ํต์ ๋ ฌ, ๋ณํฉ์ ๋ ฌ, ํ์ ๋ ฌ) (0) | 2022.02.21 |
| [์๊ณ ๋ฆฌ์ฆ] ๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ (feat. Leetcode) (0) | 2022.02.06 |
| [์๋ฃ๊ตฌ์กฐ] ํ (Heaps) (0) | 2022.02.05 |