May i use char_first_index from 2.3 in order to solve 2.4 (any_char_present)?
14 days after - still no answer
I also would like to know an answer to this question.
We prefer that you don't compare to the empty string, since it is a legal string and we asked not to compare between strings.
A good alternative is using Python's "not" operator: Empty sequences (strings, lists, tuples) are evaluated as False.
do you mean like if s1 is not "" or s1 is "" instead of s1!="" and s1==""?
It says not to compare strings directly using the == operator, but can i compare a string to the '' char? it says that one can compare strings to chars
In the first function, can the lengths of the two strings be compared, by using s1>s2?
s1>s2 does not compare the length of strings, instead it compares them lexicographically.
If you're interested in the length of strings, consider writing your own recursive my_len(str) function.
len() is forbidden because it is built in, I get that.
can I use functions that exist inside the objects I am given?
like.. "…"'.some_function_name() ??
In 2c, can we use "global"? and if not, can we define a counter outside the recursion? I might be missing something, but defining a counter to keep track of our current location inside the recursion is not applicable ("counter = 0" will reset it every recurrence)
Never mind, found a more elegant and pythonic way without using the techniques mentioned above, though I'd still interested if er're allowed using "global"
We prefer you don't use global in Q2 since it take some of the challenge out.
Instead think how you could use an envelope (Ma'atefet) function here to propagate additional information to the recursive calls.
Should I write it inside the function itself, or outside at the top of the of the skeleton file?
Always write all the code relevant to question x right after the
- QUESTION x
As for how exactly to define a recursive function and its envelope function, please take a look at the example given in question 1 of HW4.
You can use static function variables, like this:
def foo(my_static_variable = None): if my_static_variable is None: my_static_variable = DEFAULT_INIT_VALUE ... ... def foo2(): foo() # <-- correct way of using static variables foo(my_static_variable = 2) # <-- hack, incorrect way of using static variables
but you should probably use this recursive technique to count things in this exercise, to my opinion at least:
def recursive_counter(iterable): ITERATE_ONCE_WITHOUT_LOOPS(iterable) if CAN_ITERATE_MORE(iterable): return 1 + recursive_counter(iterable) return 0
בשאלה 2 סעיף ג האם break נחשב כפעולת של פיטון האסורה?
האם מותר להשתמש בבריאק?
"break" is only allowed inside a loop.
Are you sure you want to use it in a question which forbids the use of loops ?
in 2.5 can I use" for loop"?
I miss understand what I can and what cnt
We're not allowed to use for/while loops in Q2, only recursion..
Thanks for the help, Palarya, but this is not the case.
You *are* allowed to use any kind of loop in part 5 of Q2 (but not in any other part of this question).
It explicitly says, regarding char_first_index:
ממשו את הפונקציה מסעיף 4 … באמצעות לולאה יחידה
and then regarding any_char_present:
ממשו גם את הפונקציה מסעיף 3 ללא שימוש ברקורסיה
in the latter case we require that you use loops but not necessarily a single, simple loop.
just for clarification:
i can't use the built in func len() but i can write my own recursive my_len() that does the same thing as len()?
Yes, as long as it works by the limitations given in Q2-
not using built-in functions, not using 'in' and not comparing with "==" (if stated in the question.)
For the iterative solution of any_char_present_it, are we to use one loop, or can we use two? For char_first_index_it it is explicitly stated to use only one.