-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathlab02.py
More file actions
95 lines (82 loc) · 2.35 KB
/
lab02.py
File metadata and controls
95 lines (82 loc) · 2.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import string
def numberOfLannister(myFile):
num = 0
for str in myFile:
tmp = str.split(' ')
for word in tmp:
if 'Lannister' in word:
num+=1
return num
def inverseLetter(str):
resStr = ''
for ch in str:
if 'A' <= ch <= 'Z':
resStr += ch.lower()
elif 'a' <= ch <= 'z':
resStr += ch.upper()
else:
resStr += ch
return resStr
def longestWord(inputName, outputName):
try:
maxLength = 0
words = []
infile = open(inputName,"r")
outfile = open(outputName,"w")
for str in infile:
newStr = ''
for ch in str:
if ch not in string.punctuation:
newStr += ch
newStr.split()
listOfWords = newStr.split()
for word in listOfWords:
if len(word) > maxLength:
words = []
words.append(word)
maxLength = len(word)
elif len(word) == maxLength:
words.append(word)
print('The length of longest words is',maxLength,'\nThe list of them:',file=outfile)
for str in words:
print(str, file=outfile)
infile.close()
outfile.close()
except FileNotFoundError:
print('The given file is not findable.')
#MAIN------------------------------------------------
# # EX1:
while True:
try:
a = int(input('Give me a number: '))
print(a)
break
except ValueError:
print('That was not a valid number.')
# # EX2:
try:
myFile = open("C:\\Users\\otthoni\\Desktop\\input.txt","r")
for str in myFile:
print(str)
myFile.close()
except FileNotFoundError:
print('The given file is not findable.')
# # EX3:
try:
myFile = open("input.txt","r")
print('"Lannister" occurs', numberOfLannister(myFile), 'times in the text.')
myFile.close()
except FileNotFoundError:
print('The given file is not findable.')
# # EX4:
try:
infile = open("..\\input.txt","r")
outfile = open("..\\output.txt","w")
for str in infile:
print(inverseLetter(str),file=outfile)
infile.close()
outfile.close()
except FileNotFoundError:
print('The given file is not findable.')
# EX5:
longestWord('..\\input.txt','..\\longestWords.txt')