Download Assignment 1 (alternative)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Department of Computer Science, SUNY Korea
(컴퓨터 과학,한국뉴욕주립대학교)
CSE 101 – Introduction to Computers
Assignment 1 (과제1) Alternate assignment
Spring 2016 학년도 1학기
Grading: Show the instructor all parts of the code during class time. For recording purposes, please
email a copy of your script to the TA.
1.
In the python interpreter, create a list of the following. For your convenience, you can copy
and paste from below
text=["Cherry", "10780", "Tomato", "780", "Strawberry", "9900"]
2.
Write a function to extract only a word (getWord) and only a number (getNumber) from
this list. For your convenience, follow the following format
def …
# document string
# create empty list
# use for loop to get each word in list
# use if statement (not w.isnumeric()) for a word (w.isnumeric()) for a number
# use the .append function to add the accepted word to the list
# return the list
3.
Create a python module called “prices.py” and define the above two functions in this
module.
A.
Create a directory / folder (the desktop is not, in general, a good location for this)
B.
Create a new file in this directory / folder called “prices.py”
i.
This can be done by right-clicking in the folder and selecting new text document;
subsequently, editing the file name. Ensure that the icon changes to a python icon.
C.
Re-write the two function definitions into this file.
D.
In the python interpreter, type in “import prices”
E.
However, modules are imported only one time per interpreter session. Therefore, to
reload the module, you can use:
Import importlib
#this only needs to be done once
reload = lambda: importlib.reload(prices)
reload()
F.
# this will reload the prices module
To run the script, use “prices.<function-name>”
i.
4.
Note: the notation <blah-blah> means to replace it with the correct value.
Create a dictionary of word-prices as follows.
A.
Create a variable to store the information source filename “cse101-a1-inputfilea.txt”
(you may need to store the full path”
B.
Define a function called “createDict” with one argument for the filename. This function
does the following:
i.
Sets the return list to empty
ii.
Uses readlines() to read in the entire file
iii.
For each line in the file
1.
Unpack each line using split()
2.
Append to the return list a packed structure corresponding with a valid
dictionary format
iv.
C.
Return the return list
In the interpreter, create a variable for the dictionary returned from executing the
above function.
D.
Test out dictionary queries. Use the getWrod and getNumber functions to obtain either
the word or its price, respectively.
5.
Helpful Notes
A.
To clear the interpreter screen, one simple way is to do the following
import os
def cls lambda: os.system(‘cls’)
cls()
# this will now clear the screen