Get start and end index from jagged string array for particular values - c#-3.0

I have a jagged string array:
where on first row column values are like:
[0][0] = "A"
[0][1] = "A"
[0][2] = "A"
[0][3] = "A"
[0][4] = "A"
[0][5] = "B"
[0][5] = "B"
[0][5] = "C1"
[0][5] = "C1"
... so on.
Is there an easier way to get start and end index of "B"?
can this be done using linq? I tried this:
var a = Enumerable.Range(0, jagged.GetLength(1))
.Where(index => jagged[0][index].Contains("B"))
.ToArray();
and was gonna get start and end from above array to get what i need. But this linq is not working for me.

I found what was i doing wrong. This might help someone else:
var a = Enumerable.Range(0, jagged.GetLength(1))
.Where(index => jagged[0,index].Contains("B"))
.ToArray();
Now var a has all the values which contain "B" so it can be used to get index.

Related

Default dictionary value in Swift

I know I can have a default dictionary value in swift, but I am struggling to do this for a tuple
i.e.
var freq = [Int:(Int,Int,Int)]()
I want to make a default value
freq[num.element, default: (0, num.offset, num.offset) ] = (7, 0, 0 )
This produces the frequency table with 7,0,0 for every value when the key does not exist.
Can I use the default for a more complicated dictionary?
For an example if we have an array of numbers [1,2,2,3,3,3]
we can count the number of elements using a frequency table
var freq = [Int:Int]()
for num in nums {
freq[num, default: 0] += 1
}
We want to store the initial position of each number, and the final position of each number in the frequency table so use
var freq = [Int:(Int,Int,Int)]()
for num in nums.enumerated() {
if freq[num.element] == nil {
freq[num.element] = (1,num.offset, num.offset)
} else {
freq[num.element] = (freq[num.element]!.0 + 1, freq[num.element]!.1, num.offset)
}
}
For this code I want to use a default value.
I am looking for a way of using a default value on a frequency list containing more than just a single value. It is not relevant that I am using a tuple rather than an array of values, for an example I want to find out how to use default with a tuple using arrays.
I tried to use the example above to make a default value, and on testing it does not work. I have looked at previous questions, a Google Search and looked at Apple's documentation.
Question: How to use default for a dictionary var freq = Int:(Int,Int,Int)
Is this what you looking for? Just get the current value with default value and store it in a variable first makes life easier.
var freq = [Int:(Int,Int,Int)]()
var nums = [1,2,2,3,3,3]
for num in nums.enumerated()
{
let currentTuple = freq[num.element, default: (0,num.offset,num.offset)]
freq[num.element] = (currentTuple.0 + 1, currentTuple.1,num.offset)
}
print(freq) //output: [1: (1, 0, 0), 2: (2, 1, 2), 3: (3, 3, 5)]

Scala how to group a map and then subgroup and transform values

I have an object like this:
case class MyObject(x : Int,y : String,...) {
val buckets = 3
def bucket = x % buckets // returns a number between 0 and |buckets|
}
(x is an arbitrary number)
for example assume "buckets = 3" and we have many objects
MyObject(x = 0, y = "Something", ...)
MyObject(x = 1, y = "Something else", ...)
....
...
Using "groupBy" I collect "MYObjects" using the x % buckets, so it will be like:
val objects : Seq[MyObject] = ...
val groupedObjects : Map[Int: Seq[MyObjects]] = objects.groupBy(obj => x.bucket)
now I want to transform each value and also regroup to sublists of the different type
so lets say for each item in group = 1 , I want to nest under an additional layer and store a different calculated value:
so lets say if bucket 0 after the initial grouping looked like:
bucket[0] = [obj1,obj2,...,objn]
I want to be able to transform bucket "0" to contain another nested grouping:
bucket[0] = Map(sub_bucket_0 -> [transformed(objects)...], sub_bucket_1 -> [transformed(object)...),....]
meaning that eventually I have a data structure with the type:
Map[Int,Map[Sub_bucket_type,Seq[TransformedObject_type]]]
I think what you're looking for is mapValues() which will modify the Map's value elements to new values and/or types.
groupedObjects.mapValues(_.groupBy(/*returns new key type/value*/))
.mapValues(_.mapValues(_.map(/*transform MyObject elements*/)))

Create mongodb query dynamically

I have a collection, say, test which contains some 100 fields like _1, _2, _3 etc
Now I want to form MongoDB query such that whatever input comes like 1, 2, 5 or 5, 6, 7, 8, 9.
A proper query can be generated. Please help with respect to MongoJack.
Try concatenating the input with the underscore character first then use the joined string in the query, something like:
StringBuilder sb = new StringBuilder();
sb.append("_");
sb.append(input);
String field = sb.toString();
List<Foo> bar = coll.find(DBQuery.is(field, "foo")).toArray();
I have finally found the solution. We can do something like this :
Query q = DBQuery.empty();
for(int i=0; i < params.length ; i++ ) {
q = q.or(DBQuery.greaterThan("_"+(Integer.valueOf(params[i])+1), 0));
}
Worked for me

Replace value on index in array

I have a array:
[h,e,l,l,o]
And i want to replace an value on the index, for example i want to change the first l at index 2. I know the index so that is not a problem, the only problem is to replace that value.
How can i do this?
the insert function will add a new value to the array, what you need to do is simply
var newCharacter = "a"
arr[2] = newCharacter
As mentionned here https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html
You can modify your array like that:
var array = ["h", "e", "l", "l", "o"]
array[2] = "Six eggs"

How do I determine the number of rows and columns in a Swift [[AnyObject]]

Does anyone know how to determine the dimensions of a multidimensional array in Swift? For example, the function below needs to accept a [[AnyObject]] of unknown size. I then need to iterate through the rows of one column.
public convenience init(variableNames:[String], observations: [[AnyObject]]){
self.init()
for i in 0..<numRows {
//do something with one column of observations
}
}
The total number of elements is provided by
observations.count
which is not useful in this case. Ideally I would be able to do:
(rows, cols) = observations.size
or something similar, but I haven't been able to find any such method. Going the long way I can determine the number of columns by
let cols = observations[i].count
but because
let columnOfArray = observations[][i]
does not seem to be allowed I am stuck trying to determine the number of rows. Does anyone have an answer?
What about this?
var a : [[AnyObject]] = [[1,2,3],[4,5,6]]
var row = a.count
var col = a[0].count
println("row = \(row)") // "row = 2"
println("col = \(col)") // "col = 3"
But be carreful because this var a : [[AnyObject]] = [[1,2,3],[4,6]]
will return the same since the [[AnyObject]] is not a rectangular, each array in it can have different size.
If you know that all lines have same number of row, you are safe, else, there is no solution, you will have to count cols of each rows.
for(var i=0;i<row;++i)
{
println("col = \(a[i].count)")
}
This returns:
"col = 3"
"col = 2"