汉诺塔递归算法进阶_进阶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,一经查实,立即删除!

相关文章

500. 键盘行

500. 键盘行 给你一个字符串数组 words ,只返回可以使用在 美式键盘 同一行的字母打印出来的单词。键盘如下图所示。 美式键盘 中: 第一行由字符 “qwertyuiop” 组成。 第二行由字符 “asdfghjkl” 组成。 第三行由字符 “zxcvbnm” 组成。 示例 1&a…

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…

575. 分糖果

575. 分糖果 给定一个偶数长度的数组&#xff0c;其中不同的数字代表着不同种类的糖果&#xff0c;每一个数字代表一个糖果。你需要把这些糖果平均分给一个弟弟和一个妹妹。返回妹妹可以获得的最大糖果的种类数。 示例 1:输入: candies [1,1,2,2,3,3] 输出: 3 解析: 一共有三…

如何开启并配置CITRIX Xenserver的SNMP服务

以下博文转载至虚拟人生Citrix Xenserver使用标准的NET-SNMP协议&#xff0c;关于NET-SNMP请参考www.net-snmp.org. Xenserver并没有自己的MIB库.Xenserver默认是禁止SNMP服务且并没有开启SNMP服务使用的端口,通过以下方式开启并配置SNMP服务&#xff1a;1.编辑Xenserver的/etc…

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…

C++(1)引用

引用 引用 为对象起另外一个名字&#xff0c;通过将声明符写成 &d&#xff0c;其中d是声明的变量名。一旦初始化完成&#xff0c;引用将和起初始值绑定在一起&#xff0c;无法再绑定到另一个对象&#xff0c;因此引用必须初始化。 引用就是别名&#xff0c;初始化以后&am…

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

普里姆从不同顶点出发绘制大流行时期社区的风险群图&#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 …

一步一步图文介绍SpriteKit使用TexturePacker导出的纹理集Altas

1、为什么要使用纹理集&#xff1f; 游戏是一种很耗费资源的应用&#xff0c;特别是在移动设备中的游戏&#xff0c;性能优化是非常重要的 纹理集是将多张小图合成一张大图&#xff0c;使用纹理集有以下优点&#xff1a; 1、减少内存占用&#xff0c;减少磁盘占用&#xff1b; …

BZOJ.1007.[HNOI2008]水平可见直线(凸壳 单调栈)

题目链接 可以看出我们是要维护一个下凸壳。 先对斜率从小到大排序。斜率最大、最小的直线是一定会保留的&#xff0c;因为这是凸壳最边上的两段。 维护一个单调栈&#xff0c;栈中为当前可见直线(按照斜率排序)。 当加入一条直线l时&#xff0c;可以发现 如果l与栈顶直线l的交…

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

荷兰牛栏 荷兰售价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&…

367. 有效的完全平方数

367. 有效的完全平方数 给定一个 正整数 num &#xff0c;编写一个函数&#xff0c;如果 num 是一个完全平方数&#xff0c;则返回 true &#xff0c;否则返回 false 。 进阶&#xff1a;不要 使用任何内置的库函数&#xff0c;如 sqrt 。 示例 1&#xff1a;输入&#xff1…

袁中的第三次作业

第一题&#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…

2053. 数组中第 K 个独一无二的字符串

2053. 数组中第 K 个独一无二的字符串 独一无二的字符串 指的是在一个数组中只出现过 一次 的字符串。 给你一个字符串数组 arr 和一个整数 k &#xff0c;请你返回 arr 中第 k 个 独一无二的字符串 。如果 少于 k 个独一无二的字符串&#xff0c;那么返回 空字符串 “” 。 …

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

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

linux实验二

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