汉诺塔递归算法进阶_进阶python 1递归

汉诺塔递归算法进阶

When something is specified in terms of itself, it is called recursion. The recursion gives us a new idea of how to solve a kind of problem and this gives us insights into the nature of computation. Basically, many of computational artifacts are naturally self-referential, for example:

如果根据自身指定某些内容,则称为递归。 递归为我们提供了有关如何解决一种问题的新思路,这使我们可以洞悉计算的本质。 基本上,许多计算工件自然都是自引用的,例如:

  • File system

    文件系统
  • Fractal graph

    分形图
  • Algorithms

    演算法

Any recursion function consists of two parts:

任何递归函数都包括两个部分:

  • Base case: the last case of the recursion. For every recursion, the last step must be the base case and it is going to return a specific value to us.

    基本情况:递归的最后一种情况。 对于每次递归,最后一步必须是基本情况,它将为我们返回一个特定值。

  • Reduction step: Assuming that the recursion works for smaller values of its argument, then use the function to compute a new return value.

    归约步骤:假定递归适用于其参数的较小值,然后使用该函数计算新的返回值。

Now think about the following examples:

现在考虑以下示例:

To compute a recursion function of a positive integer N as its parameter,

要计算正整数 N作为其参数的递归函数,

  • Base case: The ending case with N equals a specific number (usually 1 or 0) and this will give us a specific return result under this condition.

    基本情况: N的结束情况等于一个特定的数字(通常为1或0),在这种情况下,这将为我们提供特定的返回结果。

  • Reduction step: For each of the steps of this recursion, we use N-t as its new parameter (t could be any constant based on the question).

    归约步骤:对于该递归的每个步骤,我们将N- t用作其新参数(根据问题, t可以是任何常数)。

In this case, the positive integer N is called the depth of this recursion.

在这种情况下,正整数N称为此递归的深度。

To compute a recursion function of a sequence Seq as its parameter,

要计算序列 Seq的递归函数作为其参数,

  • Base case: The ending case with Seq equals an empty set (empty list/empty string/etc.) and this will give us a specific return result under this condition.

    基本情况:带有Seq的结束情况等于一个空集(空列表/空字符串等),在这种情况下,这将为我们提供特定的返回结果。

  • Reduction step: For each of the steps of this recursion, we use a shorter Seq (usually moves one element from the previous one) as its new parameter.

    归约步骤:对于此递归的每个步骤,我们都使用较短的Seq(通常将上一个元素移到上一个元素)作为其新参数。

问题1.倒数问题 (Question 1. Counting-down Problem)

Suppose we are working on a project of rocket and we want to count down numbers before the rocket blast off. We count down from 5 and after we count 1, we will then print “Blastoff 🚀”. Write a recursion function about it.

假设我们正在研究一个火箭项目,并且我们想在火箭发射之前计算数量。 我们从5开始倒数,再从1开始倒数,然后打印“ Blastoff🚀”。 编写有关它的递归函数。

问题2.创建斐波那契数列 (Question 2. Create a Fibonacci Sequence)

Fibonacci sequence is a series in which each number is the sum of the two preceding numbers.

斐波那契数列是一个序列,其中每个数字是前面两个数字的总和。

Image for post

Suppose that we give an index n, then we are going to return the n-th element of this Fibonacci sequence. Write a recursion function to calculate the n-th element. For example, an index n = 7 will give back 13.

假设我们给定索引n ,那么我们将返回此斐波那契数列的第n个元素。 编写一个递归函数以计算第n个元素。 例如,索引n = 7将返回13。

问题3.计算最大公约数 (Question 3. Calculate the Greatest Common Divisor)

The greatest common divisor (GCD), also called the greatest common factor, of two numbers is the largest natural number d that divides both numbers without a remainder. Let’s code up the Euclidean algorithm (one of the oldest algorithms in common use) to find the GCD. Note that this function should also work for negative numbers and the result GCD is always positive!

两个数的最大公除数(GCD),也称为最大公因数,是将两个数除而无余的最大自然数d 。 让我们编码欧几里得算法 (最常用的最古老算法之一)以找到GCD。 注意,该函数也应适用于负数 ,并且结果GCD始终为

Write a recursive function that returns the greatest common divisor for two numbers.

编写一个递归函数,该函数返回两个数字的最大公约数。

4.计算列表的长度 (4. Calculate the Length of a List)

Find a recursive function that returns the number of items in a list. In other words, write a function that reimplements the len function for lists with recursion.

查找一个返回列表中项目数的递归函数。 换句话说,编写一个函数,为带有递归的列表重新实现len函数。

5.反转字符串 (5. Reverse a string)

Suppose we have a string and we would like to reverse it by recursion. Write a function to implement this.

假设我们有一个字符串,我们想通过递归来反转它。 编写一个函数来实现这一点。

翻译自: https://medium.com/adamedelwiess/advanced-python-1-recursion-5e4a5b71f17

汉诺塔递归算法进阶

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/389893.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

windows 停止nginx

1、查找进程 tasklist | findstr nginx2、杀死进程 taskkill /pid 6508 /F3、一次杀死多个进程taskkill /pid 6508 /pid 16048 /f转载于:https://blog.51cto.com/dressame/2161759

SpringBoot返回json和xml

有些情况接口需要返回的是xml数据&#xff0c;在springboot中并不需要每次都转换一下数据格式&#xff0c;只需做一些微调整即可。 新建一个springboot项目&#xff0c;加入依赖jackson-dataformat-xml&#xff0c;pom文件代码如下&#xff1a; <?xml version"1.0&quo…

orange 数据分析_使用Orange GUI的放置结果数据分析

orange 数据分析Objective : Analysing of several factors influencing the recruitment of students and extracting information through plots.目的&#xff1a;分析影响学生招生和通过情节提取信息的几个因素。 Description : The following analysis presents the diffe…

普里姆从不同顶点出发_来自三个不同聚类分析的三个不同教训数据科学的顶点...

普里姆从不同顶点出发绘制大流行时期社区的风险群图&#xff1a;以布宜诺斯艾利斯为例 (Map Risk Clusters of Neighbourhoods in the time of Pandemic: a case of Buenos Aires) 介绍 (Introduction) Every year is unique and particular. But, 2020 brought the world the …

荷兰牛栏 荷兰售价_荷兰的公路货运是如何发展的

荷兰牛栏 荷兰售价I spent hours daily driving on one of the busiest motorways in the Netherlands when commuting was still a norm. When I first came across with the goods vehicle data on CBS website, it immediately attracted my attention: it could answer tho…

Vim 行号的显示与隐藏

2019独角兽企业重金招聘Python工程师标准>>> Vim 行号的显示与隐藏 一、当前文档的显示与隐藏 1 打开一个文档 [rootpcname ~]# vim demo.txt This is the main Apache HTTP server configuration file. It contains the configuration directives that give the s…

结对项目-小学生四则运算系统网页版项目报告

结对作业搭档&#xff1a;童宇欣 本篇博客结构一览&#xff1a; 1&#xff09;.前言(包括仓库地址等项目信息) 2&#xff09;.开始前PSP展示 3&#xff09;.结对编程对接口的设计 4&#xff09;.计算模块接口的设计与实现过程 5&#xff09;.计算模块接口部分的性能改进 6&…

袁中的第三次作业

第一题&#xff1a; 输出月份英文名 设计思路: 1:看题目&#xff1a;主函数与函数声明&#xff0c;知道它要你干什么2&#xff1a;理解与分析&#xff1a;在main中&#xff0c;给你一个月份数字n&#xff0c;要求你通过调用函数char *getmonth&#xff0c;来判断&#xff1a;若…

Python从菜鸟到高手(1):初识Python

1 Python简介 1.1 什么是Python Python是一种面向对象的解释型计算机程序设计语言&#xff0c;由荷兰人吉多范罗苏姆&#xff08;Guido van Rossum&#xff09;于1989年发明&#xff0c;第一个公开发行版发行于1991年。目前Python的最新发行版是Python3.6。 Python是纯粹的自由…

如何成为数据科学家_成为数据科学家需要了解什么

如何成为数据科学家Data science is one of the new, emerging fields that has the power to extract useful trends and insights from both structured and unstructured data. It is an interdisciplinary field that uses scientific research, algorithms, and graphs to…

阿里云对数据可靠性保障的一些思考

背景互联网时代的数据重要性不言而喻&#xff0c;任何数据的丢失都会给企事业单位、政府机关等造成无法计算和无法弥补的损失&#xff0c;尤其随着云计算和大数据时代的到来&#xff0c;数据中心的规模日益增大&#xff0c;环境更加复杂&#xff0c;云上客户群体越来越庞大&…

linux实验二

南京信息工程大学实验报告 实验名称 linux 常用命令练习 实验日期 2018-4-4 得分指导教师 系 计软院 专业 软嵌 年级 2015 级 班次 &#xff08;1&#xff09; 姓名王江远 学号20151398006 一、实验目的 1. 掌握 linux 系统中 shell 的基础知识 2. 掌握 linux 系统中文件系统的…

个人项目api接口_5个免费有趣的API,可用于学习个人项目等

个人项目api接口Public APIs are awesome!公共API很棒&#xff01; There are over 50 pieces covering APIs on just the Towards Data Science publication, so I won’t go into too lengthy of an introduction. APIs basically let you interact with some tool or servi…

咕泡-模板方法 template method 设计模式笔记

2019独角兽企业重金招聘Python工程师标准>>> 模板方法模式&#xff08;Template Method&#xff09; 定义一个操作中的算法的骨架&#xff0c;而将一些步骤延迟到子类中Template Method 使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤Template Me…

如何评价强gis与弱gis_什么是gis的简化解释

如何评价强gis与弱gisTL;DR — A Geographic Information System is an information system that specializes in the storage, retrieval and display of location data.TL; DR — 地理信息系统 是专门从事位置数据的存储&#xff0c;检索和显示的信息系统。 The standard de…

Scrum冲刺-Ⅳ

第四次冲刺任务 团队分工 成员&#xff1a;刘鹏芝&#xff0c;罗樟&#xff0c;王小莉&#xff0c;沈兴艳&#xff0c;徐棒&#xff0c;彭康明&#xff0c;胡广键 产品用户&#xff1a;王小莉 需求规约&#xff1a;彭康明&#xff0c;罗樟 UML&#xff1a;刘鹏芝&#xff0c;沈…

机器人影视对接_机器学习对接会

机器人影视对接A simple question like ‘How do you find a compatible partner?’ is what pushed me to try to do this project in order to find a compatible partner for any person in a population, and the motive behind this blog post is to explain my approach…

mysql 数据库优化之执行计划(explain)简析

数据库优化是一个比较宽泛的概念&#xff0c;涵盖范围较广。大的层面涉及分布式主从、分库、分表等&#xff1b;小的层面包括连接池使用、复杂查询与简单查询的选择及是否在应用中做数据整合等&#xff1b;具体到sql语句执行效率则需调整相应查询字段&#xff0c;条件字段&…

自我接纳_接纳预测因子

自我接纳现实世界中的数据科学 (Data Science in the Real World) Students are often worried and unaware about their chances of admission to graduate school. This blog aims to help students in shortlisting universities with their profiles using ML model. The p…

python中knn_如何在python中从头开始构建knn

python中knnk最近邻居 (k-Nearest Neighbors) k-Nearest Neighbors (KNN) is a supervised machine learning algorithm that can be used for either regression or classification tasks. KNN is non-parametric, which means that the algorithm does not make assumptions …