<jupyter_start><jupyter_text>Lecture 7：笼子里一共有多少只鸡和兔子 Chicken and Rabbits in a Cage - **Name (姓名)**: [Write Your Name Here]- **Date (日期)**: [Write the Date on which you completed this assignment]- **Score (成绩)**:[Provided by Teacher]- **Comment By Teacher (评语)**: [Provided by Teacher]  Problem1. Sophie's Daddy tells Sophie that each day after school, only when Sophie completes her homework and it is before 7 o'clock, can she play outside with her friends. Now tell Sophie if she can play outside under the following conditions:      菲菲的爸爸告诉菲菲：她每次放学回来后，只有把在7点以前把当天的作业完成才能在户外和朋友们一起玩。请你告诉菲菲在下列的各种条件下菲菲能不能出去和她的朋友们一起玩？| Is homework completed? | Is it before 7? | What's the meaning of the conditions? | Can Sophie play outside with friends? || :---------------------:|:-----------------:|:----------:|:-------------------------:|| Yes       |   Yes      | Sophie completed her homework; it is before 7. |YES || Yes       |   No       |                       |NO | | No        |   No       |                       |NO || No        |   Yes      |                       |NO |2. A healthy chicken has 1 brain and 2 feet while a healthy rabbit has 1 brain and 4 feet. In a cage are living certain number of healthy chicken and healthy rabbits. The total number of brains in this cage is 35, and the total number of feet is 94. How many chicken and rabbits in this cage respectively?       一只健康的鸡有1个脑袋2只脚，一只健康的兔子有1个脑袋4只脚。现在在一个笼子里生活着一定数量的健康的鸡和兔子，数了之后发现一共有35个脑袋和94只脚。请问这个笼子里鸡和兔子分别有几只？<jupyter_code># Guess 猜
# 鸡     兔子        头            脚
# 1      1           2 = 1 + 1     6 = 1 × 2 + 1 * 4
# 1      2           3 = 1 + 2    10 = 1 * 2 + 2 * 4 
# 1      3           4            14 = 1 * 2 + 3 * 4
# 1      34          35           138 = 1 * 2 + 34 * 4
# 2      33          35           136 = 2 * 2 + 33 × 4
# 3   
# ...

# 22     13          35           96
# 23     12                       94          (Found)
# 24     11                       92
# ...
24*2+ 11*4
# 1 ： Sophie completed her homework before 7.            YES
# 2 ：                                                    YES or NO
# 3 :
# 4 :<jupyter_output><empty_output><jupyter_text>Objective 学习目标- `and`连接的复合条件- 理解值`True`和`False`的意义- 初步学习使用`format`方法加`{}`占位符给一个字符串排版，理解`format()`前的符号`.`的意义- 熟练使用`while`引导的循环语句，复习 `while`和`if`的嵌套使用, 学习`while`间的相互嵌套- 继续复习变量自增、自减代码的完整和简便写法（例如：`a = a + 1` 和 `a += 1`)- 复习用`print`方法不换行多次接续输出一排内容。  Math- 简单的布尔逻辑与运算- 一（二）元一次（线性）方程（组）  Python  Prerequisite<jupyter_code>n_chicken, n_rabbit = 1, 34
n_feet = 2 * n_chicken + 4 * n_rabbit
print("number of chicken is:", n_chicken, ", number of rabbit is", n_rabbit)
print("number of chicken is {}, number of rabbit is {}".format(n_chicken, n_rabbit))
# Use of method format for a string 
# Symbol "." right after a string and before the method "format" 
# means the format method belongs to the type of string

age_sophie = 9
year = 2021
sentence = "In {}, Sophie is {} years old.".format(year, age_sophie)
print(sentence)
# Exercise in Class
#
# TODO build a string with {} and format() and then print that string
#
#
#
age_sophie = 9
grade_sophie = 4
year = 2021
sentence = "In year {}, Sophis is {} years old.".format(year, age_sophie)
print(sentence)<jupyter_output><empty_output><jupyter_text>Problem 1<jupyter_code># Combined condition
is_homework_finished, is_before_seven = True, True   # True is like YES, 
                                           # False is like No 
can_play_with_friends = False             # None is like "I don't know."

output = "Is homework finished? {}".format(is_homework_finished)
print(output)
print("Is it before 7? {}".format(is_before_seven))
if is_homework_finished and is_before_seven:  # and connects simple conditions
    can_play_with_friends = True
else:
    can_play_with_friends = False
print("Can Sophie play outside with her new friends? {}".format(can_play_with_friends))
# True is like YES, False is like No
is_homework_finished, is_before_seven = True, False
can_play_with_friends = None # None is like "I don't know."

