-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathright_most_char.py
89 lines (79 loc) · 2.84 KB
/
right_most_char.py
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
#-------------------------------------------------------------------------------
# Name: rightmost_char.py
# Purpose: Given a string 'S' and a character 't' print out the rightmost
# occurrence of 't' (case matters) in 'S' or -1 if there is none.
# The position to be printed out is zero based.
#
# Author: cbrink
#
# Created: 24/11/2015
# Copyright: (c) cbrink 2015
#-------------------------------------------------------------------------------
import sys
import traceback
# ------------------------------------------------------------------------------
def letter_t(lines=None):
try:
# Create new list from 'lines' that has removed all blank strings...
newLines = [string for string in lines if string != '\n']
# Find what letter 't' is...
for line in newLines:
for char in line:
if char == ',':
t = line.index(char) + 1
find_t(t, line)
except Exception, err:
print('print_exc():')
traceback.print_exc(file=sys.stdout)
# ------------------------------------------------------------------------------
def find_t(t=None,
string=None):
try:
S = string
tIndexes = []
for char in S:
# Check for case of letter...
if char.lower() == S[t].lower():
print(char.lower(), ' = ', S[t].lower())
# Create list of occurences of t....
tIndexes.append(S.index(char))
else:
if char == ',':
break
# Pass 't' occurrences list to print function...
print_output(tIndexes)
except Exception, err:
print('print_exc():')
traceback.print_exc(file=sys.stdout)
# ------------------------------------------------------------------------------
def print_output(t_occurence=None):
try:
# If no 't' was found print -1...
if len(t_occurence) == 0:
no_t = -1
print(no_t)
else:
if len(t_occurence) >= 1:
# Print right most occurence of 't' that is zero based...
rm_t = t_occurence[-1]
print(rm_t)
except Exception, err:
print('print_exc():')
traceback.print_exc(file=sys.stdout)
#-------------------------------------------------------------------------------
def main():
try:
test_cases = open(sys.argv[1],
'r')
lines = []
for test in test_cases:
lines.append(str(test))
letter_t(lines)
test_cases.close()
sys.exit(0)
except Exception, err:
print('print_exc():')
traceback.print_exc(file=sys.stdout)
# ------------------------------------------------------------------------------
if __name__ == '__main__':
main()