Python 3: Finding word which appears the most times without using import or counter or dictionary, only simple tools like .split() and .lower()
Question:
I am currently learning Python and trying to solve a free tutorial question.
Here is the question.
A writer is working on their newest poem, Turing and the Machines. They have hired you to determine the word which appears the most times. You can access the lines of the poem by calling input() repeatedly, and the last line contains the three characters ###. All lines consist of words separated by single spaces; there are no digits or punctuation. Convert all the words to lower-case, and print the word that occurs the most times (we guarantee there will not be a tie).
For example, if the input is
Here is a line like sparkling wine
Line up fast or be the last
# # #
Then the output should be
line
since it appears twice and no other word appears twice.
……….Below is the closest thing to a solution I can get.
It still lacks , among other things, the facility to allow a user to call input() repeatedly.
And it only prints out the maximum number of the most used word , not the word itself that the question wants.
The poem is supplied by the website shell , at
http://cscircles.cemc.uwaterloo.ca/15b-python-pushups/
Let’s assume the user will input the poem one line at a time.
Please help. Thanks a million.
……….
def poem(P):
P_lower = P.lower()
P_split = P_lower.split()
word_list = []
wordfreq = []
for i in P_split:
word_list.append(i)
for i in P_split:
wordfreq.append(P_split.count(i))
print(max(wordfreq))
poem('Here is a line like line sparkling line wine')
Answers:
This doesn’t use any of the “restricted” tools but only lower(), split() and sort() to return the word with the most occurrences.
def main():
done = False
P = ""
while not done:
new_line = input()
if new_line != "###":
P += new_line + " "
else:
done = True
poem_words = P.lower().split()
poem_words.sort()
# Initialize variables
temp = ""
max_count = 0
icount = 1
max_word = ""
# Do the loop
for i in range(len(poem_words)):
if temp == poem_words[i]:
icount += 1
else:
temp = poem_words[i]
icount = 1
if icount > max_count:
max_count = icount
max_word = temp
print(max_word)
return
if __name__ == "__main__":
main()
By the way, for those who are interested in the great answer by koukouviou earlier and that works in a Python 3 shell, here it is:
def words(P):
poem_words = P.lower().split()
poem_words.sort()
# Remove the # signs
poem_words = [x for x in poem_words if (x != '#' and x != '###')]
# Initialize variables
temp = ""
max_count = 0
icount = 1
max_word = ""
# Do the loop
for i in range(len(poem_words)):
if temp == poem_words[i]:
icount += 1
else:
temp = poem_words[i]
icount = 1
if icount > max_count:
max_count = icount
max_word = temp
return max_word
poem = "Here is a line like sparkling winenLine up fast or be the lastn# # #"
print(words(poem))
attached a newer version as the input are keep coming through input
A = []
counter = []
while True:
S = input().lower()
if S == "###":
print(A[counter.index(max(counter))])
break
SS = S.split()
for i in range(0,len(SS)):
if SS[i] not in A:
A.append(SS[i])
counter.append(0)
else:
counter[A.index(SS[i])]+= 1
Here is my approach. My code includes the possibility if two or more words have the same number
of appearance.
def reading():
global ListLine # We make variable 'ListLine' global in case if function CountWord() does not take it
ListLine = [] # We define an empty list 'ListLine'
while True:
Line = input() # A line of poem is entered
NewLine = Line.split() # We make a list 'NewLine' of individual words
for i in range(0, len(NewLine)):
NewString = NewLine[i].lower() # For each word in list 'NewLine' we make it lowercase
NewLine.insert(i, NewString) # We add to a list a lowercase version of a individual word value at position i
del NewLine[i + 1] # We delete original word at position i + 1
ListLine = ListLine + NewLine
if ListLine[len(ListLine) - 1] == "###": # If line ends with '###', the loop ends
break
return ListLine # We get a list of individual words
def CountWord(ListLine):
CountList = [] # We define an empty list 'CountList'
for i in range(0, len(ListLine)):
CountWord = ListLine.count(ListLine[i]) # Counting how many times an individual word appears in 'ListLine'
CountList.insert(i, CountWord) # We add to a list the result of frequency of appearance of an individual word at position i
return CountList # We get a list of counts of individual words
def Word(CountList):
global WordList # We make variable 'WordList' global in case if function RemoveDuplicate() does not take it
WordList = [] # We defined an empty list 'WordList'
for i in range(0, len(CountList)):
if CountList[i] == max(CountList): # For each index in 'CountList' that has a value of highest count of appearance of individual word,
Word = ListLine[i] # get a word from 'ListLine' which have the same index
WordList.insert(i, Word) # Add to a list a word at position i a.k.a index
elif CountList[i] != max(ListLine): # If above condition was not met, we skip a step
continue
return WordList # We get a list of individual words that appears the most in 'NewLine', but might contains duplicates.
def RemoveDuplicate(Wordlist):
ResultList = [] # We define an empty list 'CountList'
for i in WordList:
if i not in ResultList: # To remove duplicates we define a condtiotion; if a list 'ResulList' does not contain
# an individual word we insert in a list.
ResultList.append(i)
String = ' '.join(ResultList) # We convert a list into a string
if String.endswith('###'): # If string ends with '###', we remove last four characters (space included),
# else return just final result
return String[:-4]
else:
return String
print(RemoveDuplicate(Word(CountWord(reading())))) # Print the output
I’m working through the same tutorial and came up with the code sample below. It’s not great code but keeps to the limits of the tutorial and is commented to explain each step. This does pass the tester on the source page.
#create 4 lists
newList=[] #to intake each line of new text
fulList=[] #combine all text in one long list
uniqueList=[] #list of unique words
wordFreq=[] #frequency of those unique words
#take in all lines
while True:
newLine=input()
if newLine=="###":
break
newList=newLine.lower().split() #convert all words to lowercase
fulList=fulList+newList #add this to the fulList of all words
#clear memory of items we no longer need
newList=[]
newLine=[]
#find and store all unique words
for i in range(0,len(fulList)): #scan all of fulList
checkWord = fulList[i]
if checkWord in uniqueList: #if we already have that word, skip it
continue
uniqueList.append(checkWord) #otherwise add it to the uniqueList
#create a blank freq table of same length as uniqueList
#technically this was created on line 5, but now you understand the context
#note the use of len(uniqueList) so both lists are the same length
wordFreq=[0]*(len(uniqueList))
#now iterate through each word in your uniqueList using range i
#checking for every word in the fullList which we will iterate using range j
for i in range(0,len(uniqueList)):
for j in range(0,len(fulList)):
if uniqueList[i]==fulList[j]:
wordFreq[i] = wordFreq[i]+1 #increase the count for that item
#create a variable for most common word, this is the largest number in wordFreq
maxWord = max(wordFreq)
#iterate through wordFreq to find that value
for i in range(0,len(wordFreq)):
if wordFreq[i]==maxWord:
print(uniqueList[i]) #print that word from uniqueList
My solution is simplier than above.
poem=''
while True:
poem=poem+' '+(input())
if poem.endswith('###'): break
poem=poem.lower()
listOFstring=poem.split()
l=[]
for i in listOFstring:
l.append(listOFstring.count(i))
print(listOFstring[l.index(max(l))])
from statistics import mode
List = []
while True:
line = input()
if line == '###':
break
else:
List = List + line.lower().split()
print(mode(List))
I am currently learning Python and trying to solve a free tutorial question.
Here is the question.
A writer is working on their newest poem, Turing and the Machines. They have hired you to determine the word which appears the most times. You can access the lines of the poem by calling input() repeatedly, and the last line contains the three characters ###. All lines consist of words separated by single spaces; there are no digits or punctuation. Convert all the words to lower-case, and print the word that occurs the most times (we guarantee there will not be a tie).
For example, if the input is
Here is a line like sparkling wine
Line up fast or be the last
# # #
Then the output should be
line
since it appears twice and no other word appears twice.
……….Below is the closest thing to a solution I can get.
It still lacks , among other things, the facility to allow a user to call input() repeatedly.
And it only prints out the maximum number of the most used word , not the word itself that the question wants.
The poem is supplied by the website shell , at
http://cscircles.cemc.uwaterloo.ca/15b-python-pushups/
Let’s assume the user will input the poem one line at a time.
Please help. Thanks a million.
……….
def poem(P):
P_lower = P.lower()
P_split = P_lower.split()
word_list = []
wordfreq = []
for i in P_split:
word_list.append(i)
for i in P_split:
wordfreq.append(P_split.count(i))
print(max(wordfreq))
poem('Here is a line like line sparkling line wine')
This doesn’t use any of the “restricted” tools but only lower(), split() and sort() to return the word with the most occurrences.
def main():
done = False
P = ""
while not done:
new_line = input()
if new_line != "###":
P += new_line + " "
else:
done = True
poem_words = P.lower().split()
poem_words.sort()
# Initialize variables
temp = ""
max_count = 0
icount = 1
max_word = ""
# Do the loop
for i in range(len(poem_words)):
if temp == poem_words[i]:
icount += 1
else:
temp = poem_words[i]
icount = 1
if icount > max_count:
max_count = icount
max_word = temp
print(max_word)
return
if __name__ == "__main__":
main()
By the way, for those who are interested in the great answer by koukouviou earlier and that works in a Python 3 shell, here it is:
def words(P):
poem_words = P.lower().split()
poem_words.sort()
# Remove the # signs
poem_words = [x for x in poem_words if (x != '#' and x != '###')]
# Initialize variables
temp = ""
max_count = 0
icount = 1
max_word = ""
# Do the loop
for i in range(len(poem_words)):
if temp == poem_words[i]:
icount += 1
else:
temp = poem_words[i]
icount = 1
if icount > max_count:
max_count = icount
max_word = temp
return max_word
poem = "Here is a line like sparkling winenLine up fast or be the lastn# # #"
print(words(poem))
attached a newer version as the input are keep coming through input
A = []
counter = []
while True:
S = input().lower()
if S == "###":
print(A[counter.index(max(counter))])
break
SS = S.split()
for i in range(0,len(SS)):
if SS[i] not in A:
A.append(SS[i])
counter.append(0)
else:
counter[A.index(SS[i])]+= 1
Here is my approach. My code includes the possibility if two or more words have the same number
of appearance.
def reading():
global ListLine # We make variable 'ListLine' global in case if function CountWord() does not take it
ListLine = [] # We define an empty list 'ListLine'
while True:
Line = input() # A line of poem is entered
NewLine = Line.split() # We make a list 'NewLine' of individual words
for i in range(0, len(NewLine)):
NewString = NewLine[i].lower() # For each word in list 'NewLine' we make it lowercase
NewLine.insert(i, NewString) # We add to a list a lowercase version of a individual word value at position i
del NewLine[i + 1] # We delete original word at position i + 1
ListLine = ListLine + NewLine
if ListLine[len(ListLine) - 1] == "###": # If line ends with '###', the loop ends
break
return ListLine # We get a list of individual words
def CountWord(ListLine):
CountList = [] # We define an empty list 'CountList'
for i in range(0, len(ListLine)):
CountWord = ListLine.count(ListLine[i]) # Counting how many times an individual word appears in 'ListLine'
CountList.insert(i, CountWord) # We add to a list the result of frequency of appearance of an individual word at position i
return CountList # We get a list of counts of individual words
def Word(CountList):
global WordList # We make variable 'WordList' global in case if function RemoveDuplicate() does not take it
WordList = [] # We defined an empty list 'WordList'
for i in range(0, len(CountList)):
if CountList[i] == max(CountList): # For each index in 'CountList' that has a value of highest count of appearance of individual word,
Word = ListLine[i] # get a word from 'ListLine' which have the same index
WordList.insert(i, Word) # Add to a list a word at position i a.k.a index
elif CountList[i] != max(ListLine): # If above condition was not met, we skip a step
continue
return WordList # We get a list of individual words that appears the most in 'NewLine', but might contains duplicates.
def RemoveDuplicate(Wordlist):
ResultList = [] # We define an empty list 'CountList'
for i in WordList:
if i not in ResultList: # To remove duplicates we define a condtiotion; if a list 'ResulList' does not contain
# an individual word we insert in a list.
ResultList.append(i)
String = ' '.join(ResultList) # We convert a list into a string
if String.endswith('###'): # If string ends with '###', we remove last four characters (space included),
# else return just final result
return String[:-4]
else:
return String
print(RemoveDuplicate(Word(CountWord(reading())))) # Print the output
I’m working through the same tutorial and came up with the code sample below. It’s not great code but keeps to the limits of the tutorial and is commented to explain each step. This does pass the tester on the source page.
#create 4 lists
newList=[] #to intake each line of new text
fulList=[] #combine all text in one long list
uniqueList=[] #list of unique words
wordFreq=[] #frequency of those unique words
#take in all lines
while True:
newLine=input()
if newLine=="###":
break
newList=newLine.lower().split() #convert all words to lowercase
fulList=fulList+newList #add this to the fulList of all words
#clear memory of items we no longer need
newList=[]
newLine=[]
#find and store all unique words
for i in range(0,len(fulList)): #scan all of fulList
checkWord = fulList[i]
if checkWord in uniqueList: #if we already have that word, skip it
continue
uniqueList.append(checkWord) #otherwise add it to the uniqueList
#create a blank freq table of same length as uniqueList
#technically this was created on line 5, but now you understand the context
#note the use of len(uniqueList) so both lists are the same length
wordFreq=[0]*(len(uniqueList))
#now iterate through each word in your uniqueList using range i
#checking for every word in the fullList which we will iterate using range j
for i in range(0,len(uniqueList)):
for j in range(0,len(fulList)):
if uniqueList[i]==fulList[j]:
wordFreq[i] = wordFreq[i]+1 #increase the count for that item
#create a variable for most common word, this is the largest number in wordFreq
maxWord = max(wordFreq)
#iterate through wordFreq to find that value
for i in range(0,len(wordFreq)):
if wordFreq[i]==maxWord:
print(uniqueList[i]) #print that word from uniqueList
My solution is simplier than above.
poem=''
while True:
poem=poem+' '+(input())
if poem.endswith('###'): break
poem=poem.lower()
listOFstring=poem.split()
l=[]
for i in listOFstring:
l.append(listOFstring.count(i))
print(listOFstring[l.index(max(l))])
from statistics import mode
List = []
while True:
line = input()
if line == '###':
break
else:
List = List + line.lower().split()
print(mode(List))