print("Is homework finished? {}".format(is_homework_finished))
print("Is it before 7? {}".format(is_before_seven))
if is_homework_finished and is_before_seven: 
    can_play_with_friends = True
else:
    can_play_with_friends = False
print("Can Sophie play outside with her new friends? {}"\
      .format(can_play_with_friends))
is_homework_finished, is_before_seven = False, False # True is like YES, False is like No
can_play_with_friends = None # None is like "I don't know."

print("Is homework finished? {}".format(is_homework_finished))
print("Is it before 7? {}".format(is_before_seven))
if is_homework_finished and is_before_seven: 
    can_play_with_friends = True
else:
    can_play_with_friends = False
print("Can Sophie play outside with her new friends? {}"\
      .format(can_play_with_friends))
is_homework_finished, is_before_seven = False, True # True is like YES, False is like No
can_play_with_friends = None # None is like "I don't know."

print("Is homework finished? {}".format(is_homework_finished))
print("Is it before 7? {}".format(is_before_seven))
if is_homework_finished and is_before_seven: 
    can_play_with_friends = True
else:
    can_play_with_friends = False
print("Can Sophie play outside with her new friends? {}"\
      .format(can_play_with_friends))
# Exercise in Class
#
# TODO: Think about a thing happens under 2 conditions in your life and write
#       it down in a sentence
#       then list all the combination of these conditions,
#       coding and check if that thing will happen under each combination of 
#       conditions
#
#
#<jupyter_output><empty_output><jupyter_text>Problem 2<jupyter_code># Solution 2

n_brain_total, n_feet_total = 35, 94
n_chicken, n_rabbit = 0, n_brain_total

while n_chicken <= n_brain_total:
    n_rabbit = n_brain_total - n_chicken
    n_feet_current = 2 * n_chicken + 4 * n_rabbit
    if n_feet_current == n_feet_total:
        print(n_chicken, n_rabbit)
    
    n_chicken += 1
# Solution 1

n_brain_total, n_feet_total = 35, 94
n_chicken, n_rabbit = 0, 0

while n_chicken < n_brain_total:
    n_rabbit = 0
    while n_rabbit < n_brain_total:
        if (n_chicken + n_rabbit == n_brain_total) and \
           (n_chicken * 2 + n_rabbit * 4 == n_feet_total):
            print("{} chicken and {} rabbit meet the requirement"\
                  .format(n_chicken, n_rabbit))
            
        n_rabbit += 1
    
    n_chicken += 1<jupyter_output><empty_output><jupyter_text>Exercises  Exercise 1 1. Complete the following program so the the program will output the calendar of May 2021 like the follwoing:         补充填写下面的程序使得它能够输出如下格式的2021年5月的月历：   ```text              May 2021             Sun Mon Tue Wed Thu Fri Sat                             1       2   3   4   5   6   7   8       9  10  11  12  13  14  15      16  17  18  19  20  21  22      23  24  25  26  27  28  29      30  31```<jupyter_code># Attention: running original codes will go into a infinite loop.
# stop and restart the kernal if this occurs.

month_year_title = "          May 2021         "
weeks_title =      "Sun Mon Tue Wed Thu Fri Sat"
# starting from the date "1st May"
cur_date, cur_weekday, max_date = 1, 6, 31  
# spaces are used to locate the position of 1st May
n_space_filled, n_space_to_fill = 0, cur_weekday * 4 # why is 4 here? 4 is there because there are 4 spaces between each date.
print(month_year_title)
print(weeks_title)

while n_space_filled < n_space_to_fill:
    print(" ", end="")
    n_space_filled += 1
    #TODO: add a variable self-increased assign expresssion
    #      so that this while loop can terminate
    
while cur_date <= max_date:
    if cur_date < 10:
        print(end=" ")   # if the date is less than 10, we print an 
                        #    extra " " before the number
#        print(cur_date, end="  ")         # add some spaces between dates to layout them properly
    print(cur_date, end="  ")
   
    if cur_weekday == 6:       # 6 means the weekday is Saturday
        print("\n")            #TODO: add a print expression here
        cur_weekday = 0        # 0 means the weekday is Sunday,       
    else:                      # if current weekday is not Saturaday
        #TODO: add one code line here.
        cur_weekday += 1
        pass
    
    cur_date += 1             #TODO: add one code line here.
    pass<jupyter_output>May 2021         
Sun Mon Tue Wed Thu Fri Sat
                         1  

 2   3   4   5   6   7   8  

 9  10  11  12  13  14  15  

16  17  18  19  20  21  22  

23  24  25  26  27  28  29  

