-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lecture "Brute-force algorithms", exercise 4 #18
Comments
Ok, so this one I found both easier (because in terms of operations, finding the range was clearer than the enumeration) and harder than the previous exercise. As I understood, the instruction was to define in Python a function called my_range(input_list) which should behave like the built-in function range(). In the lecture notes Section "Insertion sort", it says that inputting range(4) returns the range([0, 1, 2, 3]), which is a kind of list. However, when I tried it in Python, instead of range([0, 1, 2, 3], it returned range(0, 4) -- meaning, it did not return a "list" per se. Therefore, I decided to opt for a solution that got me closer to the "list" result as defined in the lecture notes, in the form of range([0, 1, 2, 3]). My solution returns the list, but it does not return it as a function with the word 'range'.
I'll keep an eye out for you guys' answers, to see how to include range() in the result of the user-defined function. Otherwise, Prof. @essepuntato, could you please clarify if it is necessary, and if so, how to do it? Thank you 👍 |
The range() function has two sets of parameters. so the function my_range has 3 inputs one for starting number, the second one for ending number and the last one to accept an input list. here is my code I think it will work for all inputs
|
It is not possible to use the built-in function |
|
My method is basically identical to the one implemented by @Hizkie . In fact, the only difference is that I subtract "1" inside the while loop, instead of before the while statement.
Then, I looked up my colleagues' answers and found that they expressed the inversion as "result.reverse()" and not as "reversed() - the second option was the one presented in class. After using that expression, Python finally returned the expected results.
@essepuntato Professor, my questions are now: |
Given Prof. Peroni's edit of the prompt, I looked at Hizkiel's solution and decided to find a similar solution but without having to reverse the list. I used his same test examples and it works just as well:
|
1 similar comment
Given Prof. Peroni's edit of the prompt, I looked at Hizkiel's solution and decided to find a similar solution but without having to reverse the list. I used his same test examples and it works just as well:
|
|
|
@essepuntato: Professor, while reaching the same conclusions (more or less), I've also experienced the exact same problem mentioned by my colleague @federicafaithbologna. Could
|
|
|
Thanks to the help of @delfimpandiani I was finally able to run it. :D |
|
Hi all, here my take on the exercise (available also as Python file in the GitHub repository), with some comments:
Some comments:
|
Write in Python the function
def my_range(stop_number)
which behave like the built-in functionrange()
introduced in Section "Insertion sort" and returns a proper list, and accompany the function with the related test case. It is not possible to use the built-in functionrange()
in the implementation.The text was updated successfully, but these errors were encountered: