Write a definition for a type named Circle
with attributes center
and radius
, where center
is a Point
object and radius
is a number.
Instantiate a Circle
object that represents a circle with its center at (150, 100)
and radius 75
.
Write a function named point_in_circle
that takes a Circle
and a Point
and returns true
if the point lies in or on the boundary of the circle.
Write a function named rect_in_circle
that takes a Circle
and a Rectangle
and returns true
if the rectangle lies entirely in or on the boundary of the circle.
Write a function named rect_circle_overlap
that takes a Circle
and a Rectangle
and returns true
if any of the corners of the rectangle fall inside the circle. Or as a more challenging version, return true
if any part of the rectangle falls inside the circle.
Write a function called mul_time
that takes a MyTime
object and a number and returns a new MyTime
object that contains the product of the original MyTime
and the number.
Then use mul_time
to write a function that takes a MyTime
object that represents the finishing time in a race, and a number that represents the distance, and returns a MyTime
object that represents the average pace (time per mile).
The Base.Dates
module provides Time
objects that are similar to the MyTime
objects in the lecture, but they provide a rich set of methods and operators. Read the documentation at https://docs.julialang.org/en/stable/stdlib/dates/.
Use this module to write a program that gets the current date and prints the day of the week.
Write a program that takes a birthday as input and prints the user’s age and the number of days, hours, minutes and seconds until their next birthday.
For two people born on different days, there is a day when one is twice as old as the other. That’s their Double Day. Write a program that takes two birthdays and computes their Double Day.
For a little more challenge, write the more general version that computes the day when one person is n
times older than the other.
Write a definition for a type named Kangaroo
with an attribute named pouch_contents
of type Array
and the following methods:
An constructor that initializes pouch_contents
to an empty array.
A method named put_in_pouch
that takes a Kangaroo
object and an object of any type and adds it to pouch_contents
.
A show
method that returns a string representation of the Kangaroo
object and the contents of the pouch.
Test your code by creating two Kangaroo
objects, assigning them to variables named kanga
and roo
, and then adding roo
to the contents of kanga
’s pouch.
In [3]:
abstract type PingPongParent end
struct Ping <: PingPongParent
pong
end
struct Pong <: PingPongParent
pings :: Array{Ping, 1}
function Pong(pings=Array{Ping, 1}())
new(pings)
end
end
function add_ping(pong::Pong, ping::Ping)
push!(pong.pings, ping)
nothing
end
pong = Pong()
ping = Ping(pong)
add_ping(pong, ping)
The following are the possible hands in poker, in increasing order of value and decreasing order of probability:
pair: two cards with the same rank
two pair: two pairs of cards with the same rank
three of a kind: three cards with the same rank
straight: five cards with ranks in sequence (aces can be high or low, so Ace-2-3-4-5 is a straight and so is 10-Jack-Queen-King-Ace, but Queen-King-Ace-2-3 is not.)
flush: five cards with the same suit
full house: three cards with one rank, two cards with another
four of a kind: four cards with the same rank
straight flush: five cards in sequence (as defined above) and with the same suit
The goal of this exercise is to estimate the probability of drawing these various hands.
Add methods named has_pair
, has_twopair
, etc. that return true
or false
according to whether or not the hand meets the relevant criteria. Your code should work correctly for “hands” that contain any number of cards (although 5 and 7 are the most common sizes).
Write a method named classify
that figures out the highest-value classification for a hand and sets the label attribute accordingly. For example, a 7-card hand might contain a flush and a pair; it should be labeled “flush”.
When you are convinced that your classification methods are working, the next step is to estimate the probabilities of the various hands. Write a function that shuffles a deck of cards, divides it into hands, classifies the hands, and counts the number of times various classifications appear.
Print a table of the classifications and their probabilities. Run your program with larger and larger numbers of hands until the output values converge to a reasonable degree of accuracy. Compare your results to the values at http://en.wikipedia.org/wiki/Hand_rankings.
In [ ]: