i have received an array of elements from the server as web service responds. i am confused about how to handle it. i need to get those items in a single array.
(
(
(
10,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
10,
5,
5,
10,
10,
50
)
)
)
i need all objects in a single array.
so that it should look like
(
10,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
10,
5,
5,
10,
10,
50
)
Suppose yourArray is the array obtained from server as you have shown above, do this
NSArray *singleArray=[[NSArray alloc] initWithArray:[[yourArray objectAtIndex:0]objectAtIndex:0]];
This is for your understanding for the format you got in your Array :
At the first index i.e 0 you got one array. And again in that array at index 0 you got your depth array.
Simply access it like :
NSMutableArray *finalArray = [[yourArray objectAtIndex:0]objectAtIndex:0];
NSLog(#"final Array %#",finalArray);
A better way to do this would be to implement for loops if the result gave you an array with multiple arrays inside of it
NSArray *aryContents; // Your Input array
NSMutableArray *aryEditable = [[NSMutableArray alloc] init];
for (NSArray *adult in aryContents) {
for (NSArray *child in adult) {
[aryEditable addObjectsFromArray:child];
}
}
NSArray *aryFullList = [[NSArray alloc] initWithArray:aryEditable];
[aryEditable release];
Related
Given an array of integers for example let array = [1, 3, 4, 7, 8, 9, 12, 14, 15]
What is the best way of finding the largest amount of consecutive integers preferably without using a for-in loop. If we would pass this array into a function it would return 3 as '7, 8, 9' is the largest amount of consecutive integers.
let array = [1, 3, 4, 7, 8, 9, 12, 14, 15]
func getMaxConsecutives(from array: [Int]) -> Int {
var maxCount = 1
var tempMaxCount = 1
var currentNumber = array[0]
for number in array {
if currentNumber == number - 1 {
tempMaxCount += 1
maxCount = tempMaxCount > maxCount ? tempMaxCount : maxCount
currentNumber = number
} else {
tempMaxCount = 1
currentNumber = number
}
}
return maxCount
}
getMaxConsecutives(from: array)
This works as intended but I would like a more efficient solution something that is not O(n).
I appreciate any creative answers.
You can do it like this:
let array = [1, 3, 4, 7, 8, 9, 12, 14, 15]
if let maxCount = IndexSet(array).rangeView.max(by: {$0.count < $1.count})?.count {
print("The largest amount of consecutive integers: \(maxCount)")
//prints 3
}
I think I can write it more tightly (basically as a one-liner):
let array = [1, 3, 4, 7, 8, 9, 12, 14, 15]
let (_,_,result) = array.reduce((-1000,1,0)) {
$1 == $0.0+1 ? ($1,$0.1+1,max($0.2,$0.1+1)) : ($1,1,$0.2)
}
print(result) // 3
But we are still looping through the entire array — so that we are O(n) — and there is no way to avoid that. After all, think about what your eye does as it scans the array looking for the answer: it scans the whole array.
(One way to achieve some savings: You could short-circuit the loop when we are not in the middle of a run and the maximum run so far is longer than what remains of the array! But the gain might not be significant.)
I have a dasaset of length 200 and length of each row of data is also 200. This dataset is space separated. Here is sample dataset (first row).
-0.1100208269729097 0.1248460463105589 -0.01559138588255286 -0.01625839428292603 -0.05323888667281371 0.06722185430549973 -0.0490877148079949 -0.05039368886946847 0.0897270838973875 0.00754589058726465 -0.06693447805463611 -0.1193740974362337 -0.02214573804045866 0.02930806967704801 -0.009567144727872222 -0.02288991169653539 0.04256313697292451 -0.08190168271952417 0.008274133732539695 -0.02299227162395361 0.0111923018567119 -0.009872522389769637 0.06866110814693088 0.04622954799009332 0.05498202029091768 -0.06672541846259043 -0.05130079655965012 0.1107659505844031 0.07912810279475517 0.02246390669165305 -0.06997067603392053 -0.02069109953229961 -0.05191987832821615 -0.01971016519416264 -0.008691704006401698 -0.02963829527404451 0.02332929010677706 -0.1035585589634834 0.03801924036385142 -0.07035181096148016 -0.02460761051792025 0.05545479574143786 0.06632500394350074 -0.01693623441811409 -0.0202000922412099 0.0387732166529701 -0.06835009268170482 -0.06684471565316714 0.09737868086728406 -0.03776102176325794 -0.03087980353481784 -0.04630278791951752 -0.1129739647985331 0.09622849675187727 0.05975310144103099 -0.08083650075114446 0.05258346559791484 0.05583993856089118 -0.03916345795047688 -0.2981097687887527 0.04087798461219992 0.07153463501552468 0.07113045074135986 0.01717619972420815 -0.01893649865573213 -0.007503347735166889 0.06551854299072507 -0.005153581328393866 -0.08659840104899437 0.04864888731854276 0.08965801176651583 -0.004562179660153576 -0.1252787635844004 0.06896990208188783 -0.003925090827015415 -0.05755687748680104 -0.02544736897698906 0.02530385776038159 -0.125784848738536 0.07433650535349738 0.02153916317259382 0.04738213124034089 -0.03299623626264642 0.02073383160046674 -0.008966711746564809 0.04983292315200202 0.01974696673478601 -0.04419678420395467 -0.02442715323795661 -0.0694663145847256 0.1101497271416977 0.04200639135007367 -0.06082113335723243 -0.01473508072467703 0.01142600017146485 -0.03532257289246362 -0.02260329422449697 0.05396810070565884 0.1581078158241939 -0.05426153505070038 -0.01534772560258162 -0.04461245038675606 -0.05082561044342486 0.003953621713155758 -0.09395992245069541 0.02029879424655968 0.09397373054431565 -0.01540603811173099 -0.00188325436669238 0.07341578917873427 -0.07930228379622654 -0.01519407550785842 0.01388266474816023 0.09152064522133056 0.0106446218365201 -0.2157572256227169 0.04804075039482639 0.01970079327929429 -0.04738197196862703 0.06770927522186629 0.1006260778362594 -0.06299061441376895 0.02961951153113571 0.01572783315493193 0.1349089347411493 -0.0242042239418958 -0.07337276266118564 -0.09620055007994345 0.04754719051788902 -0.04777964847293222 0.01477148963357754 0.06678924792453055 0.05579081171364433 -0.03405429131223387 0.03615588517175376 -0.1554971840439641 -0.04581567263300179 -0.07873107398807083 0.05966093431149457 -0.128446162280915 -0.05912532817875745 0.1194692701951161 0.1103496401807509 0.0153127716173752 0.01607453121383664 -0.07114032721360454 0.03276185612322021 0.1169776569257143 0.07706242373764424 0.04889932405415184 0.0008715101384050066 0.006894007893755344 0.04519320187367908 -0.001306669064508431 0.0291067296150834 -0.02697983215093226 -0.07374490898814057 -0.04408652590757124 0.118965444980577 0.08668199929217432 0.02704832616237655 0.01473294258443707 0.02049896556673346 -0.0569226246137925 -0.0120183686689177 -0.1007080842912528 0.03517628230997978 -0.2003177929062758 0.01491215547976228 0.04590546935765301 0.1670139443078561 -0.05992676476987346 0.07038240324837636 -0.003567431692839979 0.08197255057946093 -0.01384071718153512 0.01443837418022523 -0.0393556604031245 0.003264844777785919 -0.190455395258628 -0.09122702488367737 -0.007113243408323287 0.1221344569965773 -0.06583221256210335 0.002275841418885295 -0.02418590378253777 -0.02462843336523757 -0.1054326841702153 -0.009075125286585313 0.05233463322601897 -0.09944517224527978 0.08201627957443283 0.1144830692826725 -0.1488155291532296 0.001711351371442085 0.06463339531524601 0.02089587578959802 -0.05699940762150812 0.01798950350182588 -0.01642350646709232
I tried in following way to convert it into comma seperated data. Here is my code
val bufferedSource1 = Source.fromFile(Path1 + name)
val lines1 : Iterator[String] = bufferedSource1.getLines()
val lines2 = lines1.toArray
println( lines2(0).toList )
Result of last line of code is
List(-, 0, ., 1, 1, 0, 0, 2, 0, 8, 2, 6, 9, 7, 2, 9, 0, 9, 7, , 0, ., 1, 2, 4, 8, 4, 6, 0, 4, 6, 3, 1, 0, 5, 5, 8, 9, , -, 0, ., 0, 1, 5, 5, 9, 1, 3, 8, 5, 8, 8, 2, 5, 5, 2, 8, 6, , -, 0, ., 0, 1, 6, 2, 5, 8, 3, 9, 4, 2, 8, 2, 9, 2, 6, 0, 3, , -, 0, ., 0, 5, 3, 2,.........
This is returning me single character but I want complete row that will be space separated. How can I fix This issue?
here is remaining code for conversion
val data1 : Array[Array[Double]] = lines2.flatMap{xz : String =>
Seq (xz.replaceAll(" ", ",").split(",").map(_.toDouble) )
}.toArray
import spark.implicits._
val ds = List("-0.1100208269729097 0.1248460463105589 -0.01559138588255286 -0.01625839428292603 -0.05323888667281371 0.06722185430549973 -0.0490877148079949 -0.05039368886946847 0.0897270838973875 0.00754589058726465 -0.06693447805463611 -0.1193740974362337 -0.02214573804045866 0.02930806967704801 -0.009567144727872222 -0.02288991169653539 0.04256313697292451 -0.08190168271952417 0.008274133732539695 -0.02299227162395361 0.0111923018567119 -0.009872522389769637 0.06866110814693088 0.04622954799009332 0.05498202029091768 -0.06672541846259043 -0.05130079655965012 0.1107659505844031 0.07912810279475517 0.02246390669165305 -0.06997067603392053 -0.02069109953229961 -0.05191987832821615 -0.01971016519416264 ","-0.1100208269729097 0.1248460463105589 -0.01559138588255286 -0.01625839428292603 -0.05323888667281371 0.06722185430549973 -0.0490877148079949 -0.05039368886946847 0.0897270838973875 0.00754589058726465 -0.06693447805463611 -0.1193740974362337 -0.02214573804045866 0.02930806967704801 -0.009567144727872222 -0.02288991169653539 0.04256313697292451 -0.08190168271952417 0.008274133732539695 -0.02299227162395361 0.0111923018567119 -0.009872522389769637 0.06866110814693088 0.04622954799009332 0.05498202029091768 -0.06672541846259043 -0.05130079655965012 0.1107659505844031 0.07912810279475517 0.02246390669165305 -0.06997067603392053 -0.02069109953229961 -0.05191987832821615 -0.01971016519416264 ").toDS()
ds.map(i=> i.split(" ").mkString(",")).show(false)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|-0.1100208269729097,0.1248460463105589,-0.01559138588255286,-0.01625839428292603,-0.05323888667281371,0.06722185430549973,-0.0490877148079949,-0.05039368886946847,0.0897270838973875,0.00754589058726465,-0.06693447805463611,-0.1193740974362337,-0.02214573804045866,0.02930806967704801,-0.009567144727872222,-0.02288991169653539,0.04256313697292451,-0.08190168271952417,0.008274133732539695,-0.02299227162395361,0.0111923018567119,-0.009872522389769637,0.06866110814693088,0.04622954799009332,0.05498202029091768,-0.06672541846259043,-0.05130079655965012,0.1107659505844031,0.07912810279475517,0.02246390669165305,-0.06997067603392053,-0.02069109953229961,-0.05191987832821615,-0.01971016519416264|
|-0.1100208269729097,0.1248460463105589,-0.01559138588255286,-0.01625839428292603,-0.05323888667281371,0.06722185430549973,-0.0490877148079949,-0.05039368886946847,0.0897270838973875,0.00754589058726465,-0.06693447805463611,-0.1193740974362337,-0.02214573804045866,0.02930806967704801,-0.009567144727872222,-0.02288991169653539,0.04256313697292451,-0.08190168271952417,0.008274133732539695,-0.02299227162395361,0.0111923018567119,-0.009872522389769637,0.06866110814693088,0.04622954799009332,0.05498202029091768,-0.06672541846259043,-0.05130079655965012,0.1107659505844031,0.07912810279475517,0.02246390669165305,-0.06997067603392053,-0.02069109953229961,-0.05191987832821615,-0.01971016519416264|
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
I have two arrays
var availableIndex: Int[] = [0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14]
var answerIndex: Int[] = [1, 3, 10, 8]
I want to remove 1, 3, 10, 8 from availableIndex array. I've seen the documentation how to achieve that using removeObjectsInArray
availableIndex.removeObjectsInArray(answerIndex)
but I can't use that method, it gave me an error. I have no idea where's my fault. Sorry if my bad English
edit:
here is the error 'Int[]' does not have a member named 'removeObjectsInArray'
The proper Swifty way to do this is
availableIndex = availableIndex.filter { value in
!answerIndex.contains(value)
}
(will create a new filtered array only with values not contained in answerIndex)
Of course, a better solution would be to convert answerIndex into a Set.
removeObjectsInArray is defined only on Obj-C mutable arrays (NSMutableArray).
An Obj-C workaround is to define the array directly as NSMutableArray
var availableIndex: NSMutableArray = [0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14]
var answerIndex: Int[] = [1, 3, 10, 8]
availableIndex.removeObjectsInArray(answerIndex)
It's also possible to do it like that:
availableIndex.removeAll(where: { answerIndex.contains($0) })
I want to try the ReedSolomonDecoder from the ZXing library on the example given on page 10 of this paper
Basically, it encodes the message
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
using the generator polynomial
x^4 + 15x^3 + 3x^2 + x + 12
which results in
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 3, 12, 12
I want to decode this in the following manner:
int[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 3, 12, 12};
GenericGF field = new GenericGF(?, 16, 1); // what integer should I use for primitive here?
ReedSolomonDecoder decoder = new ReedSolomonDecoder(field);
decoder.decode(data, 4);
I don't know how to create a GenericGF object from the given generator polynomial. I know that it expects a binary integer representation of the polynomial, but to do that, I would need the polynomial to be in an irreducible form, i.e. all the coefficients to be either 0 or 1. How can I achieve that from this given generator polynomial?
I'm pretty new to this as well but I think you would want to use
public static GenericGF AZTEC_PARAM = new GenericGF(0x13, 16, 1);
I am porting an Android app I made to iPhone and running into problems with the syntax (I think)
I'm basing the project off of the example from
http://iphonedevelopment.blogspot.com/2009/04/opengl-es-from-ground-up-part-2-look-at.html
I would like to pull out the geometry from the rendering process to keep the code modular but I can't seem to get it to work. I've created a class called Icosahedron (comments are my understanding of what's going on)
Icosahedron.h
#import <Foundation/Foundation.h>
#import "OpenGLCommon.h"
#interface Icosahedron : NSObject {
Vertex3D *vertices[12]; //allocate a group of 12 Vertex3D pointers
Vertex3D *normals[12]; // ditto
GLubyte *faces[60]; // ditto but with 60 face values
}
// Declare methods
-(Vertex3D *) vertices;
-(void) setVertices:(Vector3D *)setVerts;
-(Vertex3D *) normals;
-(void) setNormals:(Vector3D *)setNorms;
-(GLubyte *) faces;
-(void) setFaces:(GLubyte *)setFaces;
#end
Icosahedron.m
#import "Icosahedron.h"
#implementation Icosahedron
// Returns the pointer to the vertices instance variable
-(Vector3D *) vertices{
return *vertices;
}
-(void) setVertices:(Vector3D *)setVerts
{
//vertices=setVerts[0];
}
-(Vector3D *) normals{
return *normals;
}
-(void) setNormals:(Vector3D *)setNorms
{
//normals=setNorms;
}
-(GLubyte *) faces{
return *faces;
}
-(void) setFaces:(GLubyte *)setFaces
{
//faces=setFaces;
}
/**/
-(id)init
{
// super method
self=[super init];
// create 12 Vector3D objects and populate them...
Vector3D tempVert[12]={
{0, -0.525731, 0.850651}, // vertices[0]
{0.850651, 0, 0.525731}, // vertices[1]
{0.850651, 0, -0.525731}, // vertices[2]
{-0.850651, 0, -0.525731}, // vertices[3]
{-0.850651, 0, 0.525731}, // vertices[4]
{-0.525731, 0.850651, 0}, // vertices[5]
{0.525731, 0.850651, 0}, // vertices[6]
{0.525731, -0.850651, 0}, // vertices[7]
{-0.525731, -0.850651, 0}, // vertices[8]
{0, -0.525731, -0.850651}, // vertices[9]
{0, 0.525731, -0.850651}, // vertices[10]
{0, 0.525731, 0.850651} // vertices[11]
};
// same...
Vector3D tempNorm[12]={
{0.000000, -0.417775, 0.675974},
{0.675973, 0.000000, 0.417775},
{0.675973, -0.000000, -0.417775},
{-0.675973, 0.000000, -0.417775},
{-0.675973, -0.000000, 0.417775},
{-0.417775, 0.675974, 0.000000},
{0.417775, 0.675973, -0.000000},
{0.417775, -0.675974, 0.000000},
{-0.417775, -0.675974, 0.000000},
{0.000000, -0.417775, -0.675973},
{0.000000, 0.417775, -0.675974},
{0.000000, 0.417775, 0.675973},
};
// face values
GLubyte tempFaces[60]={
1, 2, 6,
1, 7, 2,
3, 4, 5,
4, 3, 8,
6, 5, 11,
5, 6, 10,
9, 10, 2,
10, 9, 3,
7, 8, 9,
8, 7, 0,
11, 0, 1,
0, 11, 4,
6, 2, 10,
1, 6, 11,
3, 5, 10,
5, 4, 11,
2, 7, 9,
7, 1, 0,
3, 9, 8,
4, 8, 0,
};
// set the instance pointers to the temp values
*vertices=tempVert;
*normals=tempNorm;
*faces=tempFaces;
at this point the values are NOT properly populated, only the first value is correct.
return self;
}
#end
All I want to do is to be able to call something like
...
ico=[[Icosahedron alloc] init];
glVertexPointer(3, GL_FLOAT, 0, [ico vertices]);
...
in the rendering section but it the farthest I've been able to get is setting the first value of the Vertex3Ds inside the Icosahedron class and I get 'out of scope' in the debugger for any of the Icosahedron's values in the rendering class.
I suspect that this is just me learning Objective-C's quirks but I've tried many different approaches for a few days and nothing seems to get me anywhere.
Please help me Overflow-Wan, you're my only hope.
You’re getting your pointers and arrays mixed up. You’d want something like this:
Vertex3D vertices[12];
- (void) setVertices: (Vertex3D *)newVertices;
{
memcpy( vertices, newVertices, 12 * sizeof( Vertex3D ) );
}
- (Vertex3D *) vertices;
{
return vertices;
}
To copy arrays in C you have to use memcpy (or a hand-written loop), you cannot do this with the assignment operator.
Firstly, you seem to be using "Vertex3D" and "Vector3D" interchangeably. I don't know if that's actually a problem...
I think the arrays should be declared as arrays of values, not arrays of pointers. So...
Vertex3D vertices[12]; //allocate a group of 12 Vertex3D pointers
Vertex3D normals[12]; // ditto
GLubyte faces[60]; // ditto but with 60 face values
That way you have room for all the values, not just pointers to other vectors (which you haven't allocated).
As an aside, the typical init sequence is:
-(id)init
{
if (self = [super init])
{
//do initialization stuff
}
return self;
}
It handles errors more gracefully.
Your declarations of vertices, normals and faces are to arrays of pointers to the entities in question. Whereas it seems like what you really want is to have arrays of the structs/values themselves. So your interface should say:
Vertex3D vertices[12];
Vertex3D normals[12];
GLubyte faces[60];
Your object will then own a chunk of memory containing 12 vertices, 12 normals and 60 faces (um, whatever those are -- GLubyte values, anyway).
Pointers and arrays are sort of interchangeable in C/Objective-C. So vertices and normals can also be thought of as Vertex3D* values, and faces as a GLubyte*, and they can be passed as such to functions that want those types.
To make it clear what's what, it would be better to have the initial values as single static const arrays at the top level of your implementation file, and then copy those values into the object-owned arrays at initialisation time. You can do this several ways, but the simplest is probably to use memcpy as in Sven's answer.