I am trying to only have the words print out if they occur the same number of times as in the fibonacci sequence. If a words show up 1,2,3,5,8 etc then it will print up. I have gotten the program to print up the words based on how many times the appear. I am having trouble figuring out how to use the sequence in my program. Any tips or examples would be very appreciated.

def fib():
    a,b = 0, 1
    while 1:
            yield a
            a, b= b, a+b

from collections import Counter 
import string


while True:
    filename=raw_input('Enter a file name: ')
    if filename == 'exit':
        break
    try:
        file = open(filename, 'r') 
        text=file.read() 
        file.close() 
    except:
        print('file does not exist')
    else:

        for word in string.punctuation:
            text=text.replace(word, "")
        word_list = text.lower().split(None)
        word_freq = {}
        
        for word in word_list:
            if len(word) > 1:
                word_freq[word] = word_freq.get(word, 0) + 1
          
        print(sorted(word_freq.items(), key=lambda item: item[1])) 
// I am pretty sure something with the seqeunce should go into the above line
// but have been unable to figure it out.         
            
print('Bye')

Recommended Answers

All 4 Replies

I would use itertools.groupby with helper function is_fibonacci, before that the values must be sorted by the sme helper function as key.

I am writing a program where it prints out how the freqeuncy of a word as long as the freqeuncy is in the fibonacci sequence (1,2,3,5,8,etc). I have figured out how to print all the words that appear once, however I am having trouble figuring out how to iterate so it prints out the words that have a higher freqeuncy.

def fib():
    a,b = 0, 1
    while 1:
            yield b
            a, b= b, a+b
import string
import itertools


while True:
    filename=raw_input('Enter a file name: ')
    if filename == 'exit':
        break
    try:
        file = open(filename, 'r') 
        text=file.read() 
        file.close() 
    except:
        print('file does not exist')
    else:

        for word in string.punctuation:
            text=text.replace(word, "")
        word_list = text.lower().split(None)
        word_freq = {}
        
        for word in word_list:
            if len(word) > 1:
                word_freq[word] = word_freq.get(word, 0) + 1
          
        frequencies = sorted(word_freq.items(), key=lambda item: item[1])
        a=fib()
        order=sorted(word_freq.values())
        n=1
        a=next(a)
        for words in frequencies:
            try:
                if a ==words.index(n):
                    print(words)
            except:
                print('nope')//how would I iterate here??

        
print('Bye')

Threads merged.

Simple Python coding rules:
import
define
main program flow

Giving your code a common sense structure makes it easier to read and understand for people you are asking for help.

Also, a few temporary test-prints will help you a lot here!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.