How do i implement this python tree linked list code in dart?

2024/10/6 12:25:27

Here is the python code


def tree(root_label, branches=[]):for branch in branches:assert is_tree(branch), 'branches must be trees'return [root_label] + list(branches)def label(tree):return tree[0]def branches(tree):return tree[1:]def is_tree(tree):if type(tree) != list or len(tree) < 1:return Falsefor branch in branches(tree):if not is_tree(branch):return Falsereturn Truedef is_leaf(tree):return not branches(tree)t = tree(3, [tree(1), tree(2, [tree(1), tree(1)])])

Here is my implementation of the above code in dart.

isTree(tree) {if ((tree is! List) | (tree.length < 1)) {return false;}for (final branch in branches(tree)) {if (!isTree(branch)) {return false;}return true;}
}branches(tree) {return tree.sublist(1);
}label(tree) {return tree[0];
}tree(rootLabel, [branches = const []]) {for (final branch in branches) {assert(isTree(branch));}return ([rootLabel] + branches);
}var t = tree(3, [tree(1),tree(2, [tree(1), tree(1)])
]);

When i try to declare "t" it is giving the error too many positional arguments. This is the expected output of t.

[3, [1], [2, [1], [1]]]

Original source of python code can be found here https://composingprograms.com/pages/23-sequences.html#trees

I have tried this code in python before and it works perfectly. In dart I am running into errors i have mentioned above.

I am getting this error

<Y0>({bool growable}) => List<Y0>' is not a subtype of type 'Iterable<dynamic>

I can't figure out whats causing this error. :/

LTJ was also helpful, but i got this solution from a redditor, apparently the error was being caused by

[branches = List.empty] - List.empty

was the problem all along!

Replacing it with const [] and making some other small changes in the

code helped!!

tHANKS!

Answer

Named keyword arguments (declared with using {}) require keyword to be used when calling the function. You probably wanted to use named positional argument (declared with []). Modify your tree function to look like this:

tree(rootLabel, [branches = List.empty])

Or if you want to keep the keyword argument, use the keyword when calling the function:

var t = tree(3, branches: [tree(1),tree(2, branches: [tree(1), tree(1)])
]);
https://en.xdnf.cn/q/119431.html

Related Q&A

How can i find the ips in network in python

How can i find the TCP ips in network with the range(i.e 132.32.0.3 to 132.32.0.44) through python programming and also want to know the which ips are alive and which are dead. please send me.. thanks …

Python Coding (call function / return values)

I am having trouble writing code for this question. I have seen this question asked in a few places but I still cannot figure out the answer from the tips they provided. The question is: Write a progr…

discord.py Mention user by name

I am trying to mention a user by their name in discord.py. My current code is: @bot.command(name=mention) @commands.has_role(OwnerCommands) async def mention(ctx, *, member: discord.Member):memberid = …

Python unexpected EOF while parsing : syntax error

I am trying to do a simple toto history with a dictionary and function however I have this funny syntax error that keeps appearing that states "unexpected EOF while parsing" on the python she…

How can I read part of file and write the rest to another file?

I have multiple large csv file. How can I read part of each file and write 10% of the data/rows to another file?

Encryption/Decryption - Python GCSE [duplicate]

This question already has an answer here:Encryption and Decryption within the alphabet - Python GCSE(1 answer)Closed 8 years ago.I am currently trying to write a program, for school, in order to encryp…

Convert decimal to binary (Python) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Kivy Digital Clock Issues

Im trying to add a digital clock to my Kivy program, it seems to be having trouble.Here is the .py:import kivykivy.require(1.10.0)from kivy.lang import Builder from kivy.uix.screenmanager import Screen…

Read a file name and create a column with it [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…

Python : T test ind looping over columns of df

My dataframe is composed of accounting variables and a dummy variable that allows me to identify two types of company. I would like to perform a t-test for every column of my dataframe in order to comp…