今日事项-2024年6月28日

今日事项

| 今日事项:2024年6月28日

| 事项一

收集3个经典算法案例,并分析它的实现思路,然后上传至个人博客网站上

1. 欧几里得算法(Euclidean Algorithm)

产生原因与背景:

想象你在做除法时,想要找到两个数的最大公约数。欧几里得算法就像是用尺子测量布的长度,不断地减去一个较小的尺寸,直到不能再减,剩下的就是两个布匹的最大公约。

算法思路:

欧几里得算法就像是不断地找两个数直到剩下一个数为止,每次操作都是将较大的数除以较小的数,然后用除余的结果替换原来的较大数,直到余数为0,最后的除数就是这两个数的最大公约数。

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
def gcd(a, b):
# 只要b不为0,就继续操作
while b != 0:
# 将a除以b的余数存储到remainder中
remainder = a % b
# 将b的值赋给a,remainder的值赋给b
a, b = b, remainder
# 当b为0时,a就是最大公约数
return a

# 使用示例
print(gcd(48, 18)) # 输出应该是6

算法优缺点:

优点:欧几里得算法非常高效,其时间复杂度是O(log min(a, b)),对于大数计算特别有效。
缺点:这个算法只能计算两个整数的最大公约数,对于其他类型的数或更多数量的数无能为力。

解决缺点的方法:

如果需要处理更多数量的数,可以迭代地将当前找到的最大公约数与下一个数应用欧几里得算法。

解决缺点后的代码案例:

1
2
3
4
5
6
7
8
9
10
def gcd_multiple(numbers):
# 从numbers中取出两个数进行计算
num1, num2 = numbers[0], numbers[1]
for number in numbers[2:]:
# 迭代地更新最大公约数
num1 = gcd(num1, number)
return num1

# 使用示例
print(gcd_multiple([48, 18, 24])) # 输出应该是6

算法的实际应用案例:

  • 密码学:在公钥加密中,需要用到最大公约数来生成密钥。
  • 音乐理论:计算音乐中的和声和调式。
  • 计算机图形学:在变换和旋转中,使用最大公约数来简化计算。

2. 深度优先搜索(Depth-First Search, DFS)

产生原因与背景:

想象你在一座错综复杂的迷宫中,想要找到一条通往出口的路。深度优先搜索就像是沿着迷宫的一条路径一直走到底,如果发现是死路,再回溯到上一个分叉路口,尝试另一条路径。

算法思路:

深度优先搜索就像是在探索一个未知的森林,每次选择一个方向深入,直到无法前进,然后返回到上一个分叉点,尝试其他方向,直到探索完所有可能的路径。

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def depth_first_search(graph, start, visited=None):
if visited is None:
visited = set() # 创建一个集合来记录访问过的节点
visited.add(start) # 将起始节点标记为已访问
print(start) # 处理当前节点

# 遍历与当前节点相邻的所有节点
for neighbor in graph[start]:
if neighbor not in visited:
depth_first_search(graph, neighbor, visited) # 递归地访问未访问的邻居

# 定义一个图
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}

# 执行深度优先搜索
depth_first_search(graph, 'A')

算法优缺点:

优点:深度优先搜索可以探索图或树中所有的路径,对于查找连接组件或解决谜题特别有效。
缺点:如果图很大,深度优先搜索可能会陷入深的分支,导致效率低下。

解决缺点的方法:

使用迭代深度优先搜索,配合显式栈来避免递归调用,提高效率。

解决缺点后的代码案例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def iterative_dfs(graph, start):
stack = [start] # 使用栈来存储待访问的节点
visited = set() # 创建一个集合来记录访问过的节点

while stack:
vertex = stack.pop() # 取出栈顶的节点
if vertex not in visited:
visited.add(vertex) # 将节点标记为已访问
print(vertex) # 处理当前节点
# 将与当前节点相邻的未访问节点压入栈中
stack.extend(neighbor for neighbor in graph[vertex] if neighbor not in visited)

# 执行迭代深度优先搜索
iterative_dfs(graph, 'A')

算法的实际应用案例:

  • 网络爬虫:在网页抓取中,深度优先搜索可以用来从一个页面开始,探索整个网站。
  • 社交网络分析:分析社交网络中的连接模式和社区结构。
  • 游戏AI:在游戏开发中,用于角色导航和路径搜索。

3. 动态规划(Dynamic Programming)

产生原因与背景:

想象你要爬上一段楼梯,可以选择每次爬一个台阶或两个台阶。动态规划就像是记录下每次爬楼梯的方式,这样当你再次经过某个台阶时,就可以直接使用之前的方法,而不必重新考虑。

算法思路:

动态规划就像是解决一个拼图,你将大问题分解为小问题,然后解决这些小问题,并将解决方案存储起来。当你需要解决一个大问题时,可以利用这些小问题的解决方案来构造出大问题的解决方案。

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def climb_stairs(n):
# 如果只有一级或两级台阶,只需要1种或2种方法
if n <= 2:
return n
# 初始化动态规划表格,存储爬到第i级台阶的方法数
dp = [0] * (n + 1)
dp[1], dp[2] = 1, 2

# 动态规划填表过程
for i in range(3, n + 1):
# 爬到第i级台阶的方法数是爬到第i-1级和第i-2级台阶的方法数之和
dp[i] = dp[i - 1] + dp[i - 2]

# 返回爬到第n级台阶的方法数
return dp[n]

# 使用示例
print(climb_stairs(4)) # 输出应该是5

算法优缺点:

优点:动态规划可以高效地解决具有重叠子问题和最优子结构的问题,如优化问题。
缺点:动态规划需要存储子问题的解,这可能导致较高的空间复杂度。

解决缺点的方法:

使用空间优化技术,如滚动数组,来减少存储需求。

解决缺点后的代码案例:

1
2
3
4
5
6
7
8
9
10
def climb_stairs_space_optimized(n):
# 空间优化:只存储前两个状态
first, second = 1, 2
for i in range(3, n + 1):
# 计算当前状态,并更新前两个状态
first, second = second, first + second
return second

# 使用示例
print(climb_stairs_space_optimized(4)) # 输出应该是5

算法的实际应用案例:

  • 资源分配:在预算有限的情况下,如何分配资源以最大化效益。
  • 生物信息学:在基因序列比对中,找到最优的序列对齐方式。
  • 网络设计:在网络路由中,找到数据传输的最优路径。

| 事项二

执行C#计划

第二阶段:面向对象编程(4-6个月)

学习模块:面向对象编程(OOP)

  1. 类和对象(Classes and Objects)

    • 比喻:想象一下,类是一张制作蛋糕的食谱。食谱上详细列出了制作蛋糕所需的所有材料和步骤,但它本身并不是蛋糕。对象则是根据食谱实际制作出来的蛋糕。每个蛋糕(对象)都是食谱(类)的具体实例,但可以有不同的装饰或口味变化。
    • 内容
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      class Cake { // 蛋糕食谱
      public string Flavor; // 蛋糕的口味
      public int Layers; // 蛋糕的层数

      public void Decorate() { // 装饰蛋糕的方法
      Console.WriteLine($"Decorating the {Flavor} cake with {Layers} layers.");
      }
      }

      Cake chocolateCake = new Cake(); // 制作一个巧克力蛋糕
      chocolateCake.Flavor = "Chocolate"; // 选择巧克力口味
      chocolateCake.Layers = 2; // 决定蛋糕有两层
      chocolateCake.Decorate(); // 装饰蛋糕
  2. 继承(Inheritance)

    • 比喻:继承可以比作是一棵家族树。树的根是最基本的类(祖先),而树枝和叶子代表了从祖先继承并发展出的新类(后代)。后代拥有祖先的所有特性,但也可能有一些独特的特征。
    • 内容
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      class Animal {
      public void Eat() {
      Console.WriteLine("The animal is eating.");
      }
      }

      class Dog : Animal { // 狗是动物的一种
      public void Bark() {
      Console.WriteLine("The dog barks.");
      }
      }

      Dog myDog = new Dog(); // 我的狗
      myDog.Eat(); // 狗继承了吃的行为
      myDog.Bark(); // 狗有独特的叫行为
  3. 封装(Encapsulation)

    • 比喻:封装就像是一个俄罗斯套娃,每个娃娃(对象)都有自己的秘密(私有成员),只能通过特定的方式(公共方法)来与外界交互。
    • 内容
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      class BankAccount {
      private double balance; // 账户余额是私有的,就像套娃的秘密

      public void Deposit(double amount) { // 存款方法
      if (amount > 0) {
      balance += amount;
      Console.WriteLine($"Deposited {amount}. New balance is {balance}.");
      }
      }

      public double GetBalance() { // 提供获取余额的方法
      return balance;
      }
      }

      BankAccount myAccount = new BankAccount();
      myAccount.Deposit(100); // 存钱
      Console.WriteLine($"Current balance: {myAccount.GetBalance()}"); // 查看余额
  4. 多态(Polymorphism)

    • 比喻:多态可以比作是一把多功能钥匙,它可以打开多种类型的锁(方法)。不同的锁(子类)可能需要不同的开启方式,但钥匙(父类引用)是相同的。
    • 内容
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      class Vehicle {
      public virtual void Move() {
      Console.WriteLine("The vehicle moves.");
      }
      }

      class Car : Vehicle {
      public override void Move() { // 重写移动方法
      Console.WriteLine("The car drives.");
      }
      }

      Vehicle myVehicle = new Car(); // 用车辆类型引用汽车对象
      myVehicle.Move(); // 根据实际对象类型执行相应的移动方式
  5. 接口(Interfaces)

    • 比喻:接口就像是一个插头规范,不同的设备(类)可以制造出符合这个规范的插头(实现接口),这样它们就可以插入同一个插座(被调用)。
    • 内容
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      interface IChargeable {
      void Charge(); // 充电的方法
      }

      class Smartphone : IChargeable {
      public void Charge() { // 实现充电接口
      Console.WriteLine("Charging the smartphone.");
      }
      }

      Smartphone myPhone = new Smartphone();
      myPhone.Charge(); // 使用符合规范的充电方法

