Advanced Coding Class Python

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Computer Science 113 | Computer Science 114

Advanced Class 1
23 February 2024

Structure:
1. A quick introduction to if statements, loops, python types, conversion and the difference
between primitive and non-primitive types will be given.
2. Getting the most out of the session: Challenge yourself to not only find a solution but the best
(fastest/clearest/most direct) one. Collaboration is encouraged but ensure you understand
how the solution was reached. At the end of the session we will look at some of the solutions
that students came up with and discuss them.
3. Google documentation, how to do simple tasks (such as how to take a square root, how to
get a random number or how to write a for loop) and error messages but not the answer to
the question itself.
4. Most importantly, the goal of these sessions is to play around and have fun with programming.
5. Questions marked with SACO have a corresponding evaluator. This means you can upload
your program and it will mark it for you. Try and get full marks for these. I highly suggest you
register an account here. Hint These questions use standard input and output as opposed
to command line arguments. Use the .3.py template until you understand this format.
6. Don’t feel the need to do all the questions. Do the ones at and slightly above your level.

Questions:
1. Euclidean Distance
Write a program that takes 2 integer command line arguments (x & y) and prints the distance
from the point (x, y) to the origin (0, 0).
2. Dice
Write a program that prints the sum of 2 randomly generated integers between 1 and 6 to
produce an output with the same probabilities as rolling 2 dice.
Extension: Could you generate a large set of numbers using your dice program and check that
numbers occur with approximately the correct probability? Does the probability of a number
occurring get better if the size of the data set is increased?
3. Gaussian Random Numbers
Write a program that prints a random number r drawn from the Gaussian distribution. You

can do this using the Box-Muller formula: r = sin(2πv) −2 ln u where u & v are real
numbers between 0 & 1.

1
4. Legs
SACO https://saco-evaluator.org.za/cms/sapo2020round1/tasks/legs/description
Given the amount of chickens, cows and bees calculate the amount of legs.
Input Template chickens, cows, bees = map(int,input().split())
Output Template print(total)
5. Prime Numbers A
Create a program that takes in 1 command line argument and determines whether it is a
prime number or not (print the number followed by “ is a prime number” or “ is not a prime
number”). Use a for loop to iterate through the possible divisors.
Requires Loops, If, Modulus (%)
Hint The mod (%) operator can be used to find the remainder between two numbers, for
example: 5 % 2 = 1. x % y = 0 if and only if x is divisible by y, why?

6. Sum of Factors
SACO https://saco-evaluator.org.za/cms/sapo2016round1/tasks/maths/description
Calculate the sum of the factors of an integer.
Requires Loops, If, Modulus (%)
Hint The mod (%) operator can be used to find the remainder between two numbers, for
example: 5 % 2 = 1. x % y = 0 if and only if x is divisible by y, why?
7. Rövarspråket
SACO https://saco-evaluator.org.za/cms/sapo2018round1/tasks/rovarspraket/description
Given a phrase double up each consonant and put an ’o’ between them. E.g. dogs to
dodogogsos.
Requires If, Loops, String operations (%)
Hint myString[i] gets the ith letter (starting from 0) of myString. Build up the the new
string from an empty string using concatenation: +.
8. Prime Numbers B
Write a Python function that returns true if a number is prime and false otherwise. Write a
function that takes in a list of integers and returns a new list containing only the elements
(in the order given) that were prime.
Hint Use the first function in the second.
Background Functions: A function is a block of code that only runs when it is called.
Functions generally take in some input and returns some output. For example, I can write
a function to add two numbers which will take two input arguments (the two numbers you
want to add) and then the function will return the sum. (Functions are very powerful and is
the backbone to pretty much all programs around the world)
9. Encryption
SACO https://saco-evaluator.org.za/cms/sapo2016round1/tasks/encryption/description
Given a word and a non-negative integer n, output the word cycled n characters to the right
with each character also being advanced n characters along the alphabet (with wrap-around).
E.g. stellies 2 -> esstelli -> guuvgnnk

You might also like