in question 2b, the amount of +/-/* that occur are a function of the amount of digits in n, not n directly. Should I write, for example:
nplus = len(str(n))
nminus = 3*(len(str(n))-d)
or is there a more formal representation of the number of digits in n? (since my example is in Python, rather than being generic…)
or perhaps I should write:
Nplus = floor(log10(n)+1)
nminus = 3*(floor(log10(n)+1)-d)
Is it ok to define extra functions, besides the ones required in the assignment?
def useful_function(param1, param2): #recursive function body def altsum_digits(n,d): #function body useful_function(n,d) #rest of function body
If so- where should I include them?
Yes, you may define and use additional functions.
Place them in the relevant part of the skeleton file; the exact location (before/after altsum_digits(n,d)) doesn't matter, since the order of definition doesn't either. All that matters is that all the functions are defined before altsum_digits(n,d) is called.
In question 1d, how should we handle input that the "end" index is smaller than the "start" index? is it valid to enter negative indexes as input? as negative indexes are counted from the list's end.
if i did something like this: -(expression),
does it count like a minus or a multiply? (u can see this as a 0-exp or as -1*exp)
Arithmetic operators are shortcuts for invoking functions, in this case of class int.
0-x will invokes a subtraction method which takes two parameters.
(-1)*x will invokes a multiplication method which takes two parameters.
Interestingly, -x will invoke a third function which takes a single parameter (and returns this parameter in the opposite sign). Apparently there's an efficient implementation to this specific operation, and if you measure the running time you will indeed see a difference.
You can get a list of the methods invoked by the different operators if you type "help(int)" at the interactive IDLE prompt.
As for our exercise, I agree that this situation is not well defined. I suggest you replace your current syntax with one of the other variants; that should solve the counting issue (albeit at the price of a slightly higher running time).
another question about this topic, when i calculate the sum of d numbers, should i add it up as d times + or should i divide it to + and -?
and what about when i'm trying to approach the [d+i] char in my string, while i is used in the loop? does it count?
The question asks to count the number of additions and subtractions separately.
It also asks to count all operations that are not inside a loop, regardless of their exact context.
yael, thanks for the answer, but it's mentioned in the question that its possible to omit all calculations that aren't inside a loop.
I do not understand what exactly are we asked to do in question 2b. May anybody explain it to me, please!
In 2b - if I am adding a negative number, should I refer to it as plus?
for i in range(n):
1 + (-1)*5
nplus = n
nmult = n
In question 2b, am I allowed to use "mod" or should I write an arithmetic expression for it?
in 2b how do I count the the action pow(-1,i%2).
to be more specific:
how do I count the power (and what if i i%2 is 0) ?
how do I count the modulu?