Project Euler #62: Cubic permutations

  • + 1 comment

    I don't understand why my code doesn't work?!

    from collections import defaultdict
    d=defaultdict(lambda:[])
    
    n,k=map(int,input().split(' '))
    
    l=[]
    for i in range(1,n):
        temp=i*i*i
        s="".join(sorted(str(temp)))
        d[s].append(i)
        if len(d[s])==k:
            #d[s].sort()
            l.append(pow(min(d[s]),3))
        
    #print(d)
    new=sorted(l)
    for v in new:
        print(v)
    
    • + 1 comment

      The problem says, 'exactly' k, but in your code you add every number that reachs k.

      from collections import defaultdict
      d=defaultdict(lambda:[])
      
      n,k=map(int,input().split(' '))
      
      l=[]
      for i in range(1,n):
          temp=i*i*i
          s="".join(sorted(str(temp)))
          d[s].append(i)
      
      for item in d.items():
          if len(item[1]) == k:
              l.append(pow(min(item[1]), 3))
      
      new=sorted(l)
      for v in new:
          print(v)
      
      • + 0 comments

        Omg it worked! I realized the mistake. Thank you so much!!!