-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackendtest.py
More file actions
40 lines (28 loc) · 885 Bytes
/
backendtest.py
File metadata and controls
40 lines (28 loc) · 885 Bytes
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
#problems 1, 2 and 4
import sys
def reverseletters(r): #reverses letters in a word
answer = []
answer = r[::-1]
return answer
def reversewords(r): #reverses words in a sentence
answer = []
x = r.split()
for elem in x:
answer.insert(0, elem)
string = ' '.join(answer)
return string
def main():
filename = sys.argv[1] #argv[0] being the py file
switch = sys.argv[2] #an arguement for -r and -w
f = open(filename, 'r')
raw = f.read() # file to string
if switch == '-r':
x = reverseletters(raw)
print(x)
if switch == "-w":
x = reversewords(raw)
print(x)
#this project assumes the input file is nothing but single words,
#or words seperated by whitespace. If I were to elaborate, I would use
# .sub(), .split(), .join() and regex to clean up the raw file input.
main()