Swift Fun Algorithms #6: Fibonacci Sequence

Поділитися
Вставка
  • Опубліковано 20 гру 2024

КОМЕНТАРІ • 34

  • @coderlife200
    @coderlife200 7 років тому +1

    Hi Brian,
    Great tutorials you showing us. For fibonacci, i've included one boolean parameter indicating for the first step to include [0,1]
    func fibRecursiveFor(numSteps: Int, first: Int, second: Int, firstStep: Bool) -> [Int]{
    if numSteps == 0 {
    return []
    }
    if firstStep {
    return [0,1] + [first + second] + fibRecursiveFor(numSteps: numSteps - 1, first: second, second: first + second, firstStep: false)
    }
    return [first + second] + fibRecursiveFor(numSteps: numSteps - 1, first: second, second: first + second, firstStep: false)
    }
    fibRecursiveFor(numSteps: 9, first: 0, second: 1, firstStep: true)

  • @richardgmartinonline
    @richardgmartinonline 7 років тому +3

    Hi Brian: I think your recursive technique is offset by one index. When you call with numSteps as '5', it should return [0, 1, 1, 2, 3, 5], instead it returns [0, 1, 1, 2, 3, 5, 8] - which is an index, or numStep, of 6.

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 років тому +2

      +Richard Martin good catch, as long as you understand the algorithm you’re good

  • @desmondbraxton6387
    @desmondbraxton6387 2 роки тому

    Good video, it explains alot. Do you think I can use this for the running sum leetcode problem?

  • @krutomjer
    @krutomjer 5 років тому

    I love it how you're secretly smiling while writing the recursive function :D

  • @Simran95100
    @Simran95100 8 років тому +4

    could you please add more videos relating algorithm..as these are also quite helpful thanks

    • @LetsBuildThatApp
      @LetsBuildThatApp  8 років тому

      Hi Simran, I think I ran out of ideas for algorithms so I stopped...

    • @quickfingers5982
      @quickfingers5982 8 років тому

      Lets Build That App more algorithms please!

    • @jelly1928
      @jelly1928 5 років тому

      @@LetsBuildThatApp dynamic programming

  • @MAX-uh6kh
    @MAX-uh6kh 5 років тому

    hey, is the a way to do this with only var and print?

  • @eduardovanin9824
    @eduardovanin9824 3 роки тому

    Lol, I've been studying swift for two months and when I see you doing things like these so fast, and explaining it I just get like
    hell yeah, he is so good
    hopefully ur doing good, lets keep up the good work!

    • @ahmedmohna1986
      @ahmedmohna1986 3 роки тому

      Its prepared lesson bro , no one can think in this fast, dont think like this way and compare yourself with him u will frustrate

  • @Brofehst
    @Brofehst 8 років тому

    Hey man! Great video - I really like your video setup. What are you using to record yourself / your screen at the same time? Are you using anything in particular for editing? Thanks!

  • @wdw5000
    @wdw5000 8 років тому +1

    Noticed your videos are mostly algorithm based now. Will you be recreating any apps in the near future?

    • @LetsBuildThatApp
      @LetsBuildThatApp  8 років тому +2

      +wdw5000 Yes, I just got back from an 8 day Taiwan vacation and getting back into the groove of things. Currently working on producing how to create UA-cam app series but takes time as you can imagine. These algorithm videos are here to satisfy your hunger for now :)

    • @superwsuperw
      @superwsuperw 8 років тому

      How was your Taiwan vacation?

    • @LetsBuildThatApp
      @LetsBuildThatApp  8 років тому +2

      Taiwan is really awesome, I enjoyed staying in Taipei, yilan, and hualien. Being able to travel to all these places without a car was real nice.

  • @jasonbakthakumar3533
    @jasonbakthakumar3533 7 років тому

    Please make a video on String algorithms in Swift.. It'll be really great....

  • @ahmedmohna1986
    @ahmedmohna1986 3 роки тому

    Theres wrong with shorter way its give one more noumber , you should make numsteps-2

  • @jameshe754
    @jameshe754 7 років тому

    Cannot use mutating member on immutable value: 'fibForNumbers' returns immutable value
    anyone know why?
    when I use return fibForNumbers(numberOfSteps: numberOfSteps-1).append(fibNumber(number: numberOfSteps))
    .instead when I use return fibForNumbers(numberOfSteps: numberOfSteps-1) + [fibNumber(number: numberOfSteps)] is fine
    any now knows the difference between append a element to an army and just + one
    func fibForNumbers (numberOfSteps: Int) -> [Int]{
    // if numberOfSteps Int{
    if number == 1{
    return 0
    }
    else if number == 2{
    return 1
    }
    else{
    return fibNumber(number: number-1) + fibNumber(number: number - 2)
    }
    }
    fibNumber(number: 5)
    fibForNumbers(numberOfSteps: 5)

  • @killerwolf1983
    @killerwolf1983 5 років тому +2

    thank yo, but there is a simpler solution: func fib(_ n: Int) -> Int {
    guard n > 1 else {return n}
    return fib(n - 1) + fib(n - 2)
    }

    • @nikitatroin
      @nikitatroin 3 роки тому

      simpler, но с n = 100, он будет выполнять эту функцию 50 000 лет

  • @JasonMitchellAZ
    @JasonMitchellAZ 6 років тому

    Good stuff!

  • @elielimelech5151
    @elielimelech5151 7 років тому

    Very good and helpful 👌

  • @scollaco1027
    @scollaco1027 8 років тому

    Hey man, I really like your algorithm videos, but read below that you ran out of ideas. So what you think of making videos based on the "swift algorithm club" (github.com/raywenderlich/swift-algorithm-club)? Just a suggestion :)

    • @LetsBuildThatApp
      @LetsBuildThatApp  8 років тому

      +Saturnino collaço I'm actually an android author on rays website, I'm not going to encroach on their content :)

  • @taylormaxwell1277
    @taylormaxwell1277 5 років тому

    Thanks you, very helpful

  • @PythonPlusPlus
    @PythonPlusPlus 5 років тому +2

    I don't like the way you did the recursive function, it's not very intuitive. Here's my version:
    func recursiveFib(for numSteps: Int) -> [Int] {
    if numSteps < 0 {
    return []
    }
    if numSteps < 2 {
    return [Int](0...numSteps)
    }
    let previous = recursiveFib(for: numSteps - 1)
    return previous + [previous[numSteps - 2] + previous[numSteps - 1]]
    }

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому +3

      Good job. I think there are many different solutions to this problem, good that you found one that suits your coding preference.

    • @falsanomo
      @falsanomo 5 років тому

      I like this because it's all self-contained within the function. The function runs beautifully without the first if-statement so I'm not sure why that was added. Edit: Oh, I see the if-statement is used to handle negatives. :)