User Tools

Site Tools


public:t-malv-15-3:3

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

public:t-malv-15-3:3 [2015/09/04 15:48]
hrafnloftsson [2. myscript.py: argv]
public:t-malv-15-3:3 [2015/09/07 13:40] (current)
orvark
Line 128: Line 128:
  
 **If you feel this problem is easy you should also try your hand at problems 31 and 41.** **If you feel this problem is easy you should also try your hand at problems 31 and 41.**
 +
 +===== Possible Solutions =====
 +
 +<code python>
 +#1
 +>>> monty[::-1] == 'nohtyP ytnoM'
 +True
 +
 +#2
 +from sys import argv
 +
 +print('Number of parameters: ', len(argv)-1)
 +print('Script name: ', argv[0])
 +print('First parameter: ', argv[1])
 +print('Second parameter: ', argv[2])
 +
 +#3
 +from sys import argv
 +from nltk import word_tokenize
 +from nltk.corpus import stopwords
 +
 +with open(argv[1]) as infile:
 +    for w in word_tokenize(infile.read()):
 +        if w.lower() not in stopwords.words('english'):
 +            print(w)
 +
 +#Since files are context managers, they can be used in a with-statement.
 +#The file will close when the code block is finished, even if an exception occurs
 +
 +#4
 +from sys import argv
 +from codecs import encode
 +
 +with open(argv[1]) as infile, open(argv[2], 'w') as outfile:
 +    for line in infile:
 +        outfile.write(encode(line, 'rot_13'))
 +
 +#5
 +[(w, len(w)) for w in sent]
 +</code>
/var/www/ailab/WWW/wiki/data/pages/public/t-malv-15-3/3.txt ยท Last modified: 2015/09/07 13:40 by orvark