recursion

Appending a List and Recursion (Hamming Distance) - Python 3

你。 提交于 2020-01-06 08:14:02
问题 I'm supposed to write a program that takes a string of binary code and a number, and outputs all the strings within that hamming distance of the original string. I have a function that does everything, but in the output there are lists within lists. I understand why this is - the function is recursive, and sometimes it will return a list of possible values. The problem is, I don't know how to change it so it outputs complete strings. For example, for a string of "0000" and hamming distance "2

Recursively add a node at a certain index on Linked List

拟墨画扇 提交于 2020-01-06 07:28:08
问题 I'm trying to add a list node at a specified index recursively. By that I mean the List class addAtRec() calls addAtRec() in the ListNode class, that method is supposed to be recursive. This is what I did: List: public class List implements Cloneable { private ListNode firstNode; private ListNode lastNode; private String name; private int counter; public List(){ this("list"); } public void addAtRec(Object obj, int k) { if(firstNode != null) firstNode.addAtRec(obj, k, firstNode); } } That's of

How to keep recording key strokes while program not in focus (in background) in F#?

独自空忆成欢 提交于 2020-01-06 07:10:34
问题 The code below is part of a bigger ML project, but I want it to run while it's minimised too. How can I do this? I think I need to run it as a windows process to achieve this, but there isn't much information on how to do that in F#. Or is there another way? let rec f() = let c = System.Console.ReadKey().KeyChar printfn "%c" c f() f() 回答1: From the link in Aaron's comment I decided to give the port a try. This works for me. Note that this could definitely be cleaned up a bit more. I placed

Function which will return particular node from tree structure

瘦欲@ 提交于 2020-01-06 07:01:41
问题 I am writing the function which will return particular node from tree structure. But when I search in a tree using LINQ it is searching in the first branch and finally when it reaches to leaf it is throwing null reference exception as leaf don't have any child. Here is my class, public class Node { public int Id { get; set; } public string Name { get; set; } public string Content { get; set; } public IEnumerable<Node> Children { get; set; } public IEnumerable<Node> GetNodeAndDescendants() //

Function which will return particular node from tree structure

霸气de小男生 提交于 2020-01-06 07:00:54
问题 I am writing the function which will return particular node from tree structure. But when I search in a tree using LINQ it is searching in the first branch and finally when it reaches to leaf it is throwing null reference exception as leaf don't have any child. Here is my class, public class Node { public int Id { get; set; } public string Name { get; set; } public string Content { get; set; } public IEnumerable<Node> Children { get; set; } public IEnumerable<Node> GetNodeAndDescendants() //

Recursively Calling A Class in React-Native

丶灬走出姿态 提交于 2020-01-06 06:54:34
问题 I have a JSON object storing my menus. By default the first menu gets displayed. On Press of any menu item I want to display the menu number defined against "seq". Here is my JSON file **menu.json" {"main": [ {"mid": 0, "menu": [ {"id": "1", "title": "Item-1-0", "kw": "Item-1", "type" : "M", "seq" : 1}, {"id": "2", "title": "Item-2", "kw": "Item-2", "type" : "M", "seq" : 2}, {"id": "3", "title": "Item-3", "kw": "Item-3", "type" : "M", "seq" : 3}, {"id": "4", "title": "Item-4", "kw": "Item-4",

compare lists using wild cards

ぐ巨炮叔叔 提交于 2020-01-06 06:52:23
问题 I am trying to write a function which compares two lists for homework. When the function run it should be something like this ;(cmp ‘(cat ?x mat ?x) ‘(cat bat mat bat)) => t ;(cmp ‘(cat ?x mat ?x) ‘(cat bat mat sat)) => nil. meaning that in the first list when equal to ?x and the second ?x return true if both are pointing to the same value. When I run the program now is giving me "error while parsing arguments to special form if: invalid number of elements" Here is my code if you can give me

How can I programme a minimax algorithm for nim game python?

泄露秘密 提交于 2020-01-06 06:50:00
问题 I tried to programme a minimax algorithm in python. But it is so confusing. I am new to recursion functions. My mind structure has some error at somewhere but I could not solve it. My minimax tree returns with '-100' that must be 100 to achieve true answer. If anything is missing or not clear, please just let me know. Thank you def startposition(): return 2, 'max' def terminalstate(state): if state == (0, 'min') or state == (0, 'max'): return True else: return False def minimax(state): if

Modified Towers of Hanoi with 5 pegs n disks

巧了我就是萌 提交于 2020-01-06 06:34:26
问题 My Question is that I have a Tower of Hanoi with 5 pegs but movement is limited to Start -> Aux1 <-> Aux2 <-> Aux3 -> Destination Start can only move out Only Aux1, Aux2, Aux3 can exchange disks Once a disk arrive Destination, it cannot go back How can I expand the algorithm from 3 peg to 5 peg version with n disks? n = 1: Aux1 -> Aux2 Aux2 -> Aux3 n >= 2 : Hanoi(Aux1,Aux2,Aux3,n-1) Aux1 -> Aux2 Hanoi(Aux3,Aux2,Aux1,n-1) Aux2 -> Aux3 Hanoi(Aux1,Aux2,Aux3,n-1) 来源: https://stackoverflow.com

Why memory limit exceeded and how can i avoid this?

痴心易碎 提交于 2020-01-06 06:28:38
问题 import java.util.*; public class AlmostPerfect { public static void main(String[] args) { Scanner x = new Scanner(System.in); while(x.hasNext()){ int n = x.nextInt(); int sum = recursion(n,n-1,0); if (sum == n) { System.out.println(n + " perfect"); } else if ((n - sum) <= 2) { System.out.println(n + " almost perfect"); } else { System.out.println(n + " not perfect"); } } } public static int recursion(int n, int x,int sum) { if(x==0){ return sum; } else if (n % x == 0) { sum+=x; return