漢諾塔遞歸算法進階_進階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.pswp.cn/news/389893.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389893.shtml
英文地址,請注明出處:http://en.pswp.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 系統中文件系統的…