recursion

How to draw right triangle using recursion in prolog?

折月煮酒 提交于 2021-01-04 04:34:31
问题 I get the answer for this right triangle as shown below: shape:- shape(0, 6). shape(S, A) :- S < A, count(0, S), S1 is S+1, shape(S1, A). shape(S, X) :- S >= X. count(A, B) :- A =< B, write('*'), A1 is A+1, count(A1,B). count(A, B) :- A > B, nl. * ** *** **** ***** ****** What should i modify to print the right triangle of this type? * ** *** **** ***** ****** 回答1: shape(S, _) :- S =< 0. shape(S, N) :- S > 0, S1 is S-1, count(0, S1, N), shape(S1, N). count(A, _, N) :- A >= N, nl. count(A, B,

How to draw right triangle using recursion in prolog?

久未见 提交于 2021-01-04 04:29:39
问题 I get the answer for this right triangle as shown below: shape:- shape(0, 6). shape(S, A) :- S < A, count(0, S), S1 is S+1, shape(S1, A). shape(S, X) :- S >= X. count(A, B) :- A =< B, write('*'), A1 is A+1, count(A1,B). count(A, B) :- A > B, nl. * ** *** **** ***** ****** What should i modify to print the right triangle of this type? * ** *** **** ***** ****** 回答1: shape(S, _) :- S =< 0. shape(S, N) :- S > 0, S1 is S-1, count(0, S1, N), shape(S1, N). count(A, _, N) :- A >= N, nl. count(A, B,

Evaluate expression tree in Javascript

萝らか妹 提交于 2021-01-03 06:11:39
问题 I have input consisting of nested logical expression objects Ex: var obj = { 'OR': [ { 'AND': [ false, true, true ] }, { 'OR': [ true, false, false, { 'AND': [true, true] } ] }, true ] }; Which is equivalent to ((false && true && true) || (true || false || false || (true && true)) || true) We need to write a function that calculates this Approach: Go to the innermost level and evaluate it first, moving to the top var expressionEvaluator = function(opArr){ var hasChildObjects = function(arr){

How to write foldr (right fold) generator in Python?

戏子无情 提交于 2021-01-03 01:09:08
问题 Python's reduce is a left-fold, which means it is tail-recursive and its uses can be neatly rewritten as a loop. However, Python does not have a built-in function for doing right folds. Since right-folds are most naturally written with recursion (and Python doesn't like recursion as much as functional languages), I'm interested in writing a right fold ( foldr ) in terms of a generator. How can this be done? And very specifically, how can it be done in Python 2.7? EDIT: I should have mentioned

How to write foldr (right fold) generator in Python?

吃可爱长大的小学妹 提交于 2021-01-03 00:56:08
问题 Python's reduce is a left-fold, which means it is tail-recursive and its uses can be neatly rewritten as a loop. However, Python does not have a built-in function for doing right folds. Since right-folds are most naturally written with recursion (and Python doesn't like recursion as much as functional languages), I'm interested in writing a right fold ( foldr ) in terms of a generator. How can this be done? And very specifically, how can it be done in Python 2.7? EDIT: I should have mentioned

Range error when overriding Object.prototype method with Function constructor

北战南征 提交于 2021-01-01 13:33:34
问题 I'm trying to override Object.prototype.toString in a bid to add functionality for additional class descriptions. Here's the initial code: (function(toString){ Object.prototype.toString = function(){ if(this instanceof TestClass) { return '[object TestClass]'; } return toString.apply(this, arguments); } })(Object.prototype.toString); function TestClass(){} var instance_obj = new TestClass(); Object.prototype.toString.call(instance_obj); When I run this in the console, I get the following

Python: Recursive Function. How to return all subsets of targetsum

情到浓时终转凉″ 提交于 2021-01-01 06:45:53
问题 My code is not showing the shortest subset eg [7] or it is not reading all the subsets, [7], [3,4] to return the shortest subset. Can explain why only 1 set of result is return and how should I modify it to show all subset? Thanks Image of Code that i wanted to follow as below def howsum(targetsum,numbers,combo=None): if combo == None: combo = list() if targetsum == 0: return [ ] if targetsum < 0: return None shortcombo = None for number in numbers: remainder = targetsum - number combo =

Python: Recursive Function. How to return all subsets of targetsum

梦想与她 提交于 2021-01-01 06:45:36
问题 My code is not showing the shortest subset eg [7] or it is not reading all the subsets, [7], [3,4] to return the shortest subset. Can explain why only 1 set of result is return and how should I modify it to show all subset? Thanks Image of Code that i wanted to follow as below def howsum(targetsum,numbers,combo=None): if combo == None: combo = list() if targetsum == 0: return [ ] if targetsum < 0: return None shortcombo = None for number in numbers: remainder = targetsum - number combo =

Looping through tree hierarchy in python?

本小妞迷上赌 提交于 2021-01-01 06:39:39
问题 I'm new here and pretty new to python! We got a homework, and I already was able to do rest of it, but one problem remains: If I have a tree hierarchy like this: root = [ parent1 = [ child1, child2 = [ sub_child ] child3 ], parent2 = [ child1, child2 ] ] And they are all instances of one class named TreeHierarchyClass , and they all have a name attribute, how can I find the one with name I input? I tried to use for loops but there's no way to know how many I need? Getting the name is easy:

Python every possible combination of a string

不羁的心 提交于 2020-12-29 08:44:38
问题 Hi so I'm working with python and I'm trying to write a method where given a string, it would find every combination of that string and append it to a list. I'll give the string and show the outcome that I want. string: x = 'god' outcome: lst = ['g', 'o', 'd', 'go', 'gd', 'og', 'od', 'dg', 'do', 'god', 'gdo', 'ogd', 'odg', 'dgo', 'dog'] A letter can only be used by the number of times it appears on the string given, so if our string is 'god' , 'gg' or 'goo' etc. cannot be appended. If this