#Wei Liu
#11/9/21
#U3M2A2.2
#We are learning about Advanced Loop Structures

# Does nothing, but syntactically valid
def useless_function():
pass

# Does nothing, but syntactically valid
for i in range(10):
pass

# Does nothing, but syntactically valid
if (5 < 6):
pass

# Find the index of the first occurrence of 611 in an arbitrarily long list
# Define a long list
lst = [976, 618, 812, 898, 804, 689, 611, 630, 467, 411, 648, 931, 618, 425, 328, 196, 56, 615, 458, 166, 115, 118, 22, 585, 213, 855, 615, 478, 234, 360, 981, 174, 993, 627, 911, 385, 219, 577, 540, 49, 164, 109, 893, 727, 687, 709, 971, 781, 802, 701, 789, 421, 863, 44, 573, 91, 140, 338, 36, 102, 502, 723, 134, 336, 940, 969, 649, 598, 413, 307, 929, 951, 993, 436, 149, 280, 907, 733, 713, 268, 29, 946, 102, 277, 352, 449, 527, 201, 566, 643, 118, 587, 717, 358, 542, 223, 846, 244, 83, 268]
# Find the index (without break)
index = 0
iteration_count = 0
for num in lst:
if (num == 611):
found_at = index
index = index + 1
iteration_count = iteration_count + 1
print("Without using a break, 611 was found at index:", found_at, "using", iteration_count, "iterations")
# Find the index (with break)
index = 0
iteration_count = 0
for num in lst:
if (num == 611):
found_at = index
break # adding a break to improve efficiency
index = index + 1
iteration_count = iteration_count + 1
print("Using a break, 611 was found at index:", found_at, "using", iteration_count, "iterations")

```
Without using a break, 611 was found at index: 6 using 100 iterations
Using a break, 611 was found at index: 6 using 6 iterations
```

# Prompt the user for a positive number
while True:
x = int(input("Enter a positive number: "))
if (x > 0):
break #x is positive, break the loop

# Print the odd elements in a list
lst = [14, 6, 5, 10, 6, 9, 33, 103, 21, 55]
for num in lst:
# Skip all even numbers
if (num % 2 == 0):
continue
print(num)

```
5
9
33
103
21
55
```

# [ ] The following program tests if `num` is prime or not, use a `break` statement to improve its efficiency
# and reduce the number of necessary iterations
# Compare the number of necessary iterations with and without the `break` statement
# Use the following numbers for the comparison:
num = 45345
num = 11579
num = 948240
#num = 128093
#num = 519937
#num = 694394
prime = True # assume num is prime unless proven otherwise
iteration_count = 0
for i in range(2, num):
if num % i == 0:
prime = False;
iteration_count = iteration_count + 1
# Display results
if prime:
print(num, "is prime")
else:
print(num, "is NOT prime")
print("Total number of iterations:", iteration_count)

```
948240 is NOT prime
Total number of iterations: 948238
```

# [ ] Write a program to prompt the user for an odd number; use an infinite loop and a `break` statement.
while True:
x=int(input("Please enter an odd number: "))
if (x % 2 != 0):
break

# [ ] Modify the following program to display numbers that are divisible by 7 along with their square roots.
# HINT: Use a `continue` statement in the loop
from math import sqrt
for num in range(1, 100):
if (num % 7 != 0):
continue
print(num, sqrt(num))

```
7 2.6457513110645907
14 3.7416573867739413
21 4.58257569495584
28 5.291502622129181
35 5.916079783099616
42 6.48074069840786
49 7.0
56 7.483314773547883
63 7.937253933193772
70 8.366600265340756
77 8.774964387392123
84 9.16515138991168
91 9.539392014169456
98 9.899494936611665
```

# list of lists
table = [[5, 2, 6], [4, 6, 0], [9, 1, 8], [7, 3, 8]]
for row in table:
for col in row:
# print the value col followed by a tab
print(col, end = "\t")
# Print a new line
print()

```
5 2 6
4 6 0
9 1 8
7 3 8
```

# Generate a staircase character art
# Size controls the number of steps
def char_art(steps):
for row in range(steps):
for col in range(steps):
if(col <= row):
print("[]", end = "")
print()
# Generate a staircase with 6 steps
char_art(6)

```
[]
[][]
[][][]
[][][][]
[][][][][]
[][][][][][]
```

# [ ] Write a program to display the sum of each row in table
table = [[5, 2, 6], [4, 6, 0], [9, 1, 8], [7, 3, 8]]
for row in table:
sum=0
for col in row:
sum+=col
print("Sum = ", sum)

```
Sum = 13
Sum = 10
Sum = 18
Sum = 18
```

# [ ] Complete the function `generate_star` so it displays a star of variable `size` using "*"
# For size = 5 the star should look like:
# * *
# * *
# *
# * *
# * *
def generate_star(size):
for row in range(size):
for col in range(size):
if (row == col):
print("*",
end = "")
elif (row == size - col -1):
print("*", end = "")
else:
print(" ", end = "")
print()
# Display star
generate_star(5)

```
* *
* *
*
* *
* *
```

lst = [102, 34, 55, 166, 20, 67, 305]
# Nesting a compound conditional in a for loop
largest = 0
for num in lst:
# test if num is even and greater than largest
if((largest < num) and (num % 2 == 0)):
largest = num
print("Largest even number in the list is: ", largest)

```
Largest even number in the list is: 166
```

# Ages of 100 people
ages = [86, 38, 30, 19, 29, 6, 95, 22, 23, 82, 39, 73, 30, 98, 5, 68, 57, 34, 35, 81, 54, 77, 29, 75, 83, 14, 88, 7, 8, 32, 93, 76, 42, 1, 32, 70, 70, 3, 34, 52, 44, 41, 7, 77, 73, 97, 34, 13, 33, 54, 8, 82, 21, 55, 72, 41, 34, 98, 72, 73, 24, 55, 50, 63, 38, 92, 43, 68, 52, 68, 69, 51, 19, 24, 35, 55, 74, 47, 8, 19, 69, 12, 96, 96, 11, 30, 97, 73, 22, 25, 19, 85, 37, 68, 39, 76, 73, 18, 45, 42]
# Initial count
children = 0
teens = 0
young_adults = 0
adults = 0
# Nesting compound conditionals within a for loop
for age in ages:
if(age <= 12):
children = children + 1
elif((age >= 13) and (age <= 19)):
teens = teens + 1
elif((age >= 20) and (age <= 30)):
young_adults = young_adults + 1
elif(age > 30):
adults = adults + 1
print("Number of children: ", children)
print("Number of teens: ", teens)
print("Number of young_adults: ", young_adults)
print("Number of adults: ", adults)

```
Number of children: 11
Number of teens: 7
Number of young_adults: 12
Number of adults: 70
```

# Prompt the user for a number between 50 and 60
# Using infinite loop and break
while True:
x = int(input("Enter a number between 50 and 60: "))
if ((x >= 50) and (x <= 60)):
print("Thank you!")
break

```
Thank you!
```

# [ ] Complete the following program to count the number of even positive numbers, odd negative numbers, and zeros in `lst`
lst = [9, 0, -2, -4, -5, 2, -15, 6, -65, -7]
even_positives = 0
odd_negatives = 0
zeros = 0
#TODO: Count even_positives, odd_negatives, and zeros
for num in lst:
if (num > 0 and num % 2 == 0):
even_positives += 1
elif (num < 0 and num % 2 != 0):
odd_negatives += 1
elif num == 0:
zeros += 1
print("Number of even positives:", even_positives)
print("Number of odd negatives:", odd_negatives)
print("Number of zeros:", zeros)

```
Number of even positives: 2
Number of odd negatives: 4
Number of zeros: 1
```

# [ ] Write a program to count the number of punctuation marks (. , ? ! ' " : ;) in `s`
s = "Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth." # Sherlock Holmes (by Sir Arthur Conan Doyle, 1859-1930)
punc = 0
for c in s:
if (c == "." or c == "," or c == "?" or c == "!" or c == "'" or c == ' " ' or c == ":" or c == ":" ):
punc +=1
print("The number of punctuation marks is: ", punc)

```
The number of punctuation marks is: 4
```

# [ ] Write a program to prompt the user for an odd positive number; use an infinite loop and a `break` statement.
while True:
x = int(input("Enter an odd postive number: "))
if ((x>0) and (x % 2 != 0)):
break
print("You entered an odd postive number! ")

```
You entered an odd postive number!
```