Skip to content

Commit dbf8033

Browse files
authored
Merge pull request #1 from geekcomputers/master
update for Feb 2017
2 parents 5dd84de + 8f98168 commit dbf8033

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

CountMillionCharacter.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
#test by kjin
2-
import pprint
3-
info = '''SCENE I. Yorkshire. Gaultree Forest.
1+
'''Simple million word count program.
2+
main idea is Python pairs words
3+
with the number of times
4+
that number appears in the triple quoted string.
5+
Credit to William J. Turkel and Adam Crymble for the word
6+
frequency code used below. I just merged the two ideas.
7+
'''
48

9+
wordstring = '''SCENE I. Yorkshire. Gaultree Forest.
510
Enter the ARCHBISHOP OF YORK, MOWBRAY, LORD HASTINGS, and others
611
ARCHBISHOP OF YORK
712
What is this forest call'd?
@@ -29,7 +34,6 @@
2934
Thus do the hopes we have in him touch ground
3035
And dash themselves to pieces.
3136
Enter a Messenger
32-
3337
HASTINGS
3438
Now, what news?
3539
Messenger
@@ -43,7 +47,6 @@
4347
ARCHBISHOP OF YORK
4448
What well-appointed leader fronts us here?
4549
Enter WESTMORELAND
46-
4750
MOWBRAY
4851
I think it is my Lord of Westmoreland.
4952
WESTMORELAND
@@ -226,7 +229,6 @@
226229
ARCHBISHOP OF YORK
227230
My lord, we will do so.
228231
Exit WESTMORELAND
229-
230232
MOWBRAY
231233
There is a thing within my bosom tells me
232234
That no conditions of our peace can stand.
@@ -279,7 +281,6 @@
279281
Be it so.
280282
Here is return'd my Lord of Westmoreland.
281283
Re-enter WESTMORELAND
282-
283284
WESTMORELAND
284285
The prince is here at hand: pleaseth your lordship
285286
To meet his grace just distance 'tween our armies.
@@ -288,9 +289,14 @@
288289
ARCHBISHOP OF YORK
289290
Before, and greet his grace: my lord, we come.
290291
Exeunt'''
291-
count = {}
292-
for character in info.upper():
293-
count[character] = count.get(character, 0) + 1
294292

295-
value = pprint.pformat(count)
296-
print(value)
293+
wordlist = wordstring.split()
294+
295+
wordfreq = []
296+
for w in wordlist:
297+
wordfreq.append(wordlist.count(w))
298+
299+
print("String\n" + wordstring +"\n")
300+
print("List\n" + str(wordlist) + "\n")
301+
print("Frequencies\n" + str(wordfreq) + "\n")
302+
print("Pairs\n" + str(list(zip(wordlist, wordfreq))))

batch_file_rename.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import os
1313
import sys
14-
14+
import argparse
1515

1616
def batch_rename(work_dir, old_ext, new_ext):
1717
'''
@@ -24,25 +24,38 @@ def batch_rename(work_dir, old_ext, new_ext):
2424
file_ext = os.path.splitext(filename)[1]
2525
# Start of the logic to check the file extensions, if old_ext = file_ext
2626
if old_ext == file_ext:
27-
# Set newfile to be the filename, replaced with the new extension
28-
newfile = filename.replace(old_ext, new_ext)
27+
# Returns changed name of the file with new extention
28+
name_list=list(filename)
29+
name_list[len(name_list)-len(old_ext):]=list(new_ext)
30+
newfile=''.join(name_list)
31+
2932
# Write the files
3033
os.rename(
3134
os.path.join(work_dir, filename),
3235
os.path.join(work_dir, newfile)
3336
)
3437

38+
def get_parser():
39+
parser = argparse.ArgumentParser(description='change extension of files in a working directory')
40+
parser.add_argument('work_dir', metavar='WORK_DIR', type=str, nargs=1, help='the directory where to change extension')
41+
parser.add_argument('old_ext', metavar='OLD_EXT', type=str, nargs=1, help='old extension')
42+
parser.add_argument('new_ext', metavar='NEW_EXT', type=str, nargs=1, help='new extension')
43+
return parser
3544

3645
def main():
3746
'''
3847
This will be called if the script is directly invoked.
3948
'''
49+
# adding command line argument
50+
parser = get_parser()
51+
args = vars(parser.parse_args())
52+
4053
# Set the variable work_dir with the first argument passed
41-
work_dir = sys.argv[1]
54+
work_dir = args['work_dir'][0]
4255
# Set the variable old_ext with the second argument passed
43-
old_ext = sys.argv[2]
56+
old_ext = args['old_ext'][0]
4457
# Set the variable new_ext with the third argument passed
45-
new_ext = sys.argv[3]
58+
new_ext = args['new_ext'][0]
4659

4760
batch_rename(work_dir, old_ext, new_ext)
4861

0 commit comments

Comments
 (0)