Week 1#

Python refresher and Git#

The goal of this week’s exercises is to get you writing some Python code again and to familiarize yourself with Git. If you have never written Python before, you will probably need to spend more time, doing more exercises, in which case Langtangen could be a good resource.

The first part of the exercises revolves around creating a your first repository on github.uio.no. Next you do some short Python exercises, which should then be committed to your repo and pushed to GitHub.

Note that these exercises are not mandatory and will not be handed in or graded. However, programming is primarily learnt by doing, and so we strongly recommend you try to do most of the weekly exercises.

If you want more to do than these exercises, there are plenty of exercises to be found in Langtangen, but also online. Here we would especially recommend projecteuler.net for fun challenging problems to be solved. These start out fairly easy, but quickly become challenging. After solving a problem, you get access to a forum where you can see how others solved the problems in a broad range of programming languages.

Part A — Git#

Later in the course, you will do mandatory assignments which shall be carried out in git and handed in through git, but there will be more info on this later. For now, you will just create a repository to try out git and get some experience with it. For this we will use the UiO GitHub server

  • github.uio.no On this server you can log in using your UiO username and password. The nice thing about using this server is that it is a bit more private than the public GitHub server. If you prefer using the public server: , feel free to do so. If you do this, we recommend you sign up using your UiO email.

Now carry out the following steps. If you are unsure of how to do these steps, they are explained in the lecture notes, you can ask for help, or you can look it up online.

  1. Install Git for your OS. See this for install instructions

  2. Clone the IN1910 repository down to your machine

  3. Create your own repository through the browser

  4. Clone your repository down to your own machine

  5. Edit the README.md file (or create one) and commit and push your changes

  6. Create a .gitignore file and use https://gitignore.io to compile its contents (add your editor, OS, Python), and commit and push these changes.

  7. Write a simple hello_world.py program, and commit and push these changes

  8. Go to your repository through your browser and look at your commit history.

If you have done all these steps, you can now either move on to the Python exercises, our download the interactive tutorial git-it, to get a more thorough introduction to git.

Part B — Python#

We now turn to some Python exercises. These are meant to refresh some of your Python knowledge. For each exercise you do, you should stage your changes, commit them and push your commit to Github. Remember to include a descriptive commit message. If you want to implement the exercises directly into this notebook, you need to download it and move it to your own Git repository. To open it, navigate to the location in your terminal and type jupyter notebook.

Exercise 1 (Less than)

Write a function less_than(original, n) that takes a list of integers (original) and a number \(n\), and returns a new list of the elements in the original list that were smaller than \(n\). Your function should use a list comprehension to do this. Write a few simple tests of your function.

Exercise 2 (A deck of cards)

A normal playing card is identified by its value and its suit. Let a card be represented as a tuple of the value and the suit, where the value should be an integer between 1 and 13, and the suit by a single letter. For example, (12, 'C') would represent the queen of clubs.

  1. Use nested loops to create a list that contains a normal deck of 52 playing cards.

  2. Use random.shuffle to shuffle the deck.

  3. After shuffling, draw 13 cards from the deck.

  4. Sort and print the drawn cards out so that they are separated by suit, and shown in increasing value within each suit.

Hint: To sort a list, you can use the list.sort() method. If you are unfamiliar with this, you can read this mini how-to:

Exercise 3 (Letter Counts)

Write a function count_chars that takes a string as input, and counts the number of times each character appears in the string. Case should be ignored, so that both A and a count as the same letter. The resulting counts should be returned as a dictionary.

  • Hint 1: Case can be ignored by converting the input to lowercase before analyzing it.

  • Hint 2: You can loop over the characters of a string

Test your function using the following test-block:

example = "Hello, world!"
for char, count in count_chars(example).items():
    print(f"{char:3}{count:10}")

Finally, copy this print example in the test-block and change it so that the characters are lister in alphabetical order. Now add an example where they are printed in order of how many times they occur in the text.

Exercise 4 (Factorizing a number)

Write a function factorize that takes in an integer \(n\), and returns the prime-factorization of that number as a list. For example factorize(18) should return [2, 3, 3] and factorize(23) should return [23], because 23 is a prime.

Use this to test your implementation:

def test_factorize():
    assert factorize(412415) == [5, 82483]
    assert factorize(27) == [3, 3, 3]
    assert factorize(31) == [31]

Exercise 5 (Name Scores)

(This is Project Euler problem 22, and is used under a Creative Commons BY-NC-SA 4.0 license)

Using names.txt (right click and ‘Save Link/Target As…’), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?

Hint: By “alphabetical value” they mean the sum of the characters in the name, if each character is converted into its position in the alphabet, so A=1, B=2, etc.