Ch 5 - Conditionals and Recursion Flashcards

1
Q

Think Julia

What is the: floor division operator?

A

**Floor Division and Modulus **

The floor division operator, ÷ (\ div TAB), divides two numbers and rounds down to an integer. For example, suppose the running time of a movie is 105 minutes. You might want to know how long that is in hours. Conventional division returns a floating-point number:

julia > minutes = 105
105

julia > minutes / 60
1.75

But we don’t normally write hours with decimal points. Floor division returns the integer number of hours, rounding down:

julia > hours = minutes ÷ 60
1

To get the remainder, you could subtract one hour in minutes:

julia > remainder = minutes - hours * 60
45

An alternative is to use the modulus operator, %, which divides two numbers and returns the remainder:

julia > remainder = minutes % 60 45

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 1490-1500). Kindle Edition.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 1475-1489). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Think Julia

What is the: modulus operator

A

**Floor Division and Modulus **

The floor division operator, ÷ (\ div TAB), divides two numbers and rounds down to an integer. For example, suppose the running time of a movie is 105 minutes. You might want to know how long that is in hours. Conventional division returns a floating-point number:

julia > minutes = 105
105

julia > minutes / 60
1.75

But we don’t normally write hours with decimal points. Floor division returns the integer number of hours, rounding down:

julia > hours = minutes ÷ 60
1

To get the remainder, you could subtract one hour in minutes:

julia > remainder = minutes - hours * 60
45

An alternative is to use the modulus operator, %, which divides two numbers and returns the remainder:

julia > remainder = minutes % 60 45

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 1490-1500). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Think Julia

What are the three logical operators?

A

**There are three logical operators: && (and), || (or), and ! (not). **The semantics (meaning) of these operators is similar to their meaning in English. For example, x > 0 && x < 10 is true only if x is greater than 0 and less than 10.

n % 2 = = 0 || n % 3 = = 0 is true if either or both of the conditions is true; that is, if the number is divisible by 2 or 3.

Both && and || associate to the right (i.e., grouped from the right), but && has higher precedence than || does.

Finally, the ! operator negates a Boolean expression, so !( x > y) is true if x > y is false; that is, if x is less than or equal to y.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 1556-1573). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Think Julia

What is a function that calls itself?

A

A function that calls itself is recursive; the process of executing it is called recursion.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 1689-1692). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Think Julia

How do you write a recursive function?

A

function countdown( n)
….if n ≤ 0
……..println(“ Blastoff!”)
….else
……..print( n, “ “)
……..countdown( n-1)
….end
end

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 1666-1672). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Think Julia

What is the function for keyboard input?

A

Julia provides a built-in function called readline that stops the program and waits for the user to type something. When the user presses Return or Enter, the program resumes and readline returns what the user typed as a string:

julia > text = readline()
What are you waiting for? #this is written by user
“What are you waiting for?” #this outputed to screen by computer

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 1747-1753). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Think Julia

What is: readline()

A

Julia provides a built-in function called readline that stops the program and waits for the user to type something. When the user presses Return or Enter, the program resumes and readline returns what the user typed as a string:

julia > text = readline()
What are you waiting for? #this is written by user
“What are you waiting for?” #this outputed to screen by computer

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 1747-1753). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Think Julia

What is a semicolon ; good for when using the REPL?

A

A semicolon (;) allows you to put multiple statements on the same line. In the REPL, only the last statement returns its value. semicolon (;) allows you to put multiple statements on the same line.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 1765-1769). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly