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.
defdecibinaryNumbers(x):defdecibinary(n):#createtheequationandlengthaccordingtotheintegernlong=int(math.log(n,2)+1)list_x=[]foriinrange(long):a=2**ilist_x.append(a)#ifnin(4:7)-list_x=[421]list_x.reverse()#ifnin(8:15)-list_x=[8421]# print(list_x)# create a dictionary with all the ranks of each element according to the length of the equation log(n,2)variables={}foriinrange(len(list_x)):key_var="range_"+str(i+1)value_var=list(range(0,int(n/list_x[i])+1))variables[key_var]=value_var#print(variables.items())list_prod=product(*variables.values())prod_values=list(list_prod)# print(prod_values)# create the variables according to the number of lists in the dictcharact=[]forpainrange(len(list_x)):charact.append(f"{chr(97+pa)}") characters=",".join(charact)#print(characters)iflen(list_x)==2:combinations=[(b,a)forb,ainprod_valuesif2*b+a==n]iflen(list_x)==3:combinations=[(c,b,a)forc,b,ainprod_valuesif4*c+2*b+a==n]iflen(list_x)==4:combinations=[(d,c,b,a)ford,c,b,ainprod_valuesif8*d+4*c+2*b+a==n]iflen(list_x)==5:combinations=[(e,d,c,b,a)fore,d,c,b,ainprod_valuesif16*e+8*d+4*c+2*b+a==n]iflen(list_x)==6:combinations=[(f,e,d,c,b,a)forf,e,d,c,b,ainprod_valuesif32*f+16*e+8*d+4*c+2*b+a==n]iflen(list_x)==7:combinations=[(g,f,e,d,c,b,a)forg,f,e,d,c,b,ainprod_valuesif64*g+32*f+16*e+8*d+4*c+2*b+a==n]numeros=[int(''.join(map(str,t)).replace(',',''))fortincombinations]#print(numeros) returnnumeroslist_total=[0,1]m=2whilelen(list_total)<x:list_total+=decibinary(m)m+=1#print(list_total)return(list_total[x-1])
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Decibinary Numbers
You are viewing a single comment's thread. Return to all comments →
this code is crazy, my god give me patience