-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram2.py
More file actions
51 lines (43 loc) · 1.35 KB
/
program2.py
File metadata and controls
51 lines (43 loc) · 1.35 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import sys
def find_duplicates(strings):
word_count = {}
for word in strings:
#converts word to lowercase for case-insensitive comparison
word_lower = word.lower()
if word_lower in word_count:
#updates the count if the word is already seen
word_count[word_lower][1] += 1
else:
#stores the word with its original case and a count of 1
word_count[word_lower] = [word, 1]
#filters the dictionary to only include words with count > 1
duplicates = [original[0] for original in word_count.values() if original[1] > 1]
return duplicates
#function to split a string into words
def split_words(arg):
words = []
word = ""
for char in arg:
if char.isalpha():
word += char
elif word:
words.append(word)
word = ""
if word:
words.append(word)
return words
def main():
if len(sys.argv) < 2:
print("ERROR: You must provide at least one string")
return
words = []
for arg in sys.argv[1:]:
#split each argument into words using the split_words function
words.extend(split_words(arg))
duplicates = find_duplicates(words)
if duplicates:
for word in duplicates:
print(word)
else:
print("No duplicates found.")
main()