Commit f667932b authored by Swicech's avatar Swicech
Browse files

Upload assignment 3

parent 3eda0682
%% Cell type:markdown id: tags:
# Alternating sum
**Learning goals:**
* Loops
**Starting Out with Python:**
* Chapt. 4.2-4.3
In this assignment you are going evaluate the sum of numbers, based on user input.
%% Cell type:markdown id: tags:
## a)
Write a program that reads in an integer $n$ from the user and evaluates the series below.
**$1^{2}-2^{2}+3^{2}-4^{2}+5^{2}-\cdot \cdot \cdot \pm n^{2}$**
Notice that all the even numbers have a negative sign and all the odd numbers have a positive sign. Also recall that the name of your variable ***cannot*** be **sum**, as this is the name of a function in python. Also remember that range() is inclusive for the starting number but exclusive for the end number.
Example run:
```python
n = 7
The sum of the series is 28
```
***Write your code here***
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
## b)
Write a new program such that it ends the iteration **before** the sum of the numbers is greater than the nonnegative integer $k$. That is the sum at the last term should be printed before it surpasses the number $k$.
Keep track of how many terms from the series which have been included in the sum and print this out with the result.
Example run:
```python
k = 6
The sum of the numbers before the sum surpassed k is -10. Number of iterations: 4
```
```python
k = 12
The sum of the numbers before the sum surpassed k is -10. Number of iterations: 4
```
```python
k = 15
The sum of the numbers before the sum surpassed k is -21. Number of iterations: 6
```
***Write your code here:***
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
[Back to Assignment 3](_Oving3.ipynb)
# Code Comrehension
**Learning Goals:**
* Understanding code
The code below cannot be run, here you have to think about the answers! About. 20% of the exam is about understanding code so look at this as valuable training!
%% Cell type:markdown id: tags:
### task a)
%% Cell type:markdown id: tags:
**a)** What is printed in the code below?
%% Cell type:markdown id: tags:
```python
a=345
b=''
while a or b=='':
b=str(a%2)+b
a=a//2
print(b)
```
%% Cell type:markdown id: tags:
**Answer:** < Doubleclick here to write your answer >
%% Cell type:markdown id: tags:
### task b)
%% Cell type:markdown id: tags:
**b)** What is printed in the code below?
%% Cell type:markdown id: tags:
```python
for x in range(0, 10, 2):
print(x, end='')
if x%4==0:
print( ": This number is dividable by 4")
else:
print()
```
end="" makes the next print() at the same line as the previus print(). Normally it would print at the next line.
%% Cell type:markdown id: tags:
**Answer:** < Doubleclick here to write your answer >
%% Cell type:markdown id: tags:
### task c)
%% Cell type:markdown id: tags:
**c)** What is printed in the code below?
%% Cell type:markdown id: tags:
```python
i = 1
while i<10:
i = i*2
print(i)
```
%% Cell type:markdown id: tags:
**Answer:** < Doubleclick here to write your answer >
%% Cell type:markdown id: tags:
### task d)
%% Cell type:markdown id: tags:
**d)** What is printed in the code below?
%% Cell type:markdown id: tags:
```python
i = 1
j = 3
while j>0:
i = i*2
j = j - 1
print(i)```
%% Cell type:markdown id: tags:
**Answer:** < Doubleclick here to write your answer >
%% Cell type:markdown id: tags:
### task e)
%% Cell type:markdown id: tags:
**e)** What is printed in the code below?
```python
i = 5
for x in range(i):
for y in range(x+1):
print("*", end="")
print()```
This is a double loop, which might be unknown for many. Try it anyways! How does a loop within a loop actually work? see page 176 in Starting out with Python.
%% Cell type:markdown id: tags:
**Answer:** < Doubleclick here to write your answer >