Skip to content

Latest commit

 

History

History
433 lines (362 loc) · 11.2 KB

File metadata and controls

433 lines (362 loc) · 11.2 KB

Quiz 1.8: Functions

cd(@__DIR__)    
using Pkg      
Pkg.activate(".")  
## Pkg.resolve()   
## Pkg.instantiate()
using Random
Random.seed!(123)
using QuizQuestions

Q1: Function comparison

Given the following code snippet:

function foo(x)
  list = []
  for i in 2:x
    cond = true
    for j in list
      if i%j == 0
        cond = false
      end
    end
    if cond push!(list,i); end
  end
  return list
end

function foo2(x)
  if x == 1 return []; end
  ns = foo2(x-1)
  cond = true
  for i in ns
    if x%i == 0
      cond = false
    end
  end
  if cond push!(ns,x); end
  return ns
end

Which of the following sentences are correct ?


choices = [ # hide
    "Both the functions compute the Fibonacci numbers", # hide
    "The first function returns an error", # hide
    "The second function returns an error", # hide
    "The first function calls itself recursively", # hide
    "None of the (other) sentences is correct", # hide
] # hide
answers = [5]  # hide
multiq(choices, answers;keep_order=true)  # hide

<details><summary>RESOLUTION</summary>

The two functions compute the list of prime numbers up to x, with the second one being recursive. While often recursive functions are slow, here the computations to derive the prime numbers is roughly the same in the two cases with a O(x²) complexity bound, and there isn't a version that is definitly slower. Note that much more efficient approaches to find prime number exists.

The correct answers are:

  • "None of the (other) sentences is correct"
</details>

Q2: Function optimisation (1)

Given the following code snippet:

function foo(x)
  list = XXXX[]
  for i in 2:x
    cond = true
    for j in list 
      if i%j == 0
        cond = false
      end        
    end
    if cond push!(list,i); end
  end
  return list
end

To what XXXX should be replaced to make the function more efficient?


stringq(Regex("Int64")) # hide

<details><summary>RESOLUTION</summary>

The function can be made orders of magnitute more efficient by simply specifying that the list is an array of integers rather than "any" values.

The correct answer is: "Int64"

</details>

Q3: Function optimisation (2)

Given the following code snippet:

function foo(x)
  list = []
  for i in 2:x
    cond = true
    for j in list 
      if i%j == 0
        cond = false
        XXXX
      end        
    end
    if cond push!(list,i); end
  end
  return list
end

To what XXXX should be replaced to make the function more efficient?


stringq(Regex("break")) # hide

<details><summary>RESOLUTION</summary>

The function can be made many times more efficent by breaking the loop for that specific item once the first condition leading to cond=false has been found.

The correct answer is: breack

</details>

Q4: Call by sharing

Given the following code snippet:

a = ([1,2,3],4)
function foo(x)
  x[1][1] = 10
  x[2]    = 40
end
foo(a)

Which of the following sentences are correct ?


choices = [ # hide
    "An error is raised during compilation/running", # hide
    "An error is raised during parsing of the code", # hide
    "`a` is still `([1,2,3],4)`", # hide
    "`a` is now `([10,2,3],4)`", # hide
    "`a` is now `([1,2,3],40)`", # hide
    "`a` is now `([10,2,3],40)` ", # hide
    "None of the (other) sentences is correct", # hide
] # hide
answers = [1,3]  # hide
multiq(choices, answers;keep_order=true)  # hide

<details><summary>RESOLUTION</summary>

a is a Tuple, that is a immutable type. The attempt to modify it in the command x[2] = 40 rises an error during the JIT compilation. The first modification, however (x[1][1] = 10), had already taken place.

The correct answers are:

  • An error is raised during compilation/running
  • a is now ([10,2,3],4)
</details>

Q5: Call by sharing 2

Given the following code snippet:

a = ([1,2,3],4)
function foo(x)
  x[1][1] = 10
  x       = ([10,20,30],40)
end
foo(a)

Which of the following sentences are true ?


choices = [ # hide
    "An error is raised during compilation/running", # hide
    "An error is raised during parsing of the code", # hide
    "`a` is still `([1,2,3],4)`", # hide
    "`a` is now `([10,2,3],4)`", # hide
    "`a` is now `([10,20,30],40)`", # hide
    "None of the (other) sentences is correct", # hide
] # hide
answers = [4]  # hide
multiq(choices, answers;keep_order=true)  # hide

<details><summary>RESOLUTION</summary>

a is a Tuple, that is a immutable type. The first command doesn't mutate it but mutates an object contained in it (that being an Array is mutable and doesn't create a problem). The second statement in the foo function rebinds x to a new object, without influencing the original object. Note that as the function mutate its parameter a more appropriate name would have been foo!. Append the function name with an exclamation mark for mutating functions is hovewer only a convention.

The correct answers are:

  • a is now ([10,2,3],4)
</details>

<div id="pd_rating_holder_8962705"></div>
<script type="text/javascript">
const pageURL = window.location.href;
PDRTJS_settings_8962705 = {
"id" : "8962705",
"unique_id" : "/home/lobianco/CloudFiles/lef-nancy-sync/Documents/Teaching/2021-2022/Introduction to Scientific Programming and Machine Learning with Julia/SPMLJ/lessonsSources/01_-_JULIA1_-_Basic_Julia_programming/0104q_-_QUIZ_1.8.md",
"title" : "0104q_-_QUIZ_1.8.md",
"permalink" : pageURL
};
</script>
<div class="addthis_inline_share_toolbox"></div>

<script src="https://utteranc.es/client.js"
        repo="sylvaticus/SPMLJ"
        issue-term="title"
        label="💬 website_comment"
        theme="github-dark"
        crossorigin="anonymous"
        async>
</script>
<script type="text/javascript" charset="utf-8" src="https://polldaddy.com/js/rating/rating.js"></script>
<!-- Go to www.addthis.com/dashboard to customize your tools -->
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-6256c971c4f745bc"></script>

<div id="pd_rating_holder_8962705"></div>
<script type="text/javascript">
const pageURL = window.location.href;
PDRTJS_settings_8962705 = {
"id" : "8962705",
"unique_id" : "/home/lobianco/CloudFiles/lef-nancy-sync/Documents/Teaching/2021-2022/Introduction to Scientific Programming and Machine Learning with Julia/SPMLJ/lessonsSources/01_-_JULIA1_-_Basic_Julia_programming/0104q_-_QUIZ_1.8.md",
"title" : "0104q_-_QUIZ_1.8.md",
"permalink" : pageURL
};
</script>
<div class="addthis_inline_share_toolbox"></div>

<script src="https://utteranc.es/client.js"
        repo="sylvaticus/SPMLJ"
        issue-term="title"
        label="💬 website_comment"
        theme="github-dark"
        crossorigin="anonymous"
        async>
</script>
<script type="text/javascript" charset="utf-8" src="https://polldaddy.com/js/rating/rating.js"></script>
<!-- Go to www.addthis.com/dashboard to customize your tools -->
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-6256c971c4f745bc"></script>

<div id="pd_rating_holder_8962705"></div>
<script type="text/javascript">
const pageURL = window.location.href;
PDRTJS_settings_8962705 = {
"id" : "8962705",
"unique_id" : "/home/lobianco/CloudFiles/lef-nancy-sync/Documents/Teaching/2021-2022/Introduction to Scientific Programming and Machine Learning with Julia/SPMLJ/lessonsSources/01_-_JULIA1_-_Basic_Julia_programming/0104q_-_QUIZ_1.8.md",
"title" : "0104q_-_QUIZ_1.8.md",
"permalink" : pageURL
};
</script>
<div class="addthis_inline_share_toolbox"></div>

<script src="https://utteranc.es/client.js"
        repo="sylvaticus/SPMLJ"
        issue-term="title"
        label="💬 website_comment"
        theme="github-dark"
        crossorigin="anonymous"
        async>
</script>
<script type="text/javascript" charset="utf-8" src="https://polldaddy.com/js/rating/rating.js"></script>
<!-- Go to www.addthis.com/dashboard to customize your tools -->
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-6256c971c4f745bc"></script>

<div id="pd_rating_holder_8962705"></div>
<script type="text/javascript">
const pageURL = window.location.href;
PDRTJS_settings_8962705 = {
"id" : "8962705",
"unique_id" : "/home/lobianco/CloudFiles/lef-nancy-sync/Documents/Teaching/2021-2022/Introduction to Scientific Programming and Machine Learning with Julia/SPMLJ/lessonsSources/01_-_JULIA1_-_Basic_Julia_programming/0104q_-_QUIZ_1.8.md",
"title" : "0104q_-_QUIZ_1.8.md",
"permalink" : pageURL
};
</script>
<div class="addthis_inline_share_toolbox"></div>

<script src="https://utteranc.es/client.js"
        repo="sylvaticus/SPMLJ"
        issue-term="title"
        label="💬 website_comment"
        theme="github-dark"
        crossorigin="anonymous"
        async>
</script>
<script type="text/javascript" charset="utf-8" src="https://polldaddy.com/js/rating/rating.js"></script>
<!-- Go to www.addthis.com/dashboard to customize your tools -->
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-6256c971c4f745bc"></script>

<div id="pd_rating_holder_8962705"></div>
<script type="text/javascript">
const pageURL = window.location.href;
PDRTJS_settings_8962705 = {
"id" : "8962705",
"unique_id" : "/home/lobianco/CloudFiles/lef-nancy-sync/Documents/Teaching/2021-2022/Introduction to Scientific Programming and Machine Learning with Julia/SPMLJ/lessonsSources/01_-_JULIA1_-_Basic_Julia_programming/0104q_-_QUIZ_1.8.md",
"title" : "0104q_-_QUIZ_1.8.md",
"permalink" : pageURL
};
</script>
<div class="addthis_inline_share_toolbox"></div>

<script src="https://utteranc.es/client.js"
        repo="sylvaticus/SPMLJ"
        issue-term="title"
        label="💬 website_comment"
        theme="github-dark"
        crossorigin="anonymous"
        async>
</script>
<script type="text/javascript" charset="utf-8" src="https://polldaddy.com/js/rating/rating.js"></script>
<!-- Go to www.addthis.com/dashboard to customize your tools -->
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-6256c971c4f745bc"></script>