<jupyter_start><jupyter_text>Python Learning Notes Nested For-in Loop  05/26/2020  Daily Objects1. Python-100-days: Day 52. *Beginning Python*: Read Chapter 33. *Python Programming*: Read Chapter 1 Progress1. Complete Day 52. Read *Python Programming* Chapter 1  Notes Python-100-days: Logic Programming1. Translate human logics to Python **Practice 1:** Find all Narcissistic numbers from 100 to 999<jupyter_code># Practice 1
count = 0
for number in range(100, 1000): # e.g: number = 985
    n_3 = number // 100         # 985 // 100 = 9
    n_2 = number // 10 % 10     # 985 // 10 = 98, 98 % 10 = 8
    n_1 = number % 10          # 985 % 10 = 5
    if n_3 ** 3 + n_2 ** 3 + n_1 ** 3 == number:
        count += 1
        print(number)
print('Total count: {}'.format(count))<jupyter_output>153
370
371
407
Total count: 4<jupyter_text>**Practice 2:** Reverse a positive integerFor example: 12345 to 54321<jupyter_code># Practice 2
def reversed_num(number):
    if type(number) != int:
        print('Please enter an integer!')
    elif number <= 0:
        print('Please enter a positive integer!')
    else:
        reversed_num = 0
        while number > 0:
            reversed_num = reversed_num * 10 + number % 10
            number //= 10
        print(reversed_num)
# Test
reversed_num('Not a number')
reversed_num(-2345)
reversed_num(1975)<jupyter_output>Please enter an integer!
Please enter a positive integer!
5791<jupyter_text>**Practice 3:** A hundred coins, a hundred chicken  5 coins per male chicken  3 coins per female chicken  1 coin per three baby chicken  You will use 100 coins to purchase 100 chicken. How many chicken can you buy?<jupyter_code># Practice 3
# Solution: brute force
for n_male in range(0, 21):
    for n_female in range(0, 34):
        n_baby = 100 - n_male - n_female
        if 5 * n_male + 3 * n_female + n_baby / 3 == 100:
            print('Male: {} Female: {} Baby: {}'.format(n_male, n_female, n_baby))<jupyter_output>Male: 0 Female: 25 Baby: 75
Male: 4 Female: 18 Baby: 78
Male: 8 Female: 11 Baby: 81
Male: 12 Female: 4 Baby: 84<jupyter_text>**Practice 4:** [Craps](https://en.wikipedia.org/wiki/Craps)1. Roll two dices2. For the first roll:  a. if 7 or 11, player win.    b. if 2, 3 or 12, house win.    c. if other results, keep rolling until:      i. 7, house win.        ii. The number player rolled at the first time, player win.    3. Simulate 1000 rounds and record the performance.<jupyter_code># Practice 4
# Simulate 1000 rounds
from random import randint
# Build a fair dice
def dice(): 
    return(randint(1, 6) + randint(1, 6))
n_win = 0
n_lose = 0
win_number = [7, 11]
lose_number = [2, 3, 12]

for n in range(1, 1001):
    roll_die = dice()
    if roll_die in win_number:
        n_win += 1
        n += 1
    elif roll_die in lose_number:
        n_lose += 1
        n += 1
    else:
        while True:
            roll_die_again = dice()
            if roll_die_again == 7:
                n_lose += 1
                n += 1
                break
            elif roll_die_again == roll_die:
                n_win += 1
                n += 1
                break
print('Number of wins: {} Number of loses: {}'.format(n_win, n_lose))<jupyter_output>Number of wins: 499 Number of loses: 501<jupyter_text>ExercisesExercise 1: Fibonacci sequenceExercise 2: Find perfect numbers below 10,000Exercise 3: Print prime numbers below 100<jupyter_code># Exercise 1
# Get the first 20 numbers in a Fibonacci sequence
seq = [1, 1]
for i in range(2, 20):
    seq.append(seq[i-2] + seq[i-1])
print(seq)
# Exercise 1
# Reference
a = 0
b = 1
for _ in range(20):
    a, b = b, a + b
    print(a, end=' ')
# Exercise 2
# Find perfect numbers below 10,000
n = 1
seq = [1] # Apparently, 1 is a perfect number
while n < 10000:
    for i in range(1, n):
        if n % i == 0:
            seq.append(i)
    if sum(seq) == n:
        print(n)
    seq = []
    n += 1
# Exercise 2
# Reference
import math

for num in range(1, 10000):
    result = 0
    for factor in range(1, int(math.sqrt(num)) + 1):
        if num % factor == 0:
            result += factor
            if factor > 1 and num // factor != factor:
                result += num // factor
    if result == num:
        print(num)
# Exercise 3
# Find prime numbers below 100
from math import sqrt

seq = []
for n in range(2, 101):
    is_prime = True
    for i in range(2, int(sqrt(n)) + 1):
        if n % i == 0:
            is_prime = False
            break
    if is_prime:
        seq.append(n)
print(seq)
# Exercise 3 
# Reference
import math

for num in range(2, 100):
    is_prime = True
    for factor in range(2, int(math.sqrt(num)) + 1):
        if num % factor == 0:
            is_prime = False
            break
    if is_prime:
        print(num, end=' ')<jupyter_output>2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
{'fraction_non_alphanumeric': 0.10715754833401893, 'fraction_numerical': 0.07280954339777869, 'mean_word_length': 2.431898376852505, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '341311', 'license': 'MIT', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1595, 'n_words': 617, 'path': 'Daily Progress/Day 5 - Nested for-in loop.ipynb', 'repo_name': 'tong-jin-nyu/Python-learning-notes', 'size': '4862'}