algorithm

Recursively finding all partitions of a set of n objects into k non-empty subsets

爱⌒轻易说出口 提交于 2021-02-11 15:01:20
问题 I want to find all partitions of a n elements into k subsets, this is my algorithm based on recursive formula for finding all Stirling second numbers fun main(args: Array<String>) { val s = mutableSetOf(1, 2, 3, 4, 5) val partitions = 3 val res = mutableSetOf<MutableSet<MutableSet<Int>>>() partition(s, partitions, res) //println(res) println("Second kind stirling number ${res.size}") } fun partition(inputSet: MutableSet<Int>, numOfPartitions: Int, result: MutableSet<MutableSet<MutableSet<Int>

Good algorithm for a query related problem

纵然是瞬间 提交于 2021-02-11 14:56:21
问题 Given N pairs, we have to find the count of pairs that contain an element k in their range, i.e : If a Pair i is defined as (X i ,Y i ), then if Pair i contain K in its range, then X i <= K <= Y i . Now we are given Q such queries to handle with each query consisting of an integer K. Input: The first line contains two space-separated integers N and Q. Next N lines follow where each line denotes a pair. Each line contains two space-separated integers. Next Q lines follow where each line

How to sort keys in json by another json values

拜拜、爱过 提交于 2021-02-11 14:40:46
问题 I have two jsons. First: here Second: here How to sort second json by first? I want to sort keys and it's values in second json by values in first json. 回答1: You could do something like this var first = ["name0", "name1", "name2"] var second = { {"name2":"xyz", "name0" : "xyz", "name1" : "xyz" } } for( var i = 1; i < Object.keys(second).length; i++ ){ var aux = {} first.forEach(function(element2) { resul[element2] = second[i][element2] }); } 来源: https://stackoverflow.com/questions/54029071

Determining a straight line equation from 2 cartesian coordinates

早过忘川 提交于 2021-02-11 14:26:34
问题 I can do this on paper easily enough but have a bit of a mental block in getting this into a language (I'd take any answer but Java probably easiest). I have two sets of points Point A (xA, yA) and Point B (xB, yB). Knowing this, and assuming that these two create a straight line graph I need to be able write a function that will give me xC given that I would know yC (and, obviously that the new point is on the same line). All help appreciated :) Kind Regards 回答1: (yB-yA)/(xB-xA) = (yC - yA)

Can there be an efficient way for the dual loops?

拈花ヽ惹草 提交于 2021-02-11 14:22:53
问题 I got a question in exam where I was given an array a a = [9,8,10,2] what I need to do is cross iterate the array on itself and get the concatenation of all the possible elements i.e a*a Once all elements are concatenated in that order, then I need to sum them up. My code is in the snippet: Also tried in PHP function sumIt($a) { $totalSum = 0; $len1 = count($a); for($i = 0; $i < $len1; $i++){ for($ii = 0; $ii < $len1; $ii++) { $totalSum += $a[$i].''.$a[$ii]; } } return $totalSum; } The input

Why can I evaluate a function receiving a std::pair at compile-time, but not assert it? [duplicate]

依然范特西╮ 提交于 2021-02-11 14:13:59
问题 This question already has answers here : constexpr function parameters as template arguments (4 answers) C++11 constexpr function pass parameter (3 answers) Closed 12 months ago . I wrote a function that receives a variadic number of std::pairs. It takes the pairs, subtracts the 2nd element against the 1st element of each pair, and returns a tuple of the newly generated values like this: #include <tuple> #include <utility> template<typename... pairs> inline constexpr auto foo(pairs&& ...p)

Sorting an array using a Binary Search Tree in C++

空扰寡人 提交于 2021-02-11 14:11:07
问题 I'm trying to write a program that sorts integer elements of an array, using a Binary Search Tree(BST) as support data structure. The idea is that once the array is given, then it is possible to use a BST to sort his element; for example: if my array is: {120, 30, 115, 40, 50, 100, 70} then I build a BST like this: Once I have this BST, I can do an inorder tree traversal to touch every node in order, from the lowest to the highest element and modify the array. The result would be a sorted

Sorting an array using a Binary Search Tree in C++

强颜欢笑 提交于 2021-02-11 14:08:18
问题 I'm trying to write a program that sorts integer elements of an array, using a Binary Search Tree(BST) as support data structure. The idea is that once the array is given, then it is possible to use a BST to sort his element; for example: if my array is: {120, 30, 115, 40, 50, 100, 70} then I build a BST like this: Once I have this BST, I can do an inorder tree traversal to touch every node in order, from the lowest to the highest element and modify the array. The result would be a sorted

Generate a custom pattern number sequence in one go

五迷三道 提交于 2021-02-11 13:59:45
问题 I'd like to generate the following number sequence in one go using a functional initialization construct: Array(0, 0, 0, 0, 3, 3, 6, 6, 9, 9, ..., n*3, n*3) One way is to do: Array.fill[Int](2)(0) ++ Array.tabulate(4)(_*3) but I'd need to double each value of the second part of the construct i.e. to get 0, 0 then 3, 3 etc. How can I duplicate the values of the second construct? I also couldn't figure out a mathematical function that would generate such sequence. 回答1: Consider tail-recursive

Generate a custom pattern number sequence in one go

这一生的挚爱 提交于 2021-02-11 13:58:59
问题 I'd like to generate the following number sequence in one go using a functional initialization construct: Array(0, 0, 0, 0, 3, 3, 6, 6, 9, 9, ..., n*3, n*3) One way is to do: Array.fill[Int](2)(0) ++ Array.tabulate(4)(_*3) but I'd need to double each value of the second part of the construct i.e. to get 0, 0 then 3, 3 etc. How can I duplicate the values of the second construct? I also couldn't figure out a mathematical function that would generate such sequence. 回答1: Consider tail-recursive