Define a function named insert that takes an integer atom and a sorted list as p
ID: 3813212 • Letter: D
Question
Define a function named insert that takes an integer atom and a sorted list as parameters. The function should return a list with the integer in the correct position in the sorted list. For example: insert [3, (2 4 6 8)] = (2 3 4 6 8) insert 9, ()] = (9) insert[3, (2 3 4 5)] = (23 3 4 5) Note that in the case of that last example, it does not matter which 3 comes first in the final list (the parameter or the one from the sorted list). Your solution must provide both the "design notation" used in class and an implementation of that design in Scheme. Test your code using the interpreter on stdlinux to check your work. As above, use only the elements we have discussed in class to write this code.Explanation / Answer
def insert(atom, arr):
for idx, val in enumerate(arr):
if val > atom:
arr.insert(idx, atom)
return
arr.insert(len(arr),atom)
return
def main():
x = [1,3,5,999]
insert(9,x)
print x
main()
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.