Created
April 11, 2013 22:13
-
-
Save mtourne/5367634 to your computer and use it in GitHub Desktop.
Exercice python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# exercise python #1 | |
import argparse | |
def tokenize_wrong(string, separator): | |
for index, char in enumerate(string): | |
if char == separator: | |
return index | |
# not found | |
return -1 | |
def main(): | |
parser = argparse.ArgumentParser(description= | |
'tokenize test') | |
parser.add_argument('string', type=str, nargs=1, help='string to search') | |
parser.add_argument('separator', type=str, nargs=1, help='list of separator') | |
args = parser.parse_args() | |
string = args.string[0] | |
separator = args.separator[0] | |
index_separator = tokenize_wrong(string, separator) | |
sub_string = "not found" | |
if index_separator != -1: | |
sub_string = string[0:index_separator] | |
print '''The string "{}" with separators "{}" returns: {} | |
\tstring before separator: "{}"'''.format( | |
string, | |
separator, | |
index_separator, | |
sub_string | |
) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment