Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am struggling to see if there is an obvious advantage over which method to use when passing values into a function. My code below may not be the best example to explain the decision I'm trying to make, but it is, in my opinion, the easiest to understand.
Variadic Parameter Approach
func arithmeticMean(numbers: Double...) -> Double {
var total: Double = 0
for value in numbers {
total += value
}
return total / Double(numbers.count)
}
arithmeticMean(5, 10, 15)
Array Parameter Approach
func arithmeticMean(numbers: [Double]) -> Double {
var total: Double = 0
for value in numbers {
total += value
}
return total / Double(numbers.count)
}
arithmeticMean([5, 10, 15])
Is either of the two techniques preferred? If so, why (speed, reliability or just ease of reading)? Thanks.
I think there is no speed difference.Because,inside the function,you use Variadic Parameter just as Array.
I think that if the parameters count is small,for example,less than 5,Variadic Parameter may be a better solution,because it is easy to read.
If the count of parameters is large. Array is better solution.
Also know that,Variadic Parameter have some limitation:
A function may have at most one variadic parameter, and it must always appear last in the parameter list, to avoid ambiguity when calling the function with multiple parameters.
If your function has one or more parameters with a default value, and also has a variadic parameter, place the variadic parameter after all the defaulted parameters at the very end of the list.
Just from my idea.Hopes helpful
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to say if the desired location in the field is 1 return true otherwise return false. Why is this code not working?
fireShot :: Coordinate -> Field -> Bool
fireShot coord Shipfield
| nth ( fst(coord)((nth snd(coord)) ShipField) == 1 = True
| otherwise = False
The brackets in the guard are not balanced, you open five brackets, and you close four brackets. Furthermore variables start with a lowercase, so it should (probably) be shipfield, not Shipfield.
I think it might be better to use pattern matching to obtain the first and second coordinate, since this will make the code more clean. You furthermore do not need guards to return True and False. You can replace the function with:
fireShot :: Coordinate -> Field -> Bool
fireShot (x,y) shipfield = nth x (nth y shipfield) == 1
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I've been struggling with the the very first question of the exercise from opendatatructures.org regarding datastructures. I question goes like:
A Dyck word is a sequence of +1's and -1's with the property that the
sum of any prefix of the sequence is never negative. For example,
+1,−1,+1,−1 is a Dyck word, but +1,−1,−1,+1 is not a Dyck word since the prefix +1 − 1 − 1 < 0. Describe any relationship between Dyck
words and Stack push(x) and pop() operations.
How does one find the relation between the operation?
One way to represent check if a word if a Dyck word or not is to use a stack, where you push every time you encounter a +1 and pop every time you encounter a -1. If you ever try to pop from an empty stack, it's not a Dyck word.
Consider the following psuedocode (assume that a word is represented as a array of integers, since the question isn't really about parsing):
boolean isDyck(int[] word) {
Object dummy = new Object(); // Just so you have something to push
Stack stack = new Stack();
for (item : word) {
if (item > 0) {
stack.push(dummy);
} else {
if (stack.isEmpty()) {
return false;
}
stack.pop();
}
}
return true;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Here is the code:
var n int
a, _ := fmt.Scanf("%d",&n)
Then a == 1, n has changed its value by input. Why does use of := with fmt.Scanf in Go always return 1?
fmt.Scanf() returns the number of successfully scanned items:
Scanf scans text read from standard input, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why.
So if your input is a valid integer number fitting into an int, fmt.Scanf() will succeed to parse it and store it in n, and so it will return 1.
Should you input an invalid number (e.g. the string value "a"), scanning would not succeed, so 0 would be returned along with a non-nil error, like in this example:
var n int
a, err := fmt.Sscanf("a", "%d", &n)
fmt.Println(a, err)
Which outputs (try it on the Go Playground):
0 expected integer
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 6 years ago.
Improve this question
Here is my challenge:
Create a function exists that returns true or false when an item is in the list.
1> exists(2,[1,4,5,3,2]).
true
2> exists(2,[]).
false
3> exists(2,[4,5,6,7]).
false
Create a function listLength that will return the length of a list. Using the native function length is not allowed.
1> listLength([1,2,3]).
3
2> listLength([]).
0
AS it looks like homework, I hope you will not get the solution here. I will give you some clues.
For the first one:
there is one case when you know for sure that the term does not exist in the list: when the list is empty.
When the list is not empty the only element of the list you can access is the head of the list, so you will have to use recursion to compare one by one every elements of the list to the input term.
Same thing for the second:
you know the length of an empty list: 0
you know that the length of a not empty list is 1 + length of the tail. Use recursion to count one by one all the elements.
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 have NSNumber objects stored in an NSMutableArray. I am attempting to perform a calculation on each object in the array.
What I want to do is:
1) Take a random higher number variable and keep subtracting a smaller number variable in increments until the value of the variable is equal to the value in the array.
For example:
NSMutableArray object is equal to 2.50.
I have an outside variable of 25 that is not in the array.
I want to subtract 0.25 multiple times from the variable until I reach less than or equal to 2.50. I also need a parameter so if the number does not divide evenly and goes below the array value, it resorts to the original array value of 2.50.
Lastly, for each iteration, I want to print the values as they are counting down as a string.
I was going to provide code, but I don't want to make this more confusing than it has to be.
So my output would be:
VALUE IS: 24.75
VALUE IS: 24.50
VALUE IS: 24.25
…
VALUE IS: 2.50
END
I'm on my iPad so this won't be as nice as I would like
float randomValue = 25; //or however you get the value
float subtractionValue = 0.25 // or whatever value or use a define
for (NSNumber *n in myArray)
{
while (randomValue > n.floatvalue)
{
randomValue -= subtractionValue;
NSLog(#"Value is: %f", randomValue);
if (randomValue < 2.5)
{
randomValue = n.floatvalue;
}
}
}
Well run the while loop and check like that below:-
Lets assume your variable here whose value is 25
float a=25;
while (a>=2.50)
{
a=a-0.25;
NSlog(#"count is %f",a);
}
Hope it helps, it is just the logic you can try with your code and if required please modify accordingly.