Counting abecedarian words in a list: Python - python

Working on a very common problem to identify whether word is abecedarian (all letters in alphabetical order). I can do one word in several ways as discovered in "Think Python"; but, would like to be able to iterate through a list of words determining which are abecedarian and counting those that are.
def start():
lines= []
words= []
for line in open('word_test1.txt'):
lines.append(line.strip())
numlines=len(lines)
count = 0
for word in lines[:]:
i = 0
while i < len(word)-1:
if word[i+1] < word[i]:
return
i = i+1
print (word)
count= count + 1
print (count)
start()
I think my problem lies with the "return" in the "while i" loop. In the list I'm using there are at least three abecedarian words. The above code reads the first two (which are the first entries), prints them, counts them but on the following non-abecedarian word breaks out of the loop and ends the program.
I'm new at programming and this has taken me several hours over a couple of days.

No need for low level programming on this one :-)
def is_abcedarian(s):
'Determine whether the characters are in alphabetical order'
return list(s) == sorted(s)
The use filter to run over a list of words:
>>> filter(is_abcedarian, ['apple', 'bee', 'amp', 'sun'])
['bee', 'amp']

The return statement is breaking out of the entire start() function. There are many possible ways to solve this, but the clearest might be to break your code into two functions like this:
def is_abcedarian(word):
i = 0
while i < len(word)-1:
if word[i+1] < word[i]:
return False
i = i+1
return True
def start():
lines= []
words= []
for line in open('word_test1.txt'):
lines.append(line.strip())
numlines=len(lines)
count = 0
for word in lines[:]:
if is_abcedearian(word):
print (word)
count= count + 1
print (count)
In this example, the return statements in is_abcedarian() returns only from that function, and the return value is then tested by the if statement inside the for loop.
Once you have split apart your program in this way, you have the added benefit of being able to use your is_abcedarian() function from other places (in future related code you might write).

I believe you intended to break from the while loop when you find the letter's are not in order and instead you issued the return statement, which returns you from the function start.
There could be couple of ways to do this
You can use Exception, to raise a StopIteration Exception and catch it outside the while loop.
for word in lines[:]:
try:
i = 0
while i < len(word)-1:
if word[i+1] < word[i]:
raise StopIteration
i = i+1
print (word)
except StopIteration:
None
You can also try setting a flag found and then use it later to test for printing the word

A slightly reorganized approach:
def is_abcedarian(word):
return sorted(s)==list(s)
def main():
# read input file
with open('word_test1.txt') as inf:
words = [line.strip() for line in inf]
# figure out which words are "good"
good_words = [word for word in words if is_abcedarian(word)]
# print the "good" words
print("\n".join(good_words))
print(len(good_words))
if __name__=="__main__":
main()

I like iterools:
from itertools import tee, izip
def pairwise(iterable):
a, b = tee(iterable)
next(b)
return izip(a, b)
def is_abcdarien(word):
return all(c < d for c, d in pairwise(word))
words = 'asdf', 'qwer', 'fghi', 'klmn', 'aabcd', 'abcd'
print filter(is_abcdarien, words)
print len(filter(is_abcdarien, words))
Result:
('fghi', 'klmn', 'abcd')
3
Change c < d to c <= d if you want non-strict ordering, so that "aabcd" is also abcdarian, .

I have this solution for you - I have found it in the same place as you. I hope it still helps.
def is_abecedarian(word):
word.lower()
letter_value=0
for letter in word:
if ord(letter) < letter_value:
return False
else:
letter_value = ord(letter)
return True
fin = open('words.txt')
words_no = 0
for line in fin:
word = line.strip()
if is_abecedarian(word):
words_no = words_no + 1
print words_no

Related

How to remove Triplicate Letters in Python

So I'm a little confused as far as putting this small code together. My teacher gave me this info:
Iterate over the string and remove any triplicated letters (e.g.
"byeee mmmy friiiennd" becomes "bye my friennd"). You may assume any
immediate following same letters are a triplicate.
I've mostly only seen examples for duplicates, so how do I remove triplicates? My code doesn't return anything when I run it.
def removeTriplicateLetters(i):
result = ''
for i in result:
if i not in result:
result.append(i)
return result
def main():
print(removeTriplicateLetters('byeee mmmy friiiennd'))
main()
I have generalized the scenario with "n". In your case, you can pass n=3 as below
def remove_n_plicates(input_string, n):
i=0
final_string = ''
if not input_string:
return final_string
while(True):
final_string += input_string[i]
if input_string[i:i+n] == input_string[i]*n:
i += n
else:
i += 1
if i >= len(input_string):
break
return final_string
input_string = "byeee mmmy friiiennd"
output_string = remove_n_plicates(input_string, 3)
print(output_string)
# bye my friennd
You can use this for any "n" value now (where n > 0 and n < length of input string)
Your code returns an empty string because that's exactly what you coded:
result = ''
for i in result:
...
return result
Since result is an empty string, you don't enter the loop at all.
If you did enter the loop you couldn't return anything:
for i in result:
if i not in result:
The if makes no sense: to get to that statement, i must be in result
Instead, do as #newbie showed you. Iterate through the string, looking at a 3-character slice. If the slice is equal to 3 copies of the first character, then you've identified a triplet.
if input_string[i:i+n] == input_string[i]*n:
Without going in to writing the code to resolve the problem.
When you iterate over the string, add that iteration to a new string.
If the next iteration is the same as the previous iteration then do not add that to the new string.
This will catch both the triple and the double characters in your problem.
Tweaked a previous answer to remove a few lines that were not needed.
def remove_n_plicates(input_string, n):
i=0
result = ''
while(True):
result += input_string[i]
if input_string[i:i+n] == input_string[i]*n:
i += n
else:
i += 1
if i >= len(input_string):
break
return result
input_string = "byeee mmmy friiiennd"
output_string = remove_n_plicates(input_string, 3)
print(output_string)
# bye my friennd
Here's a fun way using itertools.groupby:
def removeTriplicateLetters(s):
return ''.join(k*(l//3+l%3) for k,l in ((k,len(list(g))) for k, g in groupby(s)))
>>> removeTriplicateLetters('byeee mmmy friiiennd')
'bye my friennd'
just modifying #newbie solution and using stack data structure as solution
def remove_n_plicates(input_string, n):
if input_string =='' or n<1:
return None
w = ''
c = 0
if input_string!='':
tmp =[]
for i in range(len(input_string)):
if c==n:
w+=str(tmp[-1])
tmp=[]
c =0
if tmp==[]:
tmp.append(input_string[i])
c = 1
else:
if input_string[i]==tmp[-1]:
tmp.append(input_string[i])
c+=1
elif input_string[i]!=tmp[-1]:
w+=str(''.join(tmp))
tmp=[input_string[i]]
c = 1
w+=''.join(tmp)
return w
input_string = "byeee mmmy friiiennd nnnn"
output_string = remove_n_plicates(input_string, 3)
print(output_string)
output
bye my friennd nn
so this is a bit dirty but it's short and works
def removeTriplicateLetters(i):
result,string = i[:2],i[2:]
for k in string:
if result[-1]==k and result[-2]==k:
result=result[:-1]
else:
result+=k
return result
print(removeTriplicateLetters('byeee mmmy friiiennd'))
bye my friennd
You have already got a working solution. But here, I come with another way to achieve your goal.
def removeTriplicateLetters(sentence):
"""
:param sentence: The sentence to transform.
:param words: The words in the sentence.
:param new_words: The list of the final words of the new sentence.
"""
words = sentence.split(" ") # split the sentence into words
new_words = []
for word in words: # loop through words of the sentence
new_word = []
for char in word: # loop through characters in a word
position = word.index(char)
if word.count(char) >= 3:
new_word = [i for i in word if i != char]
new_word.insert(position, char)
new_words.append(''.join(new_word))
return ' '.join(new_words)
def main():
print(removeTriplicateLetters('byeee mmmy friiiennd'))
main()
Output: bye my friennd

How to access each string in the lists of list

I was given a task to input multiple lines each consisting of multiple words.The task is to uppercase the words with an odd length and lowercase the words with an
even length.
My code now looks like this, can you help me to solve it right?
first = []
while True:
line = input().split()
first.append(line)
if len(line) < 1:
break
for i in first:
for j in i:
if len(line[i][j]) % 2 == 0:
line[i][j] = line[i][j].lower()
elif len(line[i][j]) % 2 != 0:
line[i][j] = line[i][j].upper()
print(first[i])
it should look like this
i and j are not an indexes, they are the sublists and words themselves.You can do:
for i in first: # i is a list of strings
for j in range(len(i)): # you do need the index to mutate the list
if len(i[j]) % 2 == 0:
i[j] = i[j].lower()
else:
i[j] = i[j].upper()
print(' '.join(i))
So looking at the input output in your image, here is a better solution
sentences = []
while True:
word_list = input().split()
sentences = [*sentences, word_list]
if len(word_list) < 1:
break
So now that you have your input from command line you can do
[word.upper() if len(word)%2 == 1 else word.lower() for word_list in sentences for word in word_list]
or you could extract into a function
def apply_case(word):
if len(word)%2:
return word.upper()
return word.lower()
new_sentences = [apply_case(word) for word_list in sentences for word in word_list]
now you can print it like
output = "\n".join([" ".join(word_list) for word_list in new_sentences])
print(output)
You forgot to join the lines back together. Furthermore from a software design point of view, you are doing to much in the code fragment: it is better to encapsulate the functionalities in functions, like:
def wordcase(word):
if len(word) % 2 == 0: # even
return word.lower()
else: # odd
return word.upper()
Then we can even perform the processing "online" (as in line-per-line):
while True:
line = input()
if not line:
break
else:
print(' '.join(wordcase(word) for word in line.split()))
I don't think you need do be using i or j. You can just loop over the words in your string.
Further, although it probably won't speed things up, you don't need the elif, you can just use an else. There are only two options, odd and even so you only need to check it once.
sentance = 'I am using this as a test string with many words'
wordlist = sentance.split()
fixed_wordlist = []
for word in wordlist:
if len(word)%2==0:
fixed_wordlist.append(word.lower())
else:
fixed_wordlist.append(word.upper())
print(sentance, '\n', wordlist, '\n', fixed_wordlist)

Python code for finding number of vowels in the parameter

I am a python newbie, and am struggling for what I thought was a simple code. My instructions are, Write a function that takes one string parameter word and will return the number of vowels in the string.
Also, just for clarification, supercat is my one string parameter word.
I've been working on this code for some time, and it's gotten a little jumbled.
This is what I have so far.
vowelletters = ["A","E","I","O","U","a","e","i","o","u"]
def isVowel(supercat):
if supercat in vowel:
return True
else:
return False
print isVowel(supercat)
def countvowel(supercat):
count = 0
for index in super cat:
if isVowel(vowelletters): count += 1
return count
y = countvowel(super cat)
print(y)
you can first make the string to test lowercase() so that you don't have to check for capital vowels (this make it more efficient). Next you can count() how many times each vowel is in the teststring and make a final sum() to get a total.
vowelletters = ["a","e","i","o","u"]
teststring= "hellO world foo bAr"
count = sum(teststring.lower().count(v) for v in vowelletters)
print count #6
You can place everything in a function to easily reuse the code.
def countVowels(mystring):
vowelletters = ["a","e","i","o","u"]
return sum(mystring.lower().count(v) for v in vowelletters)
Spaces in variable names not allowed, I would say:
for index in super cat:
and
y = countvowel(super cat)
It looks to me as if your indentation has problems and you left an extra space in there. (super cat instead of supercat)
You also used vowelletters instead of index in countvowel() and forgot to use the global statement in isVowel().
vowelletters = ["A","E","I","O","U","a","e","i","o","u"]
def isVowel(supercat):
global vowelletters
if supercat in vowelletters:
return True
else:
return False
print isVowel(supercat) # This isn't executed
# because it is after a return statement.
def countvowel(supercat):
count = 0
for index in supercat:
if isVowel(index): count += 1
return count
y = countvowel("supercat")
print(y)
how about this:
vowelletters = ("a","e","i","o","u")
def countvowel(word):
word = word.lower()
count = 0
for char in word:
if char in vowelletters:
count += 1
return count
print countvowel('super cat') # prints 3
or using the list comprehension:
vowelletters = ("a","e","i","o","u")
def countvowel(word):
word = word.lower()
vowels = [char for char in word if char in vowelletters]
return len(vowels)
You can simplify this function that you're writing
def countvowel(supercat):
count = 0
for i in range(len(supercat)-1):
if supercat[i] in "AEIOUaeiou":
count += 1
print(count)
You can use sum() and a generator. 
def countVowels(word):
return sum(1 for c in word if c in "AEIOUaeiou")
print(countVowels('supercat'))

Exercise 7.9 in "How to Think Like a Computer Scientist (python)" measuring occurrences of a character in a string

The question is how to write a program that measures how many times a character appears in a string in a generalizable way in python.
The code that I wrote:
def countLetters(str, ch):
count=0
index=0
for ch in str:
if ch==str[index]:
count=count+1
index=index+1
print count
when I use this function, it measures the length of the string instead of how many times the character occurs in the string. What did I do wrong? What is the right way to write this code?
You are over-writing your 'ch' variable:
def countLetters(str, ch):
# ^ the character you are looking for
count=0
index=0
for ch in str:
# ^ the string character you are trying to check
if ch==str[index]: # ???
count=count+1
index=index+1
print count
(also, it is usually more useful to return the value than to just print it).
The built-in method is str.count:
"aaabb".count("a") -> 3
How you could rewrite your code:
def countLetters(search_in, search_for):
count = 0
for s in search_in: # iterate by string char, not by index
if s==search_for:
count += 1
return count
and a quick pythonic replacement:
def countLetters(search_in, search_for):
return sum(1 for s in search_in if s==search_for)
Think logically about what happens when you run your code: since the test in the loop succeeds on the first iteration, it is guaranteed to succeed every time! You are simply checking that iteration in Python works.
The correct formulation is
def count(s, input):
count = 0
for c in s:
if c == input:
count += 1
Or, equivalently,
def count(input):
return sum(c == input for c in s)
But you could just as well do:
s.count(c)
Your loop is wrong.
This should work:
for s in str:
if ch == s:
...
this way index variable will not be used and you can remove it. If you want to use index then change for into:
for index in range(len(str)):
... (rest is OK but ...)
... (do not increase index in loop body)
You can also increment variable by += operator like:
cnt += 1
So finished code will look like:
def countLetters(str, ch):
count = 0
for s in str:
if ch == s:
count += 1
print count
Completely untested:
def count_letters(s, c):
return sum(1 for x in s if x == c)

Letter Count on a string

Python newb here. I m trying to count the number of letter "a"s in a given string. Code is below. It keeps returning 1 instead 3 in string "banana". Any input appreciated.
def count_letters(word, char):
count = 0
while count <= len(word):
for char in word:
if char == word[count]:
count += 1
return count
print count_letters('banana','a')
The other answers show what's wrong with your code. But there's also a built-in way to do this, if you weren't just doing this for an exercise:
>>> 'banana'.count('a')
3
Danben gave this corrected version:
def count_letters(word, char):
count = 0
for c in word:
if char == c:
count += 1
return count
Here are some other ways to do it, hopefully they will teach you more about Python!
Similar, but shorter for loop. Exploits the fact that booleans can turn into 1 if true and 0 if false:
def count_letters(word, char):
count = 0
for c in word:
count += (char == c)
return count
Short for loops can generally be turned into list/generator comprehensions. This creates a list of integers corresponding to each letter, with 0 if the letter doesn't match char and 1 if it does, and then sums them:
def count_letters(word, char):
return sum(char == c for c in word)
The next one filters out all the characters that don't match char, and counts how many are left:
def count_letters(word, char):
return len([c for c in word if c == char])
One problem is that you are using count to refer both to the position in the word that you are checking, and the number of char you have seen, and you are using char to refer both to the input character you are checking, and the current character in the string. Use separate variables instead.
Also, move the return statement outside the loop; otherwise you will always return after checking the first character.
Finally, you only need one loop to iterate over the string. Get rid of the outer while loop and you will not need to track the position in the string.
Taking these suggestions, your code would look like this:
def count_letters(word, char):
count = 0
for c in word:
if char == c:
count += 1
return count
A simple way is as follows:
def count_letters(word, char):
return word.count(char)
Or, there's another way count each element directly:
from collections import Counter
Counter('banana')
Of course, you can specify one element, e.g.
Counter('banana')['a']
Your return is in your for loop! Be careful with indentation, you want the line return count to be outside the loop. Because the for loop goes through all characters in word, the outer while loop is completely unneeded.
A cleaned-up version:
def count_letters(word, to_find):
count = 0
for char in word:
if char == to_find:
count += 1
return count
You have a number of problems:
There's a problem with your indentation as others already pointed out.
There's no need to have nested loops. Just one loop is enough.
You're using char to mean two different things, but the char variable in the for loop will overwrite the data from the parameter.
This code fixes all these errors:
def count_letters(word, char):
count = 0
for c in word:
if char == c:
count += 1
return count
A much more concise way to write this is to use a generator expression:
def count_letters(word, char):
return sum(char == c for c in word)
Or just use the built-in method count that does this for you:
print 'abcbac'.count('c')
I see a few things wrong.
You reuse the identifier char, so that will cause issues.
You're saying if char == word[count] instead of word[some index]
You return after the first iteration of the for loop!
You don't even need the while. If you rename the char param to search,
for char in word:
if char == search:
count += 1
return count
Alternatively You can use:
mystring = 'banana'
number = mystring.count('a')
count_letters=""
number=count_letters.count("")
print number
"banana".count("ana") returns 1 instead of 2 !
I think the method iterates over the string (or the list) with a step equal to the length of the substring so it doesn't see this kind of stuff.
So if you want a "full count" you have to implement your own counter with the correct loop of step 1
Correct me if I'm wrong...
def count_letter(word, char):
count = 0
for char in word:
if char == word:
count += 1
return count #Your return is inside your for loop
r = count_word("banana", "a")
print r
3
x=str(input("insert string"))
c=0
for i in x:
if 'a' in i:
c=c+1
print(c)
Following program takes a string as input and output a pandas DataFrame, which represents the letter count.
Sample Input
hello
Sample Output
 char Freq.
0 h  1
1 e  1
2 l  2
3 o  1
import pandas as pd
def count_letters(word, char):
return word.count(char)
text = input()
text_split = text.split()
list1 = []
list2 = []
for i in text_split:
for j in i:
counter = count_letters (text, j)
list1.append(j)
list2.append(counter)
dictn = dict(zip(list1, list2))
df = pd.DataFrame (dictn.items(), columns = ['char', 'freq.'])
print (df)

Categories

Resources