Look and Say Sequence

228 days ago by lbalda

def countnndSay(n): # Base cases if (n == 1): return "1" if (n == 2): return "11" # Every term is generated using # previous term # Initialize previous term s = "11" for i in range(3, n + 1): s += '$' e = len(s) cnt = 1 # Initialize count tmp = "" # Initialize i'th # term in series # Process previous term to # find the next term for j in range(1 , e): # If current character # doesn't match if (s[j] != s[j - 1]): # Append count of # str[j-1] to temp tmp += str(cnt + 0) # Append str[j-1] tmp += s[j - 1] # Reset count cnt = 1 # If matches, then increment # count of matching characters else: cnt += 1 # Update str s = tmp return s; #Results for one variable N = 4 a = (countnndSay(N)) print(a) f= len(countnndSay(N)) print(f) print(log(f)) 
       
1211
4
log(4)
1211
4
log(4)
#setup for results of n terms in a list def listcountnndSay(n): results=[] lengths=[] logs=[] for i in range(1,n): output = countnndSay(i) results.append(output) lengths.append(len(output)) logs.append(log(len(output))) return results, lengths, logs 
       
#set up for size of graph size = 10 results, lengths, logs = listcountnndSay(size) x = srange(0,size) 
       
#results for n terms in a list print(results) print(lengths) print(logs) 
       
['1', '11', '21', '1211', '111221', '312211', '13112221', '1113213211',
'31131211131221']
[1, 2, 2, 4, 6, 6, 8, 10, 14]
[0, log(2), log(2), log(4), log(6), log(6), log(8), log(10), log(14)]
['1', '11', '21', '1211', '111221', '312211', '13112221', '1113213211', '31131211131221']
[1, 2, 2, 4, 6, 6, 8, 10, 14]
[0, log(2), log(2), log(4), log(6), log(6), log(8), log(10), log(14)]
#graph for lengths list_plot(lengths) 
       
#graph for logs list_plot(logs)