We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. All Contests
  2. ProjectEuler+
  3. Project Euler #230: Fibonacci Words
  4. Discussions

Project Euler #230: Fibonacci Words

Contest ends in
Problem
Submissions
Leaderboard
Discussions

    You are viewing a single comment's thread. Return to all comments →

  • 2019900005_rajat
    4 years ago+ 2 comments
    def findlengthofterm(n):
        if n == 1:
            return len(A)
        elif n==2:
            return len(B)
    
        a=len(A)
        b=len(B)
        c=0
        i=2
        while(i<n):
            c= a+b
            a,b=b,c
            i+=1
        return c
    
    def findIndex(n) : 
          
        if (n <= 1) : 
            return n 
       
        a = len(A)
        b = len(B)
        c = 0
        res = 2
    
        while (c < n) : 
            c = a + b            
            res = res + 1
            a = b 
            b = c 
              
        return res
    
    if __name__=='__main__':    
        num = int(input())
        output =[]
        for i in range(num):
            A,B, x = input().split()
            s=int(x)
            n=findIndex(s)
            a=s
            while(n > 2):
                l=findlengthofterm(n-2)
                if a>l:
                    a=a-l
                else:
                    n-=1
                n-=1
    
            if n==1:
                output.append(A[a-1])
            else:
                output.append(B[a-1])
    						
        for out in output:
            print(out)
    
    4|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy