SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Downloaden Sie, um offline zu lesen
{
func fac(_ n: Int) -> Int {
guard n > 0 else { return 1 }
return n * fac(n-1)
}
fac(4)
➔ 4 * fac(3)
➔ 4 * 3 * fac(2)
➔ 4 * 3 * 2 * fac(1)
➔ 4 * 3 * 2 * 1 * fac(0)
➔ 4 * 3 * 2 * 1 * 1
➔ 4 * 3 * 2 * 1
➔ 4 * 3 * 2
➔ 4 * 6
➔ 24
func fac(_ n: Int) -> Int {
func fac(_ index: Int, _ answer: Int) -> Int {
if index == 1 {
return answer
}
return fac(index-1, answer * index)
}
return fac(n,1)
}
fac(4)
➔ fac(4,1)
➔ fac(3,4)
➔ fac(2,12)
➔ fac(1,24)
➔ 24
.
{
func fib(_ n: Int) -> Int {
if n == 0 { return 0 }
if n == 1 { return 1 }
return fib(n-1) + fib(n-2)
}
fib(5)
fib(4) fib(3)
fib(1)
1
fib(2)
fib(0)
0
fib(1)
1
fib(2)
fib(0)
0
fib(1)
1
fib(3)
fib(1)
1
fib(2)
fib(0)
0
fib(1)
1
func fib(_ n: Int) -> Int {
func fib(_ a: Int, _ b: Int, _ n: Int) -> Int {
if n == 0 { return a }
return fib(b, a+b, n-1)
}
return fib(0,1,n)
}
fib(6)
➔ fib(0,1,6)
➔ fib(1,1,5)
➔ fib(1,2,4)
➔ fib(2,3,3)
➔ fib(3,5,2)
➔ fib(5,8,1)
➔ fib(8,13,0)
➔ 8
fac(4)
➔ fac(4,1)
➔ fac(3,4)
➔ fac(2,12)
➔ fac(1,24)
➔ 24
fac(4)
➔ 4 * fac(3)
➔ 4 * 3 * fac(2)
➔ 4 * 3 * 2 * fac(1)
➔ 4 * 3 * 2 * 1 * fac(0)
➔ 4 * 3 * 2 * 1 * 1
➔ 4 * 3 * 2 * 1
➔ 4 * 3 * 2
➔ 4 * 6
➔ 24
: https://brunch.co.kr/@sunghokimnxag/5
8
• 1 .
• index 7 (7,14,21…) 7 (7,17,27) .
• 1, 2, 3, 4, 5, 6, [7], 6, 5, 4, 3, 2, 1, [0], 1, 2, [3], 2, 1, 0, [-1], 0, 1,…
• pingpong(x) .
• For Loop Array .
• Assignment , .
• String .
pingpong(8) = 6
pingpong(22) = 0
pingpong(68) = 2
pingpong(100) = 2
func has7(_ index: Int) -> Bool {
if index % 7 == 0 {
return true
}
if (index - 7) % 10 == 0 {
return true
}
if (index / 10) > 1 {
return has7(index/10)
}
return false
}
has7(21)
➔ true
has7(723)
➔ has7(72)
➔ has7(7)
➔ true
has7(123)
➔ has7(12)
➔ has7(1)
➔ false
unc pingpong(_ index: Int) -> Int {
func pingpong(_ index: Int) -> (Int, Int) {
if index == 1 {
return (1, 1)
}
switch pingpong( index - 1 ) {
case let (answer, direction) :
if has7(index) {
return (answer+direction, -direction)
} else {
return (answer+direction, direction)
}
}
}
return pingpong(index).0
pingpong(14) ➔ (pingpong(13).answer-1, 1)
pingpong(13) ➔ (pingpong(12).answer-1, -1)
pingpong(12) ➔ (pingpong(11).answer-1, -1)
pingpong(11) ➔ (pingpong(10).answer-1, -1)
pingpong(10) ➔ (pingpong( 9).answer-1, -1)
pingpong(9) ➔ (pingpong( 8).answer-1, -1)
pingpong(8) ➔ (pingpong( 7).answer-1, -1)
pingpong(7) ➔ (pingpong( 6).answer+1, -1)
pingpong(6) ➔ (pingpong( 5).answer+1, 1)
pingpong(5) ➔ (pingpong( 4).answer+1, 1)
pingpong(4) ➔ (pingpong( 3).answer+1, 1)
pingpong(3) ➔ (pingpong( 2).answer+1, 1)
pingpong(2) ➔ (pingpong( 1).answer+1, 1)
pingpong(1) ➔ (1,1)
nc pingpong(_ count: Int) -> Int {
func pp(_ count: Int, _ index: Int, _ direction: Int, _ answer: Int) -> Int {
if count == index {
return answer
}
if has7(index+1) {
return pp(count, index+1, -direction, answer+direction)
} else {
return pp(count, index+1, direction, answer+direction)
}
}
return pp(count, 1, 1, 1)
pp(14, index: 1, direction: 1,answer: 1)
pp(14, index: 2, direction: 1,answer: 2)
pp(14, index: 3, direction: 1,answer: 3)
pp(14, index: 4, direction: 1,answer: 4)
pp(14, index: 5, direction: 1,answer: 5)
pp(14, index: 6, direction:-1,answer: 6)
pp(14, index: 7, direction:-1,answer: 7)
pp(14, index: 8, direction:-1,answer: 6)
pp(14, index: 9, direction:-1,answer: 5)
pp(14, index:10, direction:-1,answer: 4)
pp(14, index:11, direction:-1,answer: 3)
pp(14, index:12, direction:-1,answer: 2)
pp(14, index:13, direction: 1,answer: 1)
➔ 0
let array = [1,3,5,23,15]
func sum(array: [Int], initial: Int) -> Int {
guard let head = array.first else { return initial }
let tail = Array(array.dropFirst())
return sum(array: tail, initial: initial + head)
}
sum(array: array, initial: 0)
➔ sum(array: [1,3,5,23,15], initial: 0)
➔ sum(array: [3,5,23,15], initial: 1)
➔ sum(array: [5,23,15], initial: 4)
➔ sum(array: [23,15], initial: 9)
➔ sum(array: [15], initial: 32)
➔ sum(array: [], initial: 47)
➔ 47
func tailRecursive(array: [Int] ,
initial: Int,
eachOperation: (Int, Int) -> Int ) -> Int {
guard let head = array.first else { return initial }
let tail = Array(array.dropFirst())
return tailRecursive(array: tail,
initial: eachOperation(head, initial),
eachOperation: eachOperation)
}
tailRecursive(array: array,
initial: 0,
eachOperation:{(element: Int, answer: Int)->Int in
element + answer
})
➔ 47
extension Array {
func tailRecursive<Result>(
_ initial: Result,
eachOperation: (Result, Element) -> Result ) -> Result {
guard let head = self.first else { return initial }
let tail = Array(self.dropFirst())
return tail.tailRecursive(eachOperation(initial, head),
eachOperation: eachOperation)
}
}
array.tailRecursive(0, eachOperation: + )
array.tailRecursive(0) { (element: Int, answer: Int) -> Int in
return element + answer
}
array.reduce(0) { (element: Int, answer) -> Int in
return element + answer
}
Array(1...5).reduce(1, *)
• .
• .
-> .
-> ex)
-> .
func searchTopViewController(viewController: UIViewController?) -> UIViewController? {
guard let viewController = viewController else { return nil }
switch viewController {
case let viewController as UITabBarController:
return searchTopViewController(viewController: viewController.selectedViewController)
case let viewController as UINavigationController:
return searchTopViewController(viewController: viewController.viewControllers.last)
case let viewController where viewController.presentedViewController != nil:
return searchTopViewController(viewController:viewController.presentedViewController)
default:
return viewController
}
}
•
•
•
• Reduce
•

Weitere ähnliche Inhalte

Was ist angesagt?

Groovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony CodeGroovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony Code
stasimus
 
Ciklum net sat12112011-alexander fomin-expressions and all, all, all
Ciklum net sat12112011-alexander fomin-expressions and all, all, allCiklum net sat12112011-alexander fomin-expressions and all, all, all
Ciklum net sat12112011-alexander fomin-expressions and all, all, all
Ciklum Ukraine
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
riue
 

Was ist angesagt? (20)

Pooya Khaloo Presentation on IWMC 2015
Pooya Khaloo Presentation on IWMC 2015Pooya Khaloo Presentation on IWMC 2015
Pooya Khaloo Presentation on IWMC 2015
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Groovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony CodeGroovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony Code
 
Synapse india complain sharing info about php chaptr 26
Synapse india complain sharing info about php chaptr 26Synapse india complain sharing info about php chaptr 26
Synapse india complain sharing info about php chaptr 26
 
Ciklum net sat12112011-alexander fomin-expressions and all, all, all
Ciklum net sat12112011-alexander fomin-expressions and all, all, allCiklum net sat12112011-alexander fomin-expressions and all, all, all
Ciklum net sat12112011-alexander fomin-expressions and all, all, all
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
 
Codeware
CodewareCodeware
Codeware
 
RxSwift 활용하기 - Let'Swift 2017
RxSwift 활용하기 - Let'Swift 2017RxSwift 활용하기 - Let'Swift 2017
RxSwift 활용하기 - Let'Swift 2017
 
c programming
c programmingc programming
c programming
 
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf MilanFrom Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
 
Intro programacion funcional
Intro programacion funcionalIntro programacion funcional
Intro programacion funcional
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
 
Scala best practices
Scala best practicesScala best practices
Scala best practices
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
Unit 3
Unit 3 Unit 3
Unit 3
 
20180721 code defragment
20180721 code defragment20180721 code defragment
20180721 code defragment
 
Slaying the Dragon: Implementing a Programming Language in Ruby
Slaying the Dragon: Implementing a Programming Language in RubySlaying the Dragon: Implementing a Programming Language in Ruby
Slaying the Dragon: Implementing a Programming Language in Ruby
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
20181020 advanced higher-order function
20181020 advanced higher-order function20181020 advanced higher-order function
20181020 advanced higher-order function
 

Ähnlich wie Swift에서 꼬리재귀 사용기 (Tail Recursion)

Matematika diskrit: fungsi pembangkit part 3
Matematika diskrit: fungsi pembangkit part 3Matematika diskrit: fungsi pembangkit part 3
Matematika diskrit: fungsi pembangkit part 3
radar radius
 
Useful javascript
Useful javascriptUseful javascript
Useful javascript
Lei Kang
 
Let’s Talk About Ruby
Let’s Talk About RubyLet’s Talk About Ruby
Let’s Talk About Ruby
Ian Bishop
 

Ähnlich wie Swift에서 꼬리재귀 사용기 (Tail Recursion) (20)

Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
Matematika diskrit: fungsi pembangkit part 3
Matematika diskrit: fungsi pembangkit part 3Matematika diskrit: fungsi pembangkit part 3
Matematika diskrit: fungsi pembangkit part 3
 
Useful javascript
Useful javascriptUseful javascript
Useful javascript
 
FUNCTIONS L.1.pdf
FUNCTIONS L.1.pdfFUNCTIONS L.1.pdf
FUNCTIONS L.1.pdf
 
Declare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term RewritingDeclare Your Language: Transformation by Strategic Term Rewriting
Declare Your Language: Transformation by Strategic Term Rewriting
 
From Javascript To Haskell
From Javascript To HaskellFrom Javascript To Haskell
From Javascript To Haskell
 
Introduction to Recursion (Python)
Introduction to Recursion (Python)Introduction to Recursion (Python)
Introduction to Recursion (Python)
 
Swift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfSwift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdf
 
Unit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptxUnit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptx
 
A/B Testing for Game Design
A/B Testing for Game DesignA/B Testing for Game Design
A/B Testing for Game Design
 
Scala kansai summit-2016
Scala kansai summit-2016Scala kansai summit-2016
Scala kansai summit-2016
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 
Computation of Semi-Magic Squares Generated by Serpentine Matrices
Computation of Semi-Magic Squares Generated by Serpentine MatricesComputation of Semi-Magic Squares Generated by Serpentine Matrices
Computation of Semi-Magic Squares Generated by Serpentine Matrices
 
Eta
EtaEta
Eta
 
funwithalgorithms.pptx
funwithalgorithms.pptxfunwithalgorithms.pptx
funwithalgorithms.pptx
 
Week-5-Inverse-Function-NGY.pptx
Week-5-Inverse-Function-NGY.pptxWeek-5-Inverse-Function-NGY.pptx
Week-5-Inverse-Function-NGY.pptx
 
Perl6 one-liners
Perl6 one-linersPerl6 one-liners
Perl6 one-liners
 
Let’s Talk About Ruby
Let’s Talk About RubyLet’s Talk About Ruby
Let’s Talk About Ruby
 
関数プログラミングことはじめ revival
関数プログラミングことはじめ revival関数プログラミングことはじめ revival
関数プログラミングことはじめ revival
 

Kürzlich hochgeladen

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Kürzlich hochgeladen (20)

CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 

Swift에서 꼬리재귀 사용기 (Tail Recursion)

  • 1.
  • 2.
  • 3. {
  • 4. func fac(_ n: Int) -> Int { guard n > 0 else { return 1 } return n * fac(n-1) } fac(4) ➔ 4 * fac(3) ➔ 4 * 3 * fac(2) ➔ 4 * 3 * 2 * fac(1) ➔ 4 * 3 * 2 * 1 * fac(0) ➔ 4 * 3 * 2 * 1 * 1 ➔ 4 * 3 * 2 * 1 ➔ 4 * 3 * 2 ➔ 4 * 6 ➔ 24
  • 5. func fac(_ n: Int) -> Int { func fac(_ index: Int, _ answer: Int) -> Int { if index == 1 { return answer } return fac(index-1, answer * index) } return fac(n,1) } fac(4) ➔ fac(4,1) ➔ fac(3,4) ➔ fac(2,12) ➔ fac(1,24) ➔ 24 .
  • 6. {
  • 7. func fib(_ n: Int) -> Int { if n == 0 { return 0 } if n == 1 { return 1 } return fib(n-1) + fib(n-2) } fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) 0 fib(1) 1 fib(2) fib(0) 0 fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) 0 fib(1) 1
  • 8. func fib(_ n: Int) -> Int { func fib(_ a: Int, _ b: Int, _ n: Int) -> Int { if n == 0 { return a } return fib(b, a+b, n-1) } return fib(0,1,n) } fib(6) ➔ fib(0,1,6) ➔ fib(1,1,5) ➔ fib(1,2,4) ➔ fib(2,3,3) ➔ fib(3,5,2) ➔ fib(5,8,1) ➔ fib(8,13,0) ➔ 8
  • 9. fac(4) ➔ fac(4,1) ➔ fac(3,4) ➔ fac(2,12) ➔ fac(1,24) ➔ 24 fac(4) ➔ 4 * fac(3) ➔ 4 * 3 * fac(2) ➔ 4 * 3 * 2 * fac(1) ➔ 4 * 3 * 2 * 1 * fac(0) ➔ 4 * 3 * 2 * 1 * 1 ➔ 4 * 3 * 2 * 1 ➔ 4 * 3 * 2 ➔ 4 * 6 ➔ 24
  • 10.
  • 11. : https://brunch.co.kr/@sunghokimnxag/5 8 • 1 . • index 7 (7,14,21…) 7 (7,17,27) . • 1, 2, 3, 4, 5, 6, [7], 6, 5, 4, 3, 2, 1, [0], 1, 2, [3], 2, 1, 0, [-1], 0, 1,… • pingpong(x) . • For Loop Array . • Assignment , . • String . pingpong(8) = 6 pingpong(22) = 0 pingpong(68) = 2 pingpong(100) = 2
  • 12. func has7(_ index: Int) -> Bool { if index % 7 == 0 { return true } if (index - 7) % 10 == 0 { return true } if (index / 10) > 1 { return has7(index/10) } return false } has7(21) ➔ true has7(723) ➔ has7(72) ➔ has7(7) ➔ true has7(123) ➔ has7(12) ➔ has7(1) ➔ false
  • 13. unc pingpong(_ index: Int) -> Int { func pingpong(_ index: Int) -> (Int, Int) { if index == 1 { return (1, 1) } switch pingpong( index - 1 ) { case let (answer, direction) : if has7(index) { return (answer+direction, -direction) } else { return (answer+direction, direction) } } } return pingpong(index).0 pingpong(14) ➔ (pingpong(13).answer-1, 1) pingpong(13) ➔ (pingpong(12).answer-1, -1) pingpong(12) ➔ (pingpong(11).answer-1, -1) pingpong(11) ➔ (pingpong(10).answer-1, -1) pingpong(10) ➔ (pingpong( 9).answer-1, -1) pingpong(9) ➔ (pingpong( 8).answer-1, -1) pingpong(8) ➔ (pingpong( 7).answer-1, -1) pingpong(7) ➔ (pingpong( 6).answer+1, -1) pingpong(6) ➔ (pingpong( 5).answer+1, 1) pingpong(5) ➔ (pingpong( 4).answer+1, 1) pingpong(4) ➔ (pingpong( 3).answer+1, 1) pingpong(3) ➔ (pingpong( 2).answer+1, 1) pingpong(2) ➔ (pingpong( 1).answer+1, 1) pingpong(1) ➔ (1,1)
  • 14. nc pingpong(_ count: Int) -> Int { func pp(_ count: Int, _ index: Int, _ direction: Int, _ answer: Int) -> Int { if count == index { return answer } if has7(index+1) { return pp(count, index+1, -direction, answer+direction) } else { return pp(count, index+1, direction, answer+direction) } } return pp(count, 1, 1, 1) pp(14, index: 1, direction: 1,answer: 1) pp(14, index: 2, direction: 1,answer: 2) pp(14, index: 3, direction: 1,answer: 3) pp(14, index: 4, direction: 1,answer: 4) pp(14, index: 5, direction: 1,answer: 5) pp(14, index: 6, direction:-1,answer: 6) pp(14, index: 7, direction:-1,answer: 7) pp(14, index: 8, direction:-1,answer: 6) pp(14, index: 9, direction:-1,answer: 5) pp(14, index:10, direction:-1,answer: 4) pp(14, index:11, direction:-1,answer: 3) pp(14, index:12, direction:-1,answer: 2) pp(14, index:13, direction: 1,answer: 1) ➔ 0
  • 15. let array = [1,3,5,23,15] func sum(array: [Int], initial: Int) -> Int { guard let head = array.first else { return initial } let tail = Array(array.dropFirst()) return sum(array: tail, initial: initial + head) } sum(array: array, initial: 0) ➔ sum(array: [1,3,5,23,15], initial: 0) ➔ sum(array: [3,5,23,15], initial: 1) ➔ sum(array: [5,23,15], initial: 4) ➔ sum(array: [23,15], initial: 9) ➔ sum(array: [15], initial: 32) ➔ sum(array: [], initial: 47) ➔ 47
  • 16. func tailRecursive(array: [Int] , initial: Int, eachOperation: (Int, Int) -> Int ) -> Int { guard let head = array.first else { return initial } let tail = Array(array.dropFirst()) return tailRecursive(array: tail, initial: eachOperation(head, initial), eachOperation: eachOperation) } tailRecursive(array: array, initial: 0, eachOperation:{(element: Int, answer: Int)->Int in element + answer }) ➔ 47
  • 17. extension Array { func tailRecursive<Result>( _ initial: Result, eachOperation: (Result, Element) -> Result ) -> Result { guard let head = self.first else { return initial } let tail = Array(self.dropFirst()) return tail.tailRecursive(eachOperation(initial, head), eachOperation: eachOperation) } } array.tailRecursive(0, eachOperation: + ) array.tailRecursive(0) { (element: Int, answer: Int) -> Int in return element + answer } array.reduce(0) { (element: Int, answer) -> Int in return element + answer }
  • 19. • . • . -> . -> ex) -> .
  • 20. func searchTopViewController(viewController: UIViewController?) -> UIViewController? { guard let viewController = viewController else { return nil } switch viewController { case let viewController as UITabBarController: return searchTopViewController(viewController: viewController.selectedViewController) case let viewController as UINavigationController: return searchTopViewController(viewController: viewController.viewControllers.last) case let viewController where viewController.presentedViewController != nil: return searchTopViewController(viewController:viewController.presentedViewController) default: return viewController } }