Categories

## – List Solutions

``````breakfast = ["Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"]
palindromic = ["Sausage", "Eggs", "Beans", "Beans", "Eggs", "Sausage"]
nums = [1,1,3,3,3,2,2,2,1,1,1,1,4,4,4,4]
letters = ['a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e']

def print_list(list):
for item in list:
print(item)

def last_element(list):
return (list[-1])

def last_but_one(list):
return (list[-2])

# Using reversed function
def my_reverse(list):
new_list = []
for item in reversed(list):
new_list.append(item)
return new_list

# Using for loop
def my_reverse2(list):
new_list = []
for i in range (len(list)-1, -1, -1):
new_list.append(list[i])
return new_list

# Using list comprehension
def my_reverse3(list):
return [item for item in reversed(list)]

# Using slicing
def my_reverse4(list):
return list[::-1]

def is_palindrome(list):
if list == my_reverse(list):
return True
else:
return False

def compress(list):
new_list = []
last_item = list[0] + 1 # so first number is different to itself
for num in list:
if num != last_item:
new_list.append(num)
last_item = num
return new_list

def pack(list):
pack_list = []
last_item = list[0]
word = ""
for item in list:
if item == last_item:
word = word + item
else:
pack_list.append(word)
word = item
last_item = item
pack_list.append(word)
return pack_list
``````
Categories

## 3: Lists

### 3.1 Print List

Write a function that prints out a list, one element per line

#### 3.1.1 Example

``` 1: breakfast = ["Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"]
2:
3: print_list(breakfast)
4:  *** Output ***
5: Sausage
6: Eggs
7: Beans
8: Bacon
9: Tomatoes
10: Mushrooms
```

### 3.2 Last Element of Array

Write a function that returns the last element of a string array

#### 3.2.1 Example

```1: breakfast = ["Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"]
2:
3: print(last_element(breakfast));
4:  *** Output ***
5: Mushrooms
```

### 3.3 Last But One Element of Array

Write a function that returns the last but one element of a string array

#### 3.3.1 Example

```1: breakfast = ["Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"]
2:
3: print(last_but_one(breakfast));
4:  *** Output ***
5: Tomatoes
```

### 3.4 Reverse a list, leaving original intact

Return a list in reverse order, while leaving the original list intact.

#### 3.4.1 Example

``` 1: breakfast = ["Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"]
2:
3: print(my_reverse(breakfast))
4: print(breakfast)
5:  *** Output ***
6: : Mushrooms
7: : Tomatoes
8: : Bacon
9: : Beans
10: : Eggs
11: : Sausage
12: : Sausage
13: : Eggs
14: : Beans
15: : Bacon
16: : Tomatoes
17: : Mushrooms
```

### 3.5 Palindromic lists

Write a function that tests to see if a list is palindromic, i.e. the elements are the same when reversed.

#### 3.5.1 Example

```1: palindromic = ["Sausage", "Eggs", "Beans", "Beans", "Eggs", "Sausage"]
2: breakfast = ["Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"]
3:
4: print(is_palindrome(palindromic))
5: print(is_palindrome(breakfast))
6:  *** Output ***
7: True
8: False
```

### 3.6 Consecutive Duplicates

Write a function to print out list of integers with consecutive duplicates eliminated

```1: nums = [1,1,3,3,3,2,2,2,1,1,1,1,4,4,4,4]
2:
3: compress(nums)
4:  *** Output ***
5: : 1
6: : 3
7: : 2
8: : 1
9: : 4
```

### 3.7 Pack Duplicates

Pack consecutive duplicates of a char list into Strings

```1: letters = ['a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e']
2:
3: pack(letters)
4:  *** Output ***
5: : aaaa, b, cc, aa, d, eeee
```

Categories

## 2 Loop Solutions

``` 1: def oneToTen():
2:         for i in range(1,11):
3:                 print(i)
4:
5: def oneToTenAcross():
6:       print("|", end="")
7:         for i in range(1,11):
8:                 print(i,"|",end="")
9:
10: def oddNumbers():
11:         for i in range(1,21,2):
12:                 print(i)
13:
14: def squares():
15:         for i in range(1,11):
16:                 print(i*i)
17:
18: def random4():
19:         for i in range(1,5):
20:                 print(random.randint(1,10))
21:
22: def even(n):
23:         for i in range(1,n+1):
24:
25:
26: def powers(n):
27:         for i in range(1,n+1):
28:                 print(2**i)
29:
30: def triangle():
31:         for i in range(1,6):
32:                 for j in range(1,i+1):
33:                         print("*", end = "")
34:                 print()
35:
36: def areWeThereYet():
37:         s = "no"
38:         while (s != "yes"):
39:                 s = input("Are we there yet?")
40:         print ("Good!")
41:
42:
43: def tableSquare():
44:         for i in range(1,5):
45:                 for j in range (1, 5):
46:                         print("|",i*j, "\t", end = "")
47:                 print("|")
48:
49: def tableSquares(n):
50:         for i in range(1,n+1):
51:                 for j in range (1, n+1):
52:                         print("|",i*j, "\t", end = "")
53:                 print("|")```
Categories