30  31<jupyter_text>Exercise 2 2. Yunzi and Sophie are brother and sister; they haven't been seeing each other for almost 5 years. Sophie plans to visit Yunzi if the pandamic of COVID-19 will be globally declared over this summer and Sophie's mom will successfully book the ticked to China. Please work with Sophie and Yunzi to list all the possibilities of the 2 conditions and tell if Sophie is able to visit Yunzi this summer. Also write Python codes to answer the same quesitons.   耘耔和菲菲是姐弟俩，他们已经有接近5年没有见到彼此了。菲菲计划如果今年夏天全球疫情结束且她的妈妈能买到回中国的机票的话就回去拜访耘耔弟弟。请同菲菲一起列出来前面所说的两个条件(全球疫情是否结束，菲菲妈妈能否买到机票）的所有可能组合，并且针对每一个可能性指出菲菲能否在今年夏天拜访耘耔弟弟。也请编写Python代码来回答同样的问题。   Use each code cell to handle one possibility of conditions. You may not need to use all the 6 cells provided below.      针对每一种可能性的组合用一个代码单元格编写代码。你应该不需要用完所有的6个代码单元格。<jupyter_code># Combined condition
declare_over_globally, able_to_book_flight = True, True    

can_go_visit = None            

output = "Is covid over globally? {}".format(declare_over_globally)
print(output)
print("Are they able to book flight? {}".format(able_to_book_flight))
if declare_over_globally and able_to_book_flight: 
    can_go_visit = True
else:
    can_go_visit = False
print("Can Sophie visit? {}".format(can_go_visit))

declare_over_globally, able_to_book_flight = False, True    

can_go_visit = None            

output = "Is covid over globally? {}".format(declare_over_globally)
print(output)
print("Are they able to book flight? {}".format(able_to_book_flight))
if declare_over_globally and able_to_book_flight: 
    can_go_visit = True
else:
    can_go_visit = False
print("Can Sophie visit? {}".format(can_go_visit))
declare_over_globally, able_to_book_flight = True, False    

can_go_visit = None            

output = "Is covid over globally? {}".format(declare_over_globally)
print(output)
print("Are they able to book flight? {}".format(able_to_book_flight))
if declare_over_globally and able_to_book_flight: 
    can_go_visit = True
else:
    can_go_visit = False
print("Can Sophie visit? {}".format(can_go_visit))
declare_over_globally, able_to_book_flight = False, False    

can_go_visit = None            

output = "Is covid over globally? {}".format(declare_over_globally)
print(output)
print("Are they able to book flight? {}".format(able_to_book_flight))
if declare_over_globally and able_to_book_flight: 
    can_go_visit = True
else:
    can_go_visit = False
print("Can Sophie visit? {}".format(can_go_visit))<jupyter_output>Is covid over globally? False
Are they able to book flight? False
Can Sophie visit? False<jupyter_text>Exercise 3 3. A turtle is climbing up from the bottom of a 12 meter-deep well, eachday it will climb 2 meters high at daytime but will step down 0.8 meters at night. Once the turtle climbs to the surface of ground, which is the top of the well, it will not step down anymore. How many days will it take for turtle to escape from the well? Write your python Program to answer this question and print out each location indicated by the distance from the bottom of the well to location of the turtle just before the start of each next day.   一个小乌龟正打算从一个12米深的井底往上爬，每一天的白天它会向上爬2米，可是在夜间它会倒退0.8米。当有一天小乌龟爬到井口到达地面时它就不会再往下退了。现在问你，小乌龟需要花费多少天才能从井底爬到地面上。编写Python程序来回答这个问题，并且打印出每一个第二天来临前小乌龟的位置（用它距离井底的距离来表示）<jupyter_code># TODO: your answers are here
# On day 10 the turtle will climb out of the 12 meter deep well.
#
# TODO: your codes are here
day = 0
well_deep = 12
step_down_at_night = 0.8
climb_at_day = 2
left_to_climb = well_deep

while left_to_climb > climb_at_day:
    day += 1
    left_to_climb = left_to_climb - climb_at_day + step_down_at_night
    print("         DAY{}:\n There are {} meters left to climb.".format(day, left_to_climb))
 

print("The turtle needs {} days to get out.".format(day+1))<jupyter_output>DAY1:
 There are 10.8 meters left to climb.
         DAY2:
 There are 9.600000000000001 meters left to climb.
         DAY3:
 There are 8.400000000000002 meters left to climb.
         DAY4:
 There are 7.200000000000002 meters left to climb.
         DAY5:
 There are 6.000000000000002 meters left to climb.
         DAY6:
 There are 4.800000000000002 meters left to climb.
         DAY7:
 There are 3.6000000000000014 meters left to climb.
         DAY8:
 There are 2.4000000000000012 meters left to climb.
         DAY9:
 There are 1.2000000000000013 meters left to climb.
The turtle needs 10 days to get out.<jupyter_text>Exercise 4 4. Cut a 2.6 meters long rope into 4 segments as the 4 sides to make an rectangle. In order to maximize the area of the rectangle enclosed, how will you cut the rope? Write a programm to verify your result.   把一个2.6米长的绳子剪成4段作为一个长方形的4条边。应该如何切割这个绳子才能使得围成的长方形的面积最大？编写程序来验证你的计算结果。<jupyter_code># Your answers are here
# 0.64 , 0.66
#
# Your codes are here

rope_length = 2.6
side_step = 0.01
num_of_sides = 4
side_length = rope_length / 2 - side_step
side_width = side_step
area = side_length * side_width
side_squre = rope_length / num_of_sides
result_length = side_length
result_width = side_width
while side_width <= side_squre:  
    side_length = side_length - side_step
    side_width = side_width + side_step
    if (side_length * side_width) >= area:
        area = side_length * side_width
        result_length = side_length
        result_width = side_width
    
print("The length is", result_length,"and the width is", result_width)
print("The area is",area)<jupyter_output>The length is 0.6499999999999995 and the width is 0.6500000000000004
The area is 0.4224999999999999<jupyter_text>Exercise 5 5. (Hard, Optional) Jason and Tony stand at two opposite ends of a 1000 meters long straight road. Suppose Jason is at the left end and Tony is at the right. At time 8:00, both Jason and Tony start to run towards the other end of the road but with different speed: 5 meters per second for Jason, and 3 meters per second for Tony. Once either of Jason or Tony reaches the end, he will turn back immediately and run towards the other end with the value of speed unchanged, and so on until to 8:10. It's obvious that Jason and Tony will meet each other several times at some points of time and place of the road.       （难, 可选做）Jason和Tony分别站在一条1000米笔直的马路两端。假定Jason在这个马路的左侧端点，Tony在右侧。在8:00（八点整）这一时刻，Jason和Tony分别用各自恒定的速度跑向对方所在的马路端点，其中Jason每秒跑5米，Tony每秒跑3米。当他们中的任何一个跑到马路的另一端时会立即掉头再朝着另一个端点跑去，并保持自己的速度值不变，如此反复直到8:10。很明显，Jason和Tony会在某一个时间点、马路上的某一个位置相遇，并且他们会多次相遇。      Now the question is:       现在问：   - If two children keep walking until to 8:10, how many times will they meet each other?         如果两个小朋友一直跑到8:10（八点十分），他们会相遇多少次？   - At which points of time and place will they meet?         每一次相遇分别发生在什么时间和什么地点？   - At the stop time of 8:10, where will Jason and Tony be stopping and which direction are they facing to?         在8:10分停止的时候，Jason和Tony分别停在什么位置？方向如何？   - Can you draw and simulate the whole event by drawing shapes and lines on `turtle`?         你能用`turtle`来绘图模拟整个跑步的场景吗？      Hint: you can describe a place(location) by indicating how many meters it is measured from one end of the road.         提示：在描述一个地点（位置）时，可以用这个地点距离马路的某一个端点的距离来表示。<jupyter_code>speed_jason = 5
speed_tony = 3
length_of_towns = 1000
time = 600
distance = 0
t_start = 0
record_time = 0
num = 0
record_place_jason = 0
while t_start < time:
    t_start += 1
    distance = speed_jason * t_start + speed_tony * t_start
    if distance == 1000 and num == 0:
        record_time = t_start
        record_place_jason = speed_jason * t_start
        num += 1
        print(record_time, num, record_place_jason)
    elif distance == (2000 * num + 1000) and num > 0:
        record_time = t_start
        record_place_jason = speed_jason * t_start
        num += 1     
        print(record_time, num, record_place_jason)
    elif t_start == int(length_of_towns / (speed_jason - speed_tony)):
        record_time = t_start
        record_place_jason = speed_jason * t_start
        num += 1     
        print(record_time, num, record_place_jason)
print("Jason is at", time * speed_jason, "\nTony is at", time * speed_tony)<jupyter_output>125 1 625
375 2 1875
500 3 2500
Jason is at 3000 
Tony is at 1800
{'fraction_non_alphanumeric': 0.07904996101481437, 'fraction_numerical': 0.039644934924728606, 'mean_word_length': 3.1539611360239164, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 11, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '324870', 'license': 'MIT', 'n_tokens_mistral': 6620, 'n_tokens_neox': 6170, 'n_words': 1894, 'path': 'assignments/2021-05-29/Jason_assignment_009.ipynb', 'repo_name': 'qqiang00/ElementaryMathPython', 'size': '16673'}