通过这些详细的比喻和代码示例,你可以更加生动和形象地理解面向对象编程的基本概念。面向对象编程是一种强大的编程范式,它通过类和对象的抽象,以及继承、封装、多态和接口等机制,帮助我们构建出既灵活又易于维护的软件系统。

| 事项三

在poe.com上创建一个自己的AI机器人

1
name: GPT-4o-Coach
1
photo: C:\Users\admin\Pictures\ComfyUI_00094_.png
1
model: GPT-4o
1
2
3
4
5
6
7
8
9
10
Here is the brief:
- Role Name: Personal Coach for GPT-4o
- Role Positioning: Expert in personalized guidance for life and work
- Core Competencies: Listening, analysis, advising, emotional support
- Service Mission: To provide users with personalized and professional guidance to help achieve goals and enhance the quality of life
- Applicable Scenarios: Personal growth, career development, emotional management, life planning
- User Experience: Friendly, professional, easy to communicate with
- Interaction Method: Providing real-time feedback and suggestions through conversation
- Privacy Protection: Strictly protecting user privacy to ensure all communications are secure
- Continuous Support: Offering long-term companionship and guidance to assist users in continuous improvement
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Here is the prompt:
- Role: Personal Coach for GPT-4o
- Background: Users require an artificial intelligence assistant that can provide personalized guidance and communication to help them make progress in learning, work, or life.
- Profile: You are an experienced personal coach, skilled in listening, understanding, and guiding users, offering personalized advice and solutions.
- Skills: Communication, personalized guidance, problem-solving, emotional support.
- Goals: As the user's personal coach, you need to help users set goals, make plans, and provide ongoing support and encouragement.
- Constraints: Maintain a positive and encouraging attitude, respect user privacy and choices, and provide safe and helpful advice.
- OutputFormat: A friendly conversational format, combining specific suggestions and action steps.
- Workflow:
1. Welcome the user and understand their needs and goals.
2. Provide personalized advice and guidance based on the user's needs.
3. Encourage the user to take action and provide continuous support.
- Examples:
User: I've been feeling a lot of stress lately and don't know what to do.
Personal Coach 001: I understand how you feel. First, let's identify the sources of your stress together. Then, we can explore some stress-relief methods, such as deep breathing exercises, scheduling downtime, or seeking social support.
- Initialization: Welcome! I am your Personal Coach for GPT-4o. No matter what challenges you face, I am here to support you. Let's get started, please tell me where you need help today.
1
2
Here is the greeting message:
Welcome! I am your Personal Coach for GPT-4o. Whatever assistance you need today, I am here to support you. Let's get started, please tell me about your goals or where you need help.

今日事项-2024年6月28日
http://example.com/2024/06/28/今日事项-2024年6月28日/
Beitragsautor
XiaoXiangHui
Veröffentlicht am
June 28, 2024
Urheberrechtshinweis