mirror of
https://github.com/danbulant/introductionToProgramming
synced 2026-05-19 04:18:32 +00:00
creative exercise 1
This commit is contained in:
parent
c91fd07e47
commit
62c90bedff
4 changed files with 82 additions and 1 deletions
2
main.typ
2
main.typ
|
|
@ -15,7 +15,7 @@ Collection of solutions to programming exercises as part of Introduction to Prog
|
|||
)
|
||||
|
||||
#{
|
||||
let count = 10;
|
||||
let count = 11;
|
||||
for week in range(1, count + 1).filter(it => it != 8) {
|
||||
let a = "./week" + str(week) + "/doc.typ"
|
||||
include a
|
||||
|
|
|
|||
40
week11/Chaos.java
Normal file
40
week11/Chaos.java
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import common.StdDraw;
|
||||
|
||||
class Chaos {
|
||||
public static void main(String[] args) {
|
||||
var x = 0.01;
|
||||
var r = Double.parseDouble(args[0]);
|
||||
var buffer = new double[200];
|
||||
buffer[0] = x;
|
||||
StdDraw.enableDoubleBuffering();
|
||||
|
||||
var t = 0;
|
||||
while(true) {
|
||||
System.out.format("t = %3d; x = %s\n", t, x);
|
||||
x = Math.clamp(nextGeneration(x, r), 0, 1);
|
||||
|
||||
var offset = t % buffer.length;
|
||||
if(offset == 0) {
|
||||
StdDraw.clear();
|
||||
buffer = new double[200];
|
||||
}
|
||||
buffer[offset] = x;
|
||||
for(var toDraw = 0; toDraw < buffer.length; toDraw++) {
|
||||
var to = buffer[toDraw];
|
||||
StdDraw.line(toDraw / (double)buffer.length, 0, toDraw / (double) buffer.length, to);
|
||||
}
|
||||
if(offset % 10 == 0) {
|
||||
StdDraw.show();
|
||||
StdDraw.pause(100);
|
||||
}
|
||||
if(offset >= 1 && buffer[offset] == buffer[offset - 1]) { // stabilized value
|
||||
StdDraw.pause(5000);
|
||||
}
|
||||
t += 1;
|
||||
}
|
||||
}
|
||||
|
||||
static double nextGeneration(double x, double r) {
|
||||
return r * x * (1 - x);
|
||||
}
|
||||
}
|
||||
1
week11/common
Symbolic link
1
week11/common
Symbolic link
|
|
@ -0,0 +1 @@
|
|||
../common
|
||||
40
week11/doc.typ
Normal file
40
week11/doc.typ
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#import "./common/common.typ" : *
|
||||
|
||||
#show: template
|
||||
|
||||
= Week 10
|
||||
|
||||
== Exercise 1.3.45
|
||||
|
||||
_Chaos_. Write a program to study the following simple model for population growth, which might be applied to study fish in a pond, bacteria in a test tube,
|
||||
or any of a host of similar situations. We suppose that the population ranges from
|
||||
$0$ (extinct) to $1$ (maximum population that can be sustained). If the population at
|
||||
time $t$ is $x$, then we suppose the population at time $t + 1$ to be $r x (1-x)$, where the
|
||||
argument r, known as the _fecundity parameter_, controls the rate of growth. Start
|
||||
with a small population—say, $x = 0.01$—and study the result of iterating the model, for various values of $r$. For which values of $r$ does the population stabilize at
|
||||
$x = 1 - 1/r$ ? Can you say anything about the population when $r$ is $3.5$? $3.8$? $5$?
|
||||
|
||||
#table(
|
||||
columns: 3,
|
||||
$3.5$,
|
||||
$3.8$,
|
||||
$5$,
|
||||
$2$,
|
||||
[Oscillation between 4 values],
|
||||
[Sustained chaos],
|
||||
[Dies from overpopulation in 5 ticks],
|
||||
[Sustained $1-1/r$]
|
||||
)
|
||||
|
||||
== Exercise 1.4.26
|
||||
|
||||
_Music shuffling_. You set your music player to shuffle mode. It plays each of
|
||||
the n songs before repeating any. Write a program to estimate the likelihood that
|
||||
you will not hear any sequential pair of songs (that is, song 3 does not follow song
|
||||
2, song 10 does not follow song 9, and so on).
|
||||
|
||||
== Exercise 1.5.32
|
||||
|
||||
_Clock_. Write a program that displays an animation of the second, minute,
|
||||
and hour hands of an analog clock. Use the method `StdDraw.pause(1000)` to
|
||||
update the display roughly once per second.
|
||||
Loading…
Reference in a new issue