Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions Week2/Characters Names/CharactersInPlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* macabethSmall.txt
*
* @author (Aymar N)
* @version (05.03.2019)
* @version (05.03.2019 V2)
*/

import edu.duke.*;
Expand All @@ -18,7 +18,7 @@ public class CharactersInPlay {
private ArrayList<String> character_name;
private ArrayList<Integer> count;

public CharactersInPlay(){
public CharactersInPlay() {
character_name = new ArrayList<String>();
count = new ArrayList<Integer>();
}
Expand All @@ -27,28 +27,26 @@ public void update(String person){

//person = person.toLowerCase();
int index = character_name.indexOf(person);
if(index == -1)
{
if (index == -1) {
character_name.add(person);
count.add(1);
}

else{
else {
int freq = count.get(index);
count.set(index,freq+1);
}

}

public void findAllCharacters(){
public void findAllCharacters() {
character_name.clear();
count.clear();

FileResource Resource = new FileResource("macbethSmall.txt");

for(String line: Resource.lines()){
for (String line: Resource.lines()){


/* for(int i = 0; i < line.length();i++){
// CurrentChar per line
char currChar = line.charAt(i);
Expand All @@ -62,7 +60,7 @@ public void findAllCharacters(){
update(possible_name);
}
*/
if (line.contains(".")){
if (line.contains(".")) {

int idx = line.indexOf(".");
String possible_name = line.substring(0,idx);
Expand All @@ -77,39 +75,39 @@ public void findAllCharacters(){



public void tester(){
public void tester() {
findAllCharacters();

for (int k =0; k < count.size();k++){
for (int k =0; k < count.size();k++) {

if (count.get(k) > 1){
if (count.get(k) > 1) {

System.out.println("The main character is: "+ character_name.get(k) +"\t"
+"The number of speaking parts is: "+ count.get(k));

}

}
}

int num1 = 2;
int num2 = 3;
charactersWithNumParts(num1, num2);

}

public void charactersWithNumParts(int num1, int num2){
public void charactersWithNumParts(int num1, int num2) {

for (int k =0; k < count.size();k++){
for (int k =0; k < count.size();k++) {

if (count.get(k) >= num1 && count.get(k)<= num2){
if (count.get(k) >= num1 && count.get(k)<= num2) {

System.out.println("The main character between : " + num1 + " and " + num2
+ " is " + character_name.get(k) +"\t"
+"The number of speaking parts is: "+ count.get(k));
System.out.println("The main character between : " + num1 + " and " + num2
+ " is " + character_name.get(k) +"\t"
+"The number of speaking parts is: "+ count.get(k));

}

}
}

}

Expand Down
4 changes: 2 additions & 2 deletions Week2/Characters Names/CharactersInPlayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
/**
* Write a description of CharactersInPlayTest here.
*
* @author (your name)
* @version (a version number or a date)
* @author (Aymar NAHUM)
* @version (V2)
*/
public class CharactersInPlayTest {
/**
Expand Down
72 changes: 26 additions & 46 deletions Week2/Codon Count/CodonCount.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,11 @@ public class CodonCount {

private HashMap<String, Integer> DNA_count;

public CodonCount(){
public CodonCount() {
DNA_count = new HashMap<String, Integer>();
}

/*
* This method will build a new map of codons mapped to their
* counts from the string dna with the reading frame with the position
* start( a value of 0,1 or 2).You will call this method several times,
* so make sure your map is emptybefore building.
*/

public void buildCodonMap(int start, String dna){

public void buildCodonMap(int start, String dna) {


//**************************************************************
Expand Down Expand Up @@ -83,15 +76,8 @@ public void buildCodonMap(int start, String dna){
}

}

/*
* This method returns a String, the codon in a reading frame that
* has the largest count. If there are several such codons,return
* any one of them.This method assumes the HashMap of codons to
* counts has already been built.
*/

public String getMostCommonCodon(){

public String getMostCommonCodon() {

int largest = 0;
int current = 0;
Expand All @@ -106,14 +92,7 @@ public String getMostCommonCodon(){
return largest_count;
}


/*Write a void method named printCodonCounts that has two int parameters,
*start and end. This method prints all the codons in the HashMap along
* with their count if their count is between start and end, inclusive
*/

public void printCodonCounts(int start, int end)
{
public void printCodonCounts(int start, int end) {
int current = 0;
for (String index : DNA_count.keySet()) {
current = DNA_count.get(index);
Expand All @@ -122,32 +101,33 @@ public void printCodonCounts(int start, int end)
}

}
public void Test(){

public void Test() {
//String dna = "CGTTCAAGTTCAA";
FileResource DNA = new FileResource("dnaMystery1.txt");
String dna = DNA.asString();
int start = 1;
int end = 5;

buildCodonMap(0, dna);
System.out.println("Reading frame starting with 0 results in "+DNA_count.size()+" unique codons"+"\t");
String the_largest_count = getMostCommonCodon();
System.out.println("and most common codon is "+the_largest_count+" with count "+DNA_count.get(the_largest_count)+"\t");
System.out.println("Counts of codons between "+start+" and "+end+" inclusive are:"+"\t");
printCodonCounts(start, end);
buildCodonMap(0, dna);
System.out.println("Reading frame starting with 0 results in "+DNA_count.size()+" unique codons"+"\t");
String the_largest_count = getMostCommonCodon();
System.out.println("and most common codon is "+the_largest_count+" with count "+DNA_count.get(the_largest_count)+"\t");
System.out.println("Counts of codons between "+start+" and "+end+" inclusive are:"+"\t");
printCodonCounts(start, end);

buildCodonMap(1, dna);
System.out.println("Reading frame starting with 1 results in "+DNA_count.size()+" unique codons"+"\t");
the_largest_count = getMostCommonCodon();
System.out.println("and most common codon is "+the_largest_count+" with count "+DNA_count.get(the_largest_count)+"\t");
System.out.println("Counts of codons between "+start+" and "+end+" inclusive are:"+"\t");
printCodonCounts(start, end);
buildCodonMap(1, dna);
System.out.println("Reading frame starting with 1 results in "+DNA_count.size()+" unique codons"+"\t");
the_largest_count = getMostCommonCodon();
System.out.println("and most common codon is "+the_largest_count+" with count "+DNA_count.get(the_largest_count)+"\t");
System.out.println("Counts of codons between "+start+" and "+end+" inclusive are:"+"\t");
printCodonCounts(start, end);

buildCodonMap(2, dna);
System.out.println("Reading frame starting with 2 results in "+DNA_count.size()+" unique codons"+"\t");
the_largest_count = getMostCommonCodon();
System.out.println("and most common codon is "+the_largest_count+" with count "+DNA_count.get(the_largest_count)+"\t");
System.out.println("Counts of codons between "+start+" and "+end+" inclusive are:"+"\t");
printCodonCounts(start, end);
buildCodonMap(2, dna);
System.out.println("Reading frame starting with 2 results in "+DNA_count.size()+" unique codons"+"\t");
the_largest_count = getMostCommonCodon();
System.out.println("and most common codon is "+the_largest_count+" with count "+DNA_count.get(the_largest_count)+"\t");
System.out.println("Counts of codons between "+start+" and "+end+" inclusive are:"+"\t");
printCodonCounts(start, end);
}
}
16 changes: 8 additions & 8 deletions Week2/GladLib/GladLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public class GladLib {
private static String dataSourceURL = "http://dukelearntoprogram.com/course3/data";
private static String dataSourceDirectory = "datalong";

public GladLib(){
public GladLib() {
initializeFromSource(dataSourceDirectory);
myRandom = new Random();
wordSeen = new ArrayList<String>();
}

public GladLib(String source){
public GladLib(String source) {
initializeFromSource(source);
// create a random number generator
myRandom = new Random();
Expand All @@ -43,7 +43,7 @@ private void initializeFromSource(String source) {
fruitList = readIt(source+"/fruit.txt");
}

private String randomFrom(ArrayList<String> source){
private String randomFrom(ArrayList<String> source) {
//return the next integer random value from
//the random number generator.
//source.size() number of elements in ArrayList.
Expand Down Expand Up @@ -91,7 +91,7 @@ private String getSubstitute(String label) {
return "**UNKNOWN**";
}

private String processWord(String w){
private String processWord(String w) {
// indexOf first occurence of <
int first = w.indexOf("<");
// starting from first, returns the first occurence of >
Expand Down Expand Up @@ -157,7 +157,7 @@ private String processWord(String w){



private void printOut(String s, int lineWidth){
private void printOut(String s, int lineWidth) {
int charsWritten = 0;
for(String w : s.split("\\s+")){
if (charsWritten + w.length() > lineWidth){
Expand All @@ -169,7 +169,7 @@ private void printOut(String s, int lineWidth){
}
}

private String fromTemplate(String source){
private String fromTemplate(String source) {
String story = "";
if (source.startsWith("http")) {
URLResource resource = new URLResource(source);
Expand All @@ -187,7 +187,7 @@ private String fromTemplate(String source){
return story;
}

private ArrayList<String> readIt(String source){
private ArrayList<String> readIt(String source) {
ArrayList<String> list = new ArrayList<String>();
if (source.startsWith("http")) {
URLResource resource = new URLResource(source);
Expand All @@ -204,7 +204,7 @@ private ArrayList<String> readIt(String source){
return list;
}

public void makeStory(){
public void makeStory() {
wordSeen.clear();
System.out.println("\n");
//String story = fromTemplate("data/madtemplate.txt");
Expand Down
Loading