First attempts: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
OMG! My weird code works! (P.S. Week ago I didn't know Python exists)<br />
OMG! My first Python code works! (P.S. Week ago I didn't know Python exists)<br />
updated 09_10_15 (search of most_popular and least_popular words added)<br/>
 
https://38.media.tumblr.com/d617c836e0d9f3473c916528f48bc54f/tumblr_nc4o8e06Ut1r93xiko1_r1_500.gif
https://38.media.tumblr.com/d617c836e0d9f3473c916528f48bc54f/tumblr_nc4o8e06Ut1r93xiko1_r1_500.gif


Line 6: Line 6:
import string
import string
punct = set(string.punctuation + string.digits)
punct = set(string.punctuation + string.digits)
fin_sentence = ['.','...','?','!','?!']
longest = None
longest = None
shortest = None
shortest = None
popular_key = None
popular_value = None
nonpopular_key = None
nonpopular_value = None
text = raw_input('Enter some text:')
text = raw_input('Enter some text:')
free_text = ''.join(x for x in text if x not in punct) # deleting all digits and punctuation marks
free_text = ''.join(x for x in text if x not in punct) # deleting all digits and punctuation marks
punctuation = ''.join(x for x in text if x in set(string.punctuation))
sentences = ''.join(x for x in text if x in fin_sentence)
free_text = free_text.lower()
free_text = free_text.lower()
words = free_text.split() #making list named words
words = free_text.split() #making list named words
number = len(words)
number = len(words)
total = 0
total = 0
length = 0
counts = {} #dictionary
counts = {} #dictionary
pop_lst = []
longest_lst = []
shortest_lst = []
for word in words:
for word in words:
total = total + len(word) #counting number of letters
total += len(word) #counting number of letters
if longest is None or len(word) > len(longest): #searching for longest word
if longest is None or len(word) > len(longest): #searching for longest word
longest = word
longest = word
l = len(longest)
if shortest is None or len(word) < len(shortest): #searching for shortest word
if shortest is None or len(word) < len(shortest): #searching for shortest word
shortest = word
shortest = word
s = len(shortest)
counts[word] = counts.get(word,0) + 1 #filling dictionary
counts[word] = counts.get(word,0) + 1 #filling dictionary
v = counts.values()
for word in words:
if len(word) == l:
longest_lst.append(word)
if len(word) == s:
shortest_lst.append(word)
for word,count in counts.items():
for word,count in counts.items():
if popular_value is None or count > popular_value: #searching for most popular word
if count == max(v):
popular_key = word
pop_lst.append(word)
popular_value = count
number_pop = len(pop_lst)
if nonpopular_value is None or count < nonpopular_value: #searching for least popular word
for word in pop_lst:
popular_key = word
length += len(word)
nonpopular_key = word
nonpopular_value = count
if number < 1: #for no text entered
if number < 1: #for no text entered
print 'number of words:', number
print 'number of words:', number
Line 40: Line 49:
print 'number of letters:', total
print 'number of letters:', total
print 'number of words:', number
print 'number of words:', number
print 'number of unique words', len(counts.keys()), ',', counts.keys()
print 'number of unique words:', len(counts.keys())
print 'most popular word is', popular_key, ',', popular_value
print 'number of punctuation marks:', len(punctuation)
print 'least popular word is', nonpopular_key, ',', nonpopular_value
print 'number of sentences:', len(sentences)
print 'longest word is:', longest, ',', len(longest)
print 'average word length is:', float(total)/number
print 'shortest word is:', shortest, ',', len(shortest)
print 'longest words are:', longest_lst, ',', 'length:', len(longest), ',', 'number of words in text:', len(longest_lst)
print 'avarage word lenght is:', float(total)/number
print 'shortest words are:', shortest_lst, ',', 'length:', len(shortest), ',', 'number of words in text:', len(shortest_lst)
print 'most popular words are', pop_lst, ',', 'repetitions:', max(v), 'average length is:', float(length)/number_pop
print 'end of program'
print 'end of program'
</source>
</source>

Latest revision as of 04:21, 7 December 2015

OMG! My first Python code works! (P.S. Week ago I didn't know Python exists)

tumblr_nc4o8e06Ut1r93xiko1_r1_500.gif

import string
punct = set(string.punctuation + string.digits)
fin_sentence = ['.','...','?','!','?!']
longest = None
shortest = None
text = raw_input('Enter some text:')
free_text = ''.join(x for x in text if x not in punct) # deleting all digits and punctuation marks
punctuation = ''.join(x for x in text if x in set(string.punctuation))
sentences = ''.join(x for x in text if x in fin_sentence)
free_text = free_text.lower()
words = free_text.split() #making list named words
number = len(words)
total = 0
length = 0
counts = {} #dictionary
pop_lst = []
longest_lst = []
shortest_lst = []
for word in words:
	total += len(word) #counting number of letters
	if longest is None or len(word) > len(longest): #searching for longest word
		longest = word
		l = len(longest)
	if shortest is None or len(word) < len(shortest): #searching for shortest word
		shortest = word
		s = len(shortest)
	counts[word] = counts.get(word,0) + 1 #filling dictionary
	v = counts.values()
for word in words:		
	if len(word) == l:
		longest_lst.append(word)
	if len(word) == s:
		shortest_lst.append(word)
for word,count in counts.items():
	if count == max(v):
		pop_lst.append(word)
number_pop = len(pop_lst)
for word in pop_lst:
	length += len(word) 
if number < 1: #for no text entered
	print 'number of words:', number
	print 'end of program'
else:
	print 'number of letters:', total
	print 'number of words:', number
	print 'number of unique words:', len(counts.keys())
	print 'number of punctuation marks:', len(punctuation)
	print 'number of sentences:', len(sentences)
	print 'average word length is:', float(total)/number
	print 'longest words are:', longest_lst, ',', 'length:', len(longest), ',', 'number of words in text:', len(longest_lst)
	print 'shortest words are:', shortest_lst, ',', 'length:', len(shortest), ',', 'number of words in text:', len(shortest_lst)
	print 'most popular words are', pop_lst, ',', 'repetitions:', max(v), 'average length is:', float(length)/number_pop
	print 'end of program'