blob: d5282fb7fab866cadb847bd8ed57775f749ff560 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def count_unsorted_words(words):
count = 0
for word in words:
count += is_unsorted(word)
return count
def is_unsorted(word):
previous_char = word[0]
for i in range(1, len(word)):
current_char = word[i]
if current_char < previous_char:
return 1
previous_char = current_char
return 0
# Test cases
words1 = ["abc", "xyz", "tsu"]
words2 = ["rat", "cab", "dad"]
words3 = ["x", "y", "z"]
print(count_unsorted_words(words1)) # Output: 1
print(count_unsorted_words(words2)) # Output: 3
print(count_unsorted_words(words3)) # Output: 0
|