NPRG065 Programming in Python Labs, Summer 2019
11 June 2019 - Exam
Assignment
- Contact
- David Čepelík <d@dcepelik.cz>
- Schedule
- Monday 12:20 SU2
- Office hours
- After class or scheduled individually via e-mail
- Details
-
Course website,
SIS
Requirements
Please refer to
course website
for information about course requirements.
Please use
ReCodEx
to submit all homework.
Notes
Please provide feedback early.
If you're unhappy about something or if you have any suggestions,
please do let me know. I'll be willing to help.
20 May 2019
- This lab will be the second preparation for the exam test.
- Attendance points for this session will be assigned for submitting solutions for the twelfth set of exercises into your GitHub repo. Please submit them before next class.
- There are no tests, but your code should be reasonably written, commented and you should provide a minimal working example in the repo.
- I will check the solutions and provide feedback in the repo in a few days after submission.
- In case you have any questions or anything is unclear, let me know.
13 May 2019
- This lab will be the first preparation for the exam test. To pass the exam, you will have to program a given assignment in three hours time in the lab.
- Attendance points for this session will be assigned for submitting solutions for the eleventh set of exercises into your GitHub repo. Please submit them before next class.
- There are no tests, but your code should be reasonably written, commented and you should provide a minimal working example in the repo.
- I will check the solutions and provide feedback in the repo in a few days after submission.
- In case you have any questions or anything is unclear, let me know.
6 May 2019
Still not feeling well, so everyone was working on the Tenth set of excercises on their own.
29 April 2019
I was sick, the session was held by Vojtěch Aschenbrenner. (Big thanks.)
22 April 2019
Holiday!
15 April 2019
- Eight set of exercises
- Let's control the Tello drone from Python
- We'll be using
tellopy
to control the drone
- We'll use this simple script to detect faces
- We'll try to make the drone follow the face
- Think about something cool we could do with the Tello the next time and let me know
8 April 2019
- Please read the exercises for next class before attending and try to solve at least some — let's try if it's more fun when you're prepared :)
- Why
s[::-1]
works
- Let's do it the same way as the last time
- Seventh set of exercises
- Please commit your changes to the repo you've created the last time
- Please create a separate directory for this excercise session
- Possible solutions to the exercises from the last time
- I have provided feedback in the repo: run
git pull --rebase
to fetch upstream changes and re-base your current working copy on those. You will find in-line comments where appriopriate and a sum
(summary) file I've created. After rebasing, run git log
, find the commit ID of a commit with message feedback
and git show COMMIT_ID
.
Some general notes:
- You don't want your code to depend on
numpy
because of a single function, use stdlib where possible:
- Better portability
- Less pain for the user
- Less software bloat
- Less security issues
- Please remember to always use
__name__ == "__main__"
.
- It's not enough to test the simplest cases - most people submitted a broken
range
implementation because they didn't test it with a negative step
value.
- Think security:
secrets.choice
FTW!
slice
equivalent for iterators: itertools.islice
.
- Never use
print("error")
to handle errors, always use exceptions (or return values where appropriate).
- Never print errors to
stdout
, if anything, print them to stderr
.
- Don't enforce unnecessary constraints: many people have a
palindrome
implementation which would also work with lists, but they've enforced the type of the argument to be a string
.
- To mark the ommision of a value, use
None
. Typically, you don't want to use 0
or the string "N"
since they may be legitimate values (or they're just weird).
1 April 2019
- Create a GitHub repo for your tasks and add me as a collaborator.
- Work on the tasks of the sixth set of exercises.
- Once finished, push the code to your repo.
25 March 2019
Last time, we've seen a nice heapsort implementation. Since we were focusing on the Python program, we haven't discussed time complexity of the algorithm. It's useful to remember that heap construction takes either O(n) or O(nlog(n)) depending on implementation. (And our implementation constructed the heap in O(nlog(n)) time.)
Today, I'd like to share some tips for efficient (Python) programming:
-
Efficient use of
vim
(example text):
- Don't move around in command mode, it's inefficient!
- Life savers: undo & redo:
u ^R
- Useful moves:
w W b B t T f F gg G ^ $ :20
- Searching:
/ ?
- From command mode to insert mode:
o O a A i I
- To change pieces of text:
r R x X
- Combining editing with moves:
ct cf cw cW
- Visual selections:
v V vi)
- Changing indentation:
> < >> <<
- ...and almost anything can take a numeric prefix:
3dw 10V V2>
- Deleted stuff can be pasted:
cwfoo<ESC>p
, that's really useful for local refactoring
- Re-doing last action:
.
- Block selections are sometimes exceptionally useful:
^V
- Pasting is handy:
yyp
- Some things are plain awesome:
^A ^X
- Changing case:
guiw gUiw
- Powerful regex search:
/foo\|bar
(also, :set hls[earch]
)
- ...and replace:
:s/a\|b/c/g
(also, :%s
)
- ...and much, much more! The key is not to give up and never to misuse the insert mode.
- UCW keyboard layout:
setxkbmap -layout us,cz -variant ,ucw -grp:switch
- ...and putting the Esc where it belongs:
setxkbmap -layout us,cz -variant ,ucw -grp:switch,caps:swapescape
-
Using Git to keep your code-base's history:
- If you want to write any code profesionally, this is just as important as knowing what a red-black tree is.
- It all starts for you with a
git init
(if you're creating a new repo) or git clone ...
(if you're getting a copy of a project) from "upstream".
- Let's start a new repo locally!
mkdir my-repo && cd my-repo && git init .
- Create an empty
.gitignore
file: touch .gitignore
- ...add it:
git add .gitignore
- ...and commit it:
git commit
- Finally, we have got some history to show:
git log
-
The workflow:
- Do your changes
git add
your changes
- Review what you're about to commit:
git diff --cached
- Commit your changes:
git commit [-m 'commit message']
- Review commit log
- Let's work on the first assignment now, using the proper keyboard layout,
vim
and Git.
-
Our final project is awesome and we want to make it OSS - let's host it on GitHub:
- Create a repo on GitHub
git remote add origin git@github.com:dcepelik/awesome-upcase.git
git push
the changes
- Now anybody can
git clone
our project and work on it (or fork it on GitHub).
Please send me your solution of the last assignment ("Create a simple scheduler ...") from the fifth set of assignments before next class:
- Create a repo on your GitHub account to host your solution and add me (dcepelik) as a collaborator
- Work on the assignment, track changes in Git
git push
your changes to the upstream
- Send me an e-mail with a link to your repo
- I'll leave you some feedback in the repository which you can
git pull [--rebase]
later
This is not a hard requirement. But it's a chance for you to get some feedback and show that you're an awesome programmer :).
18 March 2019
Let's finish tasks from the last time: selection sort and heapsort as presented by volunteers. Thanks!
Individual work on the fourth set of assignments.
11 March 2019
Unfinished agenda from previous labs: permutations and binary tree drawing. How did it go? Questions?
Two solutions of the complete binary tree drawing problem were devised: first by V.N. which constructs a bitmap and another one which doesn't need a bitmap and instead manupulates lists (could be easily changed to manipulate strings).
Warm-up exercises:
- Construct literals of basic types (tuples, arrays, hashes, sets).
- Sometimes, it may be necessary to disambiguate between empty sets and empty hashes. How can we do it?
- What are the advantages of a
frozenset
compared to set
?
- How can we construct a list from a tuple, a tuple from a list, a set from a list, a list of hash keys etc.?
- Python Assignment Expressions (PEP 572) are available in Python 3.8
- The
if __name__ == "__main__":
mantra and what's it good for.
- Create a list that for each number in
range(10)
contains a 1-tuple containing that number. Let's try three different ways. Sneak peek: map, filter and reduce.
- How can we use a list as a stack?
- How can we count the number of occurences of something in a list/tuple/string etc.?
Once ready, please try to solve third set of assignments individually. Ask if you need anything!
4 March 2019
Recap of basic UNIX knowledge:
-
Which Linux text editors do you know? Make sure you know how to
create/edit and save a file.
-
Do you know how to create an executable Python script on Linux?
When I "run the script", how does the operating system know that
it should interpret the text as Python code rather than an executable
binary?
-
What other options to execute the script do I have?
Warm-up exercises:
-
Last time, we've seen that
sys.argv
is a list of strings. Write a piece of code that
will concatenate all the strings in sys.argv
into one big string. Example: when
print(sys.argv)
gives ["script.py", "a", "b", "c"]
, your program should print
out script.pyabc
.
-
Calculate n-th Fibonacci number, but try doing it differently than the last time.
There are multiple options: recursion, explicit multiplication through a
for
loop, or reduce
(advanced).
-
Given a list
a
, write a function that returns the reverse of a
(but keeps a
intact).
-
Given two lists
a
and b
, write a function to calculate
dot product
of the two.
As usual, you can work on
the second set of assignments
individually and ask any related questions.
25 February 2019
Unless you have already, please set-up a
ReCodEx
account and join the
Programming in Python (Mon, 12:20, SU2)
group.
The goal of the excercise is individual work on the
assignments.
I will usually do some talking to get you started.
Useful/interesting links:
Recommended vim
configuration for Python development
(put into .vimrc
for vim
or into
.config/nvim/init.vim
for nvim
):
autocmd Filetype python setlocal tabstop=4
autocmd Filetype python setlocal softtabstop=4
autocmd Filetype python setlocal shiftwidth=4
autocmd Filetype python setlocal expandtab
Creating an executable Python script on Linux:
nano hello.py
# edit the file using nano
# don't forget to add the she-bang line (#!/usr/bin/env python3)
chmod u+x hello.py
./hello.py
Hello World!
Last change:
Mon Apr 1 09:15:25 CEST 2019