Task: Four is the number of letters in the ...




Go: package main

import (
	"fmt"
	"strings"
	"unicode"
)

func main() {
	f := NewFourIsSeq()
	fmt.Print("The lengths of the first 201 words are:")
	for i := 1; i <= 201; i++ {
		if i%25 == 1 {
			fmt.Printf("\n%3d: ", i)
		}
		_, n := f.WordLen(i)
		fmt.Printf(" %2d", n)
	}
	fmt.Println()
	fmt.Println("Length of sentence so far:", f.TotalLength())
	
	for i := 1000; i <= 1e7; i *= 10 {
		w, n := f.WordLen(i)
		fmt.Printf("Word %8d is %q, with %d letters.", i, w, n)
		fmt.Println("  Length of sentence so far:", f.TotalLength())
	}
}

type FourIsSeq struct {
	i     int      
	words []string 
}

func NewFourIsSeq() *FourIsSeq {
	return &FourIsSeq{
		
		words: []string{
			"Four", "is", "the", "number",
			"of", "letters", "in", "the",
			"first", "word", "of", "this", "sentence,",
		},
	}
}


func (f *FourIsSeq) WordLen(w int) (string, int) {
	for len(f.words) < w {
		f.i++
		n := countLetters(f.words[f.i])
		ns := say(int64(n))
		os := sayOrdinal(int64(f.i+1)) + ","
		
		f.words = append(f.words, strings.Fields(ns)...)
		f.words = append(f.words, "in", "the")
		f.words = append(f.words, strings.Fields(os)...)
	}
	word := f.words[w-1]
	return word, countLetters(word)
}



func (f FourIsSeq) TotalLength() int {
	cnt := 0
	for _, w := range f.words {
		cnt += len(w) + 1
	}
	return cnt - 1
}

func countLetters(s string) int {
	cnt := 0
	for _, r := range s {
		if unicode.IsLetter(r) {
			cnt++
		}
	}
	return cnt
}










Python: 




import inflect

def count_letters(word):
    
    count = 0
    for letter in word:
        if letter != ',' and letter !='-' and letter !=' ':
            count += 1
            
    return count
    
def split_with_spaces(sentence):
    
    sentence_list = []
    curr_word = ""
    for c in sentence:
        if c == " " and curr_word != "":
            
            
            sentence_list.append(curr_word+" ")
            curr_word = ""
        else:
            curr_word += c
    
    
    
    if len(curr_word) > 0:
        sentence_list.append(curr_word)
    
    return sentence_list
    
def my_num_to_words(p, my_number):
    
    
    number_string_list = p.number_to_words(my_number, wantlist=True, andword='')
    
    number_string = number_string_list[0]
    
    for i in range(1,len(number_string_list)):
        number_string += " " + number_string_list[i]
    
    return number_string
        
def build_sentence(p, max_words):
    
    
    
    
    sentence_list = split_with_spaces("Four is the number of letters in the first word of this sentence,")
      
    num_words = 13
    
    
    
    
    word_number = 2
    
    
    
    while num_words < max_words:
        
        
        
        
        
        ordinal_string = my_num_to_words(p, p.ordinal(word_number))
        
        
        
        word_number_string = my_num_to_words(p, count_letters(sentence_list[word_number - 1]))
        
        
        
        new_string = " "+word_number_string+" in the "+ordinal_string+","

        new_list = split_with_spaces(new_string)
        
        sentence_list += new_list

        
        
        num_words += len(new_list)
        
        
        
        word_number += 1
        
    return sentence_list, num_words
    
def word_and_counts(word_num):
    
        
    sentence_list, num_words = build_sentence(p, word_num)
    
    word_str = sentence_list[word_num - 1].strip(' ,')
    
    num_letters = len(word_str)
    
    num_characters = 0
    
    for word in sentence_list:
       num_characters += len(word)
       
    print('Word {0:8d} is "{1}", with {2} letters.  Length of the sentence so far: {3}  '.format(word_num,word_str,num_letters,num_characters))
   
    
p = inflect.engine()

sentence_list, num_words = build_sentence(p, 201)

print(" ")
print("The lengths of the first 201 words are:")
print(" ")

print('{0:3d}:  '.format(1),end='')

total_characters = 0

for word_index in range(201):

    word_length = count_letters(sentence_list[word_index])
    
    total_characters += len(sentence_list[word_index])
    
    print('{0:2d}'.format(word_length),end='')
    if (word_index+1) % 20 == 0:
        
        print(" ")
        print('{0:3d}:  '.format(word_index + 2),end='')
    else:
        print(" ",end='')
 
print(" ")
print(" ")
print("Length of the sentence so far: "+str(total_characters))
print(" ")



word_and_counts(1000)
word_and_counts(10000)
word_and_counts(100000)
word_and_counts(1000000)
word_and_counts(10000000)