Comparing numbers in Array - ios

I have an array of core data objects called samples, each sample has a depthFrom and depthToo. I load each sample into a tableView to show the depthFrom and Too. I need to check for gaps between the values and if there is, insert a new sample.
The samples in the table could look like below with depthFrom and depthToo,
The issue is since there is a gap between the numbers from 100 to 210 new samples should be added to the table. using a gap of 50 as much as possible so it would look like this with the auto generated samples.
What im unsure of is how to compare the values, i would rather do it as the view loads before cellForRowAtIndexPath is called so i would not need to reload the table again. I was thinking of looping through each value and comparing them but there all in the same array so im not sure how i would do this. I have all the data displaying correctly in my app its just the gaps i need to account for and if im able to find a way to compare the values in the array then i can manage adding in the new objects i just need pointing in the right direction as this is new to me.
If theres anything about my question that is confusing then just add a comment and i will update it accordingly, thanks for any help.

To fix the gaps, you must keep track of the last depthTo and check if there's a gap between it and the current sample. If there is, insert samples with a spacing of 50*, until we reach our current sample.
Here's a pseudocode solution:
samples = NSMutableArray
int lastDepthTo = 0;
for (i = 0; i < [samples count]; i++) {
s = samples[i]
// add missing samples (upto current s.depthFrom)
while (s.depthFrom > lastDepthTo) {
genDepthTo = MIN(d.depthFrom, lastDepthTo+50)
generated = new sample(depthFrom: lastDepthTo, depthTo: genDepthTo)
[samples insert:generated atIndex:i]
i++ // increment i to skip inserted sample
lastDepthTo = genDepthTo
}
lastDepthTo = s.depthTo
}
Note: this is untested, maybe off by 1 for the indexing of i.

Related

How do I compare a specific cell with a column range and return a string associated with the column range?

Thanks for reading my first question. I'm just starting out with google sheets to please bear with me.
Here is the sheet I'm working with
So,this sheet if for a game I play and we assign guild attacks based on power levels. I'm looking to create a function or script and put it in Column O. I'd like this function to compare Column F to a specific cell in Column M and then return the user associated with Column F that is >= than the specific cell in Column M. Like this enter image description here
I highlighted the first three as an example.
I can obviously do this manually but I takes time, and was looking to automate this process so it becomes more efficient. I've tried Vlookups, MATCH, IF and I've been unsuccessful. Any help would be greatly appreciated. Again, I'm just a beginner with google sheets so please go easy on me. :)
Solution
As you metioned that you would also be happy with an scripted solution I have created this script that I believe solves your issue. It has comments explaining step by step how it works:
function myFunction() {
// Get our sheet
var ss = SpreadsheetApp.getActive().getSheetByName('Automate Test');
// Get the values of the ranges of F and M. Flat will convert the 2D array we get from getValues() into a 1D one which is easier to work with
var valuesF = ss.getRange('F2:F16').getValues().flat();
var valuesD = ss.getRange('D2:D16').getValues().flat();
var valuesM = ss.getRange('M2:M16').getValues().flat();
var valuesN = ss.getRange('N17:N31').getValues().flat();
// We will iterate through all the players in column M to find their opponents
for(i=0;i<valuesM.length;i++){
// We create an empty array to be filled with the list of possible opponents to then choose a random one of the list
var playersHigherEqual = [];
// Iterate through the opponent list
for(j=0;j<valuesF.length;j++){
// If the opponent meets the condition
if(valuesF[j]>= valuesM[i]){
// Add it to the array of possible opponents
playersHigherEqual.push(ss.getRange(j+2, 2).getValue());
}
}
//Finally we will set the opponent by choosing a random one out of the list. Note that i+2 is because the arrays start from 0
ss.getRange(i+2, 15).setValue(playersHigherEqual[Math.floor(Math.random()*playersHigherEqual.length)]);
}
// We will iterate through all the players in column M to find their opponents
for(i=0;i<valuesN.length;i++){
// We create an empty array to be filled with the list of possible opponents to then choose a random one of the list
var playersHigherEqual = [];
// Iterate through the opponent list
for(j=0;j<valuesD.length;j++){
// If the opponent meets the condition
if(valuesD[j]>= valuesN[i]){
// Add it to the array of possible opponents
playersHigherEqual.push(ss.getRange(j+2, 2).getValue());
}
}
//Finally we will set the opponent by choosing a random one out of the list. Note that i+2 is because the arrays start from 0
ss.getRange(i+17, 15).setValue(playersHigherEqual[Math.floor(Math.random()*playersHigherEqual.length)]);
}
}
Please let me know if you also need a sheet formula solution for this question. For more information about Apps Script check out the documentation.
I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)

ios- thread 1 exc_bad_instruction error in app

I am writing an app to simulate the nba lottery. I have already written the codes to generate the random combinations, and assigned them to each team.
Here is my method to simulate the drawings and assign the draft positions to each team. standingsArray is an array of Team items of type ObjectWrapper, with values of name, seed, wins, losses, draft position exc... for each team. So basically what Im doing is I have 14 balls and randomly choose 4 balls, which constitute a combination (order doesn't matter). So essentially there are a total of 1001 total possible combinations, but one is thrown out. (you can ignore the first while loop because that is just there so that the thrown out combination isnt selected). A number of combinations is assigned to the 14 lottery teams based on record (250 for worst team, 199 for second worst exc...). The argument in my method standingsArray already has the number of possibilities assigned to each team. Next, I randomly pull 4 balls from the total possibilities, and the team with that combination gets the first pick. But because all the combinations for that team selected cant be chosen again for the second pick, I have to remove all of those combinations, but that is very complicated so instead, i make a new array called tempPossibilities which appends all the combinations for every team except the one just selected, which then allows me to generate a new combination to select from.
However, I am getting an error at this line for j in 0...(standingsArray[i].possibilities?.count)!-1{ It says bad instruction error, and I cannot figure out why I am getting this error. And what else doesnt make sense is that the for loop works and the tempPossibilities array is fully populated with the correct amount of combinations (without the lottery team), even though the error happens at the for loop?
Code is below: any help is appreciated, thank you, and sorry for the really long paragraph
func setDraftPositions(var standingsArray: [Team])->[Team]{
var lottery: [Team]=[]
var totalPossibilities: [[Int]]=combosOfLength(14, m: 4)
var tempPossibilities = []
var rand = Int(arc4random_uniform(UInt32(totalPossibilities.count)))
var draw = totalPossibilities[rand]
while (draw==(unused?.first)!) {
rand = Int(arc4random_uniform(UInt32(totalPossibilities.count)))
draw = totalPossibilities[rand]
}
s: for x in 0...13{
for a in 0...(standingsArray[x].possibilities?.count)!-1{
if(draw==standingsArray[x].possibilities![a]){
standingsArray[x].setDraftingPosition(1)
standingsArray[x].isLottery=true;
lottery.append(standingsArray[x])
for i in 0...(standingsArray.count-1) {
if(standingsArray[i].firstName != standingsArray[x].firstName!) {
for j in 0... (standingsArray[i].possibilities?.count)!-1{ //ERROR is happening here
tempPossibilities.append(standingsArray[i].possibilities![j])
}
}
}
standingsArray.removeAtIndex(x)
break s;
}
}
}
(repeat this for the next 2 picks)
Try this:
for j in 0...(standingsArray[i].possibilities?.count)!-1{
should be written like this:
for j in 0...(standingsArray[i].possibilities?.count)! - 1{
it needs proper spacing.

ShinobiCharts styling / order of data issues

I wanted to ask some chart styling and data questions. My chart currently looks like this:
I would like to add a border around the chart with rounded edges, I have tried using the following code to no avail:
_chart.layer.cornerRadius = 10.0;
_chart.layer.borderWidth = 0.7;
_chart.layer.borderColor = [UIColorwhiteColor].CGColor;
I would also like to add labels to the bars to indicate their values, but have no idea how to do so.
My final question is regarding the data in the chart. Despite having a data structure in the correct order, the data appears scrambled in terms of order (it should read M T W TH F S SU). My data structure is:
_sales[0] = #{#"M" : #1.6, #"T" : #5.6, #"W" : #10.6, #"TH" : #12.6, #"F" : #15.6, #"S" : #3.6, #"Su" : #4.6};
and is used in the following way:
- (id<SChartData>)sChart:(ShinobiChart *)chart dataPointAtIndex:(NSInteger)dataIndex forSeriesAtIndex:(NSInteger)seriesIndex {
SChartDataPoint *datapoint = [[SChartDataPointalloc] init];
NSString* key = _sales[seriesIndex].allKeys[dataIndex];
datapoint.xValue = key;
datapoint.yValue = _sales[seriesIndex][key];
return datapoint;
}
Any advice or input would be appreciate, thanks in advance!
Disclaimer: I work for ShinobiControls
Chart Border
Instead of using layer.borderWidth you can use borderThickness and instead of using layer.borderColor you can use borderColor.
The following worked for me when I tested it:
_chart.borderColor = [UIColor redColor];
_chart.borderThickness = #(5);
_chart.layer.cornerRadius = 10.0;
Data Point Labels
You can turn data point labels on and control their style via the series you are returning from your data source:
series.style.dataPointLabelStyle.showLabels = YES;
Data Order
You mention that your data structure is "in the correct order", but unfortunately that isn't quite true. You are using an NSDictionary which does not guarantee order. When you call allKeys on your NSDictionary the array that is returned is not necessarily in order. As per the NSDictionary API documentation for allKeys:
The order of the elements in the array is not defined.
In order to fix this you'll need to switch to a data structure that does guarantee order.
I hope all that info is useful!
Note: There are a number of different questions in the original post, so this response contains several answers. I'm not really sure what the etiquette is for editing/splitting questions on SO is, so I've left it alone. If anyone comes across this question and has suggestions along those lines then feel free to make edits or add comments etc.

Dart: DoubleLinkedQueue length after entry prepend

I'm manipulating entries inside a DoubleLinkedQueue via the DoubleLinkedQueueElement.append/prepend methods. This results in the new elements being inserted into the queue, but fails to update the length, and the toList() method results in an error being thrown.
I understand queues are only supposed to have elements added at the start/end, but it looks like the interface should allow for adding in the middle via the entries. I find it hard to believe such a common/well understood data structure would have a bug at this point - so am I using DoubleLinkedQueues incorrectly? Is there another data structure that I should be using? I'm looking to merge values from another iterable into my own sorted iterable - a SplayTreeSet might get me there in n log n time, but a simple merge should get me there in linear time...
Example of code that acts unexpectedly:
main() {
var q = new DoubleLinkedQueue<int>.from([1]);
q.firstEntry().prepend(0);
print('length: ${q.length}');
int i = 0;
for (var qi in q){
print('${i++}: $qi');
}
}
Output:
length: 1
0: 0
1: 1
It looks like the length getter is only pointing to an internal counter. This is done because counting the elements everytime might take very long for long lists.
The internal counter is only updated if you use the methods that directly operate on the list instead of using the prepend method of an element. In your example you should use q.addFirst(0); which results in the length being updates. The .prepend() method just inserts a new element and changes the pointer. Which results in correct traversation of the elements, but the counter is wrong anyway.
Unfortunately it looks like you cannot insert elements in the middle of the list, nor can you make the list recount the elements. You should consider creating a bug over at www.dartbug.com.
// Update:
toList() throws an error because there are more elements than length.

How to work with tables in Corona SDK

I have a sort of general question but i think that if I tried to be too specific I would only make it very confusing. So basically what I want to know is this:
When you create a table in Corona/Lua you can put pretty much an unlimited number of things in it correct?
So say i create a table called
rectangles = {};
and then i put a bunch of instances of rectangles in it. If i wanted to change a property of ALL the rectangles at once, how could I do it?
I understand how it would work with a set number of items in the table, like:
for i = 1, 10 do
rectangles[i] = display.newImage("rectangle.png");
then to change all of the images x positions for instance you would simply say
rectangles[i].x = 20;
but how would you change a property of all items in the array without knowing how many there are, as in you didnt give an upper bound, and cant because the table is always growing?
For arrays that have only one kind of elements you can use #rectangles for element count.
for i = 1, #rectangles do
rectangles[i] = display.newImage("rectangle.png");
end
Regarding the youtube example,
if you add element into rectangles like this:
rectangles[b]=b;
what it actually does is
rectangles["083DF6B0"]=b"
you see when a display object b is used as a key it is converted into a hex string.
in addition, you would need to use pairs to go over each element as they are
keys (e.g. array.length,array.width,array.weight..) rather than index (e.g. array[2],array[3]..)
for key,value in pairs(rectangles) do
print(key); --prints 083DF6B0
print(value); --prints 20
rectangles[key]=30;
end
It depends on how you're storing items in the table. If you're storing by index (as in your example), you can use ipairs to iterate over indexes and values:
for index,value in ipairs(rectangles) do
value.x = 20
--or
rectangles[index].x = 20
end
If you're storing by key (as in the youtube video you mention in a comment), iterate using pairs:
for key,value in pairs(rectangles) do
value.x = 20
--or
rectangles[key].x = 20
end
Just don't store items using both index and keys, unless you know what to expect.

Resources