Question

Use Java or Python. The English word "COINS" is very interesting because one letter at a...

Use Java or Python.

The English word "COINS" is very interesting because one letter at a time can be successfully be removed (from the beginning, middle, or end), and each word along the way is a valid English word - COINS -> COIN -> CON -> ON -> O. Find the longest word with this property. Use an official Scrabble SOWPODS dictionary available at https://github.com/jesstess/Scrabble/blob/master/scrabble/sowpods.txt and augment it to include the three one-letter words 'A', 'I', and 'O'. Provide whatever computer code, algorithm, or technique you used to arrive at your solution.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

you can store words in list also.. but with lists the lookup time will be O(n) and our program runs for a long long time..

in order to reduce the time complexity i stored words in dictionary which will give lookup time O(1).. i stored each word and key like {"hello":1}

#read a lle line by line 9 with open(/home/sys1108/Desktop/sowpods.txt) as f: 10 contentf.readlines() 12#creating values for dictionary insertions 13 values [1]len(content) 14 15#converting it to a dictionary 16 words-dict(zip(content, values)) 17#to store max word length 18 m. 999 19 two for loops used are basically to get all 20 possible combinations of word by 21 remove letters from word and form new 22 word for example word hello 23 hello 24 ello 25 llo 26 lo 27 O 28 hllo 29 hlo 31 helo 32 heo 33 helo 34 for word in words: 35 36 fori in range(len (word)): 37 38 39 #10 form all combinations of word for j in range(i,len (word)): #getting new word new word[:i]+word[j:] #checking word present or not if new in words: if len(new)>m: 42 43 mFlen (new) res-word 46 print(res)

output:

code:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 4 11:55:47 2018

@author: sys1108
"""
#read a file line by line
with open("/home/sys1108/Desktop/sowpods.txt") as f:
content = f.readlines()

#creating values for dictionary insertions
values=[1]*len(content)

#converting it to a dictionary
words=dict(zip(content, values))
#to store max word length
m=-999
'''two for loops used are basically to get all
possible combinations of word by
remove letters from word and form new
word for example word hello
hello
ello
llo
lo
o
hllo
hlo
ho
helo
heo
helo'''
for word in words:
#to form all combinations of word
for i in range(len(word)):
for j in range(i,len(word)):
#getting new word
new=word[:i]+word[j:]
#checking word present or not
if new in words:
if len(new)>m:
m=len(new)
res=word

print(res)

Add a comment
Know the answer?
Add Answer to:
Use Java or Python. The English word "COINS" is very interesting because one letter at a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 2: Finding the best Scrabble word with Recursion using java Scrabble is a game in...

    Question 2: Finding the best Scrabble word with Recursion using java Scrabble is a game in which players construct words from random letters, building on words already played. Each letter has an associated point value and the aim is to collect more points than your opponent. Please see https: //en.wikipedia.org/wiki/Scrabble for an overview if you are unfamiliar with the game. You will write a program that allows a user to enter 7 letters (representing the letter tiles they hold), plus...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT