[๐Ÿ“ iOS TIL] #5 ... ๐ŸŽ Swift ๏ผ† Algorithm ๐Ÿ–ฅ๏ธ

TaeUkยท2024๋…„ 2์›” 27์ผ
0

๐ŸŽ iOS TIL

๋ชฉ๋ก ๋ณด๊ธฐ
4/18
post-thumbnail

์ŠคํŒŒ๋ฅดํƒ€ ๋‚ด๋ฐฐ์บ  3์ฃผ์ฐจ ์ •๋ฆฌ

๋ฐฐ์—ด๊ณผ ๋”•์…”๋„ˆ๋ฆฌ์˜ ์‚ฌ์šฉ

import UIKit

struct Family {
    let myname: String
    let bestname: String
    let nextname: String
}

class ViewController: UIViewController {

    let friensname: [String] = ["a", "b", "c"]
    let KoreanName: [String:String] = ["a":"์—์ด", "b":"๋น„", "c":"์‹œ"]
    
    let freindnames = Family(myname: "1",
                             bestname: "2",
                             nextname: "3"  )
    
    var count: Int = 0
    
    @IBOutlet weak var namelabel: UILabel!
    @IBOutlet weak var nextname: UILabel!
    @IBOutlet weak var bestname: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func didTapButton(_ sender: Any) {
        /*
        let friendnames = friensname[count]
        namelabel.text  = KoreanName[friendnames]
        */
        
        namelabel.text = freindnames.myname
        nextname.text = freindnames.nextname
        bestname.text = freindnames.bestname
        
        count += 1
    }
}

์ง€๋‚œ ๊ฐ•์˜์—์„œ ๋ฐฐ์šด IBAction๊ณผ IBOutlet์„ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ์–ด๋ณด์•˜๋Š”๋ฐ, ๋ฐฐ์—ด๊ณผ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๋‘˜์˜ ์ฐจ์ด์ ๊ณผ ํ™œ์šฉ๋ฐฉ์•ˆ์— ๋Œ€ํ•ด ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค!

struct๋ฅผ ํ™œ์šฉํ•œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•˜์—ฌ ๋Œ€์ดํ„ฐ ๊ด€๋ฆฌ๋ฅผ ํ•  ๋•Œ, ๋ถˆํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋”๋ผ๋„ ์‚ฌ์šฉํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๊ธฐ ๊ฐ„ํŽธํ•˜์˜€๋‹ค!

ํ…Œ์ด๋ธ” ๋ทฐ ๊ตฌํ˜„

import UIKit

class MyTableViewViewController: UIViewController {

    let friensname: [String] = ["a", "b", "c", "d"]

    
    @IBOutlet weak var table: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        table.backgroundColor = .red
        
        table.delegate = self
        table.dataSource = self
        //tableview์™€ MyTableViewViewController๋ฅผ ์—ฐ๊ฒฐํ•ด์คŒ
    }
}

extension MyTableViewViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return friensname.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = table.dequeueReusableCell(withIdentifier: "tablecell", for: indexPath)
        
        cell.textLabel?.text = friensname[indexPath.row]
        
        return cell
    }
}

๋„ค๋น„๊ฒŒ์ด์…˜ ๋ฐ ํ™”๋ฉด ์ „ํ™˜

์•ž์„œ ์ •๋ฆฌํ•œ ํ…Œ์ด๋ธ” ๋ทฐ์˜ ๋‚ด์šฉ์„ ํ™œ์šฉํ•˜์—ฌ ์ฝ”๋“œ ์ž‘์„ฑ ์—†์ด, ์˜ค์ง Drag & Drop ๋ฐฉ์‹์œผ๋กœ ๋„ค๋น„๊ฒŒ์ด์…˜์„ ๊ตฌํ˜„ํ•˜๊ณ  ์‹œ๋ฎฌ๋ ˆ์ด์…˜์—์„œ ํ™”๋ฉด ์ „ํ™˜์„ ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

๊ฐ ํ…Œ์ด๋ธ” ๋ทฐ๋ฅผ ๋„ค๋น„๊ฒŒ์ด์…˜์œผ๋กœ ์—ฐ๊ฒฐํ•  ๋•Œ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ์˜ต์…˜์ด ์žˆ๊ณ , ์ด์— ๋”ฐ๋ผ ์ „ํ™˜๋˜๋Š” ํ™”๋ฉด์ด ๋‹ฌ๋ผ์ง„๋‹ค.

์ด ๋˜ํ•œ ๋ฒ„ํŠผ ํ•œ๋ฒˆ์œผ๋กœ ์ˆ˜์ •ํ•˜๊ณ  ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด ๋งค์šฐ ํŽธ๋ฆฌํ•˜๋‹ค!


[level 1] ์•ฝ์ˆ˜์˜ ํ•ฉ - 12928

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ ํ‘ผ ๋ฌธ์ œ๋ฅผ ์ž๋™์œผ๋กœ ๊นƒํ—ˆ๋ธŒ์— ์ž๋™์œผ๋กœ ์ปค๋ฐ‹ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๊ฒŒ๋˜์–ด ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ์ •๋ฆฌํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค!

[๋ฐฑ์ค€ํ—ˆ๋ธŒ]ใ€€๋ฐฑ์ค€, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฌธ์ œ ๊นƒํ—ˆ๋ธŒ๋กœ ์ž๋™ ์ปค๋ฐ‹ํ•˜๊ธฐ

  • ๋ฌธ์ œ ์„ค๋ช…
    ์ •์ˆ˜ n์„ ์ž…๋ ฅ๋ฐ›์•„ n์˜ ์•ฝ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•œ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.
func solution(_ n:Int) -> Int {
    var arr: [Int] = []
    var val: Int = 0
    
    if n == 0 {
        return 0
    }
    else {
        for i in 1...n {
            arr.append(n % i == 0 ? i : 0)
        }
    
        val = arr.reduce(0, +)
    }
    
    return val
}

ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ 0์„ 0์œผ๋กœ ๋‚˜๋ˆŒ๋ ค๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•˜๋ฉด ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

์œ„ ๋ฌธ์ œ์—์„œ 0์ด ๋ฐœ์ƒํ•˜๋Š” ์ƒํ™ฉ์„ ์˜ˆ์™ธ๋กœ ํŒ๋‹จํ•˜๊ณ  if ์กฐ๊ฑด๋ฌธ์œผ๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์˜€๋Š”๋ฐ, ์ด ๊ณผ์ •์„ ๋”์šฑ ํšจ์œจ์ ์œผ๋กœ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ์•ˆ์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค!

0๊ฐœ์˜ ๋Œ“๊ธ€