-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
511 changed files
with
745,336 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Compiled class file | ||
*.class | ||
|
||
# Log file | ||
*.log | ||
|
||
# BlueJ files | ||
*.ctxt | ||
|
||
# Mobile Tools for Java (J2ME) | ||
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.nar | ||
*.ear | ||
*.zip | ||
*.tar.gz | ||
*.rar | ||
|
||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* | ||
replay_pid* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package ec1; | ||
|
||
import java.util.List; | ||
|
||
public class Problem1 { | ||
/** | ||
* Given the list of Airports and their possible (one-way!) flights, | ||
* find the list of flights to take (in order) that gives the largest | ||
* possible number of reward points, while visiting each airport at most once. | ||
* | ||
* Runtime: O(N^2 M^2). | ||
* With N airports and M flights | ||
*/ | ||
public List<Flight> solve() { | ||
// FIXME | ||
return null; | ||
} | ||
|
||
public List<Airport> airports; | ||
public List<Flight> flights; | ||
|
||
private static class Airport { | ||
public int id; | ||
public List<Flight> outgoing; | ||
public List<Flight> incoming; | ||
} | ||
|
||
private static class Flight { | ||
public Airport start; | ||
public Airport end; | ||
|
||
public double points; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package ec1; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class Problem2 { | ||
/** | ||
* Given a list of Cities and their pairwise distances, find the shortest cycle that | ||
* visits every City exactly once. | ||
* | ||
* Return the list of cities visited in order. | ||
* The first city must be the same as the last city. | ||
* | ||
* Use getDistance(city1, city2) to find the pairwise distance between two cities. | ||
* | ||
* Runtime: O(N^2 log N) | ||
*/ | ||
public List<City> solve() { | ||
// FIXME | ||
return null; | ||
} | ||
|
||
public Map<CityPair, Double> distances; | ||
|
||
public double getDistance(City city1, City city2) { | ||
return distances.getOrDefault(new CityPair(city1.id, city2.id), Double.NaN); | ||
} | ||
|
||
private static class City { | ||
int id; | ||
} | ||
|
||
private static class CityPair { | ||
int city1; | ||
int city2; | ||
|
||
CityPair(int city1, int city2) { | ||
if (city1 < city2) { | ||
this.city1 = city1; | ||
this.city2 = city2; | ||
} else { | ||
this.city1 = city2; | ||
this.city2 = city1; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
CityPair cityPair = (CityPair) o; | ||
return city1 == cityPair.city1 && city2 == cityPair.city2; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(city1, city2); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package ec1; | ||
|
||
import java.util.List; | ||
|
||
public class Problem3 { | ||
/** | ||
* Given the list of students and their neighbors, assign test versions to students | ||
* such that no two students sitting next to each other have the same test version. | ||
* Additionally, use the fewest number of distinct test versions possible. | ||
* | ||
* Assign the results to the students' 'version' field. | ||
* | ||
* You may assume that if A is a neighbor of B, B is a also a neighbor of A. | ||
* A student may be sitting next to an arbitrary number of other students. | ||
* | ||
* Runtime: O(N^3) | ||
*/ | ||
public void solve() { | ||
// FIXME | ||
} | ||
|
||
public List<Student> students; | ||
|
||
public static class Student { | ||
public int id; | ||
public int version; | ||
|
||
public List<Student> neighbors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
You may find the following resources useful for solving these problems: | ||
|
||
* https://bit.ly/2Gq3dTu | ||
* https://bit.ly/2uDb9PN | ||
* https://bit.ly/2H30pwY | ||
* https://bit.ly/2H30Sze |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
public class BreakContinue { | ||
public static void windowPosSum(int[] a, int n) { | ||
for (int i = 0; i < a.length ; i += 1) { | ||
if ( a[i] < 0 ) { | ||
continue; | ||
} | ||
for (int j = 1; j <= n; j += 1) { | ||
if ( i+j > a.length - 1 ) { | ||
break; | ||
} | ||
a[i] += a[i+j]; | ||
} | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] a = {1, 2, -3, 4, 5, 4}; | ||
int n = 3; | ||
windowPosSum(a, n); | ||
|
||
// Should print 4, 8, -3, 13, 9, 4 | ||
System.out.println(java.util.Arrays.toString(a)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** A client that uses the synthesizer package to replicate a plucked guitar string sound */ | ||
public class GuitarHeroLite { | ||
private static final double CONCERT_A = 440.0; | ||
private static final double CONCERT_C = CONCERT_A * Math.pow(2, 3.0 / 12.0); | ||
|
||
public static void main(String[] args) { | ||
/* create two guitar strings, for concert A and C */ | ||
synthesizer.GuitarString stringA = new synthesizer.GuitarString(CONCERT_A); | ||
synthesizer.GuitarString stringC = new synthesizer.GuitarString(CONCERT_C); | ||
|
||
while (true) { | ||
|
||
/* check if the user has typed a key; if so, process it */ | ||
if (StdDraw.hasNextKeyTyped()) { | ||
char key = StdDraw.nextKeyTyped(); | ||
if (key == 'a') { | ||
stringA.pluck(); | ||
} else if (key == 'c') { | ||
stringC.pluck(); | ||
} | ||
} | ||
|
||
/* compute the superposition of samples */ | ||
double sample = stringA.sample() + stringC.sample(); | ||
|
||
/* play the sample on standard audio */ | ||
StdAudio.play(sample); | ||
|
||
/* advance the simulation of each guitar string by one step */ | ||
stringA.tic(); | ||
stringC.tic(); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import edu.princeton.cs.algs4.StdAudio; | ||
import synthesizer.GuitarString; | ||
|
||
import javax.sound.midi.InvalidMidiDataException; | ||
import javax.sound.midi.MetaMessage; | ||
import javax.sound.midi.MidiEvent; | ||
import javax.sound.midi.MidiMessage; | ||
import javax.sound.midi.MidiSystem; | ||
import javax.sound.midi.Sequence; | ||
import javax.sound.midi.Track; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Plays guitar from MIDI files. | ||
* | ||
* @author Eli Lipsitz | ||
*/ | ||
public class GuitarPlayer { | ||
private Sequence sequence = null; | ||
private GuitarString[] strings; | ||
private double[] vol; | ||
|
||
public GuitarPlayer(InputStream source) { | ||
try { | ||
sequence = MidiSystem.getSequence(source); | ||
} catch (IOException | InvalidMidiDataException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public GuitarPlayer(File source) { | ||
try { | ||
sequence = MidiSystem.getSequence(source); | ||
} catch (IOException | InvalidMidiDataException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private void initialize() { | ||
strings = new GuitarString[128]; | ||
vol = new double[128]; | ||
for (int i = 0; i < strings.length; i++) { | ||
strings[i] = new GuitarString(440.0 * Math.pow(2.0, (i - 69.0) / 12.0)); | ||
vol[i] = 0.0; | ||
} | ||
} | ||
|
||
private void tic() { | ||
for (int i = 0; i < strings.length; i++) { | ||
if (vol[i] > 0.0) { | ||
strings[i].tic(); | ||
} | ||
} | ||
} | ||
|
||
private double sample() { | ||
double sum = 0.0f; | ||
for (int i = 0; i < strings.length; i++) { | ||
sum += vol[i] * strings[i].sample(); | ||
} | ||
return sum; | ||
} | ||
|
||
public void play() { | ||
if (sequence == null) { | ||
return; | ||
} | ||
|
||
System.out.println("starting performance..."); | ||
initialize(); | ||
double bpm = 120; | ||
double samplesPerTick = (StdAudio.SAMPLE_RATE * 60.0) / (sequence.getResolution() * bpm); | ||
|
||
Track[] tracks = sequence.getTracks(); | ||
Track track = sequence.createTrack(); | ||
int maxSize = 0; | ||
int lead = 0; | ||
for (int i = 0; i < tracks.length; i++) { | ||
for (int j = 0; j < tracks[i].size(); j++) { | ||
track.add(tracks[i].get(j)); | ||
} | ||
} | ||
|
||
long tick = 0; | ||
for (int i = 0; i < track.size(); i++) { | ||
MidiEvent event = track.get(i); | ||
MidiMessage msg = event.getMessage(); | ||
byte[] data = msg.getMessage(); | ||
|
||
if (msg instanceof MetaMessage) { | ||
MetaMessage mm = (MetaMessage) msg; | ||
if (mm.getType() == 0x51) { | ||
// set tempo | ||
data = mm.getData(); | ||
int tempo = (data[0] & 0xff) << 16 | (data[1] & 0xff) << 8 | (data[2] & 0xff); | ||
bpm = 60000000.0 / tempo; | ||
samplesPerTick = (StdAudio.SAMPLE_RATE * 60.0); | ||
samplesPerTick /= (sequence.getResolution() * bpm); | ||
} else if (mm.getType() == 0x05) { | ||
// lyrics | ||
data = mm.getData(); | ||
String lyrics = new String(data); | ||
lyrics = lyrics.replace("\r", "\r\n"); | ||
System.out.print(lyrics); | ||
} | ||
continue; | ||
} | ||
|
||
if (event.getTick() > tick) { | ||
int samplesToSkip = (int) ((event.getTick() - tick) * samplesPerTick); | ||
for (int j = 0; j < samplesToSkip; j++) { | ||
tic(); | ||
StdAudio.play(sample()); | ||
} | ||
tick = event.getTick(); | ||
} | ||
|
||
int j = 0; | ||
while (j < data.length - 2) { | ||
int s = data[j++] & 0xFF; | ||
|
||
if (s >= 0x80 && s <= 0x8F) { | ||
// note off | ||
int note = data[j++] & 0xFF; | ||
int vel = data[j++] & 0xFF; | ||
vol[note] = 0.0; | ||
} else if (s >= 0x90 && s <= 0x9F) { | ||
// note on? | ||
int note = data[j++] & 0xFF; | ||
int vel = data[j++] & 0xFF; | ||
vol[note] = vel / 127.0; | ||
strings[note].pluck(); | ||
} else { | ||
// status | ||
int d = data[j++] & 0xFF; | ||
int d2 = data[j++] & 0xFF; | ||
} | ||
} | ||
} | ||
|
||
System.out.println("please clap"); | ||
} | ||
} |
Oops, something went wrong.