Friday 18 September 2015

Typewriter

Words that can be typed by using exactly one set of keys, i.e.
Either
Q W E R T Y U I O P
or
A S D F G H J K L
or
Z X C V B N M

import re

if __name__ == '__main__':
    regex = re.compile(r'^([qwertyuiop]*|[asdfghjkl]*|[zxcvbnm]*)$')
    with open('words.txt', 'r') as fptr:
        for word in fptr:
            if regex.match(word):
                meat = regex.match(word).group(1)
                # Print words with length greater than 7:
                if len(meat) > 7:
                    print('%s (%d)' % (meat, len(meat)))
etiquette (9)
perpetuity (10)
pirouette (9)
potpourri (9)
preterite (9)
prettier (8)
priority (8)
property (8)
proprietor (10)
propriety (9)
prototype (9)
puppeteer (9)
puppetry (8)
repertoire (10)
repertory (9)
reporter (8)
territory (9)
torturer (8)
tripwire (8)
typewriter (10)

Typewriter is one of the largest in the result set, but not the only one.


Saturday 1 August 2015

Nth Term

InternetIsBeautiful

A couple of days back I stumbled upon this website: nth-test.com. Nth Test is an nth-child and nth-of-type tester. It requires you to input an nth term of a sequence and it displays the sequence formed corresponding to that nth term. What it basically does is that it calculates the Arithmetic Progression (A.P.) for that nth term. So for example if we input 2n-1, it will output the progression: 1, 3, 5, 7 ... on a tabular block in an interactive way. It makes calculating the nth child easier.

So I came out with Nth Term that does the exact opposite of the above explained concept. It takes the sequence (Arithmetic Progression) as input and displays the nth term for it. It also extends the input sequence and highlights the numbers on a table.

So suppose you need the nth term for the following sequence: 5, 9, 13 ...
All you need to do is type the numbers in the input form separated by commas and you will get the nth term and the extended sequence for the input.

I have defined n as a Natural Number, i.e. n ∈ N. So n can only take values from the set: { 1, 2, 3, 4, ... }. You may enter values of n and check if the output series is correct in the table.

For n = 1, 4n+1 = 5
For n = 2, 4n+1 = 9
For n = 3, 4n+1 = 13
For n = 4, 4n+1 = 17

... and so on.

You may have noticed a smiley to the left of the input form. A smile : ) means a hit, i.e. the nth term has been found. A sad face : ( means the series is not an A.P. and when it is waiting for a proper input by you, it would display an "indifferent" smiley : |

Saturday 28 February 2015

Skepticism


Almost every Classmate notebook has a page at the end with some interesting facts on it. In my notebook, one of the facts went like this:
Skepticism is the longest word that can be typed with alternate hands.
A list of all valid English words is available in Linux systems. You can get them too, /usr/share/dict/words. So I decided to check the fact and retrieve the largest word that can be typed with alternate hands.
I wrote a Python script to do it.

Flip = lambda X: 0 if X == 1 else 1 
Alibaba = lambda X: 0 if X in left_hand else 1

babloo = str()                # Required string    
babloo_len = 0        

left_hand = 'ASDFGQWERTZXCV'  # Alphabets assigned to Left hand
right_hand = 'LKJHPOIUYMNB'   # Alphabets assigned to Right hand

filename = 'words.txt'        # File with all valid words, 
                              # one on each line  

if __name__ == '__main__':    
    my_file = open(filename, 'r') 
    
    for line in my_file:
        word = line.rstrip().upper()
    
        Clock = Alibaba(word[0])

        sync = True
        for alphabet in word:
            if Alibaba(alphabet) != Clock:  
                sync = False                
                break
            Clock = Flip(Clock)

        if sync:
            print(word, end=',')
            if len(word) > babloo_len:
                babloo = word
                babloo_len = len(word)

    my_file.close()

    print("\n")
    print("Largest word is %s with a length of %d alphabets" 
           % (babloo, babloo_len))

# No offence meant, Babloo

This extracts all the required words and displays the largest of them. They came out to be AUTHENTICITY and ABSORPTIVITY, which have a character length of 12. There are others with a character length of 11. These include DISBANDMENT, ENCHANTMENT, ENTITLEMENT, PROFICIENCY and SHANTYTOWNS. You can modify the script to extract them too.

EDIT: A code golf solution

Written keeping in mind to traverse a word just once.

FILENAME = 'words.txt'
required_words = []

for w in map(lambda s: s.upper(), open(FILENAME, 'r').read().splitlines()):
    prev = None
    for now in map(lambda l: 1 if l in 'ASDFGQWERTZXCV' else 0, w):
        if now == prev: break
        prev = now
    else:
        required_words.append(w)

print(max(required_words, key=len))