List of lists in scala changing particular elements [closed] - list

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am given a list of list made of integers. I have to override the toString method to print this list of lists in a particular format.
However, I also need to change every '1' element that is in the list into an 'a.

Remember your list is a list of lists, so you need something like:
list.map(_.map { case 1 => "a"; case x => x})

I think you cannot override a method for the Int class, but you create your own class which inherits from Int and use instances of that class instead.
Here is a way to make your code work. I used x.toString because this returns a List[List[String]]. If you omit it, you'll get a List[List[Any]] which contains strings and integers, rather than all strings.
list.map(_.map { case 1 => "a"; case x => x.toString } )

Related

convert generator object to list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Trying to convert this generator into a list of list. Got an error: TypeError: 'tuple' object is not callable. Using python 2.7
perm = itertools.permutations(range(1, 4))
If the goal is to convert to a list of lists (instead of generator of tuples), map can do this easily:
perms = map(list, itertools.permutations(range(1,4)))
If a list of tuples is all you need, it's even easier:
perms = list(itertools.permutations(range(1,4)))
Just don't do it for permutations on a larger set of inputs, or you'll exhaust memory pretty quickly.

Haskell finding mean of cubes of odd members Any ideas? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Please find the mean of cubes of odd members of given list of Int type.
Try to use (.), ($), map, filter or foldl (You should use at least one of them).
I would give you some hints to get you started up:
Use filter function to get the odd members of the given list.
Cube the resultant list using map function.
Find the mean of them by summing the list and dividing it by the length of the list.
The only thing I would add to the other comments and answers is the use of fromIntegral to convert the Int type to Fractional for using the (/) operator.

return this-> command in C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
return this->
what is that mean in C++
..
using namespace std;
IOM ConfigurationManager::getIOM(int iomId) {
return this->IOMs[iomId];
..
the relevant part of the whole code is above.
The code that I wrote is from a huge project which was waiting for someone to finish. I am not good at C++ but I need to learn more not to lose that job. Anyway, the project is full of "return this->...." which I thought unnecessary, that's why I asked is there smt special that we should use that notation
This piece of code simply means that the IOM at index iomId in the IOMs array in the ConfigurationManager object is returned. Note that the this->IOMs is the same as IOMs in this case, thus it seems the this is only there for clarity.
this is a pointer to the current object. The -> operator allow you to access a member inside a pointer to an object.
Thus return this->IOMs[iomID] returns the IOM object in the current ConfigurationManager at index iomID.

how to create structure array with dynamic size [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to create a structure of array dynamically but i dont know the exact size so it will be added whenever i want...
Consider the following example..
Struct abc
{
double **ptrPoints;
int size;
};
i am defining pointer variable
abc* obj;
i dont know the exact size will be so i can not defile like
obj = new abc[size];
the elements will be added whenever condition satisfied.. i want it like vector but i dont want to use it ....
Please suggest me any way to write the functionality like this...
Thank u
Look up vector. Does all the leg work for you.
Wonder why you do not want to use vector. But you may initially declare array of sufficient size. Then maintain a counter of the elements being used up. If it has no space then use realloc.
Seems the only solution except vectors.
See THIS for realloc

Is there a "generics-like" feature in C++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to create a list class in C++ similar to the lists in Java. Is there a way I can have it be able to list whatever object it wants to? The class resizes arrays to create the list, but what I need to do is find out the kind of object that's needed to store.
Yes, C++ has templates that can be used to create generic containers roughly similar to Java generic containers.
While your immediate reaction might be to assume that std::list is similar to a Java list, that would be a mistake. In Java, a list basically just means a sequence. In C++, a std::list is a linked list (which is rarely useful). Most of the time you want to use an std::vector (which is more like Java's ArrayList).
Yes, there is, and it's called Templates