## 2: Loops

### 2.1 One to 10

Write a function that prints the numbers 1 to 10

#### 2.1.1 Example

``` 1: oneToTen()
2:  *** Output ***
3: 1
4: 2
5: 3
6: 4
7: 5
8: 6
9: 7
10: 8
11: 9
12: 10
```

### 2.2 One to 10 across

In Python 3, you can use the end=”” parameter in print to prevent a newline at the end of a print(). For example, print(“frog”,end=””) will print frog without moving to the next line. Use that to write a function that prints the numbers 1 to 10 across the screen as shown

#### 2.2.1 Example

```1: oneToTenAcross()
2:  *** Output ***
3: |1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |
```

### 2.3 Odd Numbers

Write a function that prints the positive odd numbers less than 20

#### 2.3.1 Example

``` 1: oddNumbers()
2:  *** Output ***
3: 1
4: 3
5: 5
6: 7
7: 9
8: 11
9: 13
10: 15
11: 17
12: 19
```

### 2.4 Square Numbers

Write a function that prints the square numbers up to 100

#### 2.4.1 Example

``` 1: squares()
2:  *** Output ***
3: 1
4: 4
5: 9
6: 16
7: 25
8: 36
9: 49
10: 64
11: 81
12: 100
```

### 2.5 Random Numbers

Write a for loop to print out four random integers between 1 and 10

#### 2.5.1 Example

```1: random4()
2:  *** Output ***
3: 3
4: 5
5: 2
6: 8
```

### 2.6 Even Numbers < n

Write a function to print out the positive even numbers less than n

#### 2.6.1 Example

``` 1: even(20)
2:  *** Output ***
3: 2
4: 4
5: 6
6: 8
7: 10
8: 12
9: 14
10: 16
11: 18
```

### 2.7 Powers of 2

Write a function to print out the powers of 2 from 21 up to 2n

#### 2.7.1 Example

``` 1: powers(8)
2:  *** Output ***
3: 2
4: 4
5: 8
6: 16
7: 32
8: 64
9: 128
10: 256
```

### 2.8 Are we there yet?

Write a program that outputs “Are we there yet?” and then waits for input. If the input is “Yes” the program outputs “Good!” and exits, otherwise the program loops.

#### 2.8.1 Example

```1: "Are we there yet?"
2: No
3: "Are we there yet?"
4: Spoons
5: "Are we there yet?"
6: Yes
7: Good!
```

### 2.9 Triangle

Write a function that uses nested loops to produce the following pattern.

```1: triangle()
2:  *** Output ***
3: *
4: **
5: ***
6: ****
7: *****
```

### 2.10 Table Square

Write a function that prints out a 4 x 4 table square

#### 2.10.1 Example

```1: tableSquare()
2:  *** Output ***
3: A 4 x 4 table square
4: | 1 | 2 |  3 |  4 |
5: | 1 | 2 |  3 |  4 |
6: | 2 | 4 |  6 |  8 |
7: | 3 | 6 |  9 | 12 |
8: | 4 | 8 | 12 | 16 |
```

### 2.11 Table Squares

Extend your answer to the last question produce a function that will print out a n x n table square

#### 2.11.1 Example

```1: tableSquares(6)
2:  *** Output ***
3: A 6 x 6 table square
4: | 1 |  2 |  3 |  4 |  5 |  6 |
5: | 2 |  4 |  6 |  8 | 10 | 12 |
6: | 3 |  6 |  9 | 12 | 15 | 18 |
7: | 4 |  8 | 12 | 16 | 20 | 24 |
8: | 5 | 10 | 15 | 20 | 25 | 30 |
9: | 6 | 12 | 18 | 24 | 30 | 36 |
```
Categories

## 1: How to Answer these Questions

Write functions to solve all of the questions. Here are two example questions and their solutions. Notice that the first function prints a value, the second function returns a value.

## 1 Hello <Name>

Write a function that accepts a name as a parameter and prints out “Hello ” <name>

### 1.1 Example

```hello("Kim")
*** Output ***
Hello Kim
```

## 2 Average of two numbers

Write a function that accepts two numbers and returns the average of the two numbers.

### 2.1 Example

```print(average(3,4));
*** Output ***
3.5
```

## 3 Solutions

```1: def hello(s):
2:     print("Hello ",s)
3:
4: def average(i,j):
5:     return (i+j)/2
6:
7: hello("Kim")
8: print(average(3,5))
```