reduce

Find the minimum and maximum values in the nested object

吃可爱长大的小学妹 提交于 2019-12-25 00:44:22
问题 I have a deeply nested javascript object with an unlimited amout of children. Every child has a value. var object = { value: 1, children: { value: 10, children:{ value: 2, children: {...} } } } All attempts to make a recursive function did not succeed, it turned out to go down only to a lower level. 回答1: After flattening your linked list into an array, you can use Array.prototype.reduce() with an accumulator that is a tuple of min and max , starting with initial values of Infinity and

ReduceByKey function In Spark

丶灬走出姿态 提交于 2019-12-24 13:59:11
问题 I've read somewhere that for operations that act on a single RDD, such as reduceByKey() , running on a pre-partitioned RDD will cause all the values for each key to be computed locally on a single machine, requiring only the final, locally reduced value to be sent from each worker node back to the master. Which means that I have to declare a partitioner like: val sc = new SparkContext(...) val userData = sc.sequenceFile[UserID, UserInfo]("hdfs://...") .partitionBy(new HashPartitioner(100)) //

Color reduction (in Java)

∥☆過路亽.° 提交于 2019-12-24 12:08:43
问题 I would like to find a way to take JPEG (or GIF/PNG) images and reduce the amount of colors to e.g. 20. Could someone recommend some library or other reference? Also source codes in other languages are welcome. 回答1: Take a look at the Java Advanced Imaging API. There are a number of algorithms implemented in that API for doing color reduction. 回答2: JAI (Java Advanced Imaging API) would do the work but it has some drawbacks. The API is far from being easy to use, especially if you care about

Reduce Time complexity of the following program

送分小仙女□ 提交于 2019-12-24 11:40:41
问题 import java.util.Scanner; class Special_Pairs{ private static Scanner scan; public static void main(String [] args) { byte t; int n; scan = new Scanner(System.in); t=scan.nextByte(); int[] a=new int[100000]; while(t>0) { int i,j,count=0; n=scan.nextInt(); for(i=0;i<n;i++) { a[i]=scan.nextInt(); } for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(((a[i]&a[j])==0)||((a[j]&a[i])==0)) { count++; } } } t--; System.out.println(count); } } } Help me reduce time complexity of this program Question : You have

Map one value to all values with a common relation Scala

☆樱花仙子☆ 提交于 2019-12-24 08:39:14
问题 Having a set of data: {sentenceA1}{\t}{sentenceB1} {sentenceA1}{\t}{sentenceB2} {sentenceA2}{\t}{sentenceB1} {sentenceA3}{\t}{sentenceB1} {sentenceA4}{\t}{sentenceB2} I want to map a sentenceA to all the sentences that have a common sentenceB in Scala so the result will be something like this: {sentenceA1}->{sentenceA2,sentenceA3,sentenceA4} or {sentenceA2}->{sentenceA1, sentenceA3} 回答1: val lines = List( "sentenceA1\tsentenceB1", "sentenceA1\tsentenceB2", "sentenceA2\tsentenceB1",

Create Nested Hashes from a List of Hashes in Ruby

…衆ロ難τιáo~ 提交于 2019-12-24 06:52:01
问题 I have a set of categories and their values stored as a list of hashes: r = [{:A => :X}, {:A => :Y}, {:B => :X}, {:A => :X}, {:A => :Z}, {:A => :X}, {:A => :X}, {:B => :Z}, {:C => :X}, {:C => :Y}, {:B => :X}, {:C => :Y}, {:C => :Y}] I'd like to get a count of each value coupled with its category as a hash like this: {:A => {:X => 4, :Y => 1, :Z => 1}, :B => {:X => 2, :Z => 1}, :C => {:X => 1, :Y => 3}} How can I do this efficiently? Here's what I have so far (it returns inconsistent values):

Create Nested Hashes from a List of Hashes in Ruby

老子叫甜甜 提交于 2019-12-24 06:51:45
问题 I have a set of categories and their values stored as a list of hashes: r = [{:A => :X}, {:A => :Y}, {:B => :X}, {:A => :X}, {:A => :Z}, {:A => :X}, {:A => :X}, {:B => :Z}, {:C => :X}, {:C => :Y}, {:B => :X}, {:C => :Y}, {:C => :Y}] I'd like to get a count of each value coupled with its category as a hash like this: {:A => {:X => 4, :Y => 1, :Z => 1}, :B => {:X => 2, :Z => 1}, :C => {:X => 1, :Y => 3}} How can I do this efficiently? Here's what I have so far (it returns inconsistent values):

Create an tree of objects from arrays

萝らか妹 提交于 2019-12-24 01:42:58
问题 i'd like to make a tree of objects from arrays. A nice solution has been provided to me (where i've discovered "reduce" method) here : Javascript build a tree from a string with object.create() Actually, my need is a bit different, and i don't manage to adapt the provided code... so I come back here to be helped !!! (thank you). I'll post the full initial code at the end of this post input : [ { "name": "John Doe", "service": "EE", }, { "name": "Jane Doe", "service": "EE.EA", }, { "name":

Jison: Reduce Conflict where actually no conflict is

我的梦境 提交于 2019-12-24 00:57:02
问题 I'm trying to generate a small JavaScript parser which also includes typed variables for a small project. Luckily, jison already provides a jscore.js which I just adjusted to fit my needs. After adding types I ran into a reduce conflict. I minimized to problem to this minimum JISON: Jison: %start SourceElements %% // This is up to become more complex soon Type : VAR | IDENT ; // Can be a list of statements SourceElements : Statement | SourceElements Statement ; // Either be a declaration or

MPI_REDUCE causing memory leak

只谈情不闲聊 提交于 2019-12-23 13:15:35
问题 I have recently encountered a weir behavior. If I run the following code on my machine (using the most recent version of cygwin, Open MPI version 1.8.6) I get a linearly growing memory usage that quickly overwhelms my pc. program memoryTest use mpi implicit none integer :: ierror,errorStatus ! error codes integer :: my_rank ! rank of process integer :: p ! number of processes integer :: i,a,b call MPI_Init(ierror) call MPI_Comm_rank(MPI_COMM_WORLD, my_rank, ierror) call MPI_Comm_size(MPI_